-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject_1.py
More file actions
152 lines (113 loc) · 3.61 KB
/
Copy pathProject_1.py
File metadata and controls
152 lines (113 loc) · 3.61 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
from collections import deque
# paper conclusion: min distance -> BFS; min time->BNART
INTERSECTION_DELAY = 2
class graph:
def __init__(self):
self.graph ={ }
# s: source node; d:destiniation node; w: weight
def add_edge(self, s, d, w):
if s not in self.graph:
self.graph[s] = { }
if d not in self.graph:
self.graph[d] = { }
self.graph[s][d] = w
self.graph[d][s] = w
# n: node, if a node has more than 2 edges, it's a intersection
def intersection(self, n):
if len(self.graph[n]) > 2:
return True
else:
return False
# find the weight between node s and d
def find_weight(self, s, d):
if s in self.graph and d in self.graph:
cost = self.graph[s][d]
if(self.intersection(s)):
cost += INTERSECTION_DELAY
# print("\nintersection: ", s)
return cost
else:
return 0
#s: source node/ root; d: destination
def BFS(self, s, d):
if s not in self.graph or d not in self.graph:
return None
visited = set()
frontier = deque([(s, 0)]) # Tuple: (node, cumulative weight)
result = { }
target = False
while (frontier != None):
n, w = frontier.popleft()
visited.add(n)
if n == d:
target = True
break
for neighbor, weight in self.graph[n].items():
if neighbor not in visited:
w += weight
frontier.append((neighbor, w))
visited.add(neighbor)
result[neighbor] = (n, w)
if target == False:
return None
path = [d]
while (path[-1] != s):
path.append(result[path[-1]][0])
path.reverse()
return path
# return the total wight of the selected path
def get_weight(self, p):
weight = 0
for i in range(len(p) - 1):
w = self.find_weight(p[i], p[i+1])
weight += w
return weight
# retruen true if two cars share a same peroid of path
def same_path(self, p1, p2):
for i in range (len(p1) -1):
if p1[i] in p2 and p1[i+1] in p2:
index1 = p2.index(p1[i])
index2 = p2.index(p1[i+1])
if index1 + 1 == index2:
return True
return False
# test 1
g1 = graph()
g1.add_edge("1", "2", 1)
g1.add_edge("2", "3", 1)
g1.add_edge("2", "4", 1)
g1.add_edge("2", "5", 1)
g1.add_edge("3", "6", 1)
g1.add_edge("4", "6", 1)
shortest1_1 = graph.BFS(g1, "1", "6")
if shortest1_1:
print("The shortest is:", shortest1_1)
else:
print("No shortest path")
cost1_1 = graph.get_weight(g1, shortest1_1)
print("The minimun weight is: ", cost1_1)
shortest1_2 = graph.BFS(g1, "2", "6")
if shortest1_2:
print("The shortest is:", shortest1_2)
else:
print("No shortest path")
cost1_2 = graph.get_weight(g1, shortest1_2)
print("The minimun weight is: ", cost1_2)
print("\nWill these two path colides?")
if(graph.same_path(g1, shortest1_1, shortest1_2)):
print("Yes, but I have no idea how to solve now\n")
else:
print("No\n")
# test 2
g2 = graph()
g2.add_edge("A", "B", 1)
g2.add_edge("A", "C", 1)
g2.add_edge("B", "D", 1)
g2.add_edge("C", "D", 1)
shortest2 = graph.BFS(g2, "A", "D")
if shortest2:
print("The shortest is:", shortest2)
else:
print("No shortest path")
cost2 = graph.get_weight(g2, shortest2)
print("The minimun weight is: ", cost2)