-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.1.py
129 lines (76 loc) · 2.51 KB
/
3.1.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
from collections import deque
from dimacs import *
import copy
'''Algorytm działa poprawnie, ale powoli'''
'''
Pomysł na algorytm:
Sprawdzam ile istnieje ścieżek pomiędzy każdymi dwoma wierzchołkami
i biorę z tego minimum
'''
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):
pathCount = 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] -= 1
M[temp][path[temp]] -= 1
temp = path[temp]
pathCount += 1
path = DFS(M, s, t)
return pathCount
def MatrixifyNotDirected(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
M[v2 - 1][v1 - 1] = w
return M
path = "C:\\Users\\48667\\Documents\\AGH UST\\Algorytmy-Grafowe\\graphs-lab3\\"
Graphs = ["clique5", "clique20", "clique100", "clique200", "cycle", "geo20_2b", "geo20_2c", "geo100_2a", "grid5x5", "grid100x100", "mc1", "mc2", "path", "rand20_100", "rand100_500", "simple", "trivial"]
trues = 0
falses = 0
for str in Graphs:
V, L = loadWeightedGraph(path + str)
M = MatrixifyNotDirected(V, L)
result = float('inf')
for i in range(V - 1):
for j in range(i + 1, V - 1):
#print(fordFulkerson(copy.deepcopy(M), i, j))
result = min(result, fordFulkerson(copy.deepcopy(M), i, j))
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)