SZI_PROJEKT_GR1_TRAKTOR/Graph.py

29 lines
861 B
Python
Raw Normal View History

2019-05-08 02:31:29 +02:00
class Graph:
2019-05-07 15:37:27 +02:00
def __init__(self):
2019-05-08 02:31:29 +02:00
self.nodes = {}
2019-05-07 15:37:27 +02:00
def neighbors(self, id):
return self.nodes[id][0]
2019-05-08 02:31:29 +02:00
def direction2point(self, point, direction):
if direction == 'N':
return (point[0] - 1, point[1])
elif direction == 'S':
return (point[0] + 1, point[1])
elif direction == 'W':
return (point[0], point[1] - 1)
else:
return (point[0], point[1] + 1)
def add_neighbor(self, node, direction):
neighbor = self.direction2point(node, direction)
if node in self.nodes:
self.nodes[node][0].add(neighbor)
2019-05-08 02:31:29 +02:00
else:
self.nodes[node] = (set([neighbor]), False)
2019-05-08 02:31:29 +02:00
if neighbor in self.nodes:
self.nodes[neighbor][0].add(node)
2019-05-08 02:31:29 +02:00
else:
self.nodes[neighbor] = (set([node]), False)
2019-05-07 15:37:27 +02:00