# Weighted-graphs-TADM2E

# Weighted Graph Algorithms

**Simulating Graph Algorithms**

<br>6-1. For the graphs in Problem (see book):

- Draw the spanning forest after every iteration of the main loop in Kruskal's algorithm.
- Draw the spanning forest after every iteration of the main loop in Prim's algorithm.
- Find the shortest path spanning tree rooted in <math>A</math>.
- Compute the maximum flow from <math>A</math> to <math>H</math>.

**Minimum Spanning Trees**

<br>6-2. Is the path between two vertices in a minimum spanning tree necessarily a shortest path between the two vertices in the full graph? Give a proof or a counterexample.

<br>6-3. Assume that all edges in the graph have distinct edge weights (i.e., no pair of edges have the same weight). Is the path between a pair of vertices in a minimum spanning tree necessarily a shortest path between the two vertices in the full graph? Give a proof or a counterexample.

<br>6-4. Can Prim's and Kruskal's algorithm yield different minimum spanning trees? Explain why or why not.

<br>6-5. Does either Prim's and Kruskal's algorithm work if there are negative edge weights? Explain why or why not.

<br>6-6.
Suppose we are *given* the minimum spanning tree <math>T</math> of a given graph <math>G</math>
(with <math>n</math> vertices and <math>m</math> edges)
and a new edge <math>e=(u,v)</math> of weight <math>w</math> that we will add to <math>G</math>.
Give an efficient algorithm to find the minimum spanning tree of the graph
<math>G + e</math>.
Your algorithm should run in <math>O(n)</math> time to receive full credit.

<br>6-7. (a) Let <math>T</math> be a minimum spanning tree of a weighted graph <math>G</math>. Construct a new graph <math>G'</math> by adding a weight of <math>k</math> to every edge of <math>G</math>. Do the edges of <math>T</math> form a minimum spanning tree of <math>G'</math>? Prove the statement or give a counterexample. <br> (b) Let <math>P=\{ s, \ldots, t \}</math> describe a shortest weighted path between vertices <math>s</math> and <math>t</math> of a weighted graph <math>G</math>. Construct a new graph <math>G'</math> by adding a weight of <math>k</math> to every edge of <math>G</math>. Does <math>P</math> describe a shortest path from <math>s</math> to <math>t</math> in <math>G'</math>? Prove the statement or give a counterexample.

<br>6-8. Devise and analyze an algorithm that takes a weighted graph <math>G</math> and finds the smallest change in the cost to a non-MST edge that would cause a change in the minimum spanning tree of <math>G</math>. Your algorithm must be correct and run in polynomial time.

<br>6-9. Consider the problem of finding a minimum weight connected subset <math>T</math> of edges from a weighted connected graph <math>G</math>. The weight of <math>T</math> is the sum of all the edge weights in <math>T</math>.

- Why is this problem not just the minimum spanning tree problem? Hint: think negative weight edges.
- Give an efficient algorithm to compute the minimum weight connected subset <math>T</math>.

<br>6-10.
Let <math>G=(V,E)</math> be an undirected graph.
A set <math>F \subseteq E</math> of edges is called a
*feedback-edge set* if every cycle of <math>G</math> has at least one edge in <math>F</math>.

- Suppose that <math>G</math> is unweighted. Design an efficient algorithm to find a minimum-size feedback-edge set.
- Suppose that <math>G</math> is a weighted undirected graph with positive edge weights. Design an efficient algorithm to find a minimum-weight feedback-edge set.

<br>6-11. Modify Prim's algorithm so that it runs in time <math>O(n \log k)</math> on a graph that has only <math>k</math> different edges costs.

**Union-Find**

<br>6-12. Devise an efficient data structure to handle the following operations on a weighted directed graph:

- Merge two given components.
- Locate which component contains a given vertex <math>v</math>.
- Retrieve a minimum edge from a given component.

<br>6-13.
Design a data structure that can perform a sequence of, <math>m</math> *union* and {\em
find} operations on a universal set of <math>n</math> elements, consisting of a
sequence of all *unions* followed by a sequence of all *finds*,
in time <math>O(m+n)</math>.

**Shortest Paths**

<br>6-14.
The *single-destination shortest path* problem for a directed graph
seeks the shortest path *from* every vertex to a specified vertex
<math>v</math>.
Give an efficient algorithm to solve the single-destination shortest paths
problem.

<br>6-15. Let <math>G=(V,E)</math> be an undirected weighted graph, and let <math>T</math> be the shortest-path spanning tree rooted at a vertex <math>v</math>. Suppose now that all the edge weights in <math>G</math> are increased by a constant number <math>k</math>. Is <math>T</math> still the shortest-path spanning tree from <math>v</math>?

<br>6-16. Answer all of the following:

