22.1 Representations of graphs
22.11
Given an adjacencylist representation of a directed graph, how long does it take to compute the $\text{outdegree}$ of every vertex? How long does it take to compute the $\text{indegree}s$?
Since it seems as though the list for the neighbors of each vertex $v$ is just an undecorated list, to find the length of each would take time $O(\text{outdegree}(v))$. So, the total cost will be
$$\sum_{v \in V}O(\text{outdegree}(v)) = O(E + V).$$
Note that the $V$ showing up in the asymptotics is necessary, because it still takes a constant amount of time to know that a list is empty. This time could be reduced to $O(V)$ if for each list in the adjacency list representation, we just also stored its length.
To compute the in degree of each vertex, we will have to scan through all of the adjacency lists and keep counters for how many times each vertex has appeared. As in the previous case, the time to scan through all of the adjacency lists takes time $O(E + V)$.
22.12
Give an adjacencylist representation for a complete binary tree on $7$ vertices. Give an equivalent adjacencymatrix representation. Assume that vertices are numbered from $1$ to $7$ as in a binary heap.

Adjacencylist representation
$$ \begin{aligned} 1 &: 2 \rightarrow 3 \\ 2 &: 1 \rightarrow 4 \rightarrow 5 \\ 3 &: 1 \rightarrow 6 \rightarrow 7 \\ 4 &: 2 \\ 5 &: 2 \\ 6 &: 3 \\ 7 &: 3. \end{aligned} $$

Adjacencymatrix representation
$$ \begin{pmatrix} 0 & 1 & 1 & 0 & 0 & 0 & 0 \\ 1 & 0 & 0 & 1 & 1 & 0 & 0 \\ 1 & 0 & 0 & 0 & 0 & 1 & 1 \\ 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 \\ \end{pmatrix}. $$
22.13
The transpose of a directed graph $G = (V, E)$ is the graph $G^\text T = (V, E^\text T)$, where $E^\text T = \{ (v, u) \in V \times V: (u, v) \in E \}$. Thus, $G^\text T$ is $G$ with all its edges reversed. Describe efficient algorithms for computing $G^\text T$ from $G$, for both the adjacencylist and adjacencymatrix representations of $G$. Analyze the running times of your algorithms.
 Adjacencylist representation: For the adjacency list representation, we will maintain an initially empty adjacency list representation of the transpose. Then, we scan through every list in the original graph. If we are in the list corresponding to vertex $v$ and see $u$ as an entry in the list, then we add an entry of $v$ to the list in the transpose graph corresponding to vertex $u$. Since this only requires a scan through all of the lists, it only takes time $O(E + V)$.
 Adjacencymatrix representation: to compute the graph transpose, we just take the matrix transpose. This means looking along every entry above the diagonal, and swapping it with the entry that occurs below the diagonal. This takes time $O(V^2)$.
22.14
Given an adjacencylist representation of a multigraph $G = (V, E)$, describe an $O(V + E)$time algorithm to compute the adjacencylist representation of the "equivalent" undirected graph $G' = (V, E')$, where $E'$ consists of the edges in $E$ with all multiple edges between two vertices replaced by a single edge and with all selfloops removed.
Create a new adjacencylist $Adj'$ of size $V$ and an empty matrix $M$ of size $V^2$ first. For each vertex $u$ in the multigraph $G$, we iterably examine each vertex $v$ of $Adj[u]$.
 If $M(u, v) == \emptyset$, mark $M(u, v)$ as $\text{true}$, then add $v$ to $Adj'[u]$.
 If $M(u, v) == true$, do nothing.
Since we lookup in the adjacencylist $Adj$ for $V + E$ times, the time complexity is $O(V + E)$.
1 2 3 4 5 6 7 8  EQUIVALENTUNDIRECTEDGRAPH let Adj'[1..V] be a new adjacencylist let M be V × V for each vertex u ∈ G.V for each v ∈ Adj[u] if M[u, v] == Ø && u != v M[u, v] = true INSERT(Adj'[u], v) 
22.15
The square of a directed graph $G = (V, E)$ is the graph $G^2 = (V, E^2)$ such that $(u, v) \in E^2$ if and only if $G$ contains a path with at most two edges between $u$ and $v$. Describe efficient algorithms for computing $G^2$ from $G$ for both the adjacencylist and adjacencymatrix representations of $G$. Analyze the running times of your algorithms.

Adjacencylist representation: To compute $G^2$ from the adjacencylist representation $Adj$ of $G$, we perform the following for each $Adj[u]$:
1 2 3 4
for each v ∈ Adj[u] for each w ∈ Adj[v] edge(u, w) ∈ E^2 INSERT(Adj2[u], w)
where $Adj2$ is the adjacencylist representation of $G^2$. After we have computed $Adj2$, we have to remove any duplicate edges from the lists (there may be more than one twoedge path in $G$ between any two vertices). For every edge in $Adj$ we scan at most $V$ vertices, we compute $Adj2$ in time $O(VE)$. Removing duplicate edges is done in $O(V + E)$ as shown in exercise 22.14. Thus the total running time is
$$O(VE) + O(V + E) = O(VE).$$

Adjacencymatrix representation: Let $A$ denote the adjacencymatrix representation of $G$. The adjacencymatrix representation of $G^2$ is the square of $A$. Computing $A^2$ can be done in time $O(V^3)$ (and even faster, theoretically; Strassen's algorithm for example will compute $A^2$ in $O(V^{\lg 7})$).
22.16
Most graph algorithms that take an adjacencymatrix representation as input require time $\Omega(V^2)$, but there are some exceptions. Show how to determine whether a directed graph $G$ contains a universal sink $$ a vertex with $\text{indegree}$ $V  1$ and $\text{outdegree}$ $0$ $$ in time $O(V)$, given an adjacency matrix for $G$.
Start by examining position $(1, 1)$ in the adjacency matrix. When examining position $(i, j)$,
 if a $1$ is encountered, examine position $(i + 1, j)$, and
 if a $0$ is encountered, examine position $(i, j + 1)$.
Once either $i$ or $j$ is equal to $V$, terminate.
We claim that if the graph contains a universal sink, then it must be at vertex $i$. To see this, suppose that vertex $k$ is a universal sink. Since $k$ is a universal sink, row $k$ in the adjacency matrix is all $0$'s, and column $k$ is all $1$'s except for position $(k, k)$ which is a $0$. Thus, once row $k$ is hit, the algorithm will continue to increment $j$ until $j = V$.
To be sure that row $k$ is eventually hit, note that once column $k$ is reached, the algorithm will continue to increment $i$ until it reaches $k$. This algorithm runs in $O(V)$ and checking whether or not $i$ in fact corresponds to a sink is done in $O(V)$. Therefore, the entire process takes $O(V)$.
22.17
The incidence matrix of a directed graph $G = (V, E)$ with no selfloops is a $V \times E$ matrix $B = (b_{ij})$ such that
$$ b_{ij} = \begin{cases} 1 & \text{if edge $j$ leaves vertex $i$}, \\ 1 & \text{if edge $j$ enters vertex $i$}, \\ 0 & \text{otherwise}. \end{cases} $$
Describe what the entries of the matrix product $BB^\text T$ represent, where $B^\text T$ is the transpose of $B$.
We have two cases, one for the diagonal entries and one for the nondiagonal entries. The entry of $[i, i]$ for some $i$ represents the sum of the in and ourt degrees of the vertex that $i$ corresponds to. To see this, we recall that an entry in a matrix product is the dot product of row $i$ in $B$ and column $i$ in $B^\text T$. But, column $i$ in $B^\text T$ is the same as row $$ in $B$. So, we have that the entry is just row $i$ of $B$ dotted with itself, that is $\sum_{j = 1}^{E} b_{ij}^2$.
However, since $b_{ij}$ only takes values in $\{−1, 0, 1\}$, we have that $b_{ij}^2$ only takes values in $\{0, 1\}$, taking zero if and only if $b_{i, j}$ is zero. So, the entry is the sum of all nonzero entries in row $i$ of $B$, Since each edge leaving $i$ is $−1$ and each edge going to $i$ is $1$, we are counting all the edges that either leave or enter $i$, as we wanted to show.
Now, suppose that our entry is indexed by $[i, j]$ where $i \ne j$. This is the dot product of row $i$ in $B$ with column $j$ in $B^\text T$, which is row $j$ in $B$. So, the entry is equal to $\sum_{k = 1}^{E} b_{i, k} \cdot b_{j, k}$.
Each term in this sum is $−1$ if $k$ goes between $i$ and $j$, or $0$ if it doesn't. Since we can't have that two different vertices ar both on the same side of an edge, no terms may ever be $1$. So, the entry is just $1$ if there is an edge between $i$ and $j$, and $0$ otherwise.
22.18
Suppose that instead of a linked list, each array entry $Adj[u]$ is a hash table containing the vertices $v$ for which $(u, v) \in E$. If all edge lookups are equally likely, what is the expected time to determine whether an edge is in the graph? What disadvantages does this scheme have? Suggest an alternate data structure for each edge list that solves these problems. Does your alternative have disadvantages compared to the hash table?
The expected loopup time is $O(1)$, but in the worst case it could take $O(V)$. If we first sorted vertices in each adjacency list then we could perform a binary search so that the worst case lookup time is $O(\lg V)$, but this has the disadvantage of having a much worse expected lookup time.