Created
May 29, 2019 12:14
-
-
Save renanvalentin/3295be3779ea263d6765bef74e8ebb3e to your computer and use it in GitHub Desktop.
graph
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import collections | |
class SimpleGraph: | |
def __init__(self): | |
self.edges = {} | |
def neighbors(self, id): | |
return self.edges[id] | |
example_graph = SimpleGraph() | |
example_graph.edges = { | |
'A': ['B'], | |
'B': ['A', 'C', 'D'], | |
'C': ['A'], | |
'D': ['E', 'A'], | |
'E': ['B'] | |
} | |
class Queue: | |
def __init__(self): | |
self.elements = collections.deque() | |
def empty(self): | |
return len(self.elements) == 0 | |
def put(self, x): | |
self.elements.append(x) | |
def get(self): | |
self.elements.popleft() | |
def breadth_first_search_1(graph, start): | |
frontier = Queue() | |
frontier.put(start) | |
visited = {} | |
visited[start] = True | |
while not frontier.empty(): | |
current = frontier.get() | |
print('Visiting %r' % current) | |
for next in graph.neighbors(current): | |
if next not in visited: | |
frontier.put(next) | |
visited[next] = True | |
breadth_first_search_1(example_graph, 'A') | |
class SquareGrid: | |
def __init__(self, width, height): | |
self.width = width | |
self.height = height | |
self.walls = [] | |
def in_bounds(self, id): | |
(x, y) = id | |
return 0 <= x < self.width and 0 <= y < self.height | |
def passable(self, id): | |
return id not in self.walls | |
def neighbors(self, id): | |
(x, y) = id | |
results = [(x+1, y), (x, y-1), (x-1, y), (x, y+1)] | |
if (x + y) % 2 == 0: | |
results.reverse() # aesthetics | |
results = filter(self.in_bounds, results) | |
results = filter(self.passable, results) | |
return results | |
def breadth_first_search_2(graph, start): | |
# return "came_from" | |
frontier = Queue() | |
frontier.put(start) | |
came_from = {} | |
came_from[start] = None | |
while not frontier.empty(): | |
current = frontier.get() | |
for next in graph.neighbors(current): | |
if next not in came_from: | |
frontier.put(next) | |
came_from[next] = current | |
return came_from | |
g = SquareGrid(30, 15) | |
g.walls = DIAGRAM1_WALLS | |
parents = breadth_first_search_2(g, (8, 7)) | |
draw_grid(g, width=2, point_to=parents, start=(8, 7)) | |
def breadth_first_search_3(graph, start, goal): | |
frontier = Queue() | |
frontier.put(start) | |
came_from = {} | |
came_from[start] = None | |
while not frontier.empty(): | |
current = frontier.get() | |
if current == goal: | |
break | |
for next in graph.neighbors(current): | |
if next not in came_from: | |
frontier.put(next) | |
came_from[next] = current | |
return came_from | |
g = SquareGrid(30, 15) | |
g.walls = DIAGRAM1_WALLS | |
parents = breadth_first_search_3(g, (8, 7), (17, 2)) | |
draw_grid(g, width=2, point_to=parents, start=(8, 7), goal=(17, 2)) | |
class GridWithWeights(SquareGrid): | |
def __init__(self, width, height): | |
super().__init__(width, height) | |
self.weights = {} | |
def cost(self, from_node, to_node): | |
return self.weights.get(to_node, 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment