wh_cyc = cycle
cycle = wh_cyc
cyc_nodes = list(cycle.keys())rep = cyc_nodes[0]
if self.verbose:
print("Found Cycle\n")
for node in cyc_nodes:
print("{0} ".format(node))
print("\n")
cyc_weight = 0.0
for node in cyc_nodes:
cyc_weight += scores[par[node], node]
for i in range(0, nw+1):
if 0 == curr_nodes[i] or (i in cycle):
continue
max1 = -np.inf
wh1 = -1
max2 = -np.inf
wh2 = -1
for j1 in cyc_nodes:
if scores[j1, i] > max1:
max1 = scores[j1, i]
wh1 = j1
// cycle weight + new edge - removal of old
scr = cyc_weight + scores[i, j1] - scores[par[j1], j1]
if scr > max2:
max2 = scr
wh2 = j1
scores[rep, i] = max1
old_I[rep, i] = old_I[wh1, i]
old_O[rep, i] = old_O[wh1, i]
scores[i, rep] = max2
old_O[i, rep] = old_O[i, wh2]
old_I[i, rep] = old_I[i, wh2]
rep_cons = []
for i in range(0, np.size(cyc_nodes)):
rep_con = {}
keys = sorted(reps[int(cyc_nodes[i])].keys())
if self.verbose:
print("{0}: ".format(cyc_nodes[i]))
for key in keys:
rep_con[key] = 0
if self.verbose:
print("{0} ".format(key))
rep_cons.append(rep_con)
if self.verbose:
print("\n")
// don"t consider not representative nodes
// these nodes have been folded
for node in cyc_nodes[1:]:
curr_nodes[node] = 0
for key in reps[int(node)]:
reps[int(rep)][key] = 0
self.chu_liu_edmonds(scores, curr_nodes, old_I, old_O, final_edges, reps)
// check each node in cycle, if one of its representatives
// is a key in the final_edges, it is the one.
if self.verbose:
print(final_edges)
wh = -1
found = False
for i in range(0, np.size(rep_cons)):
if found:
break
for key in rep_cons[i]:
if found:
break
if key in final_edges:
wh = cyc_nodes[i]
found = True
l = par[wh]
while l != wh:
ch = old_O[par[l]][l]
pr = old_I[par[l]][l]
final_edges[ch] = pr
l = par[l]
After Change
wh_cyc = cycle
cycle = wh_cyc
cyc_nodes = sorted(list(cycle.keys()))
rep = cyc_nodes[0]
if self.verbose:
print("Found Cycle\n")
for node in cyc_nodes:
print("{0} ".format(node))
print("\n")
cyc_weight = 0.0
for node in cyc_nodes:
cyc_weight += scores[par[node], node]
for i in range(0, nw+1):
if 0 == curr_nodes[i] or (i in cycle):
continue
max1 = -np.inf
wh1 = -1
max2 = -np.inf
wh2 = -1
for j1 in cyc_nodes:
if scores[j1, i] > max1:
max1 = scores[j1, i]
wh1 = j1
// cycle weight + new edge - removal of old
scr = cyc_weight + scores[i, j1] - scores[par[j1], j1]
if scr > max2:
max2 = scr
wh2 = j1
scores[rep, i] = max1
old_I[rep, i] = old_I[wh1, i]
old_O[rep, i] = old_O[wh1, i]
scores[i, rep] = max2
old_O[i, rep] = old_O[i, wh2]
old_I[i, rep] = old_I[i, wh2]
rep_cons = []
for i in range(0, np.size(cyc_nodes)):
rep_con = {}
keys = sorted(reps[int(cyc_nodes[i])].keys())
if self.verbose:
print("{0}: ".format(cyc_nodes[i]))
for key in keys:
rep_con[key] = 0
if self.verbose:
print("{0} ".format(key))
rep_cons.append(rep_con)
if self.verbose:
print("\n")
// don"t consider not representative nodes
// these nodes have been folded
for node in cyc_nodes[1:]:
curr_nodes[node] = 0
for key in reps[int(node)]:
reps[int(rep)][key] = 0
self.chu_liu_edmonds(scores, curr_nodes, old_I, old_O, final_edges, reps)
// check each node in cycle, if one of its representatives
// is a key in the final_edges, it is the one.
if self.verbose:
print(final_edges)
wh = -1
found = False
for i in range(0, np.size(rep_cons)):
if found:
break
for key in rep_cons[i]:
if found:
break
if key in final_edges:
wh = cyc_nodes[i]
found = True
l = par[wh]
while l != wh:
ch = old_O[par[l]][l]
pr = old_I[par[l]][l]
final_edges[ch] = pr
l = par[l]