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
  1. 1
    Graphs are the perfect data structure for modeling networks, which make them an indispensable piece of your data structure toolkit. They’re composed of nodes, or vertices, which hold data, and _…
  2. 2
    Graphs have varying degrees of connection. The higher the ratio of edges to vertices, the more connected the graph. This graph represents a social network; people are vertices and edges are friend…
  3. 3
    We’re building a graph of favorite neighborhood destinations (vertices) and routes (edges), but not all edges are equal. It takes longer to travel between Gym and Museum than it does to travel bet…
  4. 4
    Imagine you’re a superhero escaping a villain’s lair. As you move from perilous room to perilous room, the doors close immediately behind you, barring any return. For this dramatic example, we n…
  5. 5
    We typically represent the vertex-edge relationship of a graph in two ways: an adjacency list or an adjacency matrix. An adjacency matrix is a table. Across the top, every vertex in the graph ap…
  6. 6
    Graphs are an essential data structure in computer science for modeling networks. Let’s review some key terms: * vertex: A node in a graph. * edge: A connection between two vertices. * adjacent: W…

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