Key Concepts

Review core concepts you need to learn to master this subject

Implementing a Graph Class

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):

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.

Graphs: Conceptual
Lesson 1 of 2

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo