0% found this document useful (0 votes)
10 views5 pages

Directed Graph Analysis Exercises

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Directed Graph Analysis Exercises

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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:
[Link] = WHITE;
time = 0;
S1 = ∅;
S2 = ∅;
for each vertex u ∈ G.V:
if ([Link] == WHITE):
[Link](u);
while (S1 not empty):
v = [Link]();
if ([Link] == WHITE):
time += 1;
v.d = time;
[Link] = GREY;
for each w ∈ [Link][]:
if ([Link] == WHITE):
[Link](w);
[Link](v);
while (S2 not empty):
v = [Link]();
time += 1;
v.f = time;
[Link] = 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

Common questions

Powered by AI

Storing the adjacency list in decreasing order affects the structure of trees generated from the graph because the traversal or exploration order of vertices changes, which can lead to different tree structures. However, it does not change the depth of each node .

The process of topological sorting starts at node 140, the node with the smallest value. Following topological sorting results in an ordered sequence of nodes that respects the directed edges, meaning if there is an edge from node A to node B, A appears before B in the ordering. This impacts the understanding of node precedence by providing a linear order of nodes that does not violate the prerequisites implied by the graph's edges, thus reflecting dependencies accurately .

In an adjacency list representation, the out-degree of a node is determined by the length of its list in the adjacency list. In other words, the out-degree of a node i is equal to the number of elements in Adj[i]. The sum of the lengths of all adjacency lists in Adj equals the number of edges |E| in the graph. Therefore, computing the out-degree of every node is done in O(|V| + |E|) time complexity .

In a directed graph using depth-first search (DFS), tree edges are those that connect a vertex to a descendant in the DFS tree. Back edges point from a vertex to an ancestor in the DFS tree, indicating a cycle. Forward edges lead to successors that are not direct children, while cross edges connect vertices in different branches or to ancestors that are not directly connected in the DFS tree .

A multi-graph allows multiple edges between the same pair of nodes, whereas in non-multi-graphs there is at most one edge from node i to node j for each pair (i, j). The graph described is not a multi-graph because there is at most one edge between any pair of nodes .

The nodes with the largest in-degree in the given graph are 4, 5, 7, and 9, each with an in-degree of 3. The in-degree of a node indicates the number of edges coming into the node .

The given graph is not acyclic because it contains back edges. Back edges are edges that point from a node to one of its ancestors in a depth-first search tree, and their presence indicates cycles .

From the adjacency list and matrix, it is easy to discern the graph's directed edges between nodes, out-degrees, and in particular, whether nodes have outgoing connections only or receive connections from others. The adjacency list specifies direct connections, while the matrix provides a global overview, showing zero or one for absent/present edges for each node pair .

The in-degree of a node is computed by looping through the adjacency list and counting the number of times each node appears in the sub-lists. In more detail, to compute the in-degree of node i, you loop through the adjacency list and count how many times node i appears in Adj[j] for all j ≠ i. This computation requires looping through Adj once, hence the time complexity is O(|V| + |E|).

Node 5 has the largest out-degree because its adjacency list contains the most neighbors, specifically four, namely nodes 1, 4, 6, and 9. In an adjacency list, the out-degree is determined by counting the number of direct connections from the node to other nodes .

You might also like