forked from yangshun/lago
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Topological Sort implementation (yangshun#49)
- Loading branch information
Showing
4 changed files
with
138 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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,92 @@ | ||
import topologicalSort from '../topologicalSort'; | ||
|
||
describe('topologicalSort', () => { | ||
test('empty graph', () => { | ||
expect(topologicalSort({})).toEqual([]); | ||
}); | ||
|
||
test('graphs with one node', () => { | ||
expect(topologicalSort({ '1': [] })).toEqual(['1']); | ||
}); | ||
|
||
test('graphs with two nodes', () => { | ||
expect(topologicalSort({ '1': ['2'], '2': [] })).toEqual(['1', '2']); | ||
}); | ||
|
||
test('graphs with multiple nodes', () => { | ||
expect(topologicalSort({ '1': ['2'], '2': ['3'], '3': [] })).toEqual([ | ||
'1', | ||
'2', | ||
'3', | ||
]); | ||
expect(topologicalSort({ '1': ['2', '3'], '2': [], '3': [] })).toEqual([ | ||
'1', | ||
'2', | ||
'3', | ||
]); | ||
expect( | ||
topologicalSort({ | ||
'1': ['2', '3'], | ||
'2': [], | ||
'3': [], | ||
'4': ['2'], | ||
'5': ['3'], | ||
}), | ||
).toEqual(['1', '4', '5', '2', '3']); | ||
expect( | ||
topologicalSort({ | ||
'1': ['4', '5'], | ||
'2': [], | ||
'3': [], | ||
'4': ['2'], | ||
'5': ['3'], | ||
}), | ||
).toEqual(['1', '4', '5', '2', '3']); | ||
|
||
expect( | ||
topologicalSort({ | ||
'1': ['2', '3', '4', '5'], | ||
'2': [], | ||
'3': [], | ||
'4': ['2'], | ||
'5': ['3'], | ||
}), | ||
).toEqual(['1', '4', '5', '2', '3']); | ||
// Graph taken from https://en.wikipedia.org/wiki/Topological_sorting | ||
const graph = { | ||
'2': [], | ||
'3': ['8', '10'], | ||
'5': ['11'], | ||
'7': ['8', '11'], | ||
'8': ['9'], | ||
'9': [], | ||
'10': [], | ||
'11': ['2', '9'], | ||
}; | ||
expect(topologicalSort(graph)).toEqual([ | ||
'3', | ||
'5', | ||
'7', | ||
'10', | ||
'8', | ||
'11', | ||
'2', | ||
'9', | ||
]); | ||
}); | ||
|
||
test('graph with cycles', () => { | ||
expect(topologicalSort({ '1': ['1'] })).toEqual([]); | ||
expect(topologicalSort({ '1': ['1', '2'], '2': [] })).toEqual([]); | ||
expect(topologicalSort({ '1': ['1', '2'], '2': ['1'] })).toEqual([]); | ||
expect( | ||
topologicalSort({ | ||
'1': ['4', '5'], | ||
'2': ['1', '2', '3', '4', '5'], | ||
'3': [], | ||
'4': ['2'], | ||
'5': ['3'], | ||
}), | ||
).toEqual([]); | ||
}); | ||
}); |
This file contains 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,43 @@ | ||
import { Queue } from '../'; | ||
|
||
/** | ||
* Performs a topological sort on a directed graph. | ||
* @param {Object} graph Node to array of traversable neighboring nodes. | ||
* @return {number[]|string[]} A topological traversal of nodes. | ||
*/ | ||
function topologicalSort(graph) { | ||
const nodes = new Map(); | ||
const order = []; | ||
const queue = new Queue(); | ||
|
||
Object.keys(graph).forEach(node => { | ||
nodes.set(node, { in: 0, out: new Set(graph[node]) }); | ||
}); | ||
|
||
Object.keys(graph).forEach(node => { | ||
graph[node].forEach(neighbor => { | ||
nodes.get(neighbor).in += 1; | ||
}); | ||
}); | ||
|
||
nodes.forEach((value, node) => { | ||
if (value.in === 0) { | ||
queue.enqueue(node); | ||
} | ||
}); | ||
|
||
while (queue.length) { | ||
const node = queue.dequeue(); | ||
nodes.get(node).out.forEach(neighbor => { | ||
nodes.get(neighbor).in -= 1; | ||
if (nodes.get(neighbor).in === 0) { | ||
queue.enqueue(neighbor); | ||
} | ||
}); | ||
order.push(node); | ||
} | ||
|
||
return order.length === Object.keys(graph).length ? order : []; | ||
} | ||
|
||
export default topologicalSort; |
This file contains 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