Chevron Left Icon
Graphs: Conceptual
Lesson 1 of 2
Chevron Right Icon
  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 spreadsheet. Across the top, every vertex in the gr…
  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…
  1. 1
    In this lesson, we’ll build a robust implementation of the graph data structure. With just two classes, Vertex and Graph, we can implement a variety of graphs that satisfy the requirements of many…
  2. 2
    Let’s start with our Vertex class. This class is responsible for storing information about individual vertices in our graph. In our railway network, instances of Vertex will represent train statio…
  3. 3
    We’ll continue building out the Vertex class. Remember, it’s responsible for knowing which other vertices are connected. These connections are the edges of our graph implementation. A key in the …
  4. 4
    We’ve built a class to store information and connections between individual vertices, but we need another class that keeps track of the big picture. Our Graph class will track all the vertic…
  5. 5
    We’re keeping things organized by storing our classes in separate files, so let’s do a brief review on how to use code from another file. Python gives us the ability to import code from another …
  6. 6
    We’d like our Graph class to be able to set edges between the stored vertices. An instance of Graph is either directed or undirected, which affects how edges work between two vertices. As a remind…
  7. 7
    So far our Vertex class has stored edges inside of a dictionary with keys of the connected vertex’s name and the value simply set to True. We can make our implementation support edge weights with…
  8. 8
    Our railway has grown to four stations with two connecting tracks. How can we tell a passenger which stations are reachable from Harwick? undirected_railway = Graph() callan_station = Vertex(‘cal…
  9. 9
    Our pathfinding method is almost complete. Let’s take a step back and think how a passenger in Harwick station could find their way to Callan. First, they’d look for all the stations connected to…
  10. 10
    Our pathfinding method works when a path exists, but there is a serious bug! We’re not accounting for cycles in our graph. Consider the following undirected Graph: railway = Graph() callan =…
  11. 11
    Fantastic work! We’ve implemented a robust graph data structure in Python. Our two classes, Vertex and Graph are capable of representing the typical variations in graphs that occur in many differe…

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