-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiDiToAdjEuler.py
More file actions
233 lines (214 loc) · 7.23 KB
/
MultiDiToAdjEuler.py
File metadata and controls
233 lines (214 loc) · 7.23 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
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
228
229
230
231
from networkx.algorithms.components import connected
from networkx.readwrite import adjlist
from numpy import integer
import osmnx as ox
import matplotlib.pyplot as plt
import pickle
import os
import time
MAX = 9223372036854775807
def isEulerian(adjList):
count = 0
for e in adjList:
if len(e) % 2 != 0:
count += 1
if count > 2:
return False
return True
def is_DiEulerian(adjList):
D1 = None
D2 = None
for e in adjList:
if e[1] != 0:
if D1 == None:
D1 = e[1]
elif D2 == None:
D2 = e[1]
else:
return False
return True if (D1 == -1 and D2 == 1) or (D2 == 1 and D2 == -1) else False
def is_connected(G):
node = G[0][0]
verif = [len(G.__len__())] * False
queue = []
queue.append(node)
while queue:
tmp = queue.pop()
for a in tmp.adjacency():
if verif[a]:
continue
verif[a] = True
queue.append(a)
return not False in verif
def di_dijkstra(i,j, adjlist):
#print("DIJKSTRA")
queue = []
queue.append(([(i,0)],0))
smallestweight = 2000 #remettre MAX une fois que ça marche
path = []
visited = [False] * len(adjlist)
while queue:
#print("Boucle:")
#print("\tqueue = ",queue)
(current, currentweight) = queue.pop()
print(current)
visited[current[-1][0]] = True
#print("\tcurrent = ",current)
#print("\t CurrentAdj = ",adjlist[current[-1][0]])
print("Adj[Current] = ",adjlist[current[-1][0]])
for (voisin, poids) in adjlist[current[-1][0]][1]:
if visited[voisin] == True:
continue
if voisin == j:
if (currentweight + poids) < smallestweight:
smallestweight = currentweight + poids
path = current + [(voisin,poids)]
elif (currentweight + poids) < smallestweight:
queue.append((current + [(voisin,poids)], currentweight + poids))
time.sleep(4)
return (smallestweight, path)
def dijkstra(i,j, adjlist):
#print("adjlist in dijkstra:")
#print(adjlist)
queue = []
queue.append(([(i,0)],0))
smallestweight = MAX
path = []
visited = [False] * len(adjlist)
while queue:
(current, currentweight) = queue.pop()
visited[current[-1][0]] = True
for (voisin, poids) in adjlist[current[-1][0]]:
if visited[voisin] == True:
continue
if voisin == j:
if (currentweight + poids) < smallestweight:
smallestweight = currentweight + poids
path = current + [(voisin,poids)]
elif (currentweight + poids) < smallestweight:
queue.append((current + [(voisin,poids)], currentweight + poids))
return (smallestweight, path)
def change_degre(i, new_degree, adjlist):
adjlist[i] = new_degree
return adjlist
def di_addpath(path, adjlist):
print("path :", path)
precedent = None
for (node,poids) in path:
if precedent == None:
precedent = node
continue
print("\tprecedent = ",precedent)
print("\tnode = ",node)
print("\tDegre = ", adjlist[precedent][0])
print(type(adjlist[precedent][0]))
adjlist[precedent] = (adjlist[precedent][0] +1, adjlist[precedent][1])
adjlist[node] = (adjlist[node][0] - 1, adjlist[node][1])
adjlist[node][1].append((precedent, poids))
precedent = node
def addpath(path, adjlist):
precedent = None
for (node,poids) in path:
if precedent == None:
precedent = node
continue
adjlist[precedent].append((node, poids))
adjlist[node].append((precedent, poids))
precedent = node
return adjlist
def di_optirepair(oddslist_neg, oddslist_pos, adjlist):
print("Starting Repair")
print(oddslist_neg)
print(oddslist_pos)
while oddslist_pos:
smallestpath = (None, None)
currentdist = MAX
path = []
for i in range(0, len(oddslist_pos)):
for j in range(0, len(oddslist_neg)):
(tmp, pathtmp) = di_dijkstra(oddslist_pos[i], oddslist_neg[j], adjlist)
if tmp < currentdist:
smallestpath = (oddslist_pos[i], oddslist_neg[j])
path = pathtmp
di_addpath(path, adjlist)
if adjlist[smallestpath[0]][0] == 0:
oddslist_pos.remove(smallestpath[0])
if adjlist[smallestpath[1]][0] == 0:
oddslist_neg.remove(smallestpath[1])
print("Repair Ending")
def optirepair(oddslist, adjlist):
print("Starting Repair")
while oddslist:
smallestpath = (None, None)
currentdist = MAX
path =[]
for i in range(0,len(oddslist)-1):
for j in range (i+1, len(oddslist)):
(tmp, pathtmp) = dijkstra(oddslist[i],oddslist[j], adjlist)
if tmp < currentdist:
smallestpath = (oddslist[i],oddslist[j])
currentdist = tmp
path = pathtmp
addjlist = addpath(path, adjlist)
oddslist.remove(smallestpath[0])
oddslist.remove(smallestpath[1])
print("Repair Ending")
return adjlist
def fasterrepair(oddslist, adjlist):
#Amelioration de temps mais perte d'opti de trajet:
#Chercher le chemin le plus cours pour oddlist[0] jusqu'à [1] while oddlist
while oddslist:
path =[]
(tmp, pathtmp) = dijkstra(oddslist[0],oddslist[1], adjlist)
smallestpath = (oddslist[0],oddslist[1])
currentdist = tmp
path = pathtmp
addjlist = addpath(path, adjlist)
oddslist.remove(smallestpath[0])
oddslist.remove(smallestpath[1])
return adjlist
#Fonction to Eulerian
def to_Eulerian(adjlist):
# [[(n1,p1),(n2, p2)], ...]
oddslist = []
for i in range(0,len(adjlist)):
if len(adjlist[i]) % 2 != 0:
oddslist.append(i)
adjlist = optirepair(oddslist, adjlist)
return adjlist
def Di_to_Eulerian(adjlist):
# [(D,[(neigbhournode, weight)]), ...]
oddslist_neg = [] #moins d'arrete entrante que sortante
oddlist_positif = [] #plus d'arretes entrante que sortante
for i in range(0,len(adjlist)):
if adjlist[i][0] < 0:
oddslist_neg.append(i)
if adjlist[i][0] > 0:
oddlist_positif.append(i)
di_optirepair(oddlist_positif, oddslist_neg, adjlist)
def NodesToList(G):
resList = []
nodes = list(G.nodes())
for (count, node) in enumerate(nodes):
resList.append((count, node))
return resList
def DiGraph_to_adjlist(G):
nodes = list(G.nodes())
Trad = []
res = []
for node in nodes:
Trad.append(node)
res.append([])
for (i,j,p) in list(G.edges(data="length", default=1)):
res[Trad.index(i)].append((Trad.index(j),p))
return (Trad,res)
def DiGraph_to_Diadjlist(G):
nodes = list(G.nodes())
Trad = []
res = []
for node in nodes:
Trad.append(node)
res.append((G.degree(node),[]))
for (i,j,p) in list(G.edges(data="length", default=1)):
res[Trad.index(i)][1].append((Trad.index(j),p))
return (Trad,res)