Graph problems [00LW]
- September 14, 2026
Problems related to graphs.
[G1] Adjacency matrices (1 pt) [00LX]
- September 14, 2026
Given a simple graph G with vertices numbered from 1 to n, an adjacency matrix for G is an n \times n matrix A where A[i,j] = 1 if i and j are adjacent, and A[i,j] = 0 otherwise. In other words, the entry at row i and column j tells us whether there is an edge between i and j, with 1 for yes and 0 for no.
- Make a drawing of the graph represented by the adjacency matrix \begin {bmatrix} 0 & 0 & 1 & 1 \\ 0 & 0 & 1 & 0 \\ 1 & 1 & 0 & 1 \\ 1 & 0 & 1 & 0 \end {bmatrix}.
- Given a vertex u and the adjacency matrix A, how long does it take to list all the neighbors of u: \Theta (n), or \Theta (\deg (u))? Why?
- A directed graph is a graph where each edge has a particular direction, that is, an edge is an ordered pair of vertices rather than just a set of two vertices. Typically such graphs are drawn with a little arrow on each edge showing which direction it goes. Explain how to generalize adjacency matrices to represent directed graphs.
- A weighted graph is a graph where each edge has an associated number, or weight, representing something like cost, time, or distance. Explain how to generalize adjacency matrices to represent weighted graphs.
- (Advanced, optional) Let A be the adjacency matrix for a simple (undirected, unweighted) graph G. What information does the matrix A^2 represent? What about A^3? In general, what about A^n?
[G2] Adjacency lists (1 pt) [00LY]
- September 14, 2026
Given a simple graph G, an adjacency list representation for G consists of a map (aka dictionary) associating each vertex with the list (or set) of all its adjacent neighbors. In other words, using Java-like notation, Map<Vertex, List<Vertex> >.
- Make a drawing of the graph represented by the map \{ 1 \mapsto [3,4], 2 \mapsto [3], 3 \mapsto [1, 2, 4], 4 \mapsto [1, 3] \}.
- Given a vertex u and an adjacency list representation of a graph G, how long does it take to list all the neighbors of u: \Theta (n), or \Theta (\deg (u))? Why?
- Explain how to generalize adjacency lists to represent directed graphs.
- Explain how to generalize adjacency lists to represent weighted graphs.
[G3] Connectivity from degree (2 pts) [00LZ]
- September 14, 2026
Let G=(V,E) be an undirected graph with n vertices, with no self-loops (that is, no edges of the form (v,v) from a vertex to itself). Show that if every vertex has degree at least n/2, the graph is connected. If it makes your proof easier, you may assume that n is even.
Hint.
- September 14, 2026
Think about cuts in the graph. A cut (S,T) is a partition of the vertices into two sets S and T such that every vertex is in either S or T but not both. Is it possible to have a cut (S,T) with no edges between S and T?
[G4] Handshake lemma (1 pt) [00M5]
- September 16, 2026
Prove/explain why the sum of all vertex degrees in a graph is equal to twice the number of edges. Your proof need only be a sentence or two.
[G5] Connected graphs with n-1 edges must be acyclic (2 pts) [00M4]
- September 14, 2026
Prove that for all n \geq 1, if G is a simple connected graph with n vertices and n-1 edges, then G has no cycles. (This is the third part of the proof from class, characterizing trees as having any two out of three properties.)
You may freely assume Connected, acyclic graphs have n-1 edges and Acyclic graphs with n-1 edges are connected if they are helpful; I also recommend studying their proofs as examples.
Hint.
- September 14, 2026
Use induction on n. By counting vertex degrees, show that any connected graph with k+1 vertices and k edges must have at least one leaf.
Hint.
- September 14, 2026
Use the [G4] Handshake lemma (1 pt).
[G6] Distances in a tree (3 pts) [00MF]
- September 18, 2026
Given a graph T which is guaranteed to be a tree, and a particular starting vertex s, design an O(V + E) algorithm to find the distance of every vertex from s. In other words, your algorithm should output some kind of dictionary/map or array associating every vertex v to a natural number \ell (v) which is the length of a path from s to v. Note that the length of a path from s to itself is 0, the length of a path from s to any of its neighbors is 1, and so on.
You may assume that T is represented in whichever way is easiest for you (adjacency matrix or adjacency list).
Explain how your algorithm works, and analyze its asymptotic running time. Psuedocode is fine, though you may also choose to write real code if you wish.
[G7] Connected component labelling (3 pts) [00MG]
- September 18, 2026
Given a graph G with n vertices labelled 0 through n-1, design an O(V+E) algorithm to find and label all the connected components of G. That is, your algorithm should output a dictionary or array associating each vertex v with a component label c(v) such that two vertices u and v have the same component label (that is, c(u) = c(v)) if and only if u and v are connected. For example, you might give all the vertices in the first connected component the label 1, all the vertices in the second connected component the label 2, and so on.
You may assume that G is represented in whichever way is easiest for you (adjacency matrix or adjacency list).
Explain how your algorithm works, and analyze its asymptotic running time. Psuedocode is fine, though you may also choose to write real code if you wish.
[K5] Kattis: Sky Islands (2 pts) [00MH]
- September 18, 2026
[K6] Kattis: Reachable Roads (2 pts) [00MI]
- September 18, 2026
[K7] Kattis: BFS problem I (2 pts) [00MS]
- September 21, 2026
Solve any one of the following Kattis problems:
[K8] Kattis: BFS problem II (2 pts) [00MT]
- September 21, 2026
Solve another one of the problems from [K7] Kattis: BFS problem I (2 pts).
[G8] BFS layers in a graph of bitstrings (2 pts) [00MU]
- September 21, 2026
Consider the family of undirected graphs \mathcal {H}_k defined as follows. \mathcal {H}_k has 2^k vertices labelled with the integers 0 through 2^k - 1. Vertices u and v are connected by an edge if and only if the binary representations of u and v differ in exactly one bit position. For example, in \mathcal {H}_4, the vertices 5 and 13 are connected by an edge since 5 = 0101_2 and 13 = 1101_2 differ in the first bit position, but the rest of the bits are the same.
Consider doing a BFS in \mathcal {H}_{10} starting at node 0. How many vertices are in L_6, that is, the sixth layer generated by the BFS? Give your answer together with either a proof, or the program you used to calculate the answer. Either approach will receive full credit. (Hint if you choose to write a program: to flip the jth bit of an integer n, you can use n ^ (1 << j), that is, the bitwise XOR of n with the result of shifting 1 left j times, that is, 2^{\texttt {j}}. These operators are valid in many languages such as Java, Python, and C/C++.)
[G9] Shortest path in a large random graph (3 pts) [00MV]
- September 21, 2026
The file graph-F26.txt describes a large undirected graph. The first line of the file contains a single integer which is the number of edges in the graph. Each subsequent line of the file describes one (undirected) edge, and contains two space-separated strings which are the names of the two vertices at the endpoints of the edge.
Write a program (in a programming language of your choice) to find a shortest path from the vertex labelled with your first name to the vertex labelled END (if there are multiple shortest paths you can find any one of them). You should submit a text file containing the list of vertices along this shortest path, starting with your name and ending with END. Each vertex should be on a separate line. For example, my solution looks like this:
Brent j2l65t dae18w kr9stg 8km7u3 j14vln a0pjwk END
You should also turn in the code you used to find your path.
To avoid any ambiguity, the list of names I used was as follows: Amiko, Coen, Collin, David, Jack, Jaxon, Jordan, Mark, Natalia, Rayden, Spencer, Tanvi, Vincent.
[G10] Strong connectivity (3 pts) [00MW]
- September 21, 2026
In class, we saw the definition of an undirected graph which is connected: a graph is connected if there is always a path between any two vertices. When we add directions to the edges, the concept of connectedness becomes more interesting.
A directed graph is strongly connected if for any two vertices x and y, there is always a directed path from x to y. Note this must also be true for the vertices y and x, so there must be a directed path from y to x as well. In other words, a strongly connected graph is like a network of one-way streets where it is always possible to drive from any location to any other location while obeying the one-way signs; you can never get stuck.
This problem will walk you through the process of developing an algorithm for determining whether a directed graph is strongly connected.
- Give an example of a strongly connected graph with at least 4 vertices (you may either draw it, or list its vertices and edges).
- Prove: a directed graph G is strongly connected if and only if there is some vertex s such that s is mutually connected to every other vertex; that is, for every other vertex v there is both a directed path from s to v and also from v back to s.
- Explain how to use a graph search algorithm like DFS or BFS to determine whether there exist directed paths from a particular starting vertex s to every other vertex.
- Explain how to determine, in only O(V+E) time, whether there exist directed paths to a particular vertex s from every other vertex.
- Put all these observations together into an algorithm to determine whether a given directed graph is strongly connected. You should:
- Describe your algorithm. You may use pseudocode, but your description should be detailed enough that someone could take it and turn it into working code.
- Prove/justify the correctness of your algorithm. In other words, why does your algorithm correctly determine whether a graph is strongly connected? Of course, you may freely cite the results from the previous parts of this problem.
- Analyze the asymptotic running time of your algorithm.
[G11] Kahn's algorithm and directed cycles (2 pts) [00NV]
- October 3, 2026
In class we explored Kahn's algorithm, which can be used to find a topological ordering of any DAG. Explain how we could extend Kahn's algorithm to find a directed cycle in directed graphs which are not a DAG.
How can we tell when Kahn's Algorithm has failed to find a topological ordering? What can we say about the indegree of all the vertices that are left? What happens if you pick a random starting vertex in the leftover graph and follow edges backwards?Hint.
Hint.