Codecademy Logo

Learn Graphs

Implementing a Graph Class

The basis of a Graph class in Python is the implementation of two classes, Graph and Vertex, which establish the essential functionality to create a variety of graphs.

The Vertex class allows for storage of connecting vertices with a dictionary and adjustment of their edges as well.

The Graph class builds upon the Vertex methods and allows addition of vertices and edges, setting the directionality of edges, and determining if a path exists between two vertices.

class Vertex:
"""Key methods of Vertex class"""
def __init__(self, value):
def add_edge(self, vertex, weight = 0):
def get_edges(self):
class Graph:
"""Key methods of Graph class"""
def __init__(self, directed = False):
def add_vertex(self, vertex):
def add_edge(self, from_vertex, to_vertex, weight = 0):
def find_path(self, start_vertex, end_vertex):

Learn More on Codecademy