

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 6
click for more info
Not enough gems
Cost: 6 gems
1: Graphs
incomplete
2: Graph Review
incomplete
3: Adjacency List
incomplete
4: Representing Graphs
incomplete
5: Adjacent Nodes
incomplete
6: Unconnected Vertices
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
A graph is a set of vertices and the edges that connect those vertices. All trees are graphs, but not all graphs are trees.
For now, we'll use a matrix to represent the edges in a graph that connect each pair of vertices. Build and edit the graph, then switch to the matrix view. Notice how each undirected edge appears twice, mirrored across the diagonal.
Interactive example available with JavaScript enabled.
In Python, we can use a list of lists to represent this matrix:
[
[False, True, False, False, True],
[True, False, True, True, True],
[False, True, False, True, False],
[False, True, True, False, True],
[True, True, False, True, False],
]
In any True cell the corresponding vertices are connected by an edge.
LockedIn, like all social networks, has a social graph: each user is a vertex, and each "friendship" (or in corpo-speak "connection") is an edge. We want to represent this graph as a matrix. Our users each have a unique ID, which is an integer that we'll use for their vertex number.
| 0 | 1 | 2 | 3 | 4 | |
|---|---|---|---|---|---|
| 0 | |||||
| 1 | |||||
| 2 | True | ||||
| 3 | True | ||||
| 4 |