-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph.py
146 lines (115 loc) · 4.72 KB
/
Graph.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
import copy
import re
from Vec import *
class Node:
def __init__(self, label):
self.label = label
self.targets = []
self.vec = Vec(0, 0)
def edge(self, weights, *nodes ):
for n in nodes:
self.targets.append((weights,n))
n.targets.append((weights,self))
return self
def deg(self):
return len(self.targets)
class Graph:
def __init__(self):
self.nodes = []
self.adjacency_mx = []
def read(self, file):
f = open(file, 'r')
node_info = []
for line in f:
if ':' in line:
line = line.strip('\n')
lspl = line.split(':')
inline = (lspl[0], lspl[1].split(','))
node_info.append(inline)
self.node(lspl[0])
for nd in self.nodes:
#pick a node
for ninf in node_info:
#scroll through the info
if nd.label == ninf[0]:
#found info about the node
#process targets correctly
trgts = []
for trgt in ninf[1]:
trgts.append(trgt.split('*'))
for nd_edge in trgts:
#scroll through the targets
for nd_tar in self.nodes:
#find instances of targets
if nd_tar.label == nd_edge[0]:
nd.edge(nd_edge[1],nd_tar)
self.adjacency_mx_gen()
def node(self, label):
self.nodes.append(Node(label))
return self.nodes[-1]
def adjacency_mx_gen(self):
for nd in self.nodes: #main traverse
ad_mx_row = []
for nd_ in self.nodes: #for each node go through nodes again
flag = True
for trgt in nd.targets: #for each 2-nd layer node find out if it`s a target
if nd_.label == trgt[1].label:
ad_mx_row.append(int(trgt[0]))
flag = False
if flag :
ad_mx_row.append(0)
self.adjacency_mx.append(ad_mx_row)
def depth_check(self, cur_build, adjacency_mx, adjacency_mx_size, weight):
flag = True
for i in range(adjacency_mx_size) :
if adjacency_mx[int(cur_build[-1])-1][i] > 0 :
weight+=adjacency_mx[int(cur_build[-1])-1][i]
flag = False
perm_adjacency_mx = copy.deepcopy(adjacency_mx) #OHRENET` ESLI BI NE DOPER SHO TUT SSILKA VMESTO KOPII PEREDAVALAS` NA OBJEKT DROPNUL BI NAHUI ETU ZADACHU
perm_adjacency_mx[i][int(cur_build[-1])-1] = 0
perm_adjacency_mx[int(cur_build[-1])-1][i] = 0
traf_cur_build = copy.deepcopy(cur_build)
traf_weight = copy.deepcopy(weight)
self.depth_check(traf_cur_build+" "+str(i+1),perm_adjacency_mx,adjacency_mx_size,weight)
if flag:
f = open('traverselits.txt', 'a+')
f.writelines(cur_build+"w"+str(weight)+'\n')
f.close()
def hamilton_checks(self,nodes):
self.depth_check(nodes[0],self.adjacency_mx,len(self.adjacency_mx),0)
f_ = open('traverselits.txt','r')
brdrs=""
paths= []
for lines in f_:
paths.append(lines.strip('\n'))
brdrs = nodes[0]
brdrs_ = nodes[-1]
nodes.remove(brdrs)
nodes.remove(brdrs_)
for nd_id in nodes:
comp = re.compile(".*"+brdrs+"+.*"+nd_id+"+.*"+brdrs_+"+.*w")
temp_paths=[]
for pt in paths:
if re.match(comp, pt):
f_p=pt.split("w")[0]
comp_=re.compile(brdrs+"+.*"+brdrs_)
r=re.match(comp_,f_p)
if r is not None:
temp_paths.append(r.group(0)+"w"+pt.split("w")[1])
paths = copy.deepcopy(temp_paths)
if len(nodes) ==0:
comp = re.compile(".*"+brdrs+"+.*"+brdrs_+"+.*w")
temp_paths=[]
for pt in paths:
if re.match(comp, pt):
f_p=pt.split("w")[0]
comp_=re.compile(brdrs+"+.*"+brdrs_)
r=re.match(comp_,f_p)
if r is not None:
temp_paths.append(r.group(0)+"w"+pt.split("w")[1])
paths = copy.deepcopy(temp_paths)
p_=[]
for p in paths:
p_.append([p.split("w")[0],p.split("w")[1]])
p_=sorted(p_, key=lambda x: int(x[1]))
return p_