-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.py
More file actions
32 lines (27 loc) · 1.44 KB
/
Graph.py
File metadata and controls
32 lines (27 loc) · 1.44 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
class Graph:
def __init__(self, directed=False):
self.directed = directed
self.graph_dict = {}
def add_vertex(self, vertex):
print("Adding vertex: {VERTEX} to the graph...".format(VERTEX=vertex.get_value()))
self.graph_dict[vertex.get_value()] = vertex
def add_edge(self, from_vertex, to_vertex, weight = 0):
print("Adding edge from {fvertex} to {tvertex}".format(fvertex=from_vertex.get_value(), tvertex=to_vertex.get_value()))
self.graph_dict[from_vertex.get_value()].add_edge(to_vertex, weight)
if self.directed is False:
print("Adding edge from {tvertex} to {fvertex}".format(fvertex=from_vertex.get_value(), tvertex=to_vertex.get_value()))
self.graph_dict[to_vertex.get_value()].add_edge(from_vertex, weight)
def find_path(self, start_vertex, end_vertex):
print("Finding path between {start} and {end}".format(start=start_vertex.get_value(), end=end_vertex.get_value()))
start = [start_vertex]
seen = {}
while start != []:
current_vertex = start.pop()
seen[current_vertex] = True
if current_vertex is end_vertex:
return True
else:
next_vertices = [vertex for vertex in current_vertex.get_edges() if vertex not in seen]
start += next_vertices
print("Visiting: {VERTEX}".format(VERTEX=current_vertex.get_value()))
return False