Docsity

CMPS 214 Problem Set 6: Graph Algorithms and Data Structures, Exercises of Algorithms and Programming

Toggle sidebar
bg1
CMPS 214 Problem Set 6
Exercises
1. [BFS and DFS Recap.]
Give one example of a
connected undirected
graph on four vertices, A, B, C, and D, so
that both DFS and BFS search
discover
the vertices in the
same
order when starting
at vertex A. Then give one example of a
connected undirected
graph on four vertices,
A, B, C, and D where BFS and DFS discover the vertices in a
dierent
order when
started at A. Assume that both DFS and BFS iterate over neighbors in
alphabetical
order
.
Above,
discover
means the time that the algorithm rst reaches the vertex.
[
We are expecting:
A drawing of your two graphs
and
an ordered list of vertices
discovered by BFS and DFS for each of them.]
A
B
C D
A
B
C
D
Both DFS and BFS visit the vertices in rst graph in the following order: A, B, C, D.
DFS and BFS visit the vertices in the second graph in dierent orders:
DFS: A, B, C, D
BFS: A, B, D, C
1
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download CMPS 214 Problem Set 6: Graph Algorithms and Data Structures and more Exercises Algorithms and Programming in PDF only on Docsity!

CMPS 214 Problem Set 6

Exercises

  1. [BFS and DFS Recap.] Give one example of a connected undirected graph on four vertices, A, B, C, and D, so that both DFS and BFS search discover the vertices in the same order when starting at vertex A. Then give one example of a connected undirected graph on four vertices, A, B, C, and D where BFS and DFS discover the vertices in a dierent order when started at A. Assume that both DFS and BFS iterate over neighbors in alphabetical order.

Above, discover means the time that the algorithm rst reaches the vertex. [We are expecting: A drawing of your two graphs and an ordered list of vertices discovered by BFS and DFS for each of them.]

A

B

C D

A

B

C

D

Both DFS and BFS visit the vertices in rst graph in the following order: A, B, C, D. DFS and BFS visit the vertices in the second graph in dierent orders: ˆ DFS: A, B, C, D ˆ BFS: A, B, D, C

  1. [DFS Order.] Consider the result of a run of the depth rst search algorithm (presented in class) on a directed graph. Explain how it is possible that some vertex u that has both incoming and outgoing edges in the graph ends up in a tree containing only itself in the DFS forest, i.e., we explore no node following u's edges and that node u is not explored by following some edge. Construct an example and explain why, in your example, the DFS algorithm can end up having such a node u. For this question, assume the graph has no self-loops. [We are expecting: An example directed graph G and a brief explanation.]

D

C

B

A

Given the above graph G and the assumption that subsequent DFS calls visit the vertices in alphabetical order the following would happen: The rst DFS call would traverse A, B. The second DFS call would traverse only C, since B is already visited. Finally the third DFS call would visit only D, since C is already visited. The resultant DFS forest can be seen below:

D

C

B

A

Algorithm 1 exactTraversalExists(G) 1: Compute a topological order T on G 2: for i = 0 to n 2 do 3: if Edge (T [i]; T [i + 1]) does not exists then 4: return false 5: end if 6: end for 7: return true

This algorithm clearly runs in O(m + n)-time since the for loop runs in O(n) and since computing a topological order on a DAG takes O(m+n) using the topological sorting algorithm you've seen class.

  1. [Viewpoints.] Given a directed graph G = (V; E), let us call a node v 2 V a viewpoint if all other nodes are reachable from v (in other words, v has a path to all other nodes). (a) Recall that any directed graph can be thought of as a meta-graph of its strongly connected components (SCCs), and that this meta-graph is acyclic. Briey argue that if G has a viewpoint node, G must have only one source SCC, which is an SCC such that there are no edges from another SCC to it. (You may assume that at least one source SCC exists.) [We are expecting: A brief justication on why multiple source SCC cannot exist if a viewpoint node exists.] Consider the meta-graph G^0 of the directed graph G. First notice that if a viewpoint vertex v exists then v 2 S where S is some source SCC in G^0. To see this assume that v is not in a source SCC (Note: As the problem suggests we are assuming that at least one source SCC exists). This would lead to a contradiction: Since source SCCs have no incoming edges then they are not reachable from v , this would contradict that v is a viewpoint vertex in the rst place. So it is clear that the existence of a viewpoint vertex v implies v 2 S where S is some source SCC in G^0. Now it needs be shown that the existence of a viewpoint vertex v implies that there exists exactly one source SCC S. To see this assume that the number of source SCCs are strictly greater than 1. Now consider the SCC S such that v 2 S and any other source SCC S^0 , where clearly s 62 S^0. This would contradict that v is a viewpoint vertex since S^0 has no incoming edges so it is not reachable from v. So if a viewpoint vertex v exists then it must exist in the only source SCC that exists in G^0. (b) Let m = jEj and n = jV j, give an O(n + m)-time algorithm to nd a viewpoint node in a directed graph, or report that none exists. You may use any algorithm covered in class directly. [We are expecting: A description of the algorithm, a brief explanation of why the algorithm works and an informal justication of the algorithm's running time.]
  1. [Safe Return Party Planning.] You are a party planner. You carefully chose n  3 guests to invite to a party so that for every two guests x and y , even if x doesn't know y , at least x knows a guest who knows a guest who knows a guest who... (this might go on for any number of guests)... who knows y (we call this the fun party property). Note that "knowing" is bidirectional (i.e. a graph with edge representing "knowing" should be undirected). But now, moments before sending out the invitation, you were told that you can only invite n 1 guests. Explain why there exists a guest that you can un-invite without violating the fun party property, and give an ecient algorithm for nding such a guest. Give an algorithm that nds such a guest in O(m + n) time, where m is the number of pairs of guests who know each other. [We are expecting: A description of the algorithm, a brief explanation of why the algorithm works and an informal justication of the algorithm's running time.] Given the connected graph G = (V; E) where vertices represent people and edges represent which two people know each other, one can construct any minimum spanning tree M of G. Now, since any tree has at least two leaves then by removing one of these leaves, the remaining graph is still a tree and consequently is still connected. One simple algorithm is to rst compute the MST M of G which runs in O(m log n) when using Kruskal's algorithm, then nd a leaf by searching for a vertex of degree 1, in the worst case this can take O(n). The issue here is this algorithm runs in O(m log n + n) and not O(m + n) so we need to do something smarter. First, we need to prove the following: Given a G = (V; E) and some vertex u 2 V : If v , such that v 6 = u, is a farthest vertex from u in G then v is not a cut vertex. Where a cut vertex is a vertex that disconnects a connected graph when deleted. To see this assume that v is a cut vertex, and let w be a vertex from a component in the graph G v that does not contain u. Notice that in this case v is in every (u; w )-path, but this implies that the (u; w )-path is longer than the (u; v )-path, which contradicts that v is farthest from u.

So using the above proof we can construct a O(m + n) algorithm to solve the problem. Simply pick an arbitrary vertex u and apply BFS/DFS and keep track of the distances between the discovered vertices and u. Once BFS/DFS is complete simply nd the farthest vertex v from u, if there are multiple pick one arbitrarily and delete it. Vertex v represents the guest that when uninvited maintains that the fun party property, or in other words, maintains that the graph is still connected since it is not a cut vertex.

  1. [Optional] Robust Fuel Distribution. In September 2019, an explosion in Shelby County, Alabama severed the Colonial Pipeline which transports more than 100 million gallons of rened petroleum products from Houston to the southeast every day. The incident caused gasoline prices to jump 15% and might cause jet fuel and gasoline shortages in the region. This event has made you concerned about fuel supply/price stability in the US. As a student of CMPS 214, you would like to analyze this system. Here we will consider the robustness of the fuel distribution system under a few simplications. Let our system be a graph such that: ˆ Each edge in the graph is a pipe. ˆ Each edge is directed (fuel ow is not bidirectional). ˆ Each node in the graph is a combination renery/distribution terminal (nodes both produce and consume fuel products, so they can have both incoming and outgoing edges). ˆ Every renery produces the same fuel that every distribution terminal accepts. In other words, we will not worry about dedicated pipelines for dierent products. Assume the system has n nodes and m edges. You would like the system to have stronger robustness by designing algorithms that verify if the system has certain prop- erties: (a) Design an algorithm that veries if the fuel distribution system has the following property:

There is a way to transport fuel from any node in the graph to any other node in the graph. Your algorithm should run in O(n + m). [We are expecting: A description of the algorithm, a brief explanation of why the algorithm works and an informal justication of the algorithm's running time.] First compute the meta-graph G^0 = (V 0 ; E^0 ) of the directed graph G = (V; E) which runs in O(n + m) (recall the meta-graph of a directed is the graph of its SCCs). If jV 0 j = 1 then all of G is strongly connected and the fuel distribution system has a way to transport fuel from any node to any node to any other node so we return true, otherwise this is not the case and return false. (b) Design an algorithm that veries if the fuel distribution system has the following property: No single pipe failure can isolate any node from either distributing fuel to the rest of the system or consuming fuel from the rest of the system. That is, removing a single edge cannot cause any node to be unreachable from any other node. Your algorithm should run in O(mn + m^2 )). You may assume an algorithm that satises the property in part (a) exists and use it directly.