1
|
|
|
import unittest |
2
|
|
|
|
3
|
|
|
from pyalgs.algorithms.graphs.directed_cycle import DirectedCycle |
4
|
|
|
from pyalgs.algorithms.graphs.shortest_path import DijkstraShortestPath, TopologicalSortShortestPath, \ |
5
|
|
|
BellmanFordShortestPath |
6
|
|
|
from tests.algorithms.graphs.util import create_edge_weighted_digraph |
7
|
|
|
|
8
|
|
|
|
9
|
|
View Code Duplication |
class DijkstraShortestPathUnitTest(unittest.TestCase): |
|
|
|
|
10
|
|
|
def test_shortest_path(self): |
11
|
|
|
g = create_edge_weighted_digraph() |
12
|
|
|
s = 0 |
13
|
|
|
dijkstra = DijkstraShortestPath(g, s) |
14
|
|
|
for v in range(1, g.vertex_count()): |
15
|
|
|
if dijkstra.hasPathTo(v): |
16
|
|
|
print(str(s) + ' is connected to ' + str(v)) |
17
|
|
|
print('shortest path is ' + ' .. '.join([str(i) for i in dijkstra.shortestPathTo(v)])) |
18
|
|
|
print('path length is ' + str(dijkstra.path_length_to(v))) |
19
|
|
|
|
20
|
|
|
|
21
|
|
View Code Duplication |
class TopologicalSortShortestPathUnitTest(unittest.TestCase): |
|
|
|
|
22
|
|
|
def test_shortest_path(self): |
23
|
|
|
g = create_edge_weighted_digraph() |
24
|
|
|
assert not DirectedCycle(g).hasCycle() |
25
|
|
|
s = 0 |
26
|
|
|
dijkstra = TopologicalSortShortestPath(g, s) |
27
|
|
|
for v in range(1, g.vertex_count()): |
28
|
|
|
if dijkstra.hasPathTo(v): |
29
|
|
|
print(str(s) + ' is connected to ' + str(v)) |
30
|
|
|
print('shortest path is ' + ' .. '.join([str(i) for i in dijkstra.shortestPathTo(v)])) |
31
|
|
|
print('path length is ' + str(dijkstra.path_length_to(v))) |
32
|
|
|
|
33
|
|
|
|
34
|
|
View Code Duplication |
class BellmanFordShortestPathUnitTest(unittest.TestCase): |
|
|
|
|
35
|
|
|
def test_shortest_path(self): |
36
|
|
|
g = create_edge_weighted_digraph() |
37
|
|
|
s = 0 |
38
|
|
|
dijkstra = BellmanFordShortestPath(g, s) |
39
|
|
|
for v in range(1, g.vertex_count()): |
40
|
|
|
if dijkstra.hasPathTo(v): |
41
|
|
|
print(str(s) + ' is connected to ' + str(v)) |
42
|
|
|
print('shortest path is ' + ' .. '.join([str(i) for i in dijkstra.shortestPathTo(v)])) |
43
|
|
|
print('path length is ' + str(dijkstra.path_length_to(v))) |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
if __name__ == '__main__': |
47
|
|
|
unittest.main() |