0% found this document useful (0 votes)
74 views4 pages

Homework 10: Basic Algorithms 2024

Homework 10

Uploaded by

Ava Cheung
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)
74 views4 pages

Homework 10: Basic Algorithms 2024

Homework 10

Uploaded by

Ava Cheung
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

Basic Algorithms, Fall 2024 Instructor: Rotem Oshman

CSCI-UA.0310-001 Homework 10

Due November 22 (11:59 p.m.)


Instructions
• Answer each question on a separate page.
• Honors questions are optional. They will not count towards your grade in the course. How-
ever you are encouraged to submit your solutions to these problems to receive feedback on
your attempts. Our estimation of the difficulty level of these problems is expressed through an
indicative number of stars (′ ∗′ = easiest) to (′ ∗ ∗ ∗ ∗ ∗′ = hardest).
• You must enter the names of your collaborators or other sources as a response to Question 0.
Do NOT leave this blank; if you worked on the homework entirely on your own, please write
“None” here. Even though collaborations in groups of up to 3 people are encouraged, you are
required to write your own solution.

List all your collaborators and sources: (−∞ points if left blank)
...LIST COLLABORATORS HERE...

Question 1: Tours (5+5+5=15 points)


Suppose the New York Botanical Gardens are trying to drum up interest for their tree and shrub collections
by offering tours through the arboretum, and you are tasked with reviewing their proposed routes. There
are a set V of trees they want some tour to stop at, and a set E of routes from tree to tree their proposed
tours would make. In order to make sure all the trees are visited, they want the following property:
Property 1. For all pairs v, u ∈ V , we must have a path from u to v or a path from v to u (or both).
They provide you with many proposed plans and a strict deadline for your feedback, and so you want to
develop an efficient algorithm to automate this task.

1. Suppose G = (V, E) is a directed acyclic graph. Design a simple O(|V | + |E|) time algorithm to
determine whether or not the given graph G satisfies the desired property. For example, in Figure 1,
the first example satisfies the property while the second does not (as there is no path from A to C
nor from C to A). Argue the correctness of your algorithm.
(Hint: Base your algorithm on topological sort.)
(Hint:
difference?
one does not. Draw the edges of each graph on the vertices in topological order. What is the )
is a valid topological sort of both, but one of the graphs satisfies the property while the other
If we change the examples to A ← B → C → D and B → A → C → D, then (B, A, C, D)

2. Suppose now that G = (V, E) is an arbitrary directed graph. Design a O(|V | + |E|) time algorithm
to determine whether or not the given graph G satisfies the desired property.
(Hint: Recall that any directed graph G can be decomposed into a DAG of strongly connected
components. Can you reuse some ideas of your prior approach?)
3. Prove that your algorithm in (2) is correct and that it runs in the required time.
Basic Algorithms, Fall 2024 Instructor: Rotem Oshman
CSCI-UA.0310-001 Homework 10

A B C

A B C

Figure 1: Illustrations for Question 1-1.

Question 2: Topological Sort (5+5=10 points)


1. How many valid topological sorts does the directed graph G in Figure 2 below have? List all the
valid topological sorts in the following table. One of them has been listed as an example, where
node A has the last finish time and D has the first.

B C

A D

F E

Figure 2: Directed G for topological sort.

1. A B C F E D
2.

2. Give an example of a graph showing that the topological sort algorithm does not work if we output
vertices in order of their discovery time, instead of reverse finish time.

Question 3: Spanning Tree (4+4+4=12 points)


Recall that for an undirected graph G = (V, E) as a spanning tree T is a subgraph containing all vertices
V such that T is connected and acyclic.
In the following, let e = (u, v) ∈ E be an edge that is not part of T . Prove the following properties:

1. Assume we add e to T , i.e., consider the graph T ′ over the vertices V that contains all edges from
T and e. Show that this graph is no longer acyclic.

2. Now let e′ be an arbitrary edge on the cycle created by adding e to T ′ in part (1). Let T ′′ be the graph
obtained by removing e′ , i.e., the graph obtained by replacing e′ with e in the original spanning tree
T . Show that T ′′ is connected.

3. Now complete the proof of T ′′ being a spanning tree by also showing that T ′′ is acyclic.
Basic Algorithms, Fall 2024 Instructor: Rotem Oshman
CSCI-UA.0310-001 Homework 10

Honors Question
We present the following (false) claim:

Claim 1. If a directed graph G contains cycles (i.e., is not acyclic), then topological sort produces a vertex
ordering that minimizes the number of “bad” edges that are inconsistent with the ordering produced. More
precisely, a bad edge is one going from a vertex later in the ordering to an earlier vertex.

Disprove this claim by providing a counterexample. Briefly justify why your graph fails the claim.
Basic Algorithms, Fall 2024 Instructor: Rotem Oshman
CSCI-UA.0310-001 Homework 10

Reference: Topological Sort and SCC


Strongly connected components. Strong connectedness is an equivalence relation on the set of vertices.
It’s 1) symmetric (u ∼ v implies v ∼ u) and 2) transitive (if u ∼ v and and v ∼ w, then u ∼ w). Hence,
SCC partitions the vertex set V (G).

Claim 2. The component graph is a DAG.

Proof. If there’s a cycle, then all the component (i.e., mega nodes) in the cycle are strongly connected,
which is a contradiction.

DFS-Visit(G, u):
Explore node u on graph G.
1 time += 1
2 u.d = time
3 [Link] = GRAY
4 for v ∈ Adj[u]:
5 if [Link] = WHITE then
6 [Link] = u
7 DFS-Visit(G, v)
8 [Link] = BLACK
9 time += 1
10 u.f = time

DFS(G):
Explore node u on graph G.
1 time = 0
2 for u ∈ V (G):
3 [Link] = WHITE
4 [Link] = NULL
5 for u ∈ V (G):
6 if [Link] = WHITE then
7 DFS-Visit(G, u)

TopologicalSort(G):
1 run DFS(G).
2 output vertices in decreasing order of finish time.

SCC(G):
1 run DFS(G) to compute u.f for each u ∈ V (G).
2 run DFS(GRev ), where in the main outer loop consider vertices in decreasing order of u.f.
3 output the vertices of each tree as a separate SCC.

You might also like