-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.2.py
139 lines (79 loc) · 2.56 KB
/
2.2.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
from collections import deque
from dimacs import *
#Przechodzi wszystkie testy, ale ostatni się długo wykonuje
def DFS_old(G):
def DFSVisit(G, v):
visited[v] = True
for i in range(len(G[v])):
if G[v][i] != 0 and visited[i] == False:
path[i] = v
DFSVisit(G,i)
visited = [False for _ in range(len(G))]
path = [-1 for _ in range(len(G))]
for i in range(len(G)):
if visited[i] == False:
DFSVisit(G, i)
return path
def DFS(M, s, t):
visited = [False] * len(M)
path = [-1 for _ in range(len(M))]
stack = deque()
visited[s] = True
stack.append(s)
while len(stack) > 0:
temp = stack.pop()
for i in range(len(M[temp])):
if M[temp][i] != 0 and visited[i] == False:
path[i] = temp
if i == t:
return path
visited[i] = True
stack.append(i)
return path
def fordFulkerson(M, s, t):
flow = 0
path = deque()
path = DFS(M, s, t)
while path[t] != -1:
temp = t
mini = float('inf')
while temp != s:
mini = min(mini, M[path[temp]][temp])
temp = path[temp]
if temp == - 1:
break
if temp != s:
break
temp = t
while temp != s:
M[path[temp]][temp] -= mini
M[temp][path[temp]] += mini
temp = path[temp]
flow += mini
path = DFS(M, s, t)
return flow
def Matrixify(V, L):
M = [ [0 for j in range(V)] for i in range(V)]
for v1, v2, w in L:
M[v1 - 1][v2 - 1] = w
return M
path = "C:\\Users\\48667\\Documents\\AGH UST\\Algorytmy-Grafowe\\graphs-lab2\\flow\\"
Graphs = ["simple", "simple2", "clique5", "clique20", "clique100", "grid5x5", "pp100", "rand20_100", "rand100_500", "trivial", "trivial2", "worstcase", "grid100x100",]
#Graphs = ["grid100x100"]
trues = 0
falses = 0
for str in Graphs:
V, L = loadDirectedWeightedGraph(path + str)
M = Matrixify(V, L)
result = fordFulkerson(M, 0, V - 1)
print("Rozwiazanie dla ", str, ": ", result)
x = readSolution(path + str)
if result == int(x):
trues += 1
print("Zgodne ze wzrorcowym rozwiązaniem")
else:
falses += 1
print("Niezgodne ze wzorcowym rozwiązaniem! Jest: ", result, ", powinno być: ", x)
print("")
print("Zaliczone testy: ", trues)
print("Niezaliczone testy: ", falses)