Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 47 additions & 6 deletions Data-Structures/Graph/Graph.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
class Graph {
constructor () {
this.adjacencyMap = {}
this.numberOfVertex = 0
}

addVertex (vertex) {
this.adjacencyMap[vertex] = []
this.numberOfVertex++
}

containsVertex (vertex) {
return typeof (this.adjacencyMap[vertex]) !== 'undefined'
return typeof this.adjacencyMap[vertex] !== 'undefined'
}

addEdge (vertex1, vertex2) {
Expand All @@ -18,7 +20,7 @@ class Graph {
}
}

printGraph (output = value => console.log(value)) {
printGraph (output = (value) => console.log(value)) {
const keys = Object.keys(this.adjacencyMap)
for (const i of keys) {
const values = this.adjacencyMap[i]
Expand All @@ -34,13 +36,14 @@ 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)) {
bfs (source, output = (value) => console.log(value)) {
const queue = [[source, 0]] // level of source is 0
const visited = new Set()

while (queue.length) {
const [node, level] = queue.shift() // remove the front of the queue
if (visited.has(node)) { // visited
if (visited.has(node)) {
// visited
continue
}

Expand All @@ -56,8 +59,9 @@ 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
dfs (source, visited = new Set(), output = (value) => console.log(value)) {
if (visited.has(source)) {
// visited
return
}

Expand All @@ -67,6 +71,40 @@ class Graph {
this.dfs(neighbour, visited, output)
}
}

_topologicalSort (v, visited, stack) {
// Mark the current node as visited.
visited[v] = true

// Recur for all the vertices adjacent to thisvertex
for (const i of this.adjacencyMap[v]) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd name this v (or w to not shadow the v in scope) since this is a vertex, not an index. Perhaps better would be neighbor.

if (!visited[i]) {
this._topologicalSort(i, visited, stack)
}
}

// Push current vertex to stack which stores result
stack.push(v)
}

// The function to do Topological Sort. It uses recursive _topologicalSort()
topologicalSort () {
const stack = []

// Mark all the vertices as not visited
const visited = new Array(this.numberOfVertex)
visited.fill(false)

// Call the recursive helper function to store Topological Sort starting from all vertices one by one
for (let i = 0; i < visited.length; i++) {
if (visited[i] === false) {
this._topologicalSort(i + 1, visited, stack)
}
}

// Return stack in reverse order
return stack.reverse()
}
}

const example = () => {
Expand Down Expand Up @@ -96,6 +134,9 @@ const example = () => {

// Depth first search at node 1
g.dfs(1)

// Prints Topological sort of given graph
g.topologicalSort()
}

export { Graph, example }