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] += 1
RaviSadam marked this conversation as resolved.
Show resolved Hide resolved
}
}

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 += 1
RaviSadam marked this conversation as resolved.
Show resolved Hide resolved
for (let neighbor of graph[node]) {
inDegree[neighbor] -= 1
RaviSadam marked this conversation as resolved.
Show resolved Hide resolved
if (inDegree[neighbor] === 0) {
queue.enqueue(neighbor)
}
}
}
if (index !== n) return null
return result
}
59 changes: 59 additions & 0 deletions Graphs/TopoSortRecursive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
function TopoSortRecursiveFunction(graph, visited, stack, path, vertex) {
RaviSadam marked this conversation as resolved.
Show resolved Hide resolved
//marking current vertex as visited
RaviSadam marked this conversation as resolved.
Show resolved Hide resolved
visited.add(vertex)

//marking current vertex as being part of current path
RaviSadam marked this conversation as resolved.
Show resolved Hide resolved
path[vertex] = 1
if (graph[vertex] && graph[vertex].length !== 0) {
RaviSadam marked this conversation as resolved.
Show resolved Hide resolved
for (const neighbor of graph[vertex]) {
//if neighbor is not visited then visit else continue
RaviSadam marked this conversation as resolved.
Show resolved Hide resolved
if (!visited.has(neighbor)) {
TopoSortRecursiveFunction(graph, visited, stack, path, neighbor)
} else if (path[neighbor] == 1) return
}
}

//unmarking vertex
path[vertex] = 0

//visited all vertex coming after the current vertex
//so added to stack
stack.push(vertex)
}

/**
*
* @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 n = graph.length
const stack = []
//visited set for keep tracking of visited vertises
const visited = new Set()
RaviSadam marked this conversation as resolved.
Show resolved Hide resolved

//path array for keep tacking of vertices
//visited in current path

const path = Array(n).fill(0)
RaviSadam marked this conversation as resolved.
Show resolved Hide resolved
for (let i = 0; i < n; i++) {
if (!visited.has(i)) {
TopoSortRecursiveFunction(graph, visited, stack, path, i)
}
}
for (const value of path) {
if (value === 1) return null
}
//reverse the stack for getting exact topological order
RaviSadam marked this conversation as resolved.
Show resolved Hide resolved
stack.reverse()
return stack
}
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])
})
})
Loading