2015-08-23 20:08:46 +08:00
|
|
|
from enum import Enum # Python 2 users: Run pip install enum34
|
|
|
|
|
|
|
|
|
|
|
|
class State(Enum):
|
2016-06-27 05:48:32 +08:00
|
|
|
unvisited = 0
|
|
|
|
visiting = 1
|
2015-08-23 20:08:46 +08:00
|
|
|
visited = 2
|
2015-08-03 18:24:38 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Node:
|
|
|
|
|
2016-07-04 19:16:15 +08:00
|
|
|
def __init__(self, key):
|
|
|
|
self.key = key
|
2015-08-23 20:08:46 +08:00
|
|
|
self.visit_state = State.unvisited
|
2016-07-04 19:16:15 +08:00
|
|
|
self.incoming_edges = 0
|
|
|
|
self.adj_nodes = {} # Key = key, val = Node
|
|
|
|
self.adj_weights = {} # Key = key, val = weight
|
2015-08-03 18:24:38 +08:00
|
|
|
|
2016-07-04 19:16:15 +08:00
|
|
|
def __repr__(self):
|
|
|
|
return str(self.key)
|
|
|
|
|
|
|
|
def __lt__(self, other):
|
|
|
|
return self.key < other.key
|
2015-08-05 18:14:44 +08:00
|
|
|
|
2016-06-27 05:48:32 +08:00
|
|
|
def add_neighbor(self, neighbor, weight=0):
|
2016-11-13 20:32:33 +08:00
|
|
|
if neighbor is None or weight is None:
|
|
|
|
raise TypeError('neighbor or weight cannot be None')
|
2016-07-04 19:16:15 +08:00
|
|
|
neighbor.incoming_edges += 1
|
|
|
|
self.adj_weights[neighbor.key] = weight
|
|
|
|
self.adj_nodes[neighbor.key] = neighbor
|
|
|
|
|
|
|
|
def remove_neighbor(self, neighbor):
|
2016-11-13 20:32:33 +08:00
|
|
|
if neighbor is None:
|
|
|
|
raise TypeError('neighbor cannot be None')
|
|
|
|
if neighbor.key not in self.adj_nodes:
|
|
|
|
raise KeyError('neighbor not found')
|
2016-07-20 19:29:48 +08:00
|
|
|
neighbor.incoming_edges -= 1
|
|
|
|
del self.adj_weights[neighbor.key]
|
|
|
|
del self.adj_nodes[neighbor.key]
|
2016-06-27 05:48:32 +08:00
|
|
|
|
2015-08-03 18:24:38 +08:00
|
|
|
|
|
|
|
class Graph:
|
|
|
|
|
|
|
|
def __init__(self):
|
2016-07-04 19:16:15 +08:00
|
|
|
self.nodes = {} # Key = key, val = Node
|
|
|
|
|
|
|
|
def add_node(self, key):
|
|
|
|
if key is None:
|
2016-11-13 20:32:33 +08:00
|
|
|
raise TypeError('key cannot be None')
|
2016-11-24 10:21:38 +08:00
|
|
|
if key not in self.nodes:
|
|
|
|
self.nodes[key] = Node(key)
|
2016-07-04 19:16:15 +08:00
|
|
|
return self.nodes[key]
|
|
|
|
|
|
|
|
def add_edge(self, source_key, dest_key, weight=0):
|
|
|
|
if source_key is None or dest_key is None:
|
2016-11-24 10:21:38 +08:00
|
|
|
raise KeyError('Invalid key')
|
2016-07-04 19:16:15 +08:00
|
|
|
if source_key not in self.nodes:
|
|
|
|
self.add_node(source_key)
|
|
|
|
if dest_key not in self.nodes:
|
|
|
|
self.add_node(dest_key)
|
2016-11-24 10:21:38 +08:00
|
|
|
self.nodes[source_key].add_neighbor(self.nodes[dest_key], weight)
|
2016-07-04 19:16:15 +08:00
|
|
|
|
2016-11-13 20:32:33 +08:00
|
|
|
def add_undirected_edge(self, src_key, dst_key, weight=0):
|
|
|
|
if src_key is None or dst_key is None:
|
|
|
|
raise TypeError('key cannot be None')
|
|
|
|
self.add_edge(src_key, dst_key, weight)
|
|
|
|
self.add_edge(dst_key, src_key, weight)
|