Add test function and test files
This commit is contained in:
parent
ca4cf38a51
commit
40faa64e23
37
lol.py
37
lol.py
|
@ -31,6 +31,9 @@ class Vertex():
|
|||
"""Quickly display informations"""
|
||||
print(str(self.id)+" ("+self.name+")")
|
||||
|
||||
def idToStr(self):
|
||||
return '"'+str(self.id)+'"'
|
||||
|
||||
def reaches(self, i):
|
||||
"""Boolean function returning true if the given vertex can be reached from the calling vertex"""
|
||||
return i in map(lambda v: v.id, self.childVert)
|
||||
|
@ -78,6 +81,9 @@ class Edge():
|
|||
"""Quickly display informations"""
|
||||
print(str(self.id)+" ("+self.name+")")
|
||||
|
||||
def idToStr(self):
|
||||
return str(self.id)
|
||||
|
||||
def display(self):
|
||||
"""Displays edge"""
|
||||
print(self.name+" (id="+str(self.id)+"):")
|
||||
|
@ -102,7 +108,6 @@ class Graph():
|
|||
vertNb = len(data)
|
||||
for i in range(1, vertNb):
|
||||
l = data[i-1].split('\t')
|
||||
print(l)
|
||||
vert = Vertex(i, str(l[1]), int(l[2]))
|
||||
self.vertexMap[i] = vert
|
||||
|
||||
|
@ -156,11 +161,27 @@ def dijkstra(g, src, dest):
|
|||
e.head.previous[src.id] = (u, e)
|
||||
p = []
|
||||
t = dest
|
||||
while src.id in t.previous.keys():
|
||||
time = 0
|
||||
while src.id in t.previous and t.previous[src.id] != None and src.id in t.previous.keys():
|
||||
p = [t.previous[src.id][1], t]+p
|
||||
time += t.previous[src.id][1].time
|
||||
t = t.previous[src.id][0]
|
||||
return p
|
||||
return p, time
|
||||
|
||||
def test(g):
|
||||
out = ""
|
||||
for v in g.vertexMap.values():
|
||||
for w in g.vertexMap.values():
|
||||
if v == w:
|
||||
continue
|
||||
#print("Testing "+str(v.id)+" to "+str(w.id))
|
||||
res = dijkstra(g, v, w)
|
||||
out += "Shortest path from "+str(v.id)+" to "+str(w.id)+" :\n"+v.idToStr()
|
||||
for e in res[0]:
|
||||
out += '-'
|
||||
out += e.idToStr()
|
||||
out += " => "+str(res[1]*60)+"\n"
|
||||
print(out)
|
||||
|
||||
if __name__ == "__main__":
|
||||
g = Graph()
|
||||
|
@ -169,12 +190,4 @@ if __name__ == "__main__":
|
|||
#g.printVertexList()
|
||||
#g.printEdgeList()
|
||||
|
||||
print("Path".center(80, '='))
|
||||
path = dijkstra(g, g.vertexMap[2], g.vertexMap[5])
|
||||
for e in path:
|
||||
e.quickDisp()
|
||||
print(" V")
|
||||
|
||||
|
||||
|
||||
|
||||
test(g)
|
||||
|
|
7457
shortest_paths1.txt
Normal file
7457
shortest_paths1.txt
Normal file
File diff suppressed because it is too large
Load diff
2664
shortest_paths2.txt
Normal file
2664
shortest_paths2.txt
Normal file
File diff suppressed because it is too large
Load diff
2664
shortest_paths3.txt
Normal file
2664
shortest_paths3.txt
Normal file
File diff suppressed because it is too large
Load diff
2664
shortest_paths4.txt
Normal file
2664
shortest_paths4.txt
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue