-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcviceni11.py
227 lines (203 loc) · 5.57 KB
/
cviceni11.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import math
undirGraph1 = \
[7, 8,
0, 1,
0, 2,
0, 3,
1, 3,
3, 4,
4, 5,
4, 6,
5, 6]
undirGraph2 = \
[9, 8,
0, 1,
1, 2,
2, 3,
3, 4,
3, 5,
2, 6,
1, 7,
7, 8]
dirGraph1 = \
[4, 4,
0, 1,
1, 2,
2, 3,
3, 0]
dirGraph2 = \
[4, 4,
0, 1,
0, 2,
1, 3,
2, 3]
def loadAsMatrix(list, sym=True):
v, e, *edges = list
matrix = [[0] * v for _ in range(v)]
for i in range(0, 2 * e, 2):
matrix[edges[i]][edges[i + 1]] = 1
if sym: matrix[edges[i + 1]][edges[i]] = 1
for line in matrix:
print(*line)
return matrix
def loadAsNeighborsList(list, sym=True):
v, e, *edges = list
neighbors = [[] for _ in range(v)]
for i in range(0, 2 * e, 2):
neighbors[edges[i]].append(edges[i + 1])
if sym: neighbors[edges[i + 1]].append(edges[i])
for n in neighbors:
print(*n)
return neighbors
class Node:
def __init__(self, ix):
self.index = ix
self.neighbors = []
def __repr__(self):
return str(self.index)
def loadAsNodes(list, sym=True):
v, e, *edges = list
nodes = [Node(i) for i in range(v)]
for i in range(0, 2 * e, 2):
nodes[edges[i]].neighbors.append(nodes[edges[i + 1]])
if sym: nodes[edges[i + 1]].neighbors.append(nodes[edges[i]])
for n in nodes:
print(n.index, n.neighbors)
return nodes
# Pro následující funkce budeme používat seznam sousedů.
def undirConnected(graph):
visited = [False for _ in graph]
def dfs(vertex):
nonlocal visited
if visited[vertex]:
return
visited[vertex] = True
for next in graph[vertex]:
dfs(next)
dfs(0)
for v in visited:
if not v: return False
return True
# Pro jednoduchost v následujících "undir" funkcích předpokládáme, že je graf souvislý.
def undirCyclic(graph):
visited = [False for _ in graph]
stack = [(0, None)]
while len(stack) > 0:
vertex, parent = stack.pop()
if visited[vertex]:
return True
visited[vertex] = True
for next in graph[vertex]:
if parent != next:
stack.append((next, vertex))
return False
def undirFurthest(graph, begin=0):
bestVertex = -1
bestDistance = -1
visited = [False for _ in graph]
queue = [(begin, 0)]
while len(queue) > 0:
vertex, distance = queue.pop(0)
if visited[vertex]:
continue
visited[vertex] = True
if distance > bestDistance:
bestVertex = vertex
bestDistance = distance
for next in graph[vertex]:
queue.append((next, distance + 1))
return (bestVertex, bestDistance)
def undirLongestPath(graph):
start, _ = undirFurthest(graph)
end, _ = undirFurthest(graph, start)
return (start, end)
def undirPath(graph, begin, end):
prevs = [None for _ in graph]
visited = [False for _ in graph]
queue = [(begin, None)]
while len(queue) > 0:
vertex, prev = queue.pop(0)
if visited[vertex]:
continue
visited[vertex] = True
if prevs[vertex] == None:
prevs[vertex] = prev
for next in graph[vertex]:
queue.append((next, vertex))
if prevs[end] == None:
return None
else:
path = []
vertex = end
while vertex != None:
path.append(vertex)
vertex = prevs[vertex]
return path
def dirCyclicRec(graph):
active = [False for _ in graph]
explored = [False for _ in graph]
def dfs(vertex):
nonlocal active, explored
if explored[vertex]:
return False
if active[vertex]:
return True
active[vertex] = True
cycle = False
for next in graph[vertex]:
cycle = cycle or dfs(next)
active[vertex] = False
explored[vertex] = True
return cycle
for start in range(len(graph)):
if explored[start]:
continue
if dfs(start):
return True
return False
def dirCyclic(graph):
active = [False for _ in graph]
explored = [False for _ in graph]
for start in range(len(graph)):
if explored[start]:
continue
stack = [(start, True)]
while len(stack) > 0:
vertex, first = stack.pop()
if first:
if explored[vertex]:
continue
if active[vertex]:
return True
active[vertex] = True
stack.append((vertex, False))
for next in graph[vertex]:
stack.append((next, True))
else:
active[vertex] = False
explored[vertex] = True
return False
def dirTopologicalSort(graph):
active = [False for _ in graph]
explored = [False for _ in graph]
topo = []
for start in range(len(graph)):
if explored[start]:
continue
stack = [(start, True)]
while len(stack) > 0:
vertex, first = stack.pop()
if first:
if explored[vertex]:
continue
if active[vertex]:
return None
active[vertex] = True
stack.append((vertex, False))
for next in graph[vertex]:
stack.append((next, True))
else:
active[vertex] = False
explored[vertex] = True
topo.append(vertex)
return topo