For problem 1, assume the input graph is represented using adjacency matrix. For other problems, assume input graphs are represented using adjacency lists.
1. CLRS Exercise 22.1-6
2. Given a graph G = (V, E), the line graph of G, denoted L(G), is a graph whose vertex set is the edge set of G, and any two vertices of L(G) are joined by an edge if and only if their corresponding edges are adjacent in G.
As usual, we write n = |V| and m = |E|,
and assume that the vertices of G are labelled 1, 2, ..., n
and the vertices of L(G) are labelled 1, 2, ..., m.
The graph G is represented as adjacency lists.
Furthermore, assume we are given an O(1)-time procedure vertex(v, w)
such that for all vertices v, w in V,
calling vertex(v, w) returns 0 if vw is not an edge of G,
and it returns an integer k (1 ≤ k ≤ m) such that
vertex k of L(G) corresponds to the edge joining v to w in G.
Write a pseudocode to fill in the array dL[1..m] such that dL[v] is the degree of vertex v of L(G) in time O(m+n). (Hint: You may find that using another array dG[1..n] to keep track of the degrees of the vertices of G is convenient.)
3. CLRS Exercise 22.3-6. To make this exercise concrete, assume
you are to write, using a stack S, an iterative version of the following
recursive dfs(v) procedure.
(You may have to modify main() a bit as well.)
main() {
for v ← 1 to n do
discovered[v] ← FALSE
for v ← 1 to n do
if not discovered[v] then
dfs(v)
}
dfs(v) {
discovered[v] ← TRUE
processVertex(v)
init(Adj[v])
w ← next(Adj[v])
while w ≠ NIL do {
if not discovered[w] then {
processEdge(v, w)
dfs(w)
}
w ← next(Adj[v])
}
}
Assume every adjacency list Adj[·] is an iterable linked-list.
For an iterable list L,
the procedure init(L) gets L ready for iteration.
Also, the procedure next(L) returns the next node on L, if there is one;
it returns NIL if L contains no next node.
4. CLRS Exercise 22.4-2
5. CLRS Exercise 22.4-3. Justify why your algorithm runs in time O(|V|).