Codecademy Logo

Graphs and Graph Search

Related learning

  • Learn what data structures and algorithms are, why they are useful, and how you can use them effectively in Python.
    • With Certificate
    • Intermediate.
      26 hours
  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours

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

  • Learn what data structures and algorithms are, why they are useful, and how you can use them effectively in Python.
    • With Certificate
    • Intermediate.
      26 hours
  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours