mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
30 lines
703 B
Python
30 lines
703 B
Python
from collections import OrderedDict
|
|
|
|
|
|
class Node:
|
|
|
|
def __init__(self, id):
|
|
self.id = id
|
|
self.visited = False
|
|
self.adjacent = OrderedDict() # key = node, val = weight
|
|
|
|
def __str__(self):
|
|
return str(self.id)
|
|
|
|
|
|
class Graph:
|
|
|
|
def __init__(self):
|
|
self.nodes = OrderedDict() # key = node id, val = node
|
|
|
|
def add_node(self, id):
|
|
node = Node(id)
|
|
self.nodes[id] = node
|
|
return node
|
|
|
|
def add_edge(self, source, dest, weight=0):
|
|
if source not in self.nodes:
|
|
self.add_node(source)
|
|
if dest not in self.nodes:
|
|
self.add_node(dest)
|
|
self.nodes[source].adjacent[self.nodes[dest]] = weight |