/
main.py
111 lines (99 loc) · 2.98 KB
/
main.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from DiGraph import DiGraph
from GraphAlgo import GraphAlgo
def check():
"""
Graph: |V|=4, |E|=5
{0: 0: |edges_out| 1 |edges in| 1, 1: 1: |edges_out| 3 |edges in| 1, 2: 2: |edges_out| 1 |edges in| 1, 3: 3: |edges_out| 0 |edges in| 2}
{0: 1}
{0: 1.1, 2: 1.3, 3: 10}
(3.4, [0, 1, 2, 3])
(2.8, [0, 1, 3])
(inf, [])
(None, inf)
2.062180280059253 [1, 10, 7]
17.693921758901507 [47, 46, 44, 43, 42, 41, 40, 39, 15, 16, 17, 18, 19]
11.51061380461898 [20, 21, 32, 31, 30, 29, 14, 13, 3, 2]
inf []
([1, 9, 2, 3], 2.370613295323088)
(None, inf)
([1, 2, 3, 4], 4.5)
"""
check0()
check1()
check2()
check3()
def check0():
"""
This function tests the naming (main methods of the DiGraph class, as defined in GraphInterface.
:return:
"""
g = DiGraph() # creates an empty directed graph
for n in range(4):
g.add_node(n)
g.add_edge(0, 1, 1)
g.add_edge(1, 0, 1.1)
g.add_edge(1, 2, 1.3)
g.add_edge(2, 3, 1.1)
g.add_edge(1, 3, 1.9)
g.remove_edge(1, 3)
g.add_edge(1, 3, 10)
print(g) # prints the __repr__ (func output)
print(g.get_all_v()) # prints a dict with all the graph's vertices.
print(g.all_in_edges_of_node(1))
print(g.all_out_edges_of_node(1))
g_algo = GraphAlgo(g)
print(g_algo.shortest_path(0, 3))
g_algo.plot_graph()
def check1():
"""
This function tests the naming (main methods of the GraphAlgo class, as defined in GraphAlgoInterface.
:return:
"""
g_algo = GraphAlgo() # init an empty graph - for the GraphAlgo
file = "data/T0.json"
g_algo.load_from_json(file) # init a GraphAlgo from a json file
print(g_algo.shortest_path(0, 3))
print(g_algo.shortest_path(3, 1))
print(g_algo.centerPoint())
g_algo.save_to_json(file + '_saved')
g_algo.plot_graph()
def check2():
""" This function tests the naming, basic testing over A5 json file.
:return:
"""
g_algo = GraphAlgo()
file = '../data/A5'
g_algo.load_from_json(file)
g_algo.get_graph().remove_edge(13, 14)
g_algo.save_to_json(file + "_edited")
dist, path = g_algo.shortest_path(1, 7)
print(dist, path)
dist, path = g_algo.shortest_path(47, 19)
print(dist, path)
dist, path = g_algo.shortest_path(20, 2)
print(dist, path)
dist, path = g_algo.shortest_path(2, 20)
print(dist, path)
print(g_algo.TSP([1, 2, 3]))
g_algo.plot_graph()
def check3():
""" This function tests the naming, basic testing over A5 json file.
:return:
"""
g = DiGraph() # creates an empty directed graph
for n in range(5):
g.add_node(n)
g.add_edge(0, 1, 1)
g.add_edge(0, 4, 5)
g.add_edge(1, 0, 1.1)
g.add_edge(1, 2, 1.3)
g.add_edge(1, 3, 1.9)
g.add_edge(2, 3, 1.1)
g.add_edge(3, 4, 2.1)
g.add_edge(4, 2, .5)
g_algo = GraphAlgo(g)
print(g_algo.centerPoint())
print(g_algo.TSP([1, 2, 4]))
g_algo.plot_graph()
if __name__ == '__main__':
check()