Recitation 4

Recitation notes for the week of October 5, 2026.

Homework 3 reminders

Homework 3 is due Friday, October 9, 2026, at 11:30 p.m. Submit Q1(a), Q1(b), Q2(a), and Q2(b) as separate PDFs to their corresponding UBLearns links. Submit Q3 code to Autolab. See the homework page for the full submission instructions.

Q1 Part (b)

Using part (a) or otherwise, design an algorithm that computes an admissible assignment and its $p\times p$ output table in $O(f+p^2)$ time, where $f=|E|$. Your algorithm must handle disconnected friendship graphs, including isolated beavers. Prove its correctness and justify its running time.

Think about the graph exploration algorithms you have seen in class. BFS and DFS are not interchangeable for every task. Note that $f\le p(p-1)/2$, so the required time bound is also $O(p^2)$.

Homework 3, Question 2

We first recall the question:

The Problem

A 4-clique in a graph $G=(V,E)$ is a set of four distinct vertices $\{u,v,w,x\}$ such that $(u,v),(v,w),(w,x),(x,u),(u,w),(v,x)\in E$. (Note that $G$ is undirected.) Let $n=|V|$ and $m=|E|$. In this problem, you will design a series of algorithms that, given a connected graph $G$ as input, list all the $4$-cliques in $G$. (It is fine to list one $4$-clique more than once.) We call this the 4-clique listing problem (duh!). You can assume that as input you are given $G$ in both the adjacency matrix and adjacency list formats. For this problem, you can also assume that $G$ is connected.

Sample Input/Output pair

Question 2, part (a)

The Problem

Present an $O(n^4)$ time algorithm to solve the $4$-clique listing problem. Only an algorithm idea is required for this part.

Algorithm Idea

Let us assume we have access to a function Check-if-4-clique that takes as input $G=(V,E)$ and four vertices $u,v,w,x\in V$ and outputs true if $\{u,v,w,x\}$ is a $4$-clique in $G$ and false otherwise.

Then the algorithm is simple: just go through all possible $n^4$ $4$-tuples $\{u,v,w,x\}$ and, if Check-if-4-clique$(G,u,v,w,x)$ outputs true, then we add $\{u,v,w,x\}$ to the output.

Duplicates

The above algorithm would output the same set $\{u,v,w,x\}$ $24$ times, but that is OK for this problem. As long as you (1) only output $4$-cliques; (2) output all $4$-cliques and (3) maintain the given time bound, you can have duplicates.

Exercise

Show that Check-if-4-clique can be implemented in $O(1)$ time.

Algorithm Details

Even though this is not needed for your HW submission, we write down the details of the $O(n^4)$ time algorithm (assuming access to Check-if-4-clique as a black box, of course):

                                    Let GL and GM be the adjacency list and adjacency matrix representation of G (both are input).
                                    # GL is an array of pointers to linked list. The indices to vertices are in GL.keys()

                                    #Get the set of vertices
                                    V = GL.keys()

                                    L = null

                                    #Cycle through all 4-tuples
                                    for u in V:
                                        for v in V:
                                            for w in V:
                                                for x in V:
                                                    if Check-if-4-clique(GM,u,v,w,x) == true:
                                                        Add {u,v,w,x} to L

                                    return L
                                

Question 2, part (b)

The Problem

Present an $O(m^2)$ time algorithm to solve the $4$-clique listing problem.

A Comment

We conclude with a comment:

Homework 3, Question 3

The input is an undirected graph represented by adjacency lists and a starting vertex $s$. The graph may be disconnected. Return an array or list whose entry at index $u$ is the minimum distance from $s$ to vertex $u$, for vertex IDs $0,1,\dots,n-1$.

The starting vertex has distance $0$. Return -1 for every vertex unreachable from $s$. The homework gives a target running time of $O(m+n)$, where $m$ is the number of edges.

One useful property of BFS:

BFS and distance

Say we run BFS from node $s$ in graph $G=(V,E)$ and let the layers be denoted by $L_0,L_1,\dots$. If $u\in L_i$ for any $u\in V$ and $i\ge 0$, then the distance between $s$ and $u$ is exactly $i$. Vertices unreachable from $s$ do not appear in these layers. This is (3.3) in the textbook.

Please see the textbook for a proof of this claim.

Further Reading