|
1
|
|
|
import unittest |
|
2
|
|
|
|
|
3
|
|
|
from pyalgs.algorithms.graphs.search import DepthFirstSearch, BreadthFirstSearch |
|
4
|
|
|
from tests.algorithms.graphs.util import create_graph, create_digraph |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
View Code Duplication |
class DepthFirstSearchUnitTest(unittest.TestCase): |
|
|
|
|
|
|
8
|
|
|
def test_dfs(self): |
|
9
|
|
|
g = create_graph() # or create_digraph |
|
10
|
|
|
s = 0 |
|
11
|
|
|
dfs = DepthFirstSearch(g, s) |
|
12
|
|
|
|
|
13
|
|
|
for v in range(1, g.vertex_count()): |
|
14
|
|
|
if dfs.hasPathTo(v): |
|
15
|
|
|
print(str(s) + ' is connected to ' + str(v)) |
|
16
|
|
|
print('path is ' + ' => '.join([str(i) for i in dfs.pathTo(v)])) |
|
17
|
|
|
|
|
18
|
|
|
def test_dfs_digraph(self): |
|
19
|
|
|
g = create_digraph() |
|
20
|
|
|
s = 0 |
|
21
|
|
|
dfs = DepthFirstSearch(g, s) |
|
22
|
|
|
|
|
23
|
|
|
for v in range(1, g.vertex_count()): |
|
24
|
|
|
if dfs.hasPathTo(v): |
|
25
|
|
|
print(str(s) + ' is connected to ' + str(v)) |
|
26
|
|
|
print('path is ' + ' => '.join([str(i) for i in dfs.pathTo(v)])) |
|
27
|
|
|
|
|
28
|
|
View Code Duplication |
class BreadthFirstSearchUnitTest(unittest.TestCase): |
|
|
|
|
|
|
29
|
|
|
def test_dfs(self): |
|
30
|
|
|
g = create_graph() # or create_digraph |
|
31
|
|
|
s = 0 |
|
32
|
|
|
dfs = BreadthFirstSearch(g, s) |
|
33
|
|
|
|
|
34
|
|
|
for v in range(1, g.vertex_count()): |
|
35
|
|
|
if dfs.hasPathTo(v): |
|
36
|
|
|
print(str(s) + ' is connected to ' + str(v)) |
|
37
|
|
|
print('path is ' + ' => '.join([str(i) for i in dfs.pathTo(v)])) |
|
38
|
|
|
|
|
39
|
|
|
def test_dfs_digraph(self): |
|
40
|
|
|
g = create_digraph() |
|
41
|
|
|
s = 0 |
|
42
|
|
|
dfs = BreadthFirstSearch(g, s) |
|
43
|
|
|
|
|
44
|
|
|
for v in range(1, g.vertex_count()): |
|
45
|
|
|
if dfs.hasPathTo(v): |
|
46
|
|
|
print(str(s) + ' is connected to ' + str(v)) |
|
47
|
|
|
print('path is ' + ' => '.join([str(i) for i in dfs.pathTo(v)])) |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
if __name__ == '__main__': |
|
51
|
|
|
unittest.main() |
|
52
|
|
|
|