-
Notifications
You must be signed in to change notification settings - Fork 1
/
RG.py
155 lines (117 loc) · 4.41 KB
/
RG.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
# -*- coding: utf-8 -*-
import numpy as np
import networkx as nx
import copy
import matplotlib.pyplot as plt
class RG(object):
def __init__(self, directed = True):
self.node_list = []
self.edge_dic = {}
self.neighbor = {}
self.directed = directed
self.tag = {}
def add_node(self, node_id):
if node_id not in self.node_list:
self.node_list.append(node_id)
self.neighbor[node_id] = []
self.tag[node_id] = 0 #tag
return len(self.node_list)-1
else:
return -1
def add_edge(self, edge, weight = 1):
if edge not in self.edge_dic.keys():
self.edge_dic[edge] = weight
else:
self.edge_dic[edge] =self.edge_dic[edge] + weight
fr = edge[0]
to = edge[1]
if to not in self.neighbor[fr]:
self.neighbor[fr].append(to)
return self.edge_dic[edge]
def tran_to_nx(self):
g = nx.DiGraph()
edge_list = []
for key, val in self.edge_dic.items():
edge = (key[0], key[1], val)
edge_list.append(edge)
g.add_nodes_from(self.node_list)
g.add_weighted_edges_from(edge_list)
return g
def dfs_from(self, source, step, path = [], paths = []):
#print 'dfs_dfs_dfs', source, len(path) #test
#print 'neigh of source', self.neighbor[source] #test
#print 'paths length', len(paths) #test
path.append(source)
if len(path) == step+1:
new_path = copy.copy(path)
paths.append(new_path)
path.remove(source)
return paths
for neigh in self.neighbor[source]:
if neigh not in path:
self.dfs_from(neigh, step, path, paths)
path.remove(source)
return paths
def max_path_dfs_from(self, source, step, path = [], max_path = {}):
path.append(source)
if len(path) == step+1:
sum_EOF = self.cal_sum_EOF(path)
if len(max_path) == 0:
new_path = copy.copy(path)
max_path[tuple(new_path)] = sum_EOF
else:
max_sum_EOF = max_path.values()
if sum_EOF > max_sum_EOF[0]:
new_path = copy.copy(path)
max_path.popitem()
max_path[tuple(new_path)] = sum_EOF
path.remove(source)
return max_path
for neigh in self.neighbor[source]:
if neigh not in path:
self.max_path_dfs_from(neigh, step, path, max_path)
path.remove(source)
return max_path
def max_path_dfs_from_2(self, source, step, path = [], max_path = {}):
path.append(source)
self.tag[source] = 1
if len(path) == step+1:
sum_EOF = self.cal_sum_EOF(path)
if len(max_path) == 0:
new_path = copy.copy(path)
max_path[tuple(new_path)] = sum_EOF
else:
max_sum_EOF = max_path.values()
if sum_EOF > max_sum_EOF[0]:
new_path = copy.copy(path)
max_path.popitem()
max_path[tuple(new_path)] = sum_EOF
path.remove(source)
self.tag[source] = 0
return max_path
for neigh in self.neighbor[source]:
if self.tag[neigh] == 0:
self.max_path_dfs_from(neigh, step, path, max_path)
path.remove(source)
self.tag[source] = 0
return max_path
def cal_sum_EOF(self, path):
current_EOF = 0.0
for i in range(len(path) - 1):
edge = (path[i], path[i + 1])
current_EOF += self.edge_dic[edge]
return current_EOF
def draw_graph(self):
G = self.tran_to_nx()
pos = nx.shell_layout(G)
#weight_list = self.edge_dic.values()
weight_list = []
for edge in G.edges():
weight_list.append(self.edge_dic[edge])
nx.draw_networkx(G, pos, node_color = 'cyan', edge_color = 'navy' , label = self.node_list, node_size =800, width = [item for item in weight_list], linewidths = 0.1)
plt.show()
def save_graph(self, path):
new_file = open(path, 'w')
for item in self.edge_dic.keys():
new_file.write(str(item[0]) + ' ' + str(item[1]) + ' ' + str(self.edge_dic[item]) +'\n')
new_file.close()