-
Notifications
You must be signed in to change notification settings - Fork 2
/
mdmst.py
executable file
·184 lines (157 loc) · 5.59 KB
/
mdmst.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
from collections import *
from copy import *
def reverse_g(g):
rev_graph = {}
for node in g:
rev_graph[node] = {}
for a_node in g:
for b_node in g[a_node]:
rev_graph[b_node][a_node] = g[a_node][b_node]
return rev_graph
def update_dege(rg, root):
for node in rg:
if not node == root:
min = float('inf')
for in_nbr in rg[node]:
if rg[node][in_nbr] < min:
min = rg[node][in_nbr]
for in_nbr in rg[node]:
rg[node][in_nbr] -= min
def compute_rdst_candidate(rg, root):
candidate = {}
for node in rg:
candidate[node] = {}
for node in rg:
if not node == root:
min = float('inf')
for in_nbr in rg[node]:
if rg[node][in_nbr] < min:
min = rg[node][in_nbr]
for in_nbr in rg[node]:
if candidate[node] == {}:
if rg[node][in_nbr] == min:
candidate[node][in_nbr] = min
return candidate
def get_cycle(rdst_candidate):
node_unvisited = []
for node in rdst_candidate:
node_unvisited.append(node)
while not node_unvisited == []:
start_node = node_unvisited.pop()
stack = []
trail = []
stack.append(start_node)
while not len(stack) == 0:
node = stack.pop(-1)
for nbr in rdst_candidate[node]:
if nbr in trail:
return tuple(trail[trail.index(nbr):])
else:
stack.append(nbr)
trail.append(nbr)
if nbr in node_unvisited:
node_unvisited.remove(nbr)
return False
def contract_cycle(g, cycle):
cstar = max(g.keys()) + "1"
contracted_graph = {}
contracted_graph[cstar] = {}
for node in g:
if not node in cycle:
contracted_graph[node] = {}
for node in g:
for nbr in g[node]:
if node in cycle:
if nbr in cycle:
pass
else:
if nbr in contracted_graph[cstar]:
contracted_graph[cstar][nbr] = min(contracted_graph[cstar][nbr], g[node][nbr])
else:
contracted_graph[cstar][nbr] = g[node][nbr]
else:
if nbr in cycle:
if cstar in contracted_graph[node]:
contracted_graph[node][cstar] = min(contracted_graph[node][cstar], g[node][nbr])
else:
contracted_graph[node][cstar] = g[node][nbr]
else:
contracted_graph[node][nbr] = g[node][nbr]
return contracted_graph, cstar
def expand_graph(g, rdst_candidate, cycle, cstar):
restored_graph = {}
for node in g:
restored_graph[node] = {}
for node in rdst_candidate:
for nbr in rdst_candidate[node]:
if node == cstar:
min = float('inf')
for orig in cycle:
if nbr in g[orig]:
if g[orig][nbr] < min:
min = g[orig][nbr]
point = orig
restored_graph[point][nbr] = min
else:
if nbr == cstar:
min = float('inf')
for orig_nbr in g[node]:
if orig_nbr in cycle:
if g[node][orig_nbr] < min:
min = g[node][orig_nbr]
start_pt = orig_nbr
restored_graph[node][start_pt] = min
else:
restored_graph[node][nbr] = g[node][nbr]
for index in range(len(cycle) - 1):
restored_graph[cycle[index + 1]][cycle[index]] = g[cycle[index + 1]][cycle[index]]
restored_graph[cycle[0]][cycle[-1]] = g[cycle[0]][cycle[-1]]
index = cycle.index(start_pt)
if index == len(cycle) - 1:
restored_graph[cycle[0]].pop(cycle[index])
else:
restored_graph[cycle[index + 1]].pop(cycle[index])
return restored_graph
def bfs(g, startnode):
dist = {}
for node in g:
dist[node] = float('inf')
dist[startnode] = 0
queue = deque([startnode])
while queue:
node = queue.popleft()
for nbr in g[node]:
if dist[nbr] == float('inf'):
dist[nbr] = dist[node] + 1
queue.append(nbr)
return dist
def compute_rdmst(g, root):
if root not in g:
print "The root node does not exist"
return
distances = bfs(g, root)
for node in g:
if distances[node] == float('inf'):
print "The root does not reach every other node in the graph"
return
rdmst = compute_rdmst_helper(g, root)
rdmst_weight = 0
for node in rdmst:
for nbr in rdmst[node]:
rdmst[node][nbr] = g[node][nbr]
rdmst_weight += rdmst[node][nbr]
return (rdmst,rdmst_weight)
def compute_rdmst_helper(g, root):
rgraph = reverse_g(g)
update_dege(rgraph, root)
rdst_candidate = compute_rdst_candidate(rgraph, root)
cycle = get_cycle(rdst_candidate)
if not cycle:
return reverse_g(rdst_candidate)
else:
g_copy = deepcopy(rgraph)
g_copy = reverse_g(g_copy)
(contracted_g, cstar) = contract_cycle(g_copy, cycle)
new_rdst_candidate = compute_rdmst_helper(contracted_g, root)
rdmst = expand_graph(reverse_g(rgraph), new_rdst_candidate, cycle, cstar)
return rdmst