-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
102 lines (95 loc) · 3.38 KB
/
main.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
class Graph:
def __init__(self, numOfPoint, data):
self.matrix = [[False for i in range(numOfPoint)] for j in range(numOfPoint)]
self.numOfPoint = numOfPoint
for i, j in data:
self.matrix[i - 1][j - 1] = True
self.matrix[j - 1][i - 1] = True
def BFS(self, start, end):
"""
Find the shortest way from start to end via BFS
:param start: start point
:param end: end point
:return: a list of point from start to end
"""
# init data
if start > self.numOfPoint or end > self.numOfPoint:
raise ValueError("Value is out of range")
start -= 1
end -= 1
queue = []
path = [-1 for i in range(self.numOfPoint)]
flag = [False for i in range(self.numOfPoint)]
queue.append(start)
flag[start] = True
while len(queue) != 0:
current = queue[0]
del queue[0]
for index in range(self.numOfPoint):
if self.matrix[current][index] and current != index and not flag[index]:
path[index] = current
if index == end:
# finish, this is shortest way
numOfStep = 0
way = []
while start != end:
way.append(end + 1)
end = path[end]
numOfStep += 1
way.append(start + 1)
way.reverse()
return way
else:
# go to next point
queue.append(index)
flag[index] = True
else:
return []
def DFS(self, start, end):
"""
Find the shortest way from start to end via DFS
:param start: start point
:param end: end point
:return: a list of point from start to end
"""
if start > self.numOfPoint or end > self.numOfPoint:
raise ValueError("Value is out of range")
way = []
flag = [False for i in range(self.numOfPoint)]
ans = []
flag[start - 1] = True
self.__DFS(start - 1, end - 1, way, flag, ans)
if len(ans) != 0:
ans.insert(0, start)
return ans
def __DFS(self, start, end, way, flag, ans):
if start == end and (len(way) <= len(ans) or len(ans) == 0):
ans.clear()
ans += way
for index in range(self.numOfPoint):
if index != start and self.matrix[start][index] and not flag[index]:
way2 = way.copy()
flag2 = flag.copy()
way2.append(index + 1)
flag2[index] = True
self.__DFS(index, end, way2, flag2, ans)
def __str__(self):
s = ""
s += "Graph has {} point".format(self.numOfPoint) + "\n"
for row in self.matrix:
s += str(row) + "\n"
return s
'''This is example'''
if __name__ == "__main__":
'''
find minimum step
(1) -- (2) -- (3)
| / |
(4) _/ (5) -- (6)
'''
graph = Graph(6, [[1, 2], [2, 3], [1, 4], [2, 4], [5, 6], [2, 5]])
print(graph)
print("Shortest way from 1 -> 6 by BFS")
print(graph.BFS(1, 6))
print("Shortest way from 1 -> 6 by DFS")
print(graph.DFS(1, 7))