-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.py
More file actions
160 lines (132 loc) · 4.47 KB
/
graph.py
File metadata and controls
160 lines (132 loc) · 4.47 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
from edge import Edge
from timestamps import Timestamp
from queue import Queue
from collections import defaultdict
from collections import deque
import sys
# Global variable equivalent to infinity:
INT_MAX = sys.maxsize
class Graph(Timestamp):
stamps = Timestamp()
def __init__(self, N):
self.size = N
self.distance:list = [0] * self.size
self.parents:list =[0] * self.size
self.colors:list = [0] * self.size
# self.stamps:Timestamp = []
self.stamplist:list = [Graph.stamps]*self.size #= list()
self.Adj = defaultdict(list)
def addEdge(self, u:int, v:int) -> None:
'''
> Add an edge to our Graph G from U->V
'''
self.Adj[u].append( Edge(v,0) )
#end-AddEdge
def BFS(self, s) -> None:
for i in range(self.size):
self.distance[i] = INT_MAX
self.parents[i] = i
self.distance[ s ] = 0
aQ = Queue()
aQ.put( s )
while( aQ.empty() == False ):
u = aQ.get()
# print("U", u)
for i in range(len(self.Adj[ u ])):
v = self.Adj[u][i].neighbor
# print("(u,v) \t",u, "->", v)
if self.distance[v] == INT_MAX:
# set distance, parents and push into queue
self.distance[v] = self.distance[u] + 1
self.parents[v] = u
aQ.put( v )
#end-if(reachable)
#end-for (edge)
#end-while(items in Q)
#end-BFS
def printParents(self) -> None:
# for i in range(self.size):
print('Parents: ')
print(type(self.parents))
print(len(self.parents))
for v in range(len(self.parents)):
print(self.parents[v])
#end-for
#end-printParents
def printDistance(self) -> None:
print('Distance: ')
for v in range(len(self.distance)):
print(self.distance[v])
#end-for
#end-printDistance
def printColors(self) -> None:
print('Colors: ')
for v in range(len(self.colors)):
print(self.colors[v])
#end-for
#end-printColors
def printStamps(self) -> None:
print('TimeStamps: ')
for v in range(len(self.stamplist)):
print(self.stamplist[v].disc, self.stamplist[v].final)
#end-for
#end-print-timeStamps
def bfsPrint(self, s):
pass
def printGraph(self):
for u in range(self.size):
# print()
for i in range(len( self.Adj[u] )):
v = self.Adj[u][i].neighbor
# print
print(u, v)
# print(v, '\n')
#end-print_graph
def printPath(self, s, r):
'''
> Recursive algorithm to 'backtrack' shortest path given by BFS algorithm and parents(ancestors) array
> s is the source
> r is the target Vertex
'''
print('PrintPath...')
if r == self.parents[r]: # we've made it back to the source
print(r)
return
self.printPath(s, self.parents[r] )
print(r)
#end-printPath
def DFS(self) -> None:
'''
> Depth First Search algorithm used to traverse a graph G and visit each vertex V
'''
for i in range(self.size):
self.parents[i] = i
self.colors[i] = 'W'
t = 0
for i in range(self.size):
if self.colors[i] == 'W':
self.DFS_Visit( i, t)
# end-if
#end-for
#end-DFS
def DFS_Visit(self, u, t):
self.colors[u] = 'G'
self.stamplist[u].disc = t
# self.stamps[u] = t
t = t+1
for i in range(len(self.Adj[u])):
v = self.Adj[u][i].neighbor # get U's neighbor
if self.colors[v] == 'W': # we still need to visit the Node
# Mark the Node information as we visit
self.parents[v] = u
self.distance[v] = self.distance[u] + 1
self.colors[v] = 'G' # Mark the Node as visited. Could also use a boolean value here called 'found' to mark each Node.found=True
self.DFS_Visit(v, t)
# end-if
# end-for
# if it makes it any further set the finishing time of the node and set color to 'Black'
self.colors[u] = 'B'
self.stamplist[u].final = t
# self.stamps[u] = t
t = t+1
#end-DFS_Visit