-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.py
More file actions
180 lines (156 loc) · 5.41 KB
/
Graph.py
File metadata and controls
180 lines (156 loc) · 5.41 KB
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
class GraphDS:
graph = [] #EDGELIST
graphDS ={} #adjaceny list
weighted = True # check if graph is weighted
adj_list=[] #non-weighted ajaceny list
unvisited =[] #list of all nodes
heuristic_dict ={} #heuristic of each node
goals =[] #goals list
heuristic_valid=True
heuristic_valid_long=False
def __init__(self,Default):
self.graph=[]
with open("Default.txt") as text:
text = text.readlines()
for i in text:
i = i.rstrip()
i = i.lstrip()
i = i.split(' ')
if i == ['']:
continue
i[len(i) - 1] = i[len(i) - 1].removesuffix("\n")
i[len(i) - 1] = i[len(i) - 1].removesuffix(" ")
if i not in self.graph:
self.graph.append(i)
##mult appended !!!!!!!!!
def reload(self):
self.graph=[]
with open("FILE.txt") as text:
text = text.readlines()
for i in text:
i = i.rstrip()
i = i.lstrip()
i = i.split(' ')
if i ==['']:
continue
i[len(i) - 1] = i[len(i) - 1].removesuffix("\n")
i[len(i) - 1] = i[len(i) - 1].removesuffix(" ")
if i not in self.graph:
self.graph.append(i)
def makeDS(self,edgelist,directed):
self.graphDS={}
self.weighted =True
self.unvisited=[]
print("MAKE_DS")
self.vertix_list={}
print("HERE")
if directed:
print("directed")
for i in edgelist:
if len(i) < 3:
print(i, "LESS")
self.weighted = False
if len(i)==2:
self.graphDS.setdefault(i[0], []).append((i[1], 1))
if i[0] not in self.unvisited:
self.unvisited.append(i[0])
if i[1] not in self.unvisited:
self.unvisited.append(i[1])
continue
elif len(i)>3:
return False
print(i, "OKAY")
try:
int(i[2])
except:
return False
self.graphDS.setdefault(i[0], []).append((i[1], int(i[2])))
if i[0] not in self.unvisited:
self.unvisited.append(i[0])
if i[1] not in self.unvisited:
self.unvisited.append(i[1])
else:
print("not directed")
for i in edgelist:
if len(i) < 3:
print(i, "LESS")
self.weighted = False
if len(i)==2:
self.graphDS.setdefault(i[0], []).append((i[1], 1))
self.graphDS.setdefault(i[1], []).append((i[0], 1))
if i[0] not in self.unvisited:
self.unvisited.append(i[0])
if i[1] not in self.unvisited:
self.unvisited.append(i[1])
continue
elif len(i)>3:
return False
print(i, "OKAY")
#Exception handling
try:
int(i[2])
except:
return False
self.graphDS.setdefault(i[0],[]).append((i[1], int(i[2])))
self.graphDS.setdefault(i[1],[]).append((i[0], int(i[2])))
if i[0] not in self.unvisited:
self.unvisited.append(i[0])
if i[1] not in self.unvisited:
self.unvisited.append(i[1])
print("VERTIX NUM")
self.number_verticies = len(self.vertix_list)
print("First Done")
print(self.graphDS)
return True
def makeHeuristicsList(self,lines):
'''N 10
A 50
X E'''
self.heuristic_valid =True
self.heuristic_valid_long = False
self.heuristic_dict={}
for i in lines:
i = i.rstrip()
i = i.lstrip()
i = i.split()
print(i)
if len(i)>2:
self.heuristic_valid_long = True
return False
key = i[0]
value = i[1]
try:
int(value)
except:
self.heuristic_valid=False
return
self.heuristic_dict[key]=int(value)
def makeGoalsList(self,lines):
print("Make Goal list started",lines)
i=[]
self.goals=[]
i = lines
i = i.rstrip()
i = i.lstrip()
i = i.split()
for j in i:
if not j == " " :
self.goals.append(j)
print("goal list made",self.goals)
def makeNonWeightedAdj_list(self,directed):
print("make non weighted adj list")
self.adj_list={}
if directed:
print("directed")
for i in self.graph:
if len(i)>=2:
self.graphDS.setdefault(i[0], []).append(i[1])
print(i, "OKAY")
else:
print("not directed")
for i in self.graph:
if len(i)>=2:
self.adj_list.setdefault(i[0], []).append(i[1])
self.adj_list.setdefault(i[1], []).append(i[0])
print(i, "OKAY")
#{'2': {'3', '1'}, '3': {'2'}, '1': {'2'}}