0% found this document useful (0 votes)
12 views

GraphAlgorithms Solution

The document contains exercises related to graph representations and algorithms. It defines adjacency lists and matrices for various graphs and calculates properties like in-degrees and out-degrees of nodes. It also contains implementations of depth-first search and topological sorting.

Uploaded by

Phạm Châu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

GraphAlgorithms Solution

The document contains exercises related to graph representations and algorithms. It defines adjacency lists and matrices for various graphs and calculates properties like in-degrees and out-degrees of nodes. It also contains implementations of depth-first search and topological sorting.

Uploaded by

Phạm Châu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Exercise 1

Adj[1] = {2, 3, 6}
Adj[2] = {3, 4}
Adj[3] = {2, 4, 5}
Adj[4] = {5, 7, 8}
Adj[5] = {1, 4, 6, 9}
Adj[6] = {7, 9}
Adj[7] = {5, 8}
Adj[8] = {9}
Adj[9] = {7}

Exercise 2
The nodes 4, 5, 7, 9 have the largest in-degree value, which is 3.

Exercise 3
Node 5 has the largest out-degree value, which is 4.

Exercise 4
No. The graph in Fig 1 is not a multi-graph. Because there is at most 1
edge i → j that goes from node i to node j for each (i, j).

Exercise 5
Given an adjacency-list representation Adj of a directed graph, the
out-degree of a node i is equal to the length of Adj[i], and the sum of the
lengths of all the adjacency lists in Adj is |E|. Thus the time to compute the
out-degree of every node is O(|V| + |E|).

Exercise 6
To compute the in-degree of every node, simply loop through the adjacency
list and count the number of times each node appears in the sub-lists. i.e.
to compute the in-degree of node i, loop through the adjacency list and
count how many times node i appears in Adj[j] for all j ≠ i. So we just need
to loop through Adj once. Similar to Ex.5, we get the time to compute the
in-degree of every node is also O(|V| + |E|).

Exercise 7

- Adjacency list:
Adj[1] = {2, 3}
Adj[2] = {4, 5}
Adj[3] = {6, 7}
Adj[4] = {8}
Adj[5] = {}
Adj[6] = {}
Adj[7] = {}
Adj[8] = {}
- Adjacency matrix:
[0 1 1 0 0 0 0 0]
[0 0 0 1 1 0 0 0]
[0 0 0 0 0 1 1 0]
[0 0 0 0 0 0 0 1]
[0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0]

Exercise 8

Exercise 9
9.1. Adjacency list stored in decreasing order:

=> No. The trees will not be the same.


9.2. No. The depth of each node remains unchanged.
Exercise 10

Exercise 11
Tree edges: (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8), (8, 9)
Back edges: (3, 2), (5, 1), (5, 4), (7, 5), (9, 7)
Forward edges: (1, 3), (1, 6), (2, 4), (3, 5), (4, 7), (4, 8), (5, 9), (6, 9)
Cross edges: None

Exercise 12
No. The graph in Fig 1 is not an acyclic graph. Because we have a handful
of back edges here.

Exercise 13
DFS(G):
for each vertex u ∈ G.V:
u.color = WHITE;
time = 0;
S1 = ∅;
S2 = ∅;
for each vertex u ∈ G.V:
if (u.color == WHITE):
S1.push(u);
while (S1 not empty):
v = S1.pop();
if (v.color == WHITE):
time += 1;
v.d = time;
v.color = GREY;
for each w ∈ v.Adj[]:
if (w.color == WHITE):
S1.push(w);
S2.push(v);
while (S2 not empty):
v = S2.pop();
time += 1;
v.f = time;
v.color = BLACK;

Exercise 14
We start at 140, since it is the node with the smallest value.
After topological sort we obtain the following ordering:
142, 140, 154, 143, 374, 373, 417, 415, 414, 413, 410, 351, 352, 333, 341,
331, 403, 311, 344, 332, 312

You might also like