HW 3

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

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 professors 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 its 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 nodes 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 <1 ,1 , 2 - ,2 , 3 - ,1 , - , , 1 1 1 1 1 ln ( ) + ln ( ) + + ln ( ) + ln( )<0 ,1 , 2 ,2 , 3 ,1 , , , 1 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 ).

Sookyung Park #43448934 F12 EECS215 Assignment #3 12/06/12

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 (1) (1) () (1) 7 = min( , + ) 8 return ()

(0) 1 2 3

1 0 1

2 0 2 7 5

3 0 10

4 2 0

5 -1 3 0

6 -8 0

(1) 1 2 3

1 0 1

2 0 2 7 5

3 0 10

4 2 0

5 -1 0 -5 0

6 -8 0

4 -4 5 6

4 -4 5 6

Sookyung Park #43448934 F12 EECS215 Assignment #3 12/06/12

(2) 1 2 3

1 0 1 3

2 0 2 7 5

3 0 10

4 2 4 0 9 7

5 -1 0 2 -5 0 5

6 -8 0

(3) 1 2 3

1 0 1 3

2 0 2 7 5

3 0 10

4 2 4 0 9 7

5 -1 0 2 -5 0 5

6 -8 0

4 -4 5 6 (4) 1 8 6

4 -4 5 6 (5) 1 8 6

1 0

2 0 2 7 5

3 0 10

4 2 4 0 9 7

5 -1 -3 -1 -5 0 2

6 -8 0

1 0

2 6 0 2 2 7 5

3 0 10

4 8 2 4 0 9 7

5 -1 -3 -1 -5 0 2

6 -8 0

2 -2 3 0

2 -2 3 0

4 -4 5 6 (6) 1 5 3

4 -4 5 6 5 3

1 0

2 6 0 -3 2 7 5

3 0 10

4 8 2 -1 0 9 7

5 -1 -3 -6 -5 0 2

6 -8 0

2 -2 3 -5 4 -4 5 6 5 3

Sookyung Park #43448934 F12 EECS215 Assignment #3 12/06/12

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 () = min* (, ) (, ) is in + 4 5 for each edge (, ) in 6 if (, ) (, ). = (, ). + () 7 else (, ). = (, ). () 8

(Residual Network)

(New Flow)

Sookyung Park #43448934 F12 EECS215 Assignment #3 12/06/12

(Residual Network)

(New Flow)

Sookyung Park #43448934 F12 EECS215 Assignment #3 12/06/12

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 GRAPH-ISOMORPHISM is NP, we should show that GRAPHISOMORPHISM 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, (3 lg ), 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 ().

Sookyung Park #43448934 F12 EECS215 Assignment #3 12/06/12

2.
Show Prims method for computing the MSF on the following graph. Fill in your own weights.

Sookyung Park #43448934 F12 EECS215 Assignment #3 12/06/12

3.
Show Kruskals algorithm for computing the MSF on the following graph. Fill in your own edge weights.

Sookyung Park #43448934 F12 EECS215 Assignment #3 12/06/12

You might also like