- Give an example of a weighted connected graph <math>G=(V,E)</math> and a vertex <math>v</math>, such that the minimum spanning tree of <math>G</math> is the same as the shortest-path spanning tree rooted at <math>v</math>.
- Give an example of a weighted connected directed graph <math>G=(V,E)</math> and a vertex <math>v</math>, such that the minimum-cost spanning tree of <math>G</math> is very different from the shortest-path spanning tree rooted at <math>v</math>.
- Can the two trees be completely disjointed?

<br>6-17. Either prove the following or give a counterexample:

- Is the path between a pair of vertices in a minimum spanning tree of an undirected graph necessarily the shortest (minimum weight) path?
- Suppose that the minimum spanning tree of the graph is unique. Is the path between a pair of vertices in a minimum spanning tree of an undirected graph necessarily the shortest (minimum weight) path?

<br>6-18. In certain graph problems, vertices have can have weights instead of or in addition to the weights of edges. Let <math>C_v</math> be the cost of vertex <math>v</math>, and <math>C_{(x,y)}</math> the cost of the edge <math>(x,y)</math>. This problem is concerned with finding the cheapest path between vertices <math>a</math> and <math>b</math> in a graph <math>G</math>. The cost of a path is the sum of the costs of the edges and vertices encountered on the path.

- Suppose that each edge in the graph has a weight of zero (while non-edges have a cost of <math>\infty</math>). Assume that <math>C_v = 1</math> for all vertices <math>1 \leq v \leq n</math> (i.e., all vertices have the same cost). Give an
*efficient*algorithm to find the cheapest path from <math>a</math> to <math>b</math> and its time complexity. - Now suppose that the vertex costs are not constant (but are all positive) and the edge costs remain as above. Give an
*efficient*algorithm to find the cheapest path from <math>a</math> to <math>b</math> and its time complexity. - Now suppose that both the edge and vertex costs are not constant (but are all positive). Give an
*efficient*algorithm to find the cheapest path from <math>a</math> to <math>b</math> and its time complexity.

<br>6-19.
Let <math>G</math> be a weighted *directed*
graph with <math>n</math> vertices and <math>m</math> edges, where all edges have positive weight.
A directed cycle is a directed path that starts and ends at the same
vertex and contains at least one edge.
Give an <math>O(n^3)</math> algorithm to find a directed cycle in <math>G</math> of minimum total
weight.
Partial credit will be given for an <math>O(n^2 m)</math> algorithm.

<br>6-20.
Can we modify Dijkstra's algorithm to solve the
single-source *longest* path problem by changing {\em
minimum} to *maximum*?
If so, then prove your algorithm correct.
If not, then provide a counterexample.

<br>6-21. Let <math>G=(V,E)</math> be a weighted acyclic directed graph with possibly negative edge weights. Design a linear-time algorithm to solve the single-source shortest-path problem from a given source <math>v</math>.

<br>6-22. Let <math>G=(V,E)</math> be a directed weighted graph such that all the weights are positive. Let <math>v</math> and <math>w</math> be two vertices in <math>G</math> and <math>k \leq |V|</math> be an integer. Design an algorithm to find the shortest path from <math>v</math> to <math>w</math> that contains exactly <math>k</math> edges. Note that the path need not be simple.

<br>6-23.
*Arbitrage* is the use of discrepancies in currency-exchange rates to
make a profit.
For example, there may be a small window of time during
which 1 U.S. dollar buys 0.75 British pounds, 1 British pound buys
2 Australian dollars, and 1 Australian dollar buys 0.70 U.S. dollars.
At such a time, a smart trader can trade one U.S. dollar and end up with
<math>0.75 \times 2 \times 0.7 = 1.05</math> U.S. dollars---a profit of 5%.
Suppose that there are <math>n</math>
currencies <math>c_1,...,c_n</math> and an <math>n \times n</math> table <math>R</math> of exchange rates, such
that one unit of currency <math>c_i</math> buys <math>R[i,j]</math> units of currency <math>c_j</math>.
Devise and analyze an algorithm to determine the maximum value of
<math> R[c_1,c_{i1}] \cdot R[c_{i1},c_{i2}] \cdots R[c_{i{k-1}},c_{ik}] \cdot R[c_{ik},c_1] </math>
Hint: think all-pairs shortest path.

**Network Flow and Matching**

<br>6-24. A matching in a graph is a set of disjoint edges---i.e., edges that do not share any vertices in common. Give a linear-time algorithm to find a maximum matching in a tree.

<br>6-25.
An *edge cover* of an undirected graph <math>G=(V,E)</math> is a set of edges such
that each vertex in the graph is incident to at least one edge from the set.
Give an efficient algorithm, based on matching,
to find the minimum-size edge cover for <math>G</math>.