-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Added topological sorting algorithms #1677
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
RaviSadam
wants to merge
15
commits into
TheAlgorithms:master
Choose a base branch
from
RaviSadam:main
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 all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
74e8ec2
feat:added kanns algorithm
9f3aa64
Updated kahn's algorithm
2f2c099
Updated kahn's algorithm
3e9c9e4
feat: added topological sorting algorithm
RaviSadam f0325a5
Merge remote-tracking branch 'origin/master'
RaviSadam 99535c5
Delete Graphs/KahnsAlgorithm.js
RaviSadam 35fe1c3
Delete Graphs/KannsAlgorithm.js
RaviSadam ef63f66
Delete Graphs/test/KahnsAlgorithm.test.js
RaviSadam 154a330
Delete Graphs/test/KannsAlgorithm.test.js
RaviSadam 191993f
Merge remote-tracking branch 'origin/master'
RaviSadam f252873
feat:added topological sorting algorithms
RaviSadam c179e51
Updated Topo Sort
RaviSadam 8eb4bda
Merge branch 'main' of https://github.com/RaviSadam/JavaScript
RaviSadam b2c3d6d
updated topological sort
RaviSadam 19d0d8f
updated topological sort
RaviSadam 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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import Queue from '../Data-Structures/Queue/Queue' | ||
/** | ||
* @author {RaviSadam} | ||
* @name TopoSortIterative | ||
* @description - | ||
* Topological sorting algorithm implementation in JavaScript(Khan's Algorithm) | ||
* @summary | ||
* Topological sorting for Directed Acyclic Graph is a linear ordering of vertices | ||
* such that for every directed edge u-v, vertex u comes before v in the ordering. | ||
* | ||
* @param graph - Graph (adjacency list) | ||
* @returns {Array} - Gives null if graph has cycle otherwise result array | ||
* | ||
*/ | ||
|
||
export function TopoSortIterative(graph) { | ||
const n = graph.length | ||
const inDegree = Array(n).fill(0) | ||
const queue = new Queue() | ||
const result = Array(n).fill(0) | ||
let index = 0 | ||
|
||
for (const neighbors of graph) { | ||
for (const neighbor of neighbors) { | ||
inDegree[neighbor]++ | ||
} | ||
} | ||
|
||
for (let i = 0; i < n; i++) { | ||
if (inDegree[i] === 0) { | ||
queue.enqueue(i) | ||
} | ||
} | ||
while (queue.length !== 0) { | ||
const node = queue.dequeue() | ||
result[index] = node | ||
index++ | ||
for (let neighbor of graph[node]) { | ||
inDegree[neighbor]-- | ||
if (inDegree[neighbor] === 0) { | ||
queue.enqueue(neighbor) | ||
} | ||
} | ||
} | ||
if (index !== n) return null | ||
return result | ||
} |
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
function TopoSort(graph) { | ||
const n = graph.length | ||
const result = [] | ||
const path = Array(n).fill(0) | ||
const visited = Array(n).fill(0) | ||
function preorder(vertex) { | ||
visited[vertex] = 1 | ||
path[vertex] = 1 | ||
for (const neighbor of graph[vertex]) { | ||
if (visited[neighbor] === 0) { | ||
preorder(neighbor) | ||
} else if (path[neighbor] === 1) { | ||
throw new Error('Graph contsins a cycle') | ||
} | ||
} | ||
path[vertex] = 0 | ||
result.push(vertex) | ||
} | ||
return function () { | ||
for (let i = 0; i < n; i++) { | ||
if (visited[i] === 0) { | ||
try { | ||
preorder(i) | ||
} catch (err) { | ||
console.log(err) | ||
return null | ||
} | ||
} | ||
} | ||
return result.reverse() | ||
} | ||
} | ||
/** | ||
* | ||
* @author {RaviSadam} | ||
* @name TopoSortRecursive | ||
* @description - | ||
* Topological sorting algorithm implementation in JavaScript | ||
* @summary | ||
* Topological sorting for Directed Acyclic Graph is a linear ordering of vertices | ||
* such that for every directed edge u-v, vertex u comes before v in the ordering. | ||
* | ||
* @param graph - Graph (adjacency list) | ||
* @returns {Array} - Gives null if graph has cycle otherwise result array | ||
* | ||
*/ | ||
export function TopoSortRecursive(graph) { | ||
const topoSort = TopoSort(graph) | ||
return topoSort() | ||
} |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { TopoSortIterative } from '../TopoSortIterative' | ||
|
||
describe('Iterative Topological Sorting', () => { | ||
test('Graph without cycle', () => { | ||
const graph = [[], [], [3], [1], [0, 1], [0, 2]] | ||
|
||
expect(TopoSortIterative(graph, 6)).toEqual([4, 5, 0, 2, 3, 1]) | ||
}) | ||
test('Graph with cycle', () => { | ||
const graph = [[2], [], [3, 5], [0, 1], [0, 2]] | ||
|
||
expect(TopoSortIterative(graph, 6)).toBe(null) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { TopoSortRecursive } from '../TopoSortRecursive' | ||
|
||
describe('Recursive Topological Sorting', () => { | ||
test('Graph without cycle', () => { | ||
const graph = [[], [], [3], [1], [0, 1], [0, 2]] | ||
|
||
expect(TopoSortRecursive(graph, 6)).toEqual([5, 4, 2, 3, 1, 0]) | ||
}) | ||
test('Graph with cycle', () => { | ||
const graph = [[2], [], [3, 5], [0, 1], [0, 2]] | ||
|
||
expect(TopoSortRecursive(graph, 6)).toBe(null) | ||
}) | ||
test('Graph with disconnected components', () => { | ||
const graph = [[1, 2], [3], [3], [], [5], []] | ||
expect(TopoSortRecursive(graph, 6)).toEqual([4, 5, 0, 2, 1, 3]) | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.
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.