-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler.py
229 lines (181 loc) · 4.3 KB
/
euler.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
228
229
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from algopy import graphmat
from random import randint, choice
import time
import sys
class LinkedList:
""" Simple class for LinkedList
Attributes:
value (int): value of the node
next (LinkedList): next node
"""
def __init__(self, value, prev):
""" Init LinkedList
Args:
value (int): value of the node
prev (LinkedList): node into which this node will be inserted
"""
if prev:
self.next = prev.next
prev.next = self
else:
self.next = None
self.value = value
def graphDeg(G):
""" Retrieve the degree of each vertices and vertices with an odd value of a graph
Args:
G (GraphMat)
Results:
int list: degree of each vertices
int list: odd vertices
"""
deg = [0] * G.order
odd = []
for i in range(G.order):
deg[i] = G.adj[i][i]
ln = -1
for j in range(G.order):
deg[i] += G.adj[i][j]
if deg[i] % 2 == 1:
odd.append(i)
return (deg, odd)
def isEulerianPath(path, G):
""" Check if the given path is a valid eulerian path for the given graph
This version destructs the provided graph
Args:
path (LinkedList)
G (GraphMat)
Results:
bool
"""
while path.next:
a, b = path.next.value, path.value
if G.adj[b][a] == 0:
return False
G.adj[b][a] -= 1
if a != b:
G.adj[a][b] -= 1
path = path.next
for i in range(G.order):
for j in range(i):
if G.adj[i][j] != 0:
return False
return True
def __hasSuccessor(G, i, s = 1):
l = G.adj[i]
if l == None:
return 0
l0 = l[0]
if l0 < 0:
if l[-l0] == 0:
s = -l0 + 1
l0 = 0
else:
return 1
if l0 == 0:
for j in range(s, G.order):
if l[j]:
l[0] = -j
return 1
G.adj[i] = None
return 0
else:
return 1
def __popSuccessor(G, i):
l = G.adj[i]
j = -l[0] if l[0] < 0 else 0
if i != j:
G.adj[j][i] -= 1
if l[j] == 1:
l[0] = 0
__hasSuccessor(G, i, s = j + 1)
else:
l[j] -= 1
return j
def createEulerPath(G, odd):
"""
Create an euler path/cycle for the given graph. The graph will be destroyed during
the process.
Args:
G (graphmat): Eulerian graph
odd (int list): List of graph nodes that have an odd degree
Returns:
LinkedList: An eulerian path/cycle
"""
stack = [LinkedList(odd[0] if odd else 0, None)]
while stack:
n = stack[-1]
if not __hasSuccessor(G, n.value):
stack.pop()
continue
n2 = LinkedList(__popSuccessor(G, n.value), n)
if not G.adj[n.value]:
stack.pop()
stack.append(n2)
return n
def testEulerPath(G, G2, odd):
""" Test function for createEulerPath
Args:
G (GraphMat): the graph which will be used to create the path
G2 (GraphMat): a copy of the graph
odd (int list): odd nodes
Results:
bool
"""
path = createEulerPath(G, odd)
return isEulerianPath(path, G2)
def createEulerianGraph(dst, verticesCount, minEdgeCount):
""" Create an eulerian graph and store it in a file (.gra)
Args:
dst (string): path for the resulting file
verticesCount (int): vertices count for the resulting graph
minEdgeCount (int): minimal edge count for the resulting graph
Result:
GraphMat
"""
G = graphmat.GraphMat(verticesCount)
for i in range(minEdgeCount):
a, b = randint(0, verticesCount - 1), randint(0, verticesCount - 1)
G.adj[a][b] += 1
if a != b:
G.adj[b][a] += 1
_, odd = graphDeg(G) # TODO à changer
for i in range(0, len(odd) - 2 * randint(0, 1), 2): # 1 chance sur 2 d'avoir des impairs
G.adj[odd[i]][odd[i+1]] += 1
G.adj[odd[i+1]][odd[i]] += 1
graphmat.savegra(G, dst)
return G
def __genConnexComponents(connexComponentCount, minVertices, maxVertices):
lens, nodes, total = [], [], 0
for i in range(connexComponentCount):
v = randint(minVertices, maxVertices)
total += v
lens.append((i, v))
nodes.append([])
for i in range(0, total):
j = randint(0, len(lens) - 1)
n, c = lens[j]
nodes[n].append(i)
if c == 1:
lens[j] = lens[-1]
lens.pop()
else:
lens[j] = (n, c - 1)
return nodes, total
def eulerTime(G):
t = time.time()
createEulerPath(G, [])
print(time.time() - t)
"""
createEulerianGraph("graph_example/eulerian5000.gra", 5000, 5000*500)
"""
"""
G = graphmat.loadgra("graph_example/eulerian" + sys.argv[1] + ".gra")
G2 = graphmat.loadgra("graph_example/eulerian" + sys.argv[1] + ".gra")
print( testEulerPath(G, G2, []) )
"""
"""
G = graphmat.loadgra("graph_example/eulerian" + sys.argv[1] + ".gra")
eulerTime(G)
"""