Skip to content

Commit bffc42b

Browse files
committed
dfs and connected components
1 parent 7aee850 commit bffc42b

File tree

1 file changed

+131
-10
lines changed

1 file changed

+131
-10
lines changed

Data Structures/Graph/graph.js

Lines changed: 131 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,24 @@ class Graph {
1616
}
1717
}
1818

19-
const graph = new Graph(5);
20-
// console.log(graph);
21-
graph.addEdge(0, 1);
22-
graph.addEdge(0, 4);
23-
graph.addEdge(1, 4);
24-
graph.addEdge(1, 3);
25-
graph.addEdge(1, 2);
26-
graph.addEdge(2, 3);
19+
const graph = new Graph(13);
20+
graph.addEdge(1, 10);
21+
graph.addEdge(1, 8);
22+
graph.addEdge(0, 9);
23+
graph.addEdge(0, 7);
24+
graph.addEdge(0, 11);
25+
graph.addEdge(9, 8);
26+
graph.addEdge(8, 12);
27+
graph.addEdge(2, 12);
2728
graph.addEdge(3, 4);
28-
console.log(JSON.stringify(graph, 0, 2));
29+
graph.addEdge(3, 2);
30+
graph.addEdge(3, 7);
31+
graph.addEdge(7, 11);
32+
graph.addEdge(6, 7);
33+
graph.addEdge(5, 6);
34+
graph.addEdge(10, 9);
35+
36+
// console.log(JSON.stringify(graph, 0, 2));
2937
// console.log(JSON.stringify(graph, 0, 2));
3038
// console.log("vertices: ", graph.vertices);
3139
// console.log("1 edges: ", graph.edges[1]);
@@ -81,4 +89,117 @@ function reconstructPath(s, e, prev) {
8189
return [];
8290
}
8391

84-
console.log(bfs(0, 3));
92+
console.log("BFS shortest path: ", bfs(0, 12));
93+
94+
function dfs(s) {
95+
let visited = new Array(graph.vertices).fill(false);
96+
97+
let visit = (at) => {
98+
if (visited[at]) return;
99+
console.log("visit node: ", at);
100+
visited[at] = true;
101+
102+
let neighbors = graph.edges[at];
103+
let curr = neighbors.head;
104+
while (curr) {
105+
visit(curr.data);
106+
curr = curr.next;
107+
}
108+
};
109+
110+
return visit(s);
111+
}
112+
113+
console.log("DFS from 0: ", dfs(0));
114+
115+
let newGraph = new Graph(18);
116+
117+
newGraph.addEdge(0, 4);
118+
newGraph.addEdge(0, 8);
119+
newGraph.addEdge(0, 13);
120+
newGraph.addEdge(0, 14);
121+
newGraph.addEdge(14, 8);
122+
newGraph.addEdge(13, 14);
123+
newGraph.addEdge(4, 8);
124+
newGraph.addEdge(1, 5);
125+
newGraph.addEdge(17, 5);
126+
newGraph.addEdge(5, 16);
127+
newGraph.addEdge(6, 11);
128+
newGraph.addEdge(11, 7);
129+
newGraph.addEdge(7, 6);
130+
newGraph.addEdge(3, 9);
131+
newGraph.addEdge(9, 2);
132+
newGraph.addEdge(2, 15);
133+
newGraph.addEdge(9, 15);
134+
newGraph.addEdge(10, 15);
135+
136+
function findComponents() {
137+
let count = 0;
138+
let components = [];
139+
let visited = new Array(newGraph.vertices).fill(false);
140+
141+
for (let i = 0; i < newGraph.vertices; i++) {
142+
if (!visited[i]) {
143+
visit(i);
144+
count++;
145+
}
146+
}
147+
148+
function visit(at) {
149+
visited[at] = true;
150+
components[at] = count;
151+
152+
let neighbors = newGraph.edges[at];
153+
let curr = neighbors.head;
154+
while (curr) {
155+
if (!visited[curr.data]) {
156+
visit(curr.data);
157+
}
158+
curr = curr.next;
159+
}
160+
}
161+
162+
return {
163+
count: count,
164+
components: components,
165+
};
166+
}
167+
168+
console.log("Find components: ", findComponents());
169+
170+
// function solvePractice(s) {
171+
// let queue = []; // Initialize the queue
172+
// queue.push(s); // enqueue node S for first visiting
173+
174+
// // Initialize visited array determine node ith is visited or not
175+
// let visited = new Array(graph.vertices).fill(false);
176+
// visited[s] = true;
177+
178+
// // Initialize prev array tracking visited nodes during BFS process
179+
// let prev = new Array(graph.vertices).fill(null);
180+
181+
// // looping until the queue is empty
182+
// while (queue.length) {
183+
// let node = queue.shift(); // dequeue
184+
// let neighbors = graph.edges[node]; // get all neighbors of nodes
185+
186+
// // looping throw the adjacency list of node
187+
// let curr = neighbors.head;
188+
// while (curr) {
189+
// // checking if the current node is not visited yet
190+
// if (!visited[curr.data]) {
191+
// // checking if the current node is not included in queue
192+
// if (!queue.includes(curr.data)) {
193+
// queue.push(curr.data);
194+
// }
195+
// // marking the current node is visited in visited array
196+
// visited[curr.data] = true;
197+
// // tracking the previous node of current node
198+
// prev[curr.data] = node;
199+
// }
200+
// curr = curr.next;
201+
// }
202+
// }
203+
204+
// return prev;
205+
// }

0 commit comments

Comments
 (0)