-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
139 lines (121 loc) · 3.56 KB
/
main.py
File metadata and controls
139 lines (121 loc) · 3.56 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from os import linesep
import sys
from edge import Edge
from graph import Graph
# adj = list(list())
adj = [ [2,1],\
[0,3],\
[0,3,4,5,6],\
[1,2,5],\
[2,5,6],\
[2,3,4,6],\
[2,4,5]\
]
# print('Length: ')
# print(len(adj))
# vs = list()
# uv = dict()
# # uv.get
# for u in range(0,(len(adj))):
# print("U->",u)
# for i in range(len(adj[u])):
# v = adj[u][i]
# print(u,"->", v)
# def printPath(aGraph:Graph, s, r):
# print('PrintPath...')
# if aGraph.parents[r] != r:
# return
# else:
# printPath(Graph, s, aGraph.parents[r])
# print(r)
def printPath(parents, r):
print('PrintPath...')
if parents[r] == r:
print(r)
print("yes")
return
else:
printPath(parents, parents[r])
print(r)
if __name__ == "__main__":
agraph = Graph(13)
agraph.addEdge(0, 2)
# agraph.addEdge(0, 1)
agraph.addEdge(0, 3)
agraph.addEdge(1,3)
agraph.addEdge(2,3)
agraph.addEdge(2,4)
agraph.addEdge(2,5)
agraph.addEdge(2,6)
agraph.addEdge(3,5)
agraph.addEdge(5,4)
agraph.addEdge(4,6)
agraph.addEdge(5,6)
agraph.addEdge(3,4)
# lines = ['addEdge 0 2', 'addEdge 0 3']
# for line in lines:
# if 'addEdge' in line.split():
# print(type(line))
# print(line.split())
# cmd = line.split()[0]
# u = line.split()[1]
# v = line.split()[2]
# print(cmd, u, v)
# for cmd in line:
# print(cmd)
# agraph.BFS(0)
# parents = agraph.parents
with open(sys.argv[1], 'r') as f:
print(sys.argv[1])
# contents = f.read()
lines = f.readlines()
print("Type: ", type(lines))
print("length: ", len(lines))
# print("Lines:")
# print(lines)
for line in lines:
commands = line.split()
if 'size' in commands:
size = int(commands[1])
graph = Graph( size )
elif 'addEdge' in commands:
u = int(commands[1])
v = int(commands[2])
graph.addEdge(u, v)
print('Added Edge')
print("(u,v) \t", u, '->', v)
elif 'BFS' in commands:
# print("BFS:")
s = int(commands[1])
print("Run Bfs on Source:", s)
graph.BFS( s )
graph.printParents()
graph.printDistance()
elif 'DFS' in commands:
graph.DFS()
graph.printParents()
graph.printColors()
graph.printStamps()
elif 'printGraph' in commands:
graph.printGraph()
else:
print('ERROR:\t command {} not found'.format(line))
# for cmd in lines:
# # print("CMD>> " , cmd)
# if cmd == 'addEdge':
# print(cmd)
# elif cmd == 'BFS':
# source = input("Enter integer source you would like to run BFS: \t")
# source = int(source)
# agraph.BFS(source)
# else:
# print('Command {} not found'.format(cmd))
# cmd = sys.argv[1]
# print(lines)
# for cmd in contents:
# print(cmd)
# print("Content: " , contents)
# agraph.printGraph()
# printPath(agraph, 0 , 6)
# printPath(parents=parents, r=4)
# agraph.printPath(0, 5)