-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.py
More file actions
74 lines (55 loc) · 1.68 KB
/
Copy pathgraph.py
File metadata and controls
74 lines (55 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class Graph:
"""
docstring
"""
def __init__(self):
"""
docstring
"""
self.adjacency_list = {}
#vertices and nodes are the same thing
def add_node(self, value):
"""
adds a new node (i.e. vertex) to the graph
the value parameter is the value of the new node
return the instance of the new node/vertex
"""
vertex = Vertex(value)
self.adjacency_list[vertex] = []
return vertex
def size(self):
"""
returns the total number of nodes in the graph
"""
return len(self.adjacency_list)
def add_edge(self, start_vertex, end_vertex, weight=None):
"""
adds a new edge between two nodes in the graph
"""
if start_vertex not in self.adjacency_list or end_vertex not in self.adjacency_list:
raise KeyError("Start or end vertex not in graph.")
edge = Edge(end_vertex, weight)
self.adjacency_list[start_vertex].append(edge)
def __repr__(self):
return f"Graph({self.adjacency_list})"
def get_neighbors(self, vertex):
"""
returns a collection of edges connected to the given node
"""
pass
def get_nodes(self):
"""
returns all of the nodes in the graph as a collection
"""
pass
class Vertex:
def __init__(self, value):
self.value = value
def __repr__(self) -> str:
return f"Vertext(\"{self.value}\")"
class Edge:
def __init__(self, vertex, weight=None):
self.vertex = vertex
self.weight = weight
def __repr__(self) -> str:
return f"Edge({self.vertex}, {self.weight})"