MCPcopy
hub / github.com/donnemartin/interactive-coding-challenges / Node

Class Node

graphs_trees/graph/graph.py:11–40  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

9
10
11class Node:
12
13 def __init__(self, key):
14 self.key = key
15 self.visit_state = State.unvisited
16 self.incoming_edges = 0
17 self.adj_nodes = {} # Key = key, val = Node
18 self.adj_weights = {} # Key = key, val = weight
19
20 def __repr__(self):
21 return str(self.key)
22
23 def __lt__(self, other):
24 return self.key < other.key
25
26 def add_neighbor(self, neighbor, weight=0):
27 if neighbor is None or weight is None:
28 raise TypeError('neighbor or weight cannot be None')
29 neighbor.incoming_edges += 1
30 self.adj_weights[neighbor.key] = weight
31 self.adj_nodes[neighbor.key] = neighbor
32
33 def remove_neighbor(self, neighbor):
34 if neighbor is None:
35 raise TypeError('neighbor cannot be None')
36 if neighbor.key not in self.adj_nodes:
37 raise KeyError('neighbor not found')
38 neighbor.incoming_edges -= 1
39 del self.adj_weights[neighbor.key]
40 del self.adj_nodes[neighbor.key]
41
42
43class Graph:

Callers 1

add_nodeMethod · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected