# Learn Graphs

Learn the graphs data structure using Python. Graphs are the perfect data structure for modeling networks!

Start## Key Concepts

Review core concepts you need to learn to master this subject

Implementing a Graph Class

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.

- 1Graphs 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 _… - 2Graphs 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…
- 3We’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…
- 4Imagine 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…
- 5We 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…
- 6Graphs 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…

- 1In 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…
- 2Let’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…
- 3We’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 …
- 4We’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… - 5We’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 … - 6We’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…
- 7So 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…
- 8Our 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…
- 9Our 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…
- 10Our 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 =… - 11Fantastic 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

## How you'll master it

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