Skip to content
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

Added topological sorting algorithms #1677

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
47 changes: 47 additions & 0 deletions Graphs/TopoSortIterative.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import Queue from '../Data-Structures/Queue/Queue'
RaviSadam marked this conversation as resolved.
Show resolved Hide resolved
/**
* @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
}
50 changes: 50 additions & 0 deletions Graphs/TopoSortRecursive.js
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()
}
14 changes: 14 additions & 0 deletions Graphs/test/TopoSortIterative.test.js
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)
})
})
18 changes: 18 additions & 0 deletions Graphs/test/TopoSortRecursive.test.js
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])
})
})
63 changes: 0 additions & 63 deletions Sorts/TopologicalSort.js

This file was deleted.