Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 473af78

Browse files
committed
dfs algorithm added
1 parent f7f8d80 commit 473af78

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

‎Data-Structure/Graph/dfs.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict';
2+
class Graph {
3+
constructor(vertex) {
4+
this.listArray = new Array(vertex + 1);
5+
this.visitedArr = new Array(vertex + 1);
6+
for (let index = 0; index <= vertex; index++) {
7+
this.listArray[index] = [];
8+
this.visitedArr[index] = 0;
9+
}
10+
}
11+
addEdge(v, u) {
12+
this.listArray[v].push(u);
13+
}
14+
15+
dfs(startVertex) {
16+
console.log(startVertex);
17+
this.visitedArr[startVertex] = 1;
18+
let tList = this.listArray[startVertex];
19+
for (let index = 0; index < tList.length; index++) {
20+
if (!this.visitedArr[tList[index]]) this.dfs(tList[index]);
21+
}
22+
}
23+
}
24+
25+
let g1 = new Graph(7);
26+
g1.addEdge(1, 2);
27+
g1.addEdge(1, 3);
28+
g1.addEdge(1, 5);
29+
30+
g1.addEdge(2, 1);
31+
g1.addEdge(2, 4);
32+
g1.addEdge(2, 7);
33+
34+
g1.addEdge(3, 1);
35+
g1.addEdge(3, 6);
36+
37+
g1.addEdge(5, 1);
38+
g1.addEdge(5, 6);
39+
40+
g1.addEdge(6, 3);
41+
g1.addEdge(6, 5);
42+
g1.dfs(1);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /