-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Added TopologicalSort implementaion on graph. #1311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bhaskarcsawant
wants to merge
4
commits into
TheAlgorithms:master
Choose a base branch
from
bhaskarcsawant:topologicalSort
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
|
@@ -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] | ||
|
@@ -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 | ||
} | ||
|
||
|
@@ -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 | ||
} | ||
|
||
|
@@ -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]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd name this |
||
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 = () => { | ||
|
@@ -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 } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.