0% found this document useful (0 votes)
115 views17 pages

Graph Algorithms & Topological Sort

The document provides practice problems and exercises related to graph algorithms, topological sorting, and shortest paths. It includes problems on elementary graph algorithms like BFS and DFS, computing strongly connected components, determining if a graph is bipartite, topological sorting of DAGs, and finding the shortest path of a knight on a chessboard. Solutions and explanations are provided for each problem. Key topics covered are graph traversal algorithms, identifying strongly connected components, properties of DAGs, and shortest path problems.

Uploaded by

Chirag Sood
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)
115 views17 pages

Graph Algorithms & Topological Sort

The document provides practice problems and exercises related to graph algorithms, topological sorting, and shortest paths. It includes problems on elementary graph algorithms like BFS and DFS, computing strongly connected components, determining if a graph is bipartite, topological sorting of DAGs, and finding the shortest path of a knight on a chessboard. Solutions and explanations are provided for each problem. Key topics covered are graph traversal algorithms, identifying strongly connected components, properties of DAGs, and shortest path problems.

Uploaded by

Chirag Sood
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

COMP 251: Fall 2021

Exercises for Students – Volume 3


Topics: elementary graph algorithms, topological sort and shortest paths

Elementary graph algorithms

Nicole:
In the following graphs, assume that if there is ever a choice amongst multiple nodes, both the BFS and
DFS algorithms will choose the left-most node first.

Starting from the green node at the top, which algorithm will visit the least number of nodes before
visiting the yellow goal node?

A. BFS
B. DFS
C. Neither BFS nor DFS will ever encounter the goal node in this graph.
D. BFS and DFS encounter same number of nodes before encounter the goal node

Solution:

A.

For BFS algorithm, visiting a node’s siblings before its children, while in DFS algorithm, visiting a node’s
children before its siblings. Before countering goal node F; BFS algorithm encounters nodes: ABCDE, and
DFS algorithm encounters nodes: ABDHLIEJMC. Thus, the answer is A. BFS.
Source: [Link]
ba110b3e763e/Practice%20Problems/[Link]

Tim:
Given the following maze, starting from the top left node, show how a Breath First Search algorithm
explores the maze. Show each step, indicating discovered, undiscovered, and finished nodes.

Solution:

[Link]
usp=sharing

KC:
A graph G = (V,E) is called bipartite if V can be partitioned into two disjoint sets L and R such that every
edge in E has one end in L and the other in R. That is, there are no edges from a vertex in L to another
vertex in L (and similarly there are no edges from a vertex R to another vertex in R). The following is an
example of a bipartite graph with L circled in blue and R circled in green:

Give an algorithm that uses depth-first search to determine whether or not a graph G is bipartite. Briefly
discuss its correctness and running time.
Solution: Use depth-first search with a modified colour scheme. Suppose that s is the first first vertex is
discovered. Colour s green. For every subsequent vertex that is discovered, colour it red if its
predecessor is coloured green and colour it green if its predecessor is coloured red. If we encounter an
edge between two vertices of the same colour, stop and return that G is not bipartite. If no such edge is
encountered, return that G is bipartite (notice that we can take L to be the green set and R to be the red
set of vertices).

The correctness of this algorithm follows from the fact that DFS encounters every edge in the graph. The
running time of this algorithm is the same as that of DFS, I.e., O(V + E).

Source: [Link]

Arnaud :
Chess Knight Problem | Find the shortest path from source to destination

Consider the following problem : Given a chessboard, find the shortest distance (minimum number of
steps) taken by a knight to reach a given destination from a given source.

To solve this problem, consider using a graph where each node is a possible position of the knight in the
table, and is connected to every reachable position within one move.

Which exploration method would you use to solve this problem? BFS or DFS? Justify your choice.

Answer :

You want to minimize the number of moves to get to the target position. Using DFS you risk searching
for very long move sequences before reaching either the target position or a node with no unexplored
neighbors. Plus, even if you reach the target position, to guaranty that it is the shortest you will have to
test each other possible path for at least the same number of moves.
For this reason, BFS is the way to go, the first time you reach the target position it is guaranteed that
you used the shortest path. Plus, for reasonable movements you have much higher chances of solving
the problem in shorter time.

More detail is given in the Shortest path course.

Linhui:
Given a directed graph, develop an algorithm to check if it’s strongly connected or not using BFS or DFS
and give the computational complexity, where V is the total number of vertices and E is the total
number of edges in the graph.
Solution:

Method 1: Perform BFS or DFS starting from every vertex in the graph. If each DFS/BFS call visits every
other vertex in the graph, then the graph is strongly connected. Cost = O(V(V+E)).

Method 2: (a better method)

If G passes both DFS, it’s strongly connected.

Cost = O(V+E)

Source: [Link]

Muhammad Bilal:
Solution:

Source: [Link]
ba110b3e763e/Practice%20Problems/[Link]

Mushfique:
Q: Traverse a given directory using BFS.

A: We can iteratively traverse the given directory, and print all files present within it and its sub-
directories using Breadth–first search. Following is the complete algorithm:

1. Create an empty queue of File class and enqueue the root directory.
2. Loop till queue becomes empty (all files and directories present inside the root directory are
processed)
a. Pop front File from the queue.
b. If the popped File is a directory, get the list of all files and directories present in it, add
each directory to the queue and print every file.

The algorithm can be implemented as follows in Java:

import [Link];
import [Link];
import [Link];

class Main
{
// Iterative function to traverse a given directory in Java using BFS
public static void listFilesIteratively(File root)
{
// maintain a queue to store files and directories
Queue<File> queue = new ArrayDeque<>();

// add root directory to the queue


[Link](root);

// loop till the queue is empty. i.e., all files and directories present
// inside the root directory are processed
while (![Link]())
{
// get the next file/directory from the queue
File current = [Link]();

// get the list of all files and directories in `current`


File[] listOfFilesAndDirectory = [Link]();

// `listFiles()` returns non-null array if `current` denotes a directory


if (listOfFilesAndDirectory != null)
{
// iterate over the list of the files and directories in
// the current directory
for (File file: listOfFilesAndDirectory)
{
// if the current file is a directory
if ([Link]()) {
[Link](file);
}
// otherwise, print it
else {
[Link](file);
}
}
}
}
}

public static void main(String[] args)


{
// root directory
String dir = "/var/www/html";
File rootDir = new File(dir);

listFilesIteratively(rootDir);
}
}

Source: [Link]
Topological sort and strongly connected components

Nicole:
Compute the strongly connected components of the graph below:

Solution:

The SCCs are ABEJ, CDFGI, and H.

Running DFS on the reverse graph we get:

Then running explore on the largest post number, C, we find the SCC CDFGI. Next, we run explore on the
largest remaining post number, H, yielding just H. Finally, we explore A, yielding the last SCC, ABEJ. Thus,
the SCCs are ABEJ, CDFGI, and H.

Source: [Link]

Tim:
Determine the SCCs of the following graph:
Solution:

The SCCs are ACDH, BEI, and GF

KC:
What is the maximum number of edges m that can exist in a DAG of n vertices? Provide a proof. (Hint:
Consider a topological ordering of the DAG).

Solution: Given a DAG, there must exist at least one topological ordering, so choose one topological
ordering and “fix it” (i.e. don’t change it). Vertex v_1 in this topological ordering has no incoming edges
(its in-degree is 0). It may have up to n-1 outgoing edges. Take v_2 in the topological ordering. It can
have at most n-2 outgoing edges, that is, to vertices v_3, … v_n. Following this pattern, v_k can have at
most n-k outgoing edges. In particular, v_n can have no outgoing edges. Thus the total number of edges
is at most (n-1) + (n-2) + (n-3) + …. + 1 + 0 = n(n-1)/2. Note that this is much less than n^2 which is the
maximum number of edges in a directed graph with n vertices.

Source: [Link]

Arnaud :
Problem : What is the maximum number of edges m that can exist in a DAG with n vertices?
Solution: Given a DAG, there must exist at least one topological ordering, so choose one topological
ordering and “fix it” (i.e. don’t change it). Vertex v_1 in this topological ordering has no incoming edges
(its in-degree is 0). It may have up to n-1 outgoing edges. Take v_2 in the topological ordering. It can
have at most n-2 outgoing edges, that is, to vertices v_3, … v_n. Following this pattern, v_k can have at
most n-k outgoing edges. In particular, v_n can have no outgoing edges. Thus the total number of edges
is at most (n-1) + (n-2) + (n-3) + …. + 1 + 0 = n(n-1)/2. Note that this is much less than n^2 which is the
maximum number of edges in a directed graph with n vertices.

Source : [Link]

Linhui:
Show the ordering of vertices produced by topological sort on the following DAG:

Solution:

Source: CLRS exercise 22.4-1


[Link] ,
[Link]

Muhammad Bilal:

Solution:

Source: [Link]

Mushfique:
Q: Let G be a DAG containing edge (v,w). Suppose that during an execution of DFS(G) there is a call
DFS(G,v). Is it possible that DFS(G,w) has already been called ? If so, can we say whether DFS(G,w) has
already returned i.e. exited?

A: Yes it possible that DFS(G,w) has already been called e.g. the DFS(G) could have started with
DFS(G,w). However, if DFS(G,w) has already been called, then it must have already returned. Otherwise,
there would be a path from w to v on the tree, namely the path defined by the (allegedly) currently
running DFS(G,w). But if there is a path from w to v then the graph cannot be a DAG (since there is an
edge (v,w)).

Source: [Link]
Shortest paths

Nicole:
(a) Calculate the shortest path from s to all other vertices by using the Dijkstra algorithm. Determine the
shortest path tree.

(b) Is the shortest path tree unique?

Solution:

(a) The solution is given by the following picture. The numbers next to vertices are the distances to the
starting vertex and crossing out a number means that there has been an update. The numbers in
squares indicate the final distances (shortest distances).

(b) No shortest path tree is not unique. By substituting the edge (s, 3) with the edge (1, 2) one gets
another shortest path tree.
Source: [Link]

Tim:
Perform Dijkstra’s single source path algorithm on the graph below using C as the source. Give the order
in which vertices are removed from the priority queue. Note that A is not reachable from C, so it is never
added (and thus never removed) from the priority queue.

Solution:

C, B, F, H, D, E, G, I

Start by picking the vertex with the shortest path to C and then remove it from the queue and insert it in
your set. Then update distance values of its adjacent sets. Then proceed to choose the next closest
vertex to your set.

Source: [Link]

KC:
Dijkstra’s algorithm assumes the edges have non-negative weights. (Where does this come up in the
proof of correctness?) But suppose we have a graph with some negative weights, and let edge e be such
that cost(e) is the smallest (most negative). Consider a new graph in which we add cost(e) to all the edge
weights, thus making all weights in the new graph non-negative. Now the conditions hold for Dijkstra to
find the shortest paths, so we could now run Dijkstra. Is this a valid way to solve for shortest paths in the
case that some edges have negative weights? If yes, prove it. If not, give a brief explanation and a
counterexample.

Solution: No. For any path in the original graph, the distance of that same path P in the new graph will
be greater by cost(e) multiplied by the number of edges in the path P, since we incremented each edges
cost by cost(e). But for two paths with a different number of edges, the totals for the extra costs that we
have added will be different. So there is no reason why you should expect to get the same solutions in
the two cases. [Note that by “same solutions” here, I don’t just mean the same distances of the shortest
paths; I mean the paths themselves!] For example, take the graph with three edges cost(u,v) = -2,
cost(v,w) = 2, cost(u, w)=1. The shortest path to w is (u, v, w). But adding 2 to the cost of each edge and
then running Dijkstra would give the shortest path as (u, w).

Solution: [Link]

Arnaud :
Suppose you have a graph representing all flights available in the country, weighted by their flight
duration. Finding the shortest path between two cities can be done using the Dijkstra algorithm for
example. The catch is: we want to take into account the stopover durations, which in one case will be
supposed constant for all cities, and in another case depending on the city. Propose a way to deal with
it.

Answer:

If the stopover duration is constant, just add the duration to all edges of the graph. Remember to
remove one time this duration when returning the minimum travel time because it will be counted one
time too much.
If the stopover durations are depending on the city, the simplest idea is to duplicate each city node in
order to have for example New_York_Arrivals and New_ York_Departures, that way, you can add the
edge between those two node and run the algorithm on the updated graph.

Linhui:

Solution:
Source: CLRS 24.3-6

[Link]

Muhammad Bilal:
Solution:

Source: [Link]

Mushfique:
Q: Consider the following undirected, weighted graph:
Step through Dijkstra’s algorithm to calculate the single-source shortest paths from A to every other
vertex. Show your steps in the table below. Cross out old values and write in new ones, from left to right
within each cell, as the algorithm proceeds. Also list the vertices in the order which you marked them
known. Finally, indicate the lowest-cast path from node A to node F.

A:

Known vertices (in order marked known): A, B, C, G, E, D or F, D or F

Lowest-cost path from A to F: A to B to C to E to F


Source:
[Link]

You might also like