We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Graphs

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.

Build a Graph

Edit the graph, then switch views to inspect its adjacency matrix.

Vertex 1 selected · 4 edges.

01234

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.

Assignment

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.

01234
0
1
2True
3True
4