Hi,
I´m currently at 6 out of eleven and I have the same issue like you. Even if I understand it, it’s not really making a lot of sense right now, but hey it could be in the end of the lesson. I will let you know…
Like the most of you I used the shortcut:
from_vertex.add_edge(to_vertex)
# if the graph is not directed, do the following:
if not self.directed:
to_vertex.add_edge(from_vertex)
Anyway, here is my code for the vertex glass and graph class so far. I also made a print_dict() funktion in the graph class, to make it more visuel. I hope my comments in the code are helpful. Feedback is always welcome.
Vertex Class functions:
def get_edges(self):
""" returns the keys of the dictionary"""
#return list(self.edges.keys())
print("\n***********************************")
print(" Current Vertex Station ")
print("***********************************")
edge_count = len(self.edges)
print("The vertex '{vertex}' has {edge_count} connection(s).".format(vertex=self.value,edge_count=edge_count))
if edge_count > 0:
for key in self.edges:
print("+ '{vertex}'".format(vertex=key.value))
#return self.edges.keys()
def add_edge(self,vertex):
print("\n***********************************")
print(" + Adding Vertex Connection + ")
print("***********************************")
"""vertex will be the value of another vertex"""
print("Starting at '{vertex}'.".format(vertex = self.value))
print(" '|' ")
print("Adding edge to '{vertex}'.".format(vertex = vertex.value))
self.edges[vertex] = True
Graph Class Functions:
def add_vertex(self,vertex):
print("\n***********************************")
print(" + Adding Graph Station + ")
print("***********************************")
print("\nThe vertex '{vertex}' which all his connections has been added to the graph.".format(vertex = vertex.value))
"""dic[new key] = new value"""
# dic = {'Grand Central Station' : Vertex}
# Vertex is {'Grand Central Station' : Vertex,Vertex....} Station : other conections
# dict{'Grand Central Station' : dict{42nd Street Station':True]}}
#vertex.value == content of varibale / vertex == variable
self.graph_dict[vertex.value] = vertex
print("\nThis is the content of the graph dictionary:")
print(self.graph_dict)
def add_edge(self,from_vertex,to_vertex):
# dict{key:Vetex Class Object} > dict{variable: value}
# in Graph dict find Key > from_vertex = staion
# take the value of the Key > .value = "Ballahoo"
# call .add_edge(value) from Vertex class on the dict
# returns
# dict{"Ballahoo" : {Vertex variable : connections}}
#self.graph_dict[from_vertex.value].add_edge(to_vertex.value)
from_vertex.add_edge(to_vertex)
# if the graph is not directed, do the following:
if not self.directed:
#self.graph_dict[to_vertex.value].add_edge(from_vertex.value)
to_vertex.add_edge(from_vertex)
# call get_edges from Vertex Class to get content
#self.graph_dict[from_vertex.value].get_edges()
#self.graph_dict[to_vertex.value].get_edges()
self.print_dict()
def print_dict(self):
vertex_count = len(self.graph_dict)
print("\n|||||||||||||||||||||||||||||||||||||||||")
print("_____________GRAPH OVERVIEW______________")
print("|||||||||||||||||||||||||||||||||||||||||")
print("\nThis graph has {vertex_count} vertices.\n".format(vertex_count=vertex_count))
for key in self.graph_dict:
self.graph_dict[key].get_edges()
Funktions Calls:
railway = Graph()
#Make the Vertex instance here
station_a = Vertex(“Grand Central Station”)
station_b = Vertex(“Ballahoo”)
station_c = Vertex(“Station C”)
station_d = Vertex(“Station D”)
station_e = Vertex(“Station E”)
#Call .add_vertex() here
railway .add_vertex(station_a)
railway .add_vertex(station_b)
railway .add_vertex(station_c)
railway .add_vertex(station_d)
railway .add_vertex(station_e)
#Call .add_edge() from Vertex class here (from vertex to vertex)
station_a.add_edge(station_b)
#Call .add_edge() from Graph class here (graph.from vertex to vertex)
railway.add_edge(station_c,station_d)
railway.add_edge(station_d,station_e)
Output: