Skip to content

Commit c179e51

Browse files
committed
Updated Topo Sort
1 parent 191993f commit c179e51

File tree

3 files changed

+33
-105
lines changed

3 files changed

+33
-105
lines changed

Graphs/TopoSortIterative.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function TopoSortIterative(graph) {
2222

2323
for (const neighbors of graph) {
2424
for (const neighbor of neighbors) {
25-
inDegree[neighbor] += 1
25+
inDegree[neighbor]++
2626
}
2727
}
2828

@@ -34,9 +34,9 @@ export function TopoSortIterative(graph) {
3434
while (queue.length !== 0) {
3535
const node = queue.dequeue()
3636
result[index] = node
37-
index += 1
37+
index++
3838
for (let neighbor of graph[node]) {
39-
inDegree[neighbor] -= 1
39+
inDegree[neighbor]--
4040
if (inDegree[neighbor] === 0) {
4141
queue.enqueue(neighbor)
4242
}

Graphs/TopoSortRecursive.js

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,35 @@
1-
function TopoSortRecursiveFunction(graph, visited, stack, path, vertex) {
2-
//marking current vertex as visited
3-
visited.add(vertex)
4-
5-
//marking current vertex as being part of current path
6-
path[vertex] = 1
7-
if (graph[vertex] && graph[vertex].length !== 0) {
1+
function TopoSort(graph) {
2+
const n = graph.length
3+
const result = []
4+
const path = Array(n).fill(0)
5+
const visited = Array(n).fill(0)
6+
function preorder(vertex) {
7+
visited[vertex] = 1
8+
path[vertex] = 1
89
for (const neighbor of graph[vertex]) {
9-
//if neighbor is not visited then visit else continue
10-
if (!visited.has(neighbor)) {
11-
TopoSortRecursiveFunction(graph, visited, stack, path, neighbor)
12-
} else if (path[neighbor] == 1) return
10+
if (visited[neighbor] === 0) {
11+
preorder(neighbor)
12+
} else if (path[neighbor] === 1) {
13+
throw new Error('Graph contsins a cycle')
14+
}
1315
}
16+
path[vertex] = 0
17+
result.push(vertex)
18+
}
19+
return function () {
20+
for (let i = 0; i < n; i++) {
21+
if (visited[i] === 0) {
22+
try {
23+
preorder(i)
24+
} catch (err) {
25+
console.log(err)
26+
return null
27+
}
28+
}
29+
}
30+
return result.reverse()
1431
}
15-
16-
//unmarking vertex
17-
path[vertex] = 0
18-
19-
//visited all vertex coming after the current vertex
20-
//so added to stack
21-
stack.push(vertex)
2232
}
23-
2433
/**
2534
*
2635
* @author {RaviSadam}
@@ -36,24 +45,6 @@ function TopoSortRecursiveFunction(graph, visited, stack, path, vertex) {
3645
*
3746
*/
3847
export function TopoSortRecursive(graph) {
39-
const n = graph.length
40-
const stack = []
41-
//visited set for keep tracking of visited vertises
42-
const visited = new Set()
43-
44-
//path array for keep tacking of vertices
45-
//visited in current path
46-
47-
const path = Array(n).fill(0)
48-
for (let i = 0; i < n; i++) {
49-
if (!visited.has(i)) {
50-
TopoSortRecursiveFunction(graph, visited, stack, path, i)
51-
}
52-
}
53-
for (const value of path) {
54-
if (value === 1) return null
55-
}
56-
//reverse the stack for getting exact topological order
57-
stack.reverse()
58-
return stack
48+
const topoSort = TopoSort(graph)
49+
return topoSort()
5950
}

Sorts/TopologicalSort.js

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)