hw3

25
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 n-node binary search tree can be transformed into any other arbitrary n-node binary search tree using O ( n) rotation. (Hint: First show that at most n1 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 n-node tree, we can apply right rotations at most n 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 n times to make a left-going tree. Therefore, we can convert any binary tree into any other binary tree within O ( n) times. Problem 15-1 Longest simple path in a directed acyclic graph Suppose that we are given a directed acyclic graph G=( V,E) with real-valued edge weights and two distinguished vertices s and t. Describe a dynamic-programming approach for finding a longest weighted simple path from s to t. What does the subproblem graph look like? What is the efficiency of your algorithm?

Upload: sookyung-park

Post on 30-Oct-2014

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: hw3

Sookyung Park

ID# 43448934Fall 2012 EECS 215 Design and Analysis of AlgorithmsProfessor. Brian DemskyAssignment #3Due. 12/06/2012

Exercise 13.2-4

Show that any arbitrary n-node binary search tree can be transformed into any other arbitrary n-node binary search tree using O(n) rotation. (Hint: First show that at most n−1 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 n-node tree, we can apply right rotations at most n 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 n times to make a left-going tree. Therefore, we can convert any binary tree into any other binary tree within O(n) times.

Problem 15-1 Longest simple path in a directed acyclic graph

Suppose that we are given a directed acyclic graph G=(V , E) with real-valued edge weights and two distinguished vertices s and t. Describe a dynamic-programming approach for finding a longest weighted simple path from s to t. What does the subproblem graph look like? What is the efficiency of your algorithm?

Given a directed acyclic graph G=(V , E), we need to first topologically sort this graph G, then compute the longest path to the vertices one by one in the sorted order. Of course, the start node s comes the first, whose distance is initialized to be zero, and the last node is t . At every iteration, a vertex is picked to compute the longest path by comparing u .distance+w(u , v) for every vertex u which is connected to v through an edge (u , v ). That is, every iteration will be responsible for a subproblem, which is to find the longest weighted simple path from s upto the current node v. Once all the vertices have been calculated, the longest weighted simple path from s to t is the t . distance, or the maximum distance among all the vertices. This algorithm topologically sorts the graph (which takes O(V +E)), and visits every vertex while checking all the edges during the computation of the longest paths for every vertex, O(V +E). Overall, this algorithm takes O(V +E).

Page 2: hw3

Sookyung Park #43448934F12 EECS215 Assignment #3

12/06/12

Page 3: hw3

Sookyung Park #43448934F12 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 m 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 order3 if distance + distance_to_next_spot > 2 mile4 mark_current_spot();5 number_of_water_stops++;6 distance = 0;7 else8 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 O(n) where n is the number of available spots.

Page 4: hw3

Sookyung Park #43448934F12 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(G, s)1 for each vertex u∈G .V−{s}2 u .¿WHITE3 u .d=∞4 u .π=NIL5 s .¿GRAY 6 s . d=0 7 s . π=NIL 8 Q=∅ 9 Enqueue(G, s)10

while Q ≠∅

11 u=¿Dequeue(G, s)12

for each vertex v∈G. Adj[u]

13

if v .¿¿WHITE

14

v .¿GRAY

15

v .d=u .d+1

16

v . π=u

17

Enqueue(G, v)

18

u .¿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.

Page 5: hw3

Sookyung Park #43448934F12 EECS215 Assignment #3

12/06/12

Page 6: hw3

Sookyung Park #43448934F12 EECS215 Assignment #3

12/06/12

Exercise 22.3-5

Show that edge (u , v ) is

a. a tree edge or forward edge if and only if u .d<v .d<v . f <u . f

(⇒) If an edge (u , v ) is a tree edge or forward edge, it means that u is an ancestor of v. Therefore, u must be discovered before v is visited, and u cannot be finished until v is finished.(⇐) If v is discovered and finished within the time period between discovery and termination of u, v is a descendant of u, because u cannot be finished until its descendants are all traveled and finished. Therefore (u , v ) is either a tree edge or a forward edge.

b. a back edge if and only if v .d ≤u .d ≤u . f ≤ v . f

(⇒) If an edge (u , v ) is a back edge, it means that u is a descendant of v. Therefore, u must be discovered after v is visited, and u must be finished before v is finished.(⇐) If u is discovered and finished within the time period between discovery and termination of v, v is an ancestor of u, because v cannot be finished until its descendants are all traveled and finished. Therefore (u , v ) is either a back edge.

c. a cross edge if and only if v .d<v . f <u .d<u . f

(⇒) If an edge (u , v ) is a cross edge, it means the periods of each node’s travel time must not parenthesized by each other. Therefore, v has to be finished before u is discovered.(⇐) If v is discovered and finished before u is visited, u is not a descendant of v, which means the edge (u , v ) is not a back edge. At the same time, u is not an ancestor of v because v is found earlier than u, which means the edge (u , v ) is not a tree edge nor a forward edge. Therefore it must be a cross edge.

Page 7: hw3

Sookyung Park #43448934F12 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.

n'={ nn−1

Page 8: hw3

Sookyung Park #43448934F12 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:x1−x2≤ 1,x1−x4 ≤−4,x2−x3≤ 2,x2−x5≤ 7,x2−x6≤ 5,x3−x6 ≤10,x4−x2 ≤ 2,x5−x1≤−1,x5−x4 ≤ 3,x6−x3 ≤−8.

As shown below, the constraint graph is constructed and solved by Bellman-Ford algorithm, which returns TRUE. Therefore, a feasible solution is x=(−5 ,−3 ,0 ,−1 ,−6 ,−8).

Page 9: hw3

Sookyung Park #43448934F12 EECS215 Assignment #3

12/06/12

Page 10: hw3

Sookyung Park #43448934F12 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 49× 2× 0.0107=1.048 U.S. dollars, thus turning a profit of 4.86 percent.Suppose that we are given n currencies c1 , c2 ,…,cn and an n× n table R of exchange rates, such that one unit of currency c i buys R[ i , j ] units of currency c j.

a. Give an efficient algorithm to determine whether or not there exists a sequence of currencies ⟨c i1

,c i2,…,cik ⟩ such that

R [ i1 , i2 ] ∙ R [i2 , i3 ] ∙∙ ∙R [ik−1 , ik ] ∙R [ik , i1 ]>1.Analyze the running time of your algorithm.

After doing some mathematical conversion of the given formula,R [ i1 , i2 ] ∙ R [i2 , i3 ] ∙∙ ∙R [ik−1 , ik ] ∙R [ik , i1 ]>1

1R [i1 ,i2 ]

∙1

R [ i2 ,i3 ]∙ ∙∙

1R [ik−1 ,ik ]

∙1

R [ik , i1 ]<1

ln ( 1R [ i1 ,i2 ] )+ln ( 1

R [ i2 ,i3 ] )+∙ ∙∙+ ln( 1R [ik−1 , ik ] )+ ln (

1R [ ik ,i1 ]

)<0

whose left expression may be seen as the sum of weight of edges, assuming that

w (u , v )= 1R [u , v ]

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 O(n2) and the running time of Bellman-Ford algorithm is

O (VE )=O (V ∙V 2 )=O(n3), where n 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 O(n3).

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

Page 11: hw3

Sookyung Park #43448934F12 EECS215 Assignment #3

12/06/12

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 O(n3).

Page 12: hw3

Sookyung Park #43448934F12 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 D(k) that results for each iteration of the outer loop.

Floyd-Warshall (W )1 n=W .rows 2 D(0)=W 3 for k=1 to n4 Let D(k)=(d ij

(k )) be a new n× n matrix5 for i=1 to n6 for j¿1 to n7 d ij

(k)=min (d ij( k−1) ,d ik

( k−1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ung Park #43448934F12 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 (G ,s ,t)1 for each edge (u , v )∈G . E2 (u , v ) . f =0 3 while a path p with a bread-first search from s to t in the residual

network Gf

4 c f ( p )=min {c f (u , v ) : (u , v ) is∈p }5 for each edge (u , v ) in p6 if (u , v ) ϵ E7 (u , v ) . f =(u , v ) . f +c f (p) 8 else ( v ,u ) . f =(v ,u ) . f −c f ( p)

(Residual Network) (New Flow)

Page 15: hw3

Sookyung Park #43448934F12 EECS215 Assignment #3

12/06/12

(Residual Network) (New Flow)

Page 16: hw3

Sookyung Park #43448934F12 EECS215 Assignment #3

12/06/12

Exercise 34.2-1

Consider the language GRAPH-ISOMORPHISM ¿{ ⟨G1 ,G 2 ⟩ :G1and G2 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 GRAPH-ISOMORPHISM can be verified by a polynomial-time algorithm. If two graphs are isomorphic, it means that the each pair of vertices u and v of the graph G1 is adjacent if and only if the corresponding pair of vertices f (u) and f (v ) of graph G2 is adjacent.To start with, we need to match vertices of each graph, G1 and G2. If both graphs have n vertices, there are n ! possibilities of matching, O (n !)=O(n lgn). Then, per each matching set, all the edges of G1 must be compared to the corresponding edge of G2 in order to see adjacency of vertex pairs satisfies. That is, it takes the number of edges, which is always less than equal to n2, per matching set.In total, it takes polynomial time, O (n3 lgn ), 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,(x ,¬ x), 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 x and ¬ x . Therefore, when n is the number of clauses and m is the (maximum possible) number of literals per clause, DNF satisfiability takes O(nm).

Page 17: hw3

Sookyung Park #43448934F12 EECS215 Assignment #3

12/06/12

2.

Show Prim’s method for computing the MSF on the following graph. Fill in your own weights.

Page 18: hw3

Sookyung Park #43448934F12 EECS215 Assignment #3

12/06/12

3.

Show Kruskal’s algorithm for computing the MSF on the following graph. Fill in your own edge weights.

Page 19: hw3

Sookyung Park #43448934F12 EECS215 Assignment #3

12/06/12