-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathtest_BreadthFirstPaths.py
executable file
·62 lines (51 loc) · 1.65 KB
/
test_BreadthFirstPaths.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python
from sys import stdout
from sys import argv
from os.path import join
from os.path import dirname
from os.path import abspath
from AlgsSedgewickWayne.Graph import Graph
from AlgsSedgewickWayne.BreadthFirstPaths import BreadthFirstPaths
from AlgsSedgewickWayne.testcode.InputArgs import cli_get_fin
TEST_DIR = dirname(abspath(__file__))
def test_0(prt=stdout):
"""Test BFS using Graph from file represented with ints."""
prt.write("\ntest_0: BFS using Graph with ints\n")
##num_args = len(argv[1:])
##g = cli_get_fin(argv[1] if num_args != 0 else join(TEST_DIR, "tinyCG.txt"))
##s = int(argv[2]) if num_args > 1 else 0
s = 0
g = cli_get_fin(join(TEST_DIR, "tinyCG.txt"))
G = Graph(g)
prt.write(str(G))
bfs = BreadthFirstPaths(G, s)
for v in range(G.V()):
if bfs.has_path_to(v):
prt.write(f"{s} to {v} ({bfs.get_dist_to(v)}): ")
for x in reversed(bfs.pathTo(v)):
if x == s: prt.write(str(x))
else: prt.write("-{}".format(x))
prt.write("\n")
else:
prt.write.printf("{} to {} (-): not connected\n".format(s, v))
def test_1(prt=stdout):
"""Test BFS using Graph from text-block represented with letters."""
txtblk = """
A: E B
B: E A F
C: D F
D: C G H
E: A B
F: C G B
G: D H F
H: G D
"""
prt.write("\ntest_1: BFS using Graph with letters\n")
G = Graph(adjtxt=txtblk)
bfs = BreadthFirstPaths(G, 'A', prt)
prt.write("\n{}\n".format(G))
def run_all(prt=stdout):
test_0()
test_1()
if __name__ == '__main__':
run_all()