Assignment 4
Introductory
Problem 1 — Minimum-Product Spanning Tree
A weighted graph \(G\) is given, where every edge weight is a positive integer. Define the weight of a spanning tree as the product of its edge weights. Give an \(\mathcal{O}(n + m\log n)\) algorithm that finds a spanning tree of minimum weight.
Problem 2 — Array MST
An array of length \(n\) is given, with value \(a_i\) at index \(i\). From it, construct a complete weighted undirected graph \(G\) on \(n\) vertices, where the edge between vertices \(i\) and \(j\) has weight \(a_i + a_j\). Give an \(\mathcal{O}(n)\) algorithm that finds an MST of this graph.
Problem 3 — A Changing Tree
A weighted undirected graph \(G\) and one of its minimum spanning trees are given. We add one edge to the graph. Give an \(O(V)\) algorithm that computes an MST of the new graph.
Problem 4 — Recursive Spanning Tree
Professor Borden proposes the following divide-and-conquer algorithm for computing minimum spanning trees:
Given a graph \(G = (V,E)\), partition \(V\) into \(V_1\) and \(V_2\) so that \(||V_1|-|V_2|| \le 1\). Let \(E_1\) be the edges whose two endpoints lie in \(V_1\), and define \(E_2\) analogously for \(V_2\). Recursively solve the MST problem on \(G_1 = (V_1,E_1)\) and \(G_2 = (V_2,E_2)\). Finally, choose a minimum-weight edge of \(E\) crossing the cut \((V_1,V_2)\) and use it to join the two resulting minimum spanning trees into a spanning tree.
Prove that this algorithm correctly computes an MST of \(G\), or give a counterexample on which it fails.
Problem 5 — Unique Weights, Unique Tree!
A weighted graph \(G\) is given, all of whose edge weights are positive and pairwise distinct. Prove that its minimum spanning tree is unique.
Problem 6 — True or False!
Determine whether each statement below is true or false. Prove every true statement and give a counterexample for every false one.
For each item, at most two sentences identifying the core argument are enough. Do not overthink it.
Assume \(G\) is a connected undirected graph with \(n\) vertices and \(m\) edges. Edge weights may be equal unless a statement explicitly says otherwise.
- If \(n \leq m\) and the maximum-weight edge of \(G\) is unique, then this edge appears in no minimum spanning tree.
- If the maximum-weight edge of \(G\) is unique and belongs to at least one cycle, then it appears in no minimum spanning tree.
- Suppose \(e\) is a minimum-weight edge of \(G\) (not necessarily the only one). Then \(e\) appears in every spanning tree of \(G\).
- If the minimum-weight edge of \(G\) is unique, then it necessarily appears in every spanning tree of \(G\).
- Prim's algorithm remains correct when edge weights may be negative.
Problem 7 — Water
Suppose \(n\) containers are arranged in a row. Container \(i\) has capacity \(a[i]\). Initially, every container is empty.
Support two types of operations:
-
Pour water: add \(x\) units of water to container \(p\). If the water already in container \(p\) plus \(x\) exceeds its capacity \(a[p]\), the excess water automatically flows into the next container, \(p+1\). This continues until either no water remains or the last container, \(n\), is reached. Any overflow from the last container spills onto the ground and is lost.
-
Query: report the current amount of water in container \(k\).
Design an \(O((n + m) \log n)\) algorithm, where \(m\) is the total number of operations.
Problem 8 — Cycle Cover
A weighted undirected graph is given. A set \(F\) is called a cycle cover if every cycle of the graph contains at least one edge of \(F\). Give a polynomial-time algorithm that finds such a set \(F\) of minimum total weight.
Solution - Mohammad Mahdi Siavashi
Problem 9 — Bounded Weights
A connected weighted graph \(G\) has \(n\) vertices and \(m\) edges, and every edge weight is an integer between \(1\) and \(K\). Give an efficient algorithm for finding an MST.
Analyze its running time in terms of \(n\), \(m\), and \(K\).
Solution - Bahar Barghabani
Problem 10 — Borůvka
Let \(G\) be a weighted graph with \(n\) vertices and \(m\) edges. For every vertex \(v\), consider its incident edges and let \(mn_v\) be a minimum-weight one. For simplicity, assume all edge weights are distinct.
- Give an \(\mathcal{O}(n + m)\) algorithm that computes \(mn_v\) for every vertex \(v\).
- Prove that the set of edges \(mn_v\) contains no cycle. Call this edge set \(M\).
- Prove that the edges of \(M\) occur in an MST; that is, at least one MST contains all of \(M\).
- Consider the connected components formed by the edges of \(M\). Contract every component into a single vertex and remove every edge whose endpoints now lie in the same component. Repeat this operation recursively until only one vertex remains.
- Briefly prove that the union of the edges selected in all rounds is an MST of the original graph.
- What is the asymptotic running time of this algorithm?
Problem 11 — Sum of Distances
A weighted graph \(G\) is given, with positive-integer edge weights. Define the distance between vertices \(u\) and \(v\), denoted \(dis(u,v)\), as the minimum value \(w\) such that \(u\) can reach \(v\) using only edges of weight at most \(w\). Also define \(dis(u,u)=0\).
Give an \(\mathcal{O}(n + m\log n)\) algorithm that computes the sum of the distances over all pairs of vertices. Treat the pairs \((u,v)\) and \((v,u)\) as identical.
Solution - Amirhossein Esfandiari
Problem 12 — Maximum Bipartite Matching
Let \(G\) be a bipartite graph. Give a method based on maximum flow that finds a maximum matching in \(G\).
A matching is a subset of the edges of \(G\) in which no two edges share an endpoint.
Hints
Construct a flow network whose maximum-flow value equals the size of a maximum matching in the input graph.
Problem 13 — Good Pairs
\(n\) positive integers are given. A pair \((a,b)\) is called good if \(a\) and \(b\) are coprime and there exists a \(c\) such that \(a^2+b^2=c^2\). Give a polynomial-time algorithm that determines whether the integers can be partitioned into \(\frac{n}{2}\) good pairs. In such a partition, every integer must belong to exactly one pair.
Solution - Mohammad Mahdi Farahani
Problem 14 — Minimum Bottleneck-Cost \(k\)-Flow
Suppose a network \(G\) is given in which every edge has a finite capacity and a cost. We want to find a flow of value \(k\) whose cost is minimum, where the cost of a flow is the maximum cost of any edge carrying nonzero flow.
Give a polynomial-time algorithm that finds this minimum cost.
Solution - Mohsen Zare
Problem 15 — Disjoint Paths
A graph \(G\) with designated vertices \(s\) and \(t\) is given. Give a polynomial-time algorithm that finds the maximum number of \(s\)-\(t\) paths such that every vertex other than \(s\) and \(t\) belongs to at most one path.
Now suppose vertices may belong to several paths, but every edge may belong to at most one path. Give a polynomial-time algorithm for this variant as well.
Solution - Kian Torokmeh
Problem 16 — Updating a Flow
Let \(G=(V,E)\) be a directed graph whose edge capacities are positive integers, with source \(s\) and sink \(t\). A maximum flow is given explicitly, including the flow on every edge. The capacity of one edge is decreased by one. Give an \(O(n+m)\) algorithm that computes a maximum flow in the new graph, where \(n\) and \(m\) are the numbers of vertices and edges. Prove correctness.
Solve the problem again when the capacity is increased by one instead of decreased by one.
What changes if edge capacities may also be positive real numbers?
Solution - Mehdi Shirin-Bayan
Problem 17 — Brothers
Mammad and Ali are twin brothers who live in the same house and attend the same school. Because they do not want people to confuse them on the streets, they decide to travel from home to school along routes that share no street, though they may pass through the same intersection. Neither wants to make his route longer, so both routes must use the minimum possible number of streets.
Their city has \(n\) intersections and \(m\) streets, and both their home and school are intersections. Give a polynomial-time algorithm that finds two routes satisfying the brothers' requirements. Such two edge-disjoint shortest routes may not exist; in that case, the algorithm must report that it is impossible.
Solution - Mohammad Amin Fakhri
Problem 18 — Chess Tournament
In a chess tournament, every pair of players plays one game. A player receives 2 points for a win, 1 for a draw, and 0 for a loss. Garry Kasparov always recorded the final standings, sorted by score, in his diary, but did not record the individual game results.
We want to determine whether these records could be genuine: does there exist a table of game results that produces the recorded scores?
Assuming there are \(n\) players, give a polynomial-time algorithm that constructs such a table or determines that none exists.
Solution - Negar yarahmadi
Problem 19 — Minimum Cut or Maximum Flow?
Find a maximum flow from \(s\) to \(t\) in the graph below.
Then give a minimum \(s\)-\(t\) cut.
Problem 20 — Projects
Mohammad has been offered \(n\) construction projects. Completing project \(i\) yields profit \(A_i\), which may be negative and therefore represent a loss.
Some projects require a specified set of other projects to be completed first. More precisely, there are \(m\) prerequisite relations. Modeling these relations as a directed graph yields a DAG.
Help Mohammad choose a set of projects that maximizes his profit while satisfying every prerequisite.
Solution - Sana Niromand
Problem 21 — Restaurant
At Bear's newly opened restaurant, the chef has prepared a list of \(n\) dishes and \(m\) ingredients. For each dish, its required ingredients are known. Placing dish \(i\) on the menu yields a total profit of \(A_i\) dollars. If ingredient \(i\) is required by any dish on the menu, buying it costs \(B_i\) dollars. An ingredient needed by several dishes is purchased only once.
Find the maximum profit the restaurant can achieve by choosing its menu optimally. Your algorithm should have the same asymptotic running time as a maximum-flow algorithm.
Solution - Narges Kari
Problem 22 — Minimum Bottleneck
A network with \(n\) vertices and two designated vertices \(s\) and \(t\) is given. Assume every edge has infinite capacity. For an \(s\)-\(t\) flow \(f\), call an edge carrying the greatest amount of flow a bottleneck edge, and call the amount of flow on that edge the bottleneck of \(f\). For a given integer \(C\), we want to send a flow of value \(C\) from \(s\) to \(t\) with minimum possible bottleneck. How many calls to the Ford–Fulkerson algorithm are sufficient to find such a flow?
Solution - Radin Baharsafat
Problem 23 — DSU with Undo
An undirected graph with \(n\) vertices is initially empty. Then \(q\) operations of the following three types are given:
add u v: add edge \((u,v)\) to the graph.undo: undo the most recentaddoperation that has not already been undone.ask u v: report whether \(u\) and \(v\) are in the same connected component.
Whenever an undo operation is issued, assume there is at least one add operation that has not yet been undone.
Give an algorithm that answers every ask query in total time \(O(n + q \log n)\).
Hints
DSU
Solution - Saba Khanmohammadi Abhari
Advanced
Problem 24 — MST in the Plane
Suppose \(n\) points are given in the plane. We want to draw line segments between \(n-1\) pairs of points so that every point can reach every other point using only the drawn segments, while minimizing the total length of the segments.
Prove that no two of the selected segments cross.
Solution - Fatima Teymarchi
Problem 25 — MST Uniqueness
Let \(G=(V,E)\) be a connected graph. For every cut \((A,B)\), place in \(E'\) every lightest edge crossing that cut; the lightest edge need not be unique. Prove that the MST of \(G\) is unique if and only if the edges of \(E'\) form a spanning tree.
Solution - Soheil Sayyah Varg
Problem 26 — Safe or Dangerous
A connected weighted graph \(G\) is given. Call an edge safe if it belongs to no cycle, and dangerous if it is a heaviest edge on some cycle. Answer the following questions.
- Show that every safe edge belongs to every MST, and that a dangerous edge which is the unique heaviest edge of some cycle belongs to no MST.
- Give and analyze an efficient implementation of anti-Kruskal. This algorithm processes the edges from heaviest to lightest. When processing edge \(e\), it removes \(e\) from \(G\) if \(e\) is currently dangerous.
Problem 27 — GPT
Mr. A wants to prepare for his Design of Algorithms exam. On a sheet of paper, he has drawn a connected graph with \(n\) vertices and \(m\) edges.
To study minimum spanning trees (MSTs), he asks ChatGPT to provide several subsets of the graph's edges. ChatGPT ultimately gives Mr. A \(k\) edge subsets, which may overlap, such that the sum of their sizes is \(s\).
For practice, Mr. A wants to determine, for every subset, whether his graph has an MST containing every edge in that subset.
Help Mr. A design an algorithm with running time
\(\mathcal{O}((s + m) \log (s + m))\) that answers this question for all subsets.
Problem 28 — All or None
A weighted graph with \(n\) vertices and \(m\) edges is given. Give an \(O(nm)\) algorithm that classifies every edge into exactly one of the following categories:
- It appears in every minimum spanning tree of the graph.
- It appears in some minimum spanning trees, but not in others.
- It appears in no minimum spanning tree.
Solution - Mohammad Bani-Ahmadi
Problem 29 — An MST for Every Edge
A connected weighted graph \(G\) is given. For every edge, we want the minimum weight of a spanning tree that contains that edge.
Give an algorithm with running time \(\mathcal{O}((n+m) log(n))\) that computes the answer for every edge.
Problem 30 — Second-Best Spanning Tree
Let \(G\) be a weighted undirected graph, and let \(T\) be the total weight of an MST of \(G\). Give an \(\mathcal{O}(n + m\log n)\) algorithm that finds a spanning tree of minimum total weight subject to its weight being strictly greater than \(T\). Report if no such spanning tree exists.
Problem 31 — Pyramid Company
There are \(n\) people in Sugarland, and \(m\) pairs of them are friends. The people are numbered from \(1\) to \(n\), and friendship is mutual.
Initially, person \(1\) is the company's only member. We want everyone to join. A new member must be invited by one of their friends who is already a member at that moment. If person \(u\) invites someone, they must be paid a commission of \(A_u\) for that invitation.
Give an \(O(n + m \log n)\) algorithm that computes the minimum total cost of recruiting everyone.
Solution - Zahra Amirbeigi
Problem 32 — Planar MST
A weighted planar graph \(G\) is given. Give a linear-time algorithm that finds an MST of \(G\).
Hints
A planar graph has at most \(3n-6\) edges.
Problem 33 — Flow with Lower Bounds
A directed network \(G=(V,E)\) is given. Every edge \(e\) has a lower bound \(\ell(e)\) and an upper bound \(u(e)\), where \(0 \leq \ell(e) \leq u(e).\)
Determine whether flows can be assigned to the edges to form a feasible circulation: for every edge \(e\), \(\ell(e) \leq f(e) \leq u(e)\), and at every vertex \(v\), total inflow equals total outflow.
Design an algorithm that decides whether such a circulation exists. Explain the main idea, prove correctness, and analyze the running time.
Solution - Milad Rostami
Problem 34 — Maximum Flow with Lower Bounds
A directed network \(G=(V,E)\) with vertices \(s\) and \(t\) is given. Every edge \(e\) has a lower bound \(\ell(e)\) and an upper bound \(u(e)\), where \(0 \leq \ell(e) \leq u(e).\)
We seek a feasible flow satisfying \(\ell(e) \leq f(e) \leq u(e)\) for every edge \(e\), and flow conservation at every vertex \(v\) other than \(s\) and \(t\).
Design a polynomial-time algorithm that finds a maximum feasible \(s\)-\(t\) flow, or reports that no feasible flow exists. Explain the main idea, prove correctness, and analyze the running time.
Problem 35 — Colored Grid
An \(n \times n\) grid has some black cells and all remaining cells white. In one operation, we may choose an \(H \times W\) subgrid and make all its cells white, at cost \(\min(H,W)\). What is the minimum cost of making the entire grid white? Give an \(\mathcal{O}(n^3)\) algorithm.
If the cost of painting a subgrid is changed to \(\max(H,W)\), an \(\mathcal{O}(n^5)\) or faster algorithm is sufficient.
Problem 36 — Closure of Minimum Cuts
Consider an arbitrary graph with vertex set \(V(G)\). For two vertices \(s\) and \(t\), an \(s\)-\(t\) cut of the graph is a partition of \(V(G)\) into two sets \(S\) and \(T\) such that \(s \in S\) and \(t \in T\). Removing all edges from \(S\) to \(T\) then separates \(s\) from \(t\). The size of the cut is the number of edges between \(S\) and \(T\). A minimum cut is a cut whose size is no greater than that of any other cut.
Suppose \((S_1, T_1)\) and \((S_2, T_2)\) are two minimum cuts of the graph. Prove that \((S_1 \cap S_2, T_1 \cup T_2)\) and \((S_1 \cup S_2, T_1 \cap T_2)\) are also minimum cuts.
Solution - Alireza Mansouri
Problem 37 — Unique Minimum Cut
A directed network \(G=(V,E)\) has source \(s\), sink \(t\), and nonnegative edge capacities.
A network may have several distinct minimum \(s\)-\(t\) cuts.
Give a necessary and sufficient condition for the minimum \(s\)-\(t\) cut to be unique. Design an algorithm that determines whether the minimum cut is unique.
Explain the main idea, prove correctness, and analyze the running time.
Hints
First find a maximum flow and consider its residual graph. Analyze the vertices reachable from \(s\), as well as the vertices from which \(t\) is reachable.
Solution - Seyed Mohammad Yasin Haji-Khalili
Problem 38 — No Negative Cycles
A directed network has capacities and costs on its edges. Let \(f\) be a feasible \(s\)-\(t\) flow of fixed value \(|f|\), and consider the residual graph with respect to \(f\).
Prove that if the residual graph contains no cycle of negative total cost, then \(f\) has minimum cost among all feasible flows of value \(|f|\).
In other words, show that the absence of a negative cycle in the residual graph is sufficient for \(f\) to be a minimum-cost \(|f|\)-flow. Also prove that this condition is necessary.
You may use the fact that the difference of two flows with equal value can be decomposed into cycles in the residual graph.
Problem 39 — Minimum-Cost Maximum Flow
A directed network \(G=(V,E)\) has \(n\) vertices, \(m\) edges, source \(s\), sink \(t\), nonnegative integer capacities \(c(e)\), and edge costs \(w(e)\). The cost of a flow is
Suppose the maximum-flow value is \(F\). Design an algorithm that finds a minimum-cost flow among all maximum \(s\)-\(t\) flows.
Your algorithm must run in \(\mathcal{O}(Fnm)\) time.
Explain how the residual graph is used at each step, how a minimum-cost augmenting path is found, why there are at most \(F\) iterations, and why the final flow is a minimum-cost maximum flow.
Problem 40 — Path Cover
A DAG with \(n\) vertices and \(m\) edges is given. We want to choose paths so that every vertex belongs to exactly one path. What is the minimum number of paths required?
Give an algorithm that uses maximum flow on an arbitrary graph at most once, plus additional computation of order \(\mathcal{O}(n + m)\) to find the answer.
Problem 41 — Strange Assignment
Call a graph with a nonnegative number assigned to each edge strange if, for every vertex subset \(S\), the sum of the numbers assigned to edges whose endpoints both lie in \(S\) is at most \(|S|-1\). Give a polynomial-time algorithm that determines whether a graph is strange.
Problem 42 — Snapp
A city consists of \(n\) squares connected by \(m\) roads. Let \(w_i\) be the time required to traverse road \(i\). Snapp expects \(k\) customers today. At time \(t_i\), customer \(i\) wants to travel from square \(a_i\) to square \(b_i\).
Customers refuse to wait, so a taxi must be present at \(a_i\) at exactly time \(t_i\). What is the minimum number of drivers needed to serve every customer?
Drivers may take any route and carry at most one customer at a time. They may also wait at squares without moving, but they may not stop or reverse direction while partway along a road.
Give a polynomial-time algorithm that finds the answer.