Professor. Brian Demsky Assignment #3
Due. 12/06/2012
Exercise 13.2-4
Show that any arbitrary ๐ -node binary search tree can be transformed into any other arbitrary ๐-node binary search tree using ๐ถ(๐) rotation. (Hint: First show that at most ๐ โ ๐ right rotations suffice to transform the tree into a right-going chain.)
Left rotation reduces the number of nodes sitting in the left subtree of the left child by pushing the target node and the rest part to the right, and right rotation reduces the nodes on the right through the same way in the opposite direction. Given an arbitrary ๐-node tree, we can apply right rotations at most ๐ times so that every node sits on the right side of the root. Then, with this right-going tree, we can apply left rotation at most ๐ times to make a left-going tree. Therefore, we can convert any binary tree into any other binary tree within ๐(๐) times.
Problem 15-1 Longest simple path in a directed acyclic graph
Suppose that we are given a directed acyclic graph ๐ฎ = (๐ฝ, ๐ฌ) with real-valued edge weights and two distinguished vertices ๐ and ๐. Describe a dynamic-programming approach for finding a longest weighted simple path from ๐ to ๐. What does the subproblem graph look like? What is the efficiency of your algorithm?
Given a directed acyclic graph ๐บ = (๐, ๐ธ), we need to first topologically sort this graph ๐บ, then compute the longest path to the vertices one by one in the sorted order. Of course, the start node ๐ comes the first, whose distance is initialized to be zero, and the last node is ๐ก. At every iteration, a vertex is picked to compute the longest path by comparing ๐ข. ๐๐๐ ๐ก๐๐๐๐ + ๐ค(๐ข, ๐ฃ) for every vertex ๐ข which is connected to ๐ฃ through an edge (๐ข, ๐ฃ). That is, every iteration will be responsible for a subproblem, which is to find the longest weighted simple path from ๐ upto the current node ๐ฃ. Once all the vertices have been calculated, the longest weighted simple path from ๐ to ๐ก is the ๐ก. ๐๐๐ ๐ก๐๐๐๐, or the maximum distance among all the vertices. This
algorithm topologically sorts the graph (which takes ๐(๐ + ๐ธ)), and visits every vertex while checking all the edges during the computation of the longest paths for every vertex, ๐(๐ + ๐ธ). Overall, this algorithm takes ๐(๐ + ๐ธ).
Exercise 16.2-4
Professor Gekko has always dreamed of inline skating across North Dakota. He plans to cross the state on highway U.S. 2, which runs from Grand Forks, on the eastern border with Minnesota, to Williston, near the western border with Montana. The professor can carry two liters of water, and he can skate ๐ miles before running out of water. (Because North Dakota is relatively flat, the professor does not have to worry about drinking water at a greater rate on uphill sections than on flat or downhill sections.) The professor will start in Grand Forks with two full liters of water. His official North Dakota state map shows all the places along U.S. 2 at which he can refill his water and the distances between these locations.
The professorโs goal is to minimize the number of water stops along this route across the state. Give an efficient method by which he can determine which water stops he should make. Prove that your strategy yields an optimal solution, and give its running time.
MinimumWaterStop (available_spot) 1 distance = 0;
2 for each spot in order
3 if distance + distance_to_next_spot > 2 mile 4 mark_current_spot(); 5 number_of_water_stops++; 6 distance = 0; 7 else 8 distance += distance_to_next_spot; 9 return marked_spot
By following the algorithm shown above, the professor will not make a water stop if the distance from last refill to the upcoming refill is less than 2 miles. He will stop for refill only when he cannot reach the next spot at any point. That is, it holds suboptimal property all along, with m minimal stops in total. Assuming that there is a better solution with m-1 stops, it is impossible to finish the section after the omitted spot. Therefore, the algorithm is optimal. And its running time is ๐(๐) where n is the number of available spots.
Exercise 22.2.3
Show that using a single bit to store each vertex color suffices by arguing that the BFS procedure would produce the same result if lines 5 and 14 were removed.
BGS(๐บ, ๐ )
1 for each vertex ๐ข โ ๐บ. ๐ โ *๐ + 2 ๐ข. ๐๐๐๐๐ = WHITE 3 ๐ข. ๐ = โ 4 ๐ข. ๐ = NIL 5 ๐ . ๐๐๐๐๐ = GRAY 6 ๐ . ๐ = 0 7 ๐ . ๐ = NIL 8 ๐ = โ 9 Enqueue(๐บ, ๐ ) 10 while ๐ โ โ 11 ๐ข = Dequeue(๐บ, ๐ )
12 for each vertex ๐ฃ โ ๐บ. ๐ด๐๐,๐ข- 13 if ๐ฃ. ๐๐๐๐๐ == WHITE 14 ๐ฃ. ๐๐๐๐๐ = GRAY 15 ๐ฃ. ๐ = ๐ข. ๐ + 1 16 ๐ฃ. ๐ = ๐ข 17 Enqueue(๐บ, ๐ฃ) 18 ๐ข. ๐๐๐๐๐ = BLACK
During the BFS procedure, color of each vertex is initialized as WHITE, changed to GRAY once the node is visited and enqueued, and, finally, set to BLACK when itโs dequeued and every neighbor vertex is visited. Than being said, color of a node stays GRAY during a period, which lasts from the point when it is enqueued to the point when it is dequeued, followed by the step to visit every neighboring vertex. Therefore the meaning of GRAY color is roughly equivalent to existence in queue. Line 5 and 14, which set vertex color to GRAY, should be removed, in order to use a single bit to store each vertex color.
Or simply, the entire BFS procedure does not have any line that distinguishes BLACK from GRAY. It only checks either it is WHITE or non-WHITE, which shows that it is meaningless to have two colors to represent โvisitedโ vertices.
Exercise 22.3-5
Show that edge (๐, ๐) is
a. a tree edge or forward edge if and only if ๐. ๐ < ๐. ๐ < ๐. ๐ < ๐. ๐ (โ) If an edge (๐ข, ๐ฃ) is a tree edge or forward edge, it means that ๐ข is an ancestor of ๐ฃ. Therefore, ๐ข must be discovered before ๐ฃ is visited, and ๐ข cannot be finished until ๐ฃ is finished.
(โ) If ๐ฃ is discovered and finished within the time period between discovery and termination of ๐ข, ๐ฃ is a descendant of ๐ข, because ๐ข cannot be finished until its descendants are all traveled and finished. Therefore (๐ข, ๐ฃ) is either a tree edge or a forward edge.
b. a back edge if and only if ๐. ๐ โค ๐. ๐ โค ๐. ๐ โค ๐. ๐
(โ) If an edge (๐ข, ๐ฃ) is a back edge, it means that ๐ข is a descendant of ๐ฃ. Therefore, ๐ข must be discovered after ๐ฃ is visited, and ๐ข must be finished before ๐ฃ is finished.
(โ) If ๐ข is discovered and finished within the time period between discovery and termination of ๐ฃ, ๐ฃ is an ancestor of ๐ข, because ๐ฃ cannot be finished until its descendants are all traveled and finished. Therefore (๐ข, ๐ฃ) is either a back edge.
c. a cross edge if and only if ๐. ๐ < ๐. ๐ < ๐. ๐ < ๐. ๐
(โ) If an edge (๐ข, ๐ฃ) is a cross edge, it means the periods of each nodeโs travel time must not parenthesized by each other. Therefore, ๐ฃ has to be finished before ๐ข is discovered.
(โ) If ๐ฃ is discovered and finished before ๐ข is visited, ๐ข is not a descendant of ๐ฃ, which means the edge (๐ข, ๐ฃ) is not a back edge. At the same time, ๐ข is not an ancestor of ๐ฃ because ๐ฃ is found earlier than ๐ข, which means the edge (๐ข, ๐ฃ) is not a tree edge nor a forward edge. Therefore it must be a cross edge.
Exercise 22.5-1
How can the number of strongly connected components of a graph change if a new edge is added?
If a new edge is added, there are two possibilities. The new edge may connect two nodes within a strongly connected component (SCC), or, the new edge may connect two nodes from different SCCs. In the first case, there is no change in the number of SCCs. In the second case, on the other hand, there is a possibility that the two SCCs that are connected through the new edge can be merged into one SCC, so that the number of SCCs decreases by one.
๐โฒ = { ๐ ๐ โ 1
Exercise 24.4-1
Find a feasible solution or determine that no feasible solution exists for the following system of difference constraints:
๐๐โ ๐๐โค ๐, ๐๐โ ๐๐โค โ๐, ๐๐โ ๐๐โค ๐, ๐๐โ ๐๐โค ๐, ๐๐โ ๐๐โค ๐, ๐๐โ ๐๐โค ๐๐, ๐๐โ ๐๐โค ๐, ๐๐โ ๐๐โค โ๐, ๐๐โ ๐๐โค ๐, ๐๐โ ๐๐โค โ๐.
As shown below, the constraint graph is constructed and solved by Bellman-Ford algorithm, which returns TRUE. Therefore, a feasible solution is ๐ฅ = (โ5, โ3, 0, โ1, โ6, โ8).
Problem 24-3
Arbitrage is the use of discrepancies in currency exchange rates to
transform one unit of a currency into more than one unit of the same currency. For example, suppose that 1 U.S. dollar buys 49 Indian rupees, 1 Indian rupee buys 2 Japanese yen, and 1 Japanese yen buys 0.0107 U.S. dollars. Then, by converting currencies, a trader can start with 1 U.S. dollar and buy ๐๐ ร ๐ ร ๐. ๐๐๐๐ = ๐. ๐๐๐ U.S. dollars, thus turning a profit of 4.86 percent.
Suppose that we are given ๐ currencies ๐๐, ๐๐, โฆ , ๐๐ and an ๐ ร ๐ table ๐น of exchange rates, such that one unit of currency ๐๐ buys ๐น,๐, ๐- units of currency ๐๐.
a. Give an efficient algorithm to determine whether or not there exists a sequence of currencies โฉ๐๐๐, ๐๐๐, โฆ , ๐๐๐โช such that
๐น,๐๐, ๐๐- โ ๐น,๐๐, ๐๐- โโโ ๐น,๐๐โ๐, ๐๐- โ ๐น,๐๐, ๐๐- > 1.
Analyze the running time of your algorithm. After doing some mathematical conversion of the given formula,
๐ ,๐1, ๐2- โ ๐ ,๐2, ๐3- โโโ ๐ ,๐๐โ1, ๐๐- โ ๐ ,๐๐, ๐1- > 1 1 ๐ ,๐1, ๐2-โ 1 ๐ ,๐2, ๐3-โโโ 1 ๐ ,๐๐โ1, ๐๐-โ 1 ๐ ,๐๐, ๐1-< 1 ln ( 1 ๐ ,๐1, ๐2-) + ln ( 1 ๐ ,๐2, ๐3-) + โโโ + ln ( 1 ๐ ,๐๐โ1, ๐๐-) + ln ( 1 ๐ ,๐๐, ๐1-) < 0 whose left expression may be seen as the sum of weight of edges, assuming that
๐ค(๐ข, ๐ฃ) = 1 ๐ ,๐ข,
๐ฃ-Therefore, we can use Bellman-Ford algorithm so that the part of detecting negative cycle eventually means existence of a sequence of currencies that yields profit. And, considering the construction of graph takes ๐(๐2) and the running time of Bellman-Ford algorithm is
๐(๐๐ธ) = ๐(๐ โ ๐2) = ๐(๐3), where ๐ is the number of vertices, noting that the number of edges is up to the square of vertices, the running time of the whole process is ๐(๐3).
b. Give an efficient algorithm to print out such a sequence if one exists. Analyze the running time of your algorithm.
After detecting the negative cycle from the algorithm described above, executing RELAX procedure will only update the vertices which compose the negative cycle. Therefore, such a sequence can be achieved by repeating RELAX procedure and printing the updated vertices at each time until the first updated vertex pops up again. And the running time of this printing procedure is also equivalent to Bellman-Ford algorithm, which is ๐(๐3).
Exercise 25.2-1
Run the Floyd-Warshall algorithm on the weighted, directed graph of Figure 25.2. Show the matrix ๐ซ(๐) that results for each iteration of the outer loop.
Floyd-Warshall (๐) 1 ๐ = ๐. ๐๐๐ค๐ 2 ๐ท(0)= ๐ 3 for ๐ = 1 to ๐
4 Let ๐ท(๐)= (๐๐๐(๐)) be a new ๐ ร ๐ matrix 5 for ๐ = 1 to ๐ 6 for j= 1 to ๐ 7 ๐๐๐(๐)= min (๐๐๐(๐โ1), ๐๐๐(๐โ1)+ ๐๐๐(๐โ1)) 8 return ๐ท(๐) ๐ท(0) 1 2 3 4 5 6 1 0 โ โ โ -1 โ 2 1 0 โ 2 โ โ 3 โ 2 0 โ โ -8 4 -4 โ โ 0 3 โ 5 โ 7 โ โ 0 โ 6 โ 5 10 โ โ 0 ๐ท(1) 1 2 3 4 5 6 1 0 โ โ โ -1 โ 2 1 0 โ 2 0 โ 3 โ 2 0 โ โ -8 4 -4 โ โ 0 -5 โ 5 โ 7 โ โ 0 โ 6 โ 5 10 โ โ 0
๐ท(2) 1 2 3 4 5 6 1 0 โ โ โ -1 โ 2 1 0 โ 2 0 โ 3 3 2 0 4 2 -8 4 -4 โ โ 0 -5 โ 5 8 7 โ 9 0 โ 6 6 5 10 7 5 0 ๐ท(3) 1 2 3 4 5 6 1 0 โ โ โ -1 โ 2 1 0 โ 2 0 โ 3 3 2 0 4 2 -8 4 -4 โ โ 0 -5 โ 5 8 7 โ 9 0 โ 6 6 5 10 7 5 0 ๐ท(4) 1 2 3 4 5 6 1 0 โ โ โ -1 โ 2 -2 0 โ 2 -3 โ 3 0 2 0 4 -1 -8 4 -4 โ โ 0 -5 โ 5 5 7 โ 9 0 โ 6 3 5 10 7 2 0 ๐ท(5) 1 2 3 4 5 6 1 0 6 โ 8 -1 โ 2 -2 0 โ 2 -3 โ 3 0 2 0 4 -1 -8 4 -4 2 โ 0 -5 โ 5 5 7 โ 9 0 โ 6 3 5 10 7 2 0 ๐ท(6) 1 2 3 4 5 6 1 0 6 โ 8 -1 โ 2 -2 0 โ 2 -3 โ 3 -5 -3 0 -1 -6 -8 4 -4 2 โ 0 -5 โ 5 5 7 โ 9 0 โ 6 3 5 10 7 2 0
Exercise 26.2-3
Show the execution of the Edmonds-Karp algorithm on the flow network of Figure 26.1(a).
Edmonds-Karp (๐บ, ๐ , ๐ก)
1 for each edge (๐ข, ๐ฃ) โ ๐บ. ๐ธ 2 (๐ข, ๐ฃ). ๐ = 0
3 while a path ๐ with a bread-first search from ๐ to ๐ก in the residual network ๐บ๐
4 ๐๐(๐) = min* ๐๐(๐ข, ๐ฃ) โถ (๐ข, ๐ฃ) is in ๐+ 5 for each edge (๐ข, ๐ฃ) in ๐
6 if (๐ข, ๐ฃ)๐ ๐ธ
7 (๐ข, ๐ฃ). ๐ = (๐ข, ๐ฃ). ๐ + ๐๐(๐) 8 else (๐ฃ, ๐ข). ๐ = (๐ฃ, ๐ข). ๐ โ ๐๐(๐)
Exercise 34.2-1
Consider the language GRAPH-ISOMORPHISM = *โฉ๐ฎ๐, ๐ฎ๐โช โถ ๐ฎ๐ and ๐ฎ๐ are isomorphic graphs+. Prove that GRAPH-ISOMORPHISM โ NP by describing a polynomial-time algorithm to verify the language.
In order to prove the language ISOMORPHISM is NP, we should show that GRAPH-ISOMORPHISM can be verified by a polynomial-time algorithm. If two graphs are isomorphic, it means that the each pair of vertices ๐ข and ๐ฃ of the graph ๐บ1 is adjacent if and only if the corresponding pair of vertices ๐(๐ข) and ๐(๐ฃ) of graph ๐บ2 is adjacent.
To start with, we need to match vertices of each graph, ๐บ1 and ๐บ2. If both graphs have ๐
vertices, there are ๐! possibilities of matching, ๐(๐!) = ๐(๐ lg ๐). Then, per each matching set, all the edges of ๐บ1 must be compared to the corresponding edge of ๐บ2 in order to see
adjacency of vertex pairs satisfies. That is, it takes the number of edges, which is always less than equal to ๐2, per matching set.
In total, it takes polynomial time, ๐(๐3lg ๐), to verify the language, so that the language GRAPH-ISOMORPHISM is NP.
Exercise 34.4-5
Show that the problem of determining the satisfiability of Boolean formulas in disjunctive normal form is polynomial-time solvable.
If a formula is in disjunctive normal form, it means this formula is a set of clauses OR-ed
together, in which each clause has literals AND-ed. That being said, a formula is TRUE if one or more clauses are TRUE, and a clause can be TRUE if no pair of literals,(๐ฅ, ยฌ๐ฅ), exist within the clause. Therefore, in order to determine a DNF formula is satisfiable, we need to check if there is any clause that does not have both of ๐ฅ and ยฌ๐ฅ. Therefore, when ๐ is the number of clauses and ๐ is the (maximum possible) number of literals per clause, DNF satisfiability takes ๐(๐๐).
2.
Show Primโs method for computing the MSF on the following graph. Fill in your own weights.
3.
Show Kruskalโs algorithm for computing the MSF on the following graph. Fill in your own edge weights.