hw3
Short Description
Download hw3...
Description
Sookyung Park ID# 43448934 Fall 2012 EECS 215 Design and Analysis of Algorithms 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 realvalued 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 ๐(๐ + ๐ธ).
Sookyung Park #43448934 F12 EECS215 Assignment #3 12/06/12
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.
Sookyung Park #43448934 F12 EECS215 Assignment #3 12/06/12
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.
Sookyung Park #43448934 F12 EECS215 Assignment #3 12/06/12
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.
Sookyung Park #43448934 F12 EECS215 Assignment #3 12/06/12
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
Sookyung Park #43448934 F12 EECS215 Assignment #3 12/06/12
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).
Sookyung Park #43448934 F12 EECS215 Assignment #3 12/06/12
Sookyung Park #43448934 F12 EECS215 Assignment #3 12/06/12
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 1 1 โ โโโ โ
View more...
Comments