-
Notifications
You must be signed in to change notification settings - Fork 20
/
dfs.py
74 lines (64 loc) · 2.6 KB
/
dfs.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
# Copyright 2019 Atikur Rahman Chitholian
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from collections import deque
class Graph:
def __init__(self, directed=True):
self.edges = {}
self.directed = directed
def add_edge(self, node1, node2, __reversed=False):
try: neighbors = self.edges[node1]
except KeyError: neighbors = set()
neighbors.add(node2)
self.edges[node1] = neighbors
if not self.directed and not __reversed: self.add_edge(node2, node1, True)
def neighbors(self, node):
try: return self.edges[node]
except KeyError: return []
def breadth_first_search(self, start, goal):
found, fringe, visited, came_from = False, deque([start]), set([start]), {start: None}
print('{:11s} | {}'.format('Expand Node', 'Fringe'))
print('--------------------')
print('{:11s} | {}'.format('-', start))
while not found and len(fringe):
current = fringe.pop()
print('{:11s}'.format(current), end=' | ')
if current == goal: found = True; break
for node in self.neighbors(current):
if node not in visited: visited.add(node); fringe.append(node); came_from[node] = current
print(', '.join(fringe))
if found: print(); return came_from
else: print('No path from {} to {}'.format(start, goal))
@staticmethod
def print_path(came_from, goal):
parent = came_from[goal]
if parent:
Graph.print_path(came_from, parent)
else: print(goal, end='');return
print(' =>', goal, end='')
def __str__(self):
return str(self.edges)
graph = Graph(directed=False)
graph.add_edge('A', 'B')
graph.add_edge('A', 'S')
graph.add_edge('S', 'G')
graph.add_edge('S', 'C')
graph.add_edge('C', 'F')
graph.add_edge('G', 'F')
graph.add_edge('C', 'D')
graph.add_edge('C', 'E')
graph.add_edge('E', 'H')
graph.add_edge('G', 'H')
start, goal = 'A', 'H'
traced_path = graph.breadth_first_search(start, goal)
if (traced_path): print('Path:', end=' '); Graph.print_path(traced_path, goal);print()