Recitation 5

Recitation notes for the week of Feb 19, 2025.

Q1 Part (b)

For part (b) you should use the graph from part (a) to come up with an algorithm that will efficiently (i.e. in time polynomials in $p$ and $f=|E|$) seat the ADFs in an admissible seating.

There might be a reduction to a problem that you have seen in class.

Homework 3, Question 2

We first recall the question:

The Problem

A triangle in a graph $G=(V,E)$ is a $3$-cycle; i.e. a set of three vertices $\{u,v,w\}$ such that $(u,v),(v,w),(u,w)\in E$. (Note that $G$ is undirected.) In this problem you will design a series of algorithms that given a connected graph $G$ as input, lists all the triangles in $G$. (It is fine to list one triangle more than once.) We call this the triangle listing problem (duh!). You can assume that as input you are given $G$ in both the adjacency matrix and adjacency list format. 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^3)$ time algorithm to solve the triangle listing problem.

Algorithm Idea

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

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

Duplicates

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

Exercise

Show that Check-if-triang 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^3)$ time algorithm (assuming Check-if-triang as a blackbox 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 triples
                                    for u in V:
                                        for v in V:
                                            for w in V:
                                                if Check-if-triang(GM,u,v,w) == true:
                                                    Add {u,v,w} to L

                                    return L
                                

Question 2, part (b)

The Problem

This has two sub-parts:

  1. Let $\Delta$ denote the maximum degree, i.e. \[\Delta=\max_u n_u.\] Present an $O(n\cdot\min(m,\Delta^2))$ time algorithm to solve the triangle listing problem.

    Hint

    It might be useful to consider all the potential triangles that have one fixed vertex $u\in V$ as one of its vertices.

  2. Present an $O(m^{3/2})$ algorithm to solve the triangle listing problem.

Two Comments

We conclude with two comments:

  1. The algorithm from part (a) is correct but it not enough for part (b) since it runs too slowly.
  2. For first sub-part (i.e. the runtime $O(n\cdot\min(m,\Delta^2))$), it is possible to solve that sub-part by looking at the triply nested loop in the algorithm for part (a) and then to figure out how to reduce the number of iterations. (Also look at the hint above.)

Homework 3, Question 3

One property of BFS that is useful for Q3 on HW 3:

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 we have that the distance between $s$ and $u$ is exactly $i$. This is (3.3) in the textbook.

Please see the textbook for a proof of this claim.

Further Reading