hw3

September 12, 2017 | Author: Sookyung Park | Category: Vertex (Graph Theory), Combinatorics, Algorithms, Mathematical Relations, Areas Of Computer Science
Share Embed Donate


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

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF