-
Notifications
You must be signed in to change notification settings - Fork 1
/
algorithms.py
173 lines (155 loc) · 4.19 KB
/
algorithms.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
from graph import *
from utils import *
# Trouver la fermeture transitive
def royWarshall(g):
closure = g.U
for i in g.X:
for x in g.X:
if (x, i) in closure:
for y in g.X:
if (i, y) in closure:
if (x, y) not in closure:
closure.append((x,y))
return Graph(g.X, closure)
# Trouver les connexite d'un pont dans un graphe non-oriente
def tarjan(g, a):
p = []
d = []
n = []
num = []
#init
for e in g.X:
p.append(0)
d.append(len(g.adja(e)))
n.append(0)
num.append(0)
k = 1
i = a
num[a-1] = 1
p[a-1] = a
# main step
while n[i-1] != d[i-1] or i != a:
if n[i-1] == d[i-1]:
i = p[i-1] # on remonte dans l'arborescence
else :
n[i-1] = n[i-1]+1 # on explore le sommet suivant de gamma(i)
j = g.adja(i)[n[i-1]-1]
if p[j-1] == 0:
p[j-1] = i
i = j
k = k+1
num[i-1] = k
return k, num
# Obtenir les cfc de G en utilisant G+
def foulkes(g):
gpp = royWarshall(g)
nc = g.X[:] # I slice g.X to get a brand new list. Not a reference
cfc = []
for i in g.X:
if i in nc: # calcul de la cfc de i
cfci = [i]
nc.remove(i)
if (i,i) in gpp:
for j in g.X[i-1:]:
if j in nc:
if (i,j) in gpp and (j,i) in gpp:
cfci.append(j)
nc.remove(j)
cfc.append(cfci)
return cfc
# obtenir les ascendants non classes
def ascNonClasse(x, g, nc):
A = []
def ancetre(y):
A.append(y)
Lpred = intersect(g.pred(y), nc)
for z in Lpred:
if z not in A:
ancetre(z)
A = []
ancetre(x)
return A
# obtenir les descendants non classes
def descNonClasse(x, g, nc):
D = []
def fils(y):
D.append(y)
Lsuc = intersect(g.succ(y), nc)
for z in Lsuc:
if z not in D:
fils(z)
D = []
fils(x)
return D
# Moore-Dijkstra
def mooreDijkstra(g):
S = [1]
L = {1:0}
pred = dict()
for i in g.X[1:]:
if (1, i) in g.U:
L[i] = g.getValue(1,i)
pred[i] = 1
else:
L[i] = (float("inf"))
while not set(S) == set(g.X):
sliced = {k:v for k,v in L.items() if k not in S}
i = min(sliced, key=sliced.get)
S.append(i)
for j in g.succ(i):
if L[j] > L[i]+g.getValue(i,j):
L[j] = L[i]+g.getValue(i,j)
pred[j] = i
return pred, L
# Bellman pour l'ordonnancement cad recherche de chemin maximum
def bellmanOrdo(g):
L = [{1:0}]
pred = {}
for i in g.X[1:]:
L[0][i] = -float('inf')
k = 1
end = False
n = len(g.X)
while k <= n and not end:
L.append({1:0})
for i in g.X[1:]:
jmax = 1
maxj = -float('inf')
for j in g.pred(i):
if L[k-1][j]+g.getValue(j,i) > maxj:
maxj = L[k-1][j]+g.getValue(j,i)
jmax = j
L[k][i] = max (L[k-1][i], maxj)
if L[k][i] != L[k-1][i]:
pred[i] = jmax
#print L[k]
if L[k] == L[k-1]:
end = True
else:
k += 1
if k == n+1:
raise CycleBellmanException("G possede des circuits absorbants")
else:
#return {str(unichr(ke+63)):v for ke,v in L[k].items() if ke!=1}, pred
return {ke:v for ke,v in L[k].items() if ke != 1}, pred
#Algo de Ford pour l'ordonnancement
def fordOrdo(g):
L = {1:0}
Lold = {}
pred = {}
for i in g.X[1:]:
L[i] = float('inf')
while Lold != L:
Lold = L
for i in g.X[1:]:
Lold = L
minj = float('inf')
jmin = 0
for j in g.pred(i):
if minj > L[j]+g.getValue(j,i):
minj = L[j]+g.getValue(j,i)
jmin = j
L[i] = min(L[i], minj)
if jmin != 0:
pred[i] = jmin
return L,pred