-
Notifications
You must be signed in to change notification settings - Fork 0
/
sortEdges.py
29 lines (25 loc) · 895 Bytes
/
sortEdges.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
def sortEdges(edges):
"""
generate a connected sequence of nodes from connected couples
:param edges: an iterable of couple of hashable nodes
:return: a list of continuosly connected nodes (random direction)
"""
edgesIndex = {}
print "indexing"
for edge in edges:
a, b = edge
edgesIndex.setdefault(a, []).append(b)
edgesIndex.setdefault(b, []).append(a)
print "ok"
print "search for head"
for k, v in edgesIndex.items():
if len(v) == 1:
head = k
break
segment = [head, edgesIndex[head][0]]
print "forward"
nextVertex = [v for v in edgesIndex.get(segment[-1]) if v != segment[-2]]
while len(nextVertex) > 0:
segment.append(nextVertex[0])
nextVertex = [v for v in edgesIndex.get(segment[-1]) if v != segment[-2]]
return segment