Introduction to Algorithms Second Edition Instructor’s Manual 2nd phần 9 pdf

43 333 0
Introduction to Algorithms Second Edition Instructor’s Manual 2nd phần 9 pdf

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Lecture Notes for Chapter 23: Minimum Spanning Trees 23-5 K RUSKAL (V, E, w) A←∅ for each vertex v ∈ V M AKE -S ET (v) sort E into nondecreasing order by weight w for each (u, v) taken from the sorted list if F IND -S ET (u) = F IND -S ET (v) then A ← A ∪ {(u, v)} U NION (u, v) return A Run through the above example to see how Kruskal’s algorithm works on it: (c, f ) : safe (g, i) : safe (e, f ) : safe (c, e) : reject (d, h) : safe ( f, h) : safe (e, d) : reject (b, d) : safe (d, g) : safe (b, c) : reject (g, h) : reject (a, b) : safe At this point, we have only one component, so all other edges will be rejected [We could add a test to the main loop of K RUSKAL to stop once |V | − edges have been added to A.] Get the shaded edges shown in the Þgure Suppose we had examined (c, e) before (e, f ) Then would have found (c, e) safe and would have rejected (e, f ) Analysis Initialize A: First for loop: Sort E: Second for loop: • O(1) |V | M AKE -S ETs O(E lg E) O(E) F IND -S ETs and U NIONs Assuming the implementation of disjoint-set data structure, already seen in Chapter 21, that uses union by rank and path compression: O((V + E) α(V )) + O(E lg E) • • • • Since G is connected, |E| ≥ |V | − ⇒ O(E α(V )) + O(E lg E) α(|V |) = O(lg V ) = O(lg E) Therefore, total time is O(E lg E) |E| ≤ |V |2 ⇒ lg |E| = O(2 lg V ) = O(lg V ) 23-6 Lecture Notes for Chapter 23: Minimum Spanning Trees • Therefore, O(E lg V ) time (If edges are already sorted, O(E α(V )), which is almost linear.) Prim’s algorithm • • • Builds one tree, so A is always a tree Starts from an arbitrary “root” r At each step, Þnd a light edge crossing cut (VA , V − V A ), where VA = vertices that A is incident on Add this edge to A VA light edge [Edges of A are shaded.] How to Þnd the light edge quickly? Use a priority queue Q: • • • • Each object is a vertex in V − VA Key of v is minimum weight of any edge (u, v), where u ∈ VA Then the vertex returned by E XTRACT-M IN is v such that there exists u ∈ VA and (u, v) is light edge crossing (VA , V − V A ) Key of v is ∞ if v is not adjacent to any vertices in VA The edges of A will form a rooted tree with root r: • • • • r is given as an input to the algorithm, but it can be any vertex Each vertex knows its parent in the tree by the attribute π [v] = parent of v π [v] = NIL if v = r or v has no parent As algorithm progresses, A = {(v, π [v]) : v ∈ V − {r} − Q} At termination, VA = V ⇒ Q = ∅, so MST is A = {(v, π [v]) : v ∈ V − {r}} Lecture Notes for Chapter 23: Minimum Spanning Trees 23-7 P RIM (V, E, w, r) Q←∅ for each u ∈ V key[u] ← ∞ π [u] ← NIL I NSERT (Q, u) £ key[r] ← D ECREASE -K EY (Q, r, 0) while Q = ∅ u ← E XTRACT-M IN (Q) for each v ∈ Adj[u] if v ∈ Q and w(u, v) < key[v] then π [v] ← u D ECREASE -K EY (Q, v, w(u, v)) Do example from previous graph [Let a student pick the root.] Analysis Depends on how the priority queue is implemented: • Suppose Q is a binary heap Initialize Q and Þrst for loop: Decrease key of r: while loop: Total: • O(V lg V ) O(lg V ) |V | E XTRACT-M IN calls ⇒ O(V lg V ) ≤ |E| D ECREASE -K EY calls ⇒ O(E lg V ) O(E lg V ) Suppose we could D ECREASE -K EY in O(1) amortized time Then ≤ |E| D ECREASE -K EY calls take O(E) time altogether ⇒ total time becomes O(V lg V + E) In fact, there is a way to D ECREASE -K EY in O(1) amortized time: Fibonacci heaps, in Chapter 20 Solutions for Chapter 23: Minimum Spanning Trees Solution to Exercise 23.1-1 Theorem 23.1 shows this Let A be the empty set and S be any set containing u but not v Solution to Exercise 23.1-4 A triangle whose edge weights are all equal is a graph in which every edge is a light edge crossing some cut But the triangle is cyclic, so it is not a minimum spanning tree Solution to Exercise 23.1-6 Suppose that for every cut of G, there is a unique light edge crossing the cut Let us consider two minimum spanning trees, T and T , of G We will show that every edge of T is also in T , which means that T and T are the same tree and hence there is a unique minimum spanning tree Consider any edge (u, v) ∈ T If we remove (u, v) from T , then T becomes disconnected, resulting in a cut (S, V − S) The edge (u, v) is a light edge crossing the cut (S, V − S) (by Exercise 23.1-3) Now consider the edge (x, y) ∈ T that crosses (S, V − S) It, too, is a light edge crossing this cut Since the light edge crossing (S, V − S) is unique, the edges (u, v) and (x, y) are the same edge Thus, (u, v) ∈ T Since we chose (u, v) arbitrarily, every edge in T is also in T Here’s a counterexample for the converse: y x z Solutions for Chapter 23: Minimum Spanning Trees 23-9 Here, the graph is its own minimum spanning tree, and so the minimum spanning tree is unique Consider the cut ({x} , {y, z}) Both of the edges (x, y) and (x, z) are light edges crossing the cut, and they are both light edges Solution to Exercise 23.1-10 Let w(T ) = (x,y)∈T w(x, y) We have w (T ) = w(T ) − k Consider any other spanning tree T , so that w(T ) ≤ w(T ) If (x, y) ∈ T , then w (T ) = w(T ) ≥ w(T ) > w (T ) If (x, y) ∈ T , then w (T ) = w(T ) − k ≥ w(T ) − k = w (T ) Either way, w (T ) ≤ w (T ), and so T is a minimum spanning tree for weight function w Solution to Exercise 23.2-4 We know that Kruskal’s algorithm takes O(V ) time for initialization, O(E lg E) time to sort the edges, and O(E α(V )) time for the disjoint-set operations, for a total running time of O(V + E lg E + E α(V )) = O(E lg E) If we knew that all of the edge weights in the graph were integers in the range from to |V |, then we could sort the edges in O(V + E) time using counting sort Since the graph is connected, V = O(E), and so the sorting time is reduced to O(E) This would yield a total running time of O(V + E + E α(V )) = O(E α(V )), again since V = O(E), and since E = O(E α(V )) The time to process the edges, not the time to sort them, is now the dominant term Knowledge about the weights won’t help speed up any other part of the algorithm, since nothing besides the sort uses the weight values If the edge weights were integers in the range from to W for some constant W , then we could again use counting sort to sort the edges more quickly This time, sorting would take O(E + W ) = O(E) time, since W is a constant As in the Þrst part, we get a total running time of O(E α(V )) Solution to Exercise 23.2-5 The time taken by Prim’s algorithm is determined by the speed of the queue operations With the queue implemented as a Fibonacci heap, it takes O(E + V lg V ) time Since the keys in the priority queue are edge weights, it might be possible to implement the queue even more efÞciently when there are restrictions on the possible edge weights We can improve the running time of Prim’s algorithm if W is a constant by implementing the queue as an array Q[0 W + 1] (using the W + slot for key= ∞), 23-10 Solutions for Chapter 23: Minimum Spanning Trees where each slot holds a doubly linked list of vertices with that weight as their key Then E XTRACT-M IN takes only O(W ) = O(1) time (just scan for the Þrst nonempty slot), and D ECREASE -K EY takes only O(1) time (just remove the vertex from the list it’s in and insert it at the front of the list indexed by the new key) This gives a total running time of O(E), which is the best possible asymptotic time (since (E) edges must be processed) However, if the range of edge weights is to |V |, then E XTRACT-M IN takes (V ) time with this data structure So the total time spent doing E XTRACT-M IN is (V ), slowing the algorithm to (E + V ) = (V ) In this case, it is better to keep the Fibonacci-heap priority queue, which gave the (E + V lg V ) time There are data structures not in the text that yield better running times: • • The van Emde Boas data structure (mentioned in the chapter notes for Chapter and the introduction to Part V) gives an upper bound of O(E + V lg lg V ) time for Prim’s algorithm A redistributive heap (used in the single-source shortest-paths algorithm of Ahuja, Mehlhorn, Orlin, and Tarjan, and mentioned in the chapter notes for Chapter 24) gives an upper bound of O E + V lg V for Prim’s algorithm Solution to Exercise 23.2-7 We start with the following lemma Lemma Let T be a minimum spanning tree of G = (V, E), and consider a graph G = (V , E ) for which G is a subgraph, i.e., V ⊆ V and E ⊆ E Let T = E − T be the edges of G that are not in T Then there is a minimum spanning tree of G that includes no edges in T Proof By Exercise 23.2-1, there is a way to order the edges of E so that Kruskal’s algorithm, when run on G, produces the minimum spanning tree T We will show that Kruskal’s algorithm, run on G , produces a minimum spanning tree T that includes no edges in T We assume that the edges in E are considered in the same relative order when Kruskal’s algorithm is run on G and on G We Þrst state and prove the following claim Claim For any pair of vertices u, v ∈ V , if these vertices are in the same set after Kruskal’s algorithm run on G considers any edge (x, y) ∈ E, then they are in the same set after Kruskal’s algorithm run on G considers (x, y) Proof of claim Let us order the edges of E by nondecreasing weight as (x1 , y1 ), (x2 , y2 ), , (xk , yk ) , where k = |E| This sequence gives the order in which the edges of E are considered by Kruskal’s algorithm, whether it is run on G or on G We will use induction, with the inductive hypothesis that if u and v are in the same set after Kruskal’s algorithm run on G considers an edge (xi , yi ), then they are in Solutions for Chapter 23: Minimum Spanning Trees 23-11 the same set after Kruskal’s algorithm run on G considers the same edge We use induction on i Basis: For the basis, i = Kruskal’s algorithm run on G has not considered any edges, and so all vertices are in different sets The inductive hypothesis holds trivially Inductive step: We assume that any vertices that are in the same set after Kruskal’s algorithm run on G has considered edges (x1 , y1 ), (x2 , y2 ), , (xi−1 , yi−1 ) are in the same set after Kruskal’s algorithm run on G has considered the same edges When Kruskal’s algorithm runs on G , after it considers (xi−1 , yi−1 ), it may consider some edges in E −E before considering (xi , yi ) The edges in E −E may cause U NION operations to occur, but sets are never divided Hence, any vertices that are in the same set after Kruskal’s algorithm run on G considers (xi−1 , yi−1 ) are still in the same set when (xi , yi ) is considered When Kruskal’s algorithm run on G considers (xi , yi ), either xi and yi are found to be in the same set or they are not • • If Kruskal’s algorithm run on G Þnds xi and yi to be in the same set, then no U NION operation occurs The sets of vertices remain the same, and so the inductive hypothesis continues to hold after considering (xi , yi ) If Kruskal’s algorithm run on G Þnds xi and yi to be in different sets, then the operation U NION (xi , yi ) will occur Kruskal’s algorithm run on G will Þnd that either xi and yi are in the same set or they are not By the inductive hypothesis, when edge (xi , yi ) is considered, all vertices in xi ’s set when Kruskal’s algorithm runs on G are in xi ’s set when Kruskal’s algorithm runs on G , and the same holds for yi Regardless of whether Kruskal’s algorithm run on G Þnds xi and yi to already be in the same set, their sets are united after considering (xi , yi ), and so the inductive hypothesis continues to hold after considering (claim) (xi , yi ) With the claim in hand, we suppose that some edge (u, v) ∈ T is placed into T That means that Kruskal’s algorithm run on G found u and v to be in the same set (since (u, v) ∈ T ) but Kruskal’s algorithm run on G found u and v to be in different sets (since (u, v) is placed into T ) This fact contradicts the claim, and we conclude that no edge in T is placed into T Thus, by running Kruskal’s algorithm on G and G , we demonstrate that there exists a minimum spanning tree of G that (lemma) includes no edges in T We use this lemma as follows Let G = (V , E ) be the graph G = (V, E) with the one new vertex and its incident edges added Suppose that we have a minimum spanning tree T for G We compute a minimum spanning tree for G by creating the graph G = (V , E ), where E consists of the edges of T and the edges in E − E (i.e., the edges added to G that made G ), and then Þnding a minimum spanning tree T for G By the lemma, there is a minimum spanning tree for G that includes no edges of E − T In other words, G has a minimum spanning tree that includes only edges in T and E − E; these edges comprise exactly the set E Thus, the the minimum spanning tree T of G is also a minimum spanning tree of G 23-12 Solutions for Chapter 23: Minimum Spanning Trees Even though the proof of the lemma uses Kruskal’s algorithm, we are not required to use this algorithm to Þnd T We can Þnd a minimum spanning tree by any means we choose Let us use Prim’s algorithm with a Fibonacci-heap priority queue Since |V | = |V | + and |E | ≤ |V | − (E contains the |V | − edges of T and at most |V | edges in E − E), it takes O(V ) time to construct G , and the run of Prim’s algorithm with a Fibonacci-heap priority queue takes time O(E + V lg V ) = O(V lg V ) Thus, if we are given a minimum spanning tree of G, we can compute a minimum spanning tree of G in O(V lg V ) time Solution to Problem 23-1 a To see that the minimum spanning tree is unique, observe that since the graph is connected and all edge weights are distinct, then there is a unique light edge crossing every cut By Exercise 23.1-6, the minimum spanning tree is unique To see that the second-best minimum spanning tree need not be unique, here is a weighted, undirected graph with a unique minimum spanning tree of weight and two second-best minimum spanning trees of weight 8: 1 minimum spanning tree second-best minimum spanning tree second-best minimum spanning tree b Since any spanning tree has exactly |V | − edges, any second-best minimum spanning tree must have at least one edge that is not in the (best) minimum spanning tree If a second-best minimum spanning tree has exactly one edge, say (x, y), that is not in the minimum spanning tree, then it has the same set of edges as the minimum spanning tree, except that (x, y) replaces some edge, say (u, v), of the minimum spanning tree In this case, T = T − {(u, v)} ∪ {(x, y)}, as we wished to show Thus, all we need to show is that by replacing two or more edges of the minimum spanning tree, we cannot obtain a second-best minimum spanning tree Let T be the minimum spanning tree of G, and suppose that there exists a second-best minimum spanning tree T that differs from T by two or more edges There are at least two edges in T − T , and let (u, v) be the edge in T − T with minimum weight If we were to add (u, v) to T , we would get a cycle c This cycle contains some edge (x, y) in T − T (since otherwise, T would contain a cycle) We claim that w(x, y) > w(u, v) We prove this claim by contradiction, so let us assume that w(x, y) < w(u, v) (Recall the assumption that edge weights are distinct, so that we not have to concern ourselves with w(x, y) = w(u, v).) If we add (x, y) to T , we get a cycle c , which contains Solutions for Chapter 23: Minimum Spanning Trees 23-13 some edge (u , v ) in T − T (since otherwise, T would contain a cycle) Therefore, the set of edges T = T − {(u , v )} ∪ {(x, y)} forms a spanning tree, and we must also have w(u , v ) < w(x, y), since otherwise T would be a spanning tree with weight less than w(T ) Thus, w(u , v ) < w(x, y) < w(u, v), which contradicts our choice of (u, v) as the edge in T −T of minimum weight Since the edges (u, v) and (x, y) would be on a common cycle c if we were to add (u, v) to T , the set of edges T − {(x, y)} ∪ {(u, v)} is a spanning tree, and its weight is less than w(T ) Moreover, it differs from T (because it differs from T by only one edge) Thus, we have formed a spanning tree whose weight is less than w(T ) but is not T Hence, T was not a second-best minimum spanning tree c We can Þll in max[u, v] for all u, v ∈ V in O(V ) time by simply doing a search from each vertex u, having restricted the edges visited to those of the spanning tree T It doesn’t matter what kind of search we do: breadth-Þrst, depth-Þrst, or any other kind We’ll give pseudocode for both breadth-Þrst and depth-Þrst approaches Each approach differs from the pseudocode given in Chapter 22 in that we don’t need to compute d or f values, and we’ll use the max table itself to record whether a vertex has been visited in a given search In particular, max[u, v] = NIL if and only if u = v or we have not yet visited vertex v in a search from vertex u Note also that since we’re visiting via edges in a spanning tree of an undirected graph, we are guaranteed that the search from each vertex u—whether breadth-Þrst or depth-Þrst—will visit all vertices There will be no need to “restart” the search as is done in the DFS procedure of Section 22.3 Our pseudocode assumes that the adjacency list of each vertex consists only of edges in the spanning tree T Here’s the breadth-Þrst search approach: BFS-F ILL -M AX (T, w) for each vertex u ∈ V for each vertex v ∈ V max[u, v] ← NIL Q←∅ E NQUEUE (Q, u) while Q = ∅ x ← D EQUEUE (Q) for each v ∈ Adj[x] if max[u, v] = NIL and v = u then if x = u or w(x, v) > max[u, x] then max[u, v] ← (x, v) else max[u, v] ← max[u, x] E NQUEUE (Q, v) return max Here’s the depth-Þrst search approach: 23-14 Solutions for Chapter 23: Minimum Spanning Trees DFS-F ILL -M AX (T, w) for each vertex u ∈ V for each vertex v ∈ V max[u, v] ← NIL DFS-F ILL -M AX -V ISIT (u, u, max) return max DFS-F ILL -M AX -V ISIT (u, x, max) for each vertex v ∈ Adj[x] if max[u, v] = NIL and v = u then if x = u or w(x, v) > max[u, x] then max[u, v] ← (x, v) else max[u, v] ← max[u, x] DFS-F ILL -M AX -V ISIT (u, v, max) For either approach, we are Þlling in |V | rows of the max table Since the number of edges in the spanning tree is |V | − 1, each row takes O(V ) time to Þll in Thus, the total time to Þll in the max table is O(V ) d In part (b), we established that we can Þnd a second-best minimum spanning tree by replacing just one edge of the minimum spanning tree T by some edge (u, v) not in T As we know, if we create spanning tree T by replacing edge (x, y) ∈ T by edge (u, v) ∈ T , then w(T ) = w(T ) − w(x, y) + w(u, v) For a given edge (u, v), the edge (x, y) ∈ T that minimizes w(T ) is the edge of maximum weight on the unique path between u and v in T If we have already computed the max table from part (c) based on T , then the identity of this edge is precisely what is stored in max[u, v] All we have to is determine an edge (u, v) ∈ T for which w(max[u, v]) − w(u, v) is minimum Thus, our algorithm to Þnd a second-best minimum spanning tree goes as follows: Compute the minimum spanning tree T Time: O(E + V lg V ), using Prim’s algorithm with a Fibonacci-heap implementation of the priority queue Since |E| < |V |2 , this running time is O(V ) Given the minimum spanning tree T , compute the max table, as in part (c) Time: O(V ) Find an edge (u, v) ∈ T that minimizes w(max[u, v]) − w(u, v) Time: O(E), which is O(V ) Having found an edge (u, v) in step 3, return T = T −{max[u, v]} ∪{(u, v)} as a second-best minimum spanning tree The total time is O(V ) Solutions for Chapter 24: Single-Source Shortest Paths 24-19 Solution to Exercise 24.5-7 Suppose we have a shortest-paths tree Gπ Relax edges in Gπ according to the order in which a BFS would visit them Then we are guaranteed that the edges along each shortest path are relaxed in order By the path-relaxation property, we would then have d[v] = δ(s, v) for all v ∈ V Since Gπ contains at most |V | − edges, we need to relax only |V | − edges to get d[v] = δ(s, v) for all v ∈ V Solution to Exercise 24.5-8 Suppose that there is a negative-weight cycle c = v0 , v1 , , vk , where v0 = vk , that is reachable from the source vertex s; thus, w(c) < Without loss of generality, c is simple There must be an acyclic path from s to some vertex of c that uses no other vertices in c Without loss of generality let this vertex of c be v0 , and let this path from s to v0 be p = u , u , , u l , where u = s and ul = v0 = vk (It may be the case that ul = s, in which case path p has no edges.) After the call to I NITIALIZE -S INGLE -S OURCE sets d[v] = ∞ for all v ∈ V − {s}, perform the following sequence of relaxations First, relax every edge in path p, in order Then relax every edge in cycle c, in order, and repeatedly relax the cycle That is, we relax the edges (u0 , u ), (u , u ), , (u l−1 , v0 ), (v0 , v1 ), (v1 , v2 ), , (vk−1 , v0 ), (v0 , v1 ), (v1 , v2 ), , (vk−1 , v0 ), (v0 , v1 ), (v1 , v2 ), , (vk−1 , v0 ), We claim that every edge relaxation in this sequence reduces a shortest-path estimate Clearly, the Þrst time we relax an edge (ui−1 , u i ) or (v j −1 , v j ), for i = 1, 2, , l and j = 1, 2, , k − (note that we have not yet relaxed the last edge of cycle c), we reduce d[ui ] or d[v j ] from ∞ to a Þnite value Now consider the relaxation of any edge (vj −1 , v j ) after this opening sequence of relaxations We use induction on the number of edge relaxations to show that this relaxation reduces d[v j ] Basis: The next edge relaxed after the opening sequence is (vk−1 , vk ) Before relaxation, d[vk ] = w( p), and after relaxation, d[vk ] = w( p) + w(c) < w( p), since w(c) < Inductive step: Consider the relaxation of edge (vj −1 , v j ) Since c is a simple cycle, the last time d[v j ] was updated was by a relaxation of this same edge By the inductive hypothesis, d[vj −1 ] has just been reduced Thus, d[v j −1 ] + w(v j −1 , v j ) < d[v j ], and so the relaxation will reduce the value of d[v j ] Solution to Problem 24-1 a Assume for contradiction that G f is not acyclic; thus G f has a cycle A cycle must have at least one edge (u, v) in which u has higher index than v This 24-20 Solutions for Chapter 24: Single-Source Shortest Paths edge is not in E f (by the deÞnition of E f ), in contradition to the assumption that G f has a cycle Thus G f is acyclic v1 , v2 , , v|V | is a topological sort for G f , because from the deÞnition of E f we know that all edges are directed from smaller indices to larger indices The proof for Eb is similar b For all vertices v ∈ V , we know that either δ(s, v) = ∞ or δ(s, v) is Þnite If δ(s, v) = ∞, then d[v] will be ∞ Thus, we need to consider only the case where d[v] is Þnite There must be some shortest path from s to v Let p = v0 , v1 , , vk−1 , vk be that path, where v0 = s and vk = v Let us now consider how many times there is a change in direction in p, that is, a situation in which (vi−1 , vi ) ∈ E f and (vi , vi+1 ) ∈ E b or vice versa There can be at most |V | − edges in p, so there can be at most |V | − changes in direction Any portion of the path where there is no change in direction is computed with the correct d values in the Þrst or second half of a single pass once the node that begins the no-change-in-direction sequence has the correct d value, because the edges are relaxed in the order of the direction of the sequence Each change in direction requires a half pass in the new direction of the path The following table shows the maximum number of passes needed depending on the parity of |V | − and the direction of the Þrst edge: |V | − even even odd odd Þrst edge direction forward backward forward backward passes (|V | − 1)/2 (|V | − 1)/2 + |V | /2 |V | /2 In any case, the maximum number of passes that we will need is |V | /2 c This scheme does not affect the asymptotic running time of the algorithm because even though we perform only |V | /2 passes instead of |V | − passes, it is still O(V ) passes Each pass still takes (E) time, so the running time remains O(V E) Solution to Problem 24-2 a Consider boxes with dimensions x = (x1 , , xd ), y = (y1 , , yd ), and z = (z , , z d ) Suppose there exists a permutation π such that xπ(i) < yi for i = 1, , d and there exists a permutation π such that yπ (i) < z i for i = 1, , d, so that x nests inside y and y nests inside z Construct a permutation π , where π (i) = π (π(i)) Then for i = 1, , d, we have xπ (i) = xπ (π(i)) < yπ (i) < z i , and so x nests inside z b Sort the dimensions of each box from longest to shortest A box X with sorted dimensions (x1 , x2 , , xd ) nests inside a box Y with sorted dimensions (y1 , y2 , , yd ) if and only if xi < yi for i = 1, 2, , d The sorting can be done in O(d lg d) time, and the test for nesting can be done in O(d) time, and so the algorithm runs in O(d lg d) time This algorithm works because a Solutions for Chapter 24: Single-Source Shortest Paths 24-21 d-dimensional box can be oriented so that every permutation of its dimensions is possible (Experiment with a 3-dimensional box if you are unsure of this) c Construct a dag G = (V, E), where each vertex vi corresponds to box Bi , and (vi , v j ) ∈ E if and only if box Bi nests inside box B j Graph G is indeed a dag, because nesting is transitive and antireßexive The time to construct the dag is O(dn + dn lg d), from comparing each of the n pairs of boxes after sorting the dimensions of each Add a supersource vertex s and a supersink vertex t to G, and add edges (s, v ) i for all vertices vi with in-degree and (v j , t) for all vertices v j with outdegree Call the resulting dag G The time to so is O(n) Find a longest path from s to t in G (Section 24.2 discusses how to Þnd a longest path in a dag.) This path corresponds to a longest sequence of nesting boxes The time to Þnd a longest path is O(n2 ), since G has n + vertices and O(n ) edges Overall, this algorithm runs in O(dn2 + dn lg d) time Solution to Problem 24-3 a We can use the Bellman-Ford algorithm on a suitable weighted, directed graph G = (V, E), which we form as follows There is one vertex in V for each currency, and for each pair of currencies ci and c j , there are directed edges (vi , v j ) and (v j , vi ) (Thus, |V | = n and |E| = n ) To determine edge weights, we start by observing that R[i , i ] · R[i , i ] · · · R[i k−1 , i k ] · R[i k , i ] > if and only if 1 1 · ··· · n If we Þnd any negative entries on the diagonal, we know that the minimum-length negative-weight cycle has more than m/2 edges and at most m edges We just need to binary search for the value of m∗ in the range m/2 < m ∗ ≤ m The key observation is that on our way to computing L(m) , we computed L (1) , L (2) , L (4) , L (8) , , L (m/2) , and these matrices sufÞce to compute every matrix we’ll need Here’s pseudocode: ... • • • (V + E) to compute G O(V E) to run B ELLMAN -F ORD (E) to compute w O(V lg V + V E) to run Dijkstra’s algorithm |V | times (using Fibonacci heap) (V ) to compute D matrix Total: O(V lg... j j T RANSITIVE -C LOSURE (E, n) for i ← to n for j ← to n if i = j or (i, j ) ∈ E[G] then ti(0) ← j else ti(0) ← j for k ← to n for i ← to n for j ← to n (k−1) (k−1) ∧ tkj ti(k) ← ti(k−1) ∨... time to create G, which has (n2 ) edges Then it takes O(n3 ) time to run B ELLMAN -F ORD Thus, the total time is O(n3 ) Another way to determine whether a negative-weight cycle exists is to create

Ngày đăng: 13/08/2014, 18:20

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan