From e9d54779fd6f54b52070cffc7ea2d5dcec1d520a Mon Sep 17 00:00:00 2001 From: Yatin Date: Thu, 25 Nov 2021 23:48:16 +0530 Subject: [PATCH 1/2] Modify Graph bfs method --- Data-Structures/Graph/Graph.js | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/Data-Structures/Graph/Graph.js b/Data-Structures/Graph/Graph.js index c7b234526f..2b7056fdb2 100644 --- a/Data-Structures/Graph/Graph.js +++ b/Data-Structures/Graph/Graph.js @@ -39,21 +39,19 @@ class Graph { * @param {number} source The source vertex to start BFS. */ bfs (source, output = value => console.log(value)) { - const queue = [] + const queue = [[source, 0]] // level of source is 0 const visited = new Set() - queue.unshift([source, 0]) // level of source is 0 - visited.add(source) + while (queue.length) { - const front = queue[0] - const node = front[0] - const level = front[1] - queue.shift() // remove the front of the queue + const [node, level] = queue.shift() // remove the front of the queue + if (visited.has(node)) { // visited + continue + } + + visited.add(node) output(`Visited node ${node} at level ${level}.`) for (const next of this.adjacencyMap[node]) { - if (!visited.has(next)) { // not visited - queue.unshift([next, level + 1]) // level 1 more than current - visited.add(next) - } + queue.push([next, level + 1]) // level 1 more than current } } } From 03a4dfaa45c6265075af63d04ff41b1df9e39919 Mon Sep 17 00:00:00 2001 From: Yatin Date: Sat, 27 Nov 2021 00:16:17 +0530 Subject: [PATCH 2/2] add dfs in graph --- Data-Structures/Graph/Graph.js | 42 ++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/Data-Structures/Graph/Graph.js b/Data-Structures/Graph/Graph.js index 2b7056fdb2..1d8e941a11 100644 --- a/Data-Structures/Graph/Graph.js +++ b/Data-Structures/Graph/Graph.js @@ -3,22 +3,19 @@ class Graph { this.adjacencyMap = {} } - addVertex (v) { - this.adjacencyMap[v] = [] + addVertex (vertex) { + this.adjacencyMap[vertex] = [] } containsVertex (vertex) { return typeof (this.adjacencyMap[vertex]) !== 'undefined' } - addEdge (v, w) { - let result = false - if (this.containsVertex(v) && this.containsVertex(w)) { - this.adjacencyMap[v].push(w) - this.adjacencyMap[w].push(v) - result = true + addEdge (vertex1, vertex2) { + if (this.containsVertex(vertex1) && this.containsVertex(vertex2)) { + this.adjacencyMap[vertex1].push(vertex2) + this.adjacencyMap[vertex2].push(vertex1) } - return result } printGraph (output = value => console.log(value)) { @@ -35,7 +32,6 @@ class Graph { /** * Prints the Breadth first traversal of the graph from source. - * * @param {number} source The source vertex to start BFS. */ bfs (source, output = value => console.log(value)) { @@ -55,6 +51,22 @@ class Graph { } } } + + /** + * Prints the Depth first traversal of the graph from source. + * @param {number} source The source vertex to start DFS. + */ + dfs (source, visited = new Set(), output = value => console.log(value)) { + if (visited.has(source)) { // visited + return + } + + output(`Visited node ${source}`) + visited.add(source) + for (const neighbour of this.adjacencyMap[source]) { + this.dfs(neighbour, visited, output) + } + } } const example = () => { @@ -69,11 +81,21 @@ const example = () => { g.addEdge(2, 4) g.addEdge(2, 5) + // Graph + // 1 -> 2 3 + // 2 -> 1 4 5 + // 3 -> 1 + // 4 -> 2 + // 5 -> 2 + // Printing the adjacency list // g.printGraph() // Breadth first search at node 1 g.bfs(1) + + // Depth first search at node 1 + g.dfs(1) } export { Graph, example }