-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.py
47 lines (39 loc) · 1.65 KB
/
graph.py
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
class Graph:
def __init__(self, directed = False):
"""Constructor for graph class.
Args:
directed (bool, optional): Sets whether graph is directional. Defaults to False.
"""
self.graph_dict = {}
self.directed = directed
def add_vertex(self, vertex):
"""Adds a vertex to the graph.
Args:
vertex (Vertex): Object initialised by Vertex class
"""
self.graph_dict[vertex.value] = vertex
def add_edge(self, from_vertex, to_vertex, weight = 0):
"""Connects two vertices of a graph to each other.
Args:
from_vertex (Vertex): Start vertex
to_vertex (Vertex): Destination vertex
weight (int, optional): For path finding. Defaults to 0.
"""
self.graph_dict[from_vertex.value].add_edge(to_vertex.value, weight)
if not self.directed:
# for undirected graph, vertices connected vertices can move back and forth
self.graph_dict[to_vertex.value].add_edge(from_vertex.value, weight)
def show_edges(self, vertex):
"""Returns a dictionary of vertices connected to a singular vertex,
similar to an ordered list the key is a number and the value is the vertex itself.
Args:
vertex (Vertex): Vertex to find edges of.
Returns:
edge_values (dict): Dictionary of connected vertices.
"""
edge_values = {}
count = 1
for neighbor in vertex.edges:
edge_values[count] = self.graph_dict[neighbor].value
count += 1
return edge_values