-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraphLib.py
executable file
·63 lines (47 loc) · 1.42 KB
/
graphLib.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def removeEdges(N, E):
for s, d in E:
if d in N[s]['out']:
N[s]['out'].remove(d)
if s in N[d]['in']:
N[d]['in'].remove(s)
def insertEdges(N, E):
for s, d in E:
if not d in N[s]['out']:
N[s]['out'].append(d)
if not s in N[d]['in']:
N[d]['in'].append(s)
def removeNode(N, n):
if N.get(n) == None:
return
for p in N[n]['in']: # for parent (remove all that have this as child)
N[p]['out'].remove(n)
for c in N[n]['out']: # for child (remove all that have this as parent)
N[c]['in'].remove(n)
del N[n] # dictionary remove
def printGraph(N):
for n in N:
print ' ' + str(n)
for e in N[n]:
print ' {}: {}'.format(e, N[n][e])
def twistEdges(N, E):
removeEdges(N, E)
E_inv = [ (d, s) for s, d in E ]
insertEdges(N, E_inv)
return E_inv
def graphFromEdges(E):
N = {}
# node: in = [] out = []
for e in E:
s, d = e
if N.get(s) == None:
N[s] = {'in': [], 'out': []}
if N.get(d) == None:
N[d] = {'in': [], 'out': []}
N[s]['out'].append(d) # out
N[d]['in'].append(s) # in
return N
if __name__ == '__main__':
edges = [ ('A', 'B'), ('A', 'E'), ('B', 'C'), ('C', 'D'), ('D', 'C'), ('A', 'C')]
printGraph(graphFromEdges(edges))