Skip to content

Assignment 4

Introductory

Problem 1 — Minimum-Product Spanning Tree

Problem statement

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.

mst

Problem 2 — Array MST

Problem statement

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.

mst

Problem 3 — A Changing Tree

Problem statement

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.

mst

Problem 4 — Recursive Spanning Tree

Problem statement

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.

mst

Problem 5 — Unique Weights, Unique Tree!

Problem statement

A weighted graph \(G\) is given, all of whose edge weights are positive and pairwise distinct. Prove that its minimum spanning tree is unique.

mst

Problem 6 — True or False!

Problem statement

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.

  1. If \(n \leq m\) and the maximum-weight edge of \(G\) is unique, then this edge appears in no minimum spanning tree.
  2. If the maximum-weight edge of \(G\) is unique and belongs to at least one cycle, then it appears in no minimum spanning tree.
  3. Suppose \(e\) is a minimum-weight edge of \(G\) (not necessarily the only one). Then \(e\) appears in every spanning tree of \(G\).
  4. If the minimum-weight edge of \(G\) is unique, then it necessarily appears in every spanning tree of \(G\).
  5. Prim's algorithm remains correct when edge weights may be negative.
mst

Problem 7 — Water

Problem statement

Suppose \(n\) containers are arranged in a row. Container \(i\) has capacity \(a[i]\). Initially, every container is empty.

Support two types of operations:

  1. 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.

  2. 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.

dsu

Problem 8 — Cycle Cover

Problem statement

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.

mst
Solution - Mohammad Mahdi Siavashi

Problem 9 — Bounded Weights

Problem statement

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\).

mst
Solution - Bahar Barghabani

Problem 10 — Borůvka

Problem statement

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.

  1. Give an \(\mathcal{O}(n + m)\) algorithm that computes \(mn_v\) for every vertex \(v\).
  2. Prove that the set of edges \(mn_v\) contains no cycle. Call this edge set \(M\).
  3. Prove that the edges of \(M\) occur in an MST; that is, at least one MST contains all of \(M\).
  4. 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.
  5. Briefly prove that the union of the edges selected in all rounds is an MST of the original graph.
  6. What is the asymptotic running time of this algorithm?
mst

Problem 11 — Sum of Distances

Problem statement

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.

mst
Solution - Amirhossein Esfandiari

Problem 12 — Maximum Bipartite Matching

Problem statement

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.

flow
Hints

Construct a flow network whose maximum-flow value equals the size of a maximum matching in the input graph.

Problem 13 — Good Pairs

Problem statement

\(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.

flow
Solution - Mohammad Mahdi Farahani

Problem 14 — Minimum Bottleneck-Cost \(k\)-Flow

Problem statement

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.

flow
Solution - Mohsen Zare
Attachments

Problem 15 — Disjoint Paths

Problem statement

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.

flow
Solution - Kian Torokmeh

Problem 16 — Updating a Flow

Problem statement

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?

flow
Solution - Mehdi Shirin-Bayan

Problem 17 — Brothers

Problem statement

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.

flow
Solution - Mohammad Amin Fakhri

Problem 18 — Chess Tournament

Problem statement

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.

flow
Solution - Negar yarahmadi

Problem 19 — Minimum Cut or Maximum Flow?

Problem statement

Find a maximum flow from \(s\) to \(t\) in the graph below.

flow graph flow graph

Then give a minimum \(s\)-\(t\) cut.

Problem 20 — Projects

Problem statement

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.

flow
Solution - Sana Niromand
Attachments

Problem 21 — Restaurant

Problem statement

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.

flow
Solution - Narges Kari

Problem 22 — Minimum Bottleneck

Problem statement

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?

flow
Solution - Radin Baharsafat

Problem 23 — DSU with Undo

Problem statement

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 recent add operation 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
Attachments

Advanced

Problem 24 — MST in the Plane

Problem statement

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.

mst
Solution - Fatima Teymarchi
Attachments

Problem 25 — MST Uniqueness

Problem statement

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.

mst
Solution - Soheil Sayyah Varg

Problem 26 — Safe or Dangerous

Problem statement

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.

  1. 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.
  2. 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.
mst

Problem 27 — GPT

Problem statement

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.

mst

Problem 28 — All or None

Problem statement

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:

  1. It appears in every minimum spanning tree of the graph.
  2. It appears in some minimum spanning trees, but not in others.
  3. It appears in no minimum spanning tree.
mst
Solution - Mohammad Bani-Ahmadi
Attachments

Problem 29 — An MST for Every Edge

Problem statement

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.

mst

Problem 30 — Second-Best Spanning Tree

Problem statement

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.

mst

Problem 31 — Pyramid Company

Problem statement

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.

mst
Solution - Zahra Amirbeigi

Problem 32 — Planar MST

Problem statement

A weighted planar graph \(G\) is given. Give a linear-time algorithm that finds an MST of \(G\).

mst
Hints

A planar graph has at most \(3n-6\) edges.

Problem 33 — Flow with Lower Bounds

Problem statement

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.

flow
Solution - Milad Rostami

Problem 34 — Maximum Flow with Lower Bounds

Problem statement

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.

flow

Problem 35 — Colored Grid

Problem statement

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.

flowdynamic programming

Problem 36 — Closure of Minimum Cuts

Problem statement

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.

flow
Solution - Alireza Mansouri
Attachments

Problem 37 — Unique Minimum Cut

Problem statement

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.

flow
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
Attachments

Problem 38 — No Negative Cycles

Problem statement

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.

flow

Problem 39 — Minimum-Cost Maximum Flow

Problem statement

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

\[ \sum_{e\in E} f(e)w(e). \]

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.

flow

Problem 40 — Path Cover

Problem statement

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.

flow

Problem 41 — Strange Assignment

Problem statement

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.

flow

Problem 42 — Snapp

Problem statement

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.

flow