AlgoFrames

Graphs: vertices, edges, directed, weighted, degree, paths

A graph is a set of vertices joined by edges, which can be directed or weighted. Learn degree, paths and cycles by hovering over a graph.

The idea

Five friends and who knows whom. Each person is a dot, and each friendship is a line between two dots.

Maps, networks, web links and course plans all have this shape. One set of words describes all of them.

Intuition

A dot is a vertex, and a line is an edge. Edge 1-3 joins vertex 1 and vertex 3.

Vertices are also called nodes. A graph is the set of vertices plus the set of edges.

In a directed graph each edge is an arrow. You may go from 0 to 1, but not back.

One-way streets and web links are directed. Friendship is undirected: it goes both ways.

A weighted graph puts a number on each edge. Edge 1-3 costs 5, for example 5 km of road.

The degree of a vertex is the number of edges that touch it. Vertex 1 has degree 3.

A path walks along edges and never repeats a vertex. 0, 1, 3, 4 is a path of length 3.

The length of a path is its number of edges, not its number of vertices.

A cycle walks along edges and ends where it started. 1, 3, 2, 1 is a cycle of length 3.

Complexity

Counting reads each edge once and updates two counters, so the work is O(V + E). The dashed V × V line is checking every pair of vertices.

On a sparse graph E is close to V, so V + 2E stays far below V × V as the graph grows.

Pitfalls

A self-loop is an edge from a vertex to itself. It touches vertex 1 twice, so it adds 2 to the degree.

In a directed graph, count arrows in and arrows out separately. Vertex 1 has in-degree 3 and out-degree 1.

Variations

A graph can fall apart into pieces with no path between them. Vertices 3 and 4 cannot reach 0.

Code

Pseudocode

deg[0..n-1] ← 0
for each edge (u, v)
  deg[u] ← deg[u] + 1
  deg[v] ← deg[v] + 1
return deg

Python

def degrees(n, edges):
    deg = [0] * n
    for u, v in edges:
        deg[u] += 1
        deg[v] += 1
    return deg

JavaScript

function degrees(n, edges) {
  const deg = new Array(n).fill(0);
  for (const [u, v] of edges) {
    deg[u] += 1;
    deg[v] += 1;
  }
  return deg;
}

Java

int[] degrees(int n, int[][] edges) {
    int[] deg = new int[n];
    for (int[] e : edges) {
        deg[e[0]]++;
        deg[e[1]]++;
    }
    return deg;
}

Practice problems (7)

Label the parts of a graph Warm-up

Match each graph term to what it means.

Count the degrees Warm-up

The counters start at 0. The edges are read in the order 0-1, 1-2, 0-3, 1-3, 3-4, 2-4, 4-5, and each edge adds 1 at both of its ends.

Answer each question before the step plays.

Path or cycle? Standard

A path follows edges and never repeats a vertex. A cycle follows edges and ends at its start, with no other repeat.

Select the edges and sequences asked for, then follow the walk 0, 2, 3, 1, 2.

Degree of every vertex Standard

graph is undirected with vertices 0 to n - 1. Return an array whose entry v is the degree of vertex v.

A self-loop [v, v] touches v twice, so it adds 2. Parallel edges each count.

Is this sequence a path? Standard

Return true when walk is a path in graph: every two neighbours in walk are joined by an edge, and no vertex appears twice.

In a directed graph an edge [u, v] only lets you step from u to v. An empty walk is not a path; a single vertex is a path of length 0.

In-degree and out-degree Standard

graph is directed. An edge [u, v] is an arrow from u to v. Return [out, in]: out[v] counts arrows leaving v and in[v] counts arrows entering v.

The handshake lemma Challenge

At a party every handshake involves two people. In graph terms: every edge has two ends, so the degrees always add up to 2 × the number of edges.

Use this rule to answer each question, then watch the degrees being counted.

Related topics

Next on the path: Adjacency matrix, adjacency list, edge list