-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathfinding.py
More file actions
98 lines (83 loc) · 3.82 KB
/
Pathfinding.py
File metadata and controls
98 lines (83 loc) · 3.82 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
class BreadthFirst:
def __init__(self, graph, start, end):
self.graph = graph
self.start_node = start
self.end_node = end
self.queue = []
self.prev = {node: None for node in
self.graph.get_nodes()} # dict for keeping track of what the previous node of a node was
self.queue.append(self.start_node) # add start node to queue
self.finished = False
self.previous_node = None
def has_finished(self):
return self.finished
def step(self):
if len(self.queue) > 0:
curr_node = self.queue.pop(0) # FIFO principal
# mark the node and its incoming connection from previous step as visited
if self.previous_node is not None:
self.previous_node.visited_from(self.prev[self.previous_node])
# mark current node as the inspected one (as well as its incoming connection if there is one)
curr_node.inspected_from(self.prev[curr_node])
if curr_node.is_end_node(): # target found
self.reconstruct_path()
self.finished = True # algorithm finished
return
for neighbour in curr_node.get_neighbours():
if not neighbour.already_visited(): # check if neighbour was already visited
self.queue.append(neighbour)
neighbour.enqueued_from(curr_node) # mark the current node and its incoming connection as "is on waitlist"
self.prev[neighbour] = curr_node
self.previous_node = curr_node
else: # no nodes in queue left and target was not found --> start and end not connected
self.finished = True # algorithm finished but not successful
print("\nSTART AND END NODE ARE NOT CONNECTED --> NO PATH CAN BE FOUND")
def reconstruct_path(self):
path = []
curr_node = self.end_node
while curr_node is not None:
path.append(curr_node)
curr_node = self.prev[curr_node]
path.reverse()
self.graph.highlight_final_path(path)
print("PATH FOUND AND HIGHLIGHTED")
class DepthFirst:
def __init__(self, graph, start, end):
self.graph = graph
self.start_node = start
self.end_node = end
self.stack = []
self.stack.append(self.start_node)
self.finished = False
self.prev = {node: None for node in self.graph.get_nodes()}
self.previous_node = None
def has_finished(self):
return self.finished
def step(self):
if len(self.stack) > 0: # check if there are nodes left to check
curr_node = self.stack.pop()
if self.previous_node is not None:
self.previous_node.visited_from(self.prev[self.previous_node])
curr_node.inspected_from(self.prev[curr_node])
if curr_node.is_end_node():
self.reconstruct_path()
self.finished = True
return
for neighbour in curr_node.get_neighbours():
if not neighbour.already_visited():
self.stack.append(neighbour)
neighbour.enqueued_from(curr_node)
self.prev[neighbour] = curr_node
self.previous_node = curr_node
else:
self.finished = True
print("\nSTART AND END NODE ARE NOT CONNECTED --> NO PATH CAN BE FOUND")
def reconstruct_path(self):
path = []
curr_node = self.end_node
while curr_node is not None:
path.append(curr_node)
curr_node = self.prev[curr_node]
path.reverse()
self.graph.highlight_final_path(path)
print("PATH FOUND AND HIGHLIGHTED")