Sei sulla pagina 1di 10

Cu hi n tp mn PTTKGT Ti liu tham kho : 1)Introduction to Algorithms(Instructor's Manual).pdf 2) Introduction to Algorithms, Second Edition.chm 3) Introduction_to_Algorithms__Third_Edition.

pdf Part 1 : Foundation Exercises 2.3-3, 2.3-4, 2.3-5, Problem 2-4b Exercise 3.1-2, 3.1-4, 3.2-4 Chapter 4 overview : Examples: page 47 Master method : Examples: page 52 ,53 Ch nhng recurrence no khng th s dung Master method Solution to Problem 4-1 : page 55, 56, 57 Solution to Problem 4-4 : page 57 den 61 Heapsort Exercises 6.1-1 What are the minimum and maximum numbers of elements in a heap of height h?

Exercises 6.3-3 There are at most n/2h+1 nodes of height h in any n-element heap. Exercises 6.4-4 The worst-case running time of heapsort is (n lg n). Exercises 6.4-5: When all elements are distinct, the best-case running time of heapsort is (n lg n). The procedure HEAP-EXTRACT-MAX implements the EXTRACT-MAX operation. It is similar to the for loop body (lines 3-5) of the HEAPSORT procedure.
HEAP-EXTRACT-MAX(A) 1 if heap-size[A] < 1 2 then error "heap underflow" 3 max A[1] 4 A[1] A[heap-size[A]] 5 heap-size[A] heap-size[A] - 1

6 MAX-HEAPIFY(A, 1) 7 return max

Hi : The running time of HEAP-EXTRACT-MAX is O(lg n),

The procedure HEAP-INCREASE-KEY implements the INCREASE-KEY operation.


HEAP-INCREASE-KEY(A, i, key) 1 if key < A[i] 2 then error "new key is smaller than current key" 3 A[i] key 4 while i > 1 and A[PARENT(i)] < A[i] 5 do exchange A[i] A[PARENT(i)] 6 i PARENT(i)

The running time of HEAP-INCREASE-KEY on an n-element heap is O(lg n), The procedure MAX-HEAP-INSERT implements the INSERT operation. It takes as an input the key of the new element to be inserted into max-heap A. The procedure first expands the max-heap by adding to the tree a new leaf whose key is -. Then it calls HEAP-INCREASE-KEY to set the key of this new node to its correct value and maintain the max-heap property.
MAX-HEAP-INSERT(A, key) 1 heap-size[A] heap-size[A] + 1 2 A[heap-size[A]] - 3 HEAP-INCREASE-KEY(A, heap-size[A], key)

The running time of MAX-HEAP-INSERT on an n-element heap is O(lg n). In summary, a heap can support any priority-queue operation on a set of size n in O(lg n) time. The key to the algorithm is the PARTITION procedure, which rearranges the subarray A[p r] in place. . PARTITION(A, p, r) 1 x A[r] 2 ip-1 3 for j p to r - 1 4 do if A[j] x 5 then i i + 1 6 exchange A[i] A[j] 7 exchange A[i + 1] A[r] 8 return i + 1 Hi : The running time of PARTITION on a subarray of size n is (n). Exercises 7.4-1 Show that in the recurrence (xem trong sch)

Exercises 7.4-2 Show that quicksort's best-case running time is (n lg n). Chng 3 : Advanced Data Structures
1) Consider a hash table of size m = 2p = 214 = 16384, w = 32. and a corresponding hash function h(k) = m(k A mod 1) for .A= ( -1)/2. Compute the locations to which the key 123456 is mapped p n : h(k) = 67

2) What statement about Red-black tree' property is true a. No two reds in a row on a simple path from the root to a leaf b. If a node is black, then both its children are red c. For each node, all paths from the node to descendant leaves contain the same number of red nodes. d. If a node is black, then its parent is red 3) A red-black tree with n internal nodes has height at most 2 lg(n + 1). 4) Exercise 13.1-4 Suppose that we "absorb" every red node in a red-black tree into its black parent, so that the children of the red node become children of the black parent. (Ignore what happens to the keys.) What are the possible degrees of a black node after all its red children are absorbed? What can you say about the depths of the leaves of the resulting tree?

Solution to Exercise 13.1-4


After absorbing each red node into its black parent, the degree of each node black node is 2, if both children were already black, 3, if one child was black and one was red, or 4, if both children were red.

(3 cu )

The approach to algorithm design that reuses part of the solution search by storing values in memory is (a) divide and conquer; (b) greedy; (c) brute force; (d) dynamic programming; (e) probabilistic

A chain A1, A2, A3 of three matrices. Suppose that the dimensions of the matrices are 10 100, 100 5, and 5 50, respectively. An optimal order for multiplying matrices that has the lowest cost (minimizes the number of scalar multiplications) is ((A1 A2) A3), for a total of 7500 scalar multiplications An optimal parenthesization of a matrix-chain product whose sequence of dimensions is 30, 35, 15, 5, 10, 20, 25> is ((A1 (A2 A3)) ((A4 A5)A6))., for a total of 15125 scalar multiplications

If the chain of matrices is A1, A2, A3, A4, the product A1 A2 A3 A4 can be fully parenthesized in ________ distinct ways: (5)

(A1 (A2 (A3 A4))) , (A1 ((A2 A3) A4)) , ((A1 A2) (A3 A4)) , ((A1 (A2 A3)) A4) , (((A1 A2) A3) A4).

Exercises 15.2-3

Use the substitution method to show that the solution to the recurrence (15.11) is (2n). Exercises 15.2-5 Show that a full parenthesization of an n-element expression has exactly n - 1 pairs of parentheses.

Determine an LCS of 1, 0, 0, 1, 0, 1, 0, 1 and 0, 1, 0, 1, 1, 0, 1, 1, 0. 1. Which of the following statements is not true? a) n2 + 2n + 6 n b) n2 n3) c) 2n+1

(2n)

d) n! ((n+1)!)
Procedure LCS-LENGTH takes two sequences X = x1, x2, ..., xm and Y = y1, y2, ..., yn as inputs. It stores the c[i, j] values in a table c[0 m, 0 n] whose entries are computed in rowmajor order. (That is, the first row of c is filled in from left to right, then the second row, and so on.) It also maintains the table b[1 m, 1 n] to simplify construction of an optimal solution. Intuitively, b[i, j] points to the table entry corresponding to the optimal subproblem solution chosen when computing c[i, j]. The procedure returns the b and c tables; c[m, n] contains the length of an LCS of X and Y. . LCS-LENGTH(X, Y) 1 m length[X] 2 n length[Y]

3 for i 1 to m 4 do c[i, 0] 0 5 for j 0 to n 6 do c[0, j] 0 7 for i 1 to m 8 do for j 1 to n 9 do if xi = yj 10 then c[i, j] c[i - 1, j - 1] + 1 11 b[i, j] "" 12 else if c[i - 1, j] c[i, j - 1] 13 then c[i, j] c[i - 1, j] 14 b[i, j] "" 15 else c[i, j] c[i, j - 1] 16 b[i, j] 17 return c and b . The running time of the procedure is : O(mn) (xem sch)

An LCS (Longest common subsequence) of X= A, B, C, B, D, A, B and Y = B, D, C, A, B, A .is BCBA (xem sch) An LCS (Longest common subsequence) of X= 0, 1, 1, 0, 1, 0, 0, 1 and Y = 1, 1, 0, 1, 1, 0.is <1, 1, 0, 1, 0>

The length of An LCS (Longest common subsequence) of X= A, B, C, B, D, A, B and Y = B,


D, C, A, B, A.is 4 (BCBA) (xem sch)

The number the number of scalar multiplications in optimal parenthesization of a matrix-chain product whose sequence of dimensions 10, 20, 50, 1, 100> is 2200 An optimal parenthesization of a matrix-chain product whose sequence of dimensions 10, 20, 50, 1, 100> is

(A1 (A2 (A3 A4))) , (A1 ((A2 A3) A4)) , ((A1 A2) (A3 A4)) , ((A1 (A2 A3)) A4) , (((A1 A2) A3) A4). CM : The number the number of scalar multiplications in optimal parenthesization of a matrix-chain
product whose sequence of dimensions 5, 4, 6, 2, 7> is 158 An optimal parenthesization of a matrix-chain product whose sequence of dimensions 5, 4, 6, 2, 7> is

(A1 (A2 (A3 A4))) (A1 ((A2 A3) A4)) ((A1 A2) (A3 A4)) ((A1 (A2 A3)) A4) (((A1 A2) A3) A4) CM : The number the number of scalar multiplications in optimal parenthesization of a matrix-chain
product whose sequence of dimensions 30, 1, 40, 10, 25> is 1400

CM : An optimal parenthesization of a matrix-chain product whose sequence of dimensions 30,


1, 40, 10, 25> is

(A1 (A2 (A3 A4))) (A1 ((A2 A3) A4)) ((A1 A2) (A3 A4)) ((A1 (A2 A3)) A4) (((A1 A2) A3) A4)

The number the number of scalar multiplications in optimal parenthesization of a matrix-chain product whose sequence of dimensions 10, 5, 10, 5, 10> is (T lm tm th t tnh ton v tng s php nhn)

The length of An LCS (Longest common subsequence) of X= president> and Y = <providence>


LCS of Algorithm and Alignment : ??? A.. The length of an LCS (Longest common subsequence) of X= <springtime> and Y =<printing> is An LCS (Longest common subsequence) of X= ncaatournament and Y = <northcarolina> is ??? ncarna ??? An LCS (Longest common subsequence) of X= ncaatournament and Y = <northcarolina> is ??? An LCS (Longest common subsequence) of X= <basketball> and Y = <krzyzewski> is ke hay sk An LCS (Longest common subsequence) of X= <basketball> and Y = <snoeyink> is se, sk OPTIMAL-BINARY SEARCH TREE ALGORITHM . OPTIMAL-BST(p, q, n) for i 1 to n + 1 do e[i, i 1] 0 w[i, i 1] 0 for l 1 to n do for i 1 to nl + 1 do j i + l1 e[i, j ]

. The running time of Optimal Binary Search Tree is O(n3) CM : phc tp ca thut ton nhn ma trn Strassen : O(nlg7) Chng 5 : Graph Algorithm

w[i, j ] w[i, j1] + pj for r i to j do t e[i, r1] + e[r + 1, j ] + w[i, j ] if t < e[i, j ] then e[i, j ] t root[i, j ] r return e and root

Exercises 23.1-1 Let (u, v) be a minimum-weight edge in a graph G. Show that (u, v) belongs to some minimum spanning tree of G.

Exercises 23.1-5 Let e be a maximum-weight edge on some cycle of G = (V, E). Prove that there is a minimum spanning tree of G = (V, E -{e}) that is also a minimum spanning tree of G. That is, there is a minimum spanning tree of G that does not include e. Exercises 23.1-8 Let T be a minimum spanning tree of a graph G, and let L be the sorted list of the edge weights of T. Show that for any other minimum spanning tree T of G, the list L is also the sorted list of edge weights of T.
MINIMUM SPANNING TREE . phc tp ca Kruskal and Prim's algorithm

1. Big-O notation and algorithm analysis


1. Function g is an upper bound on function f iff for all x, (a) g(x) f (x); (b) g(x) f (x); (c) g = O( f ); (d) f =(g); 2. Function g is a lower bound on function f iff for all x, (a) g(x) f (x); (b) g(x) f (x); (c) f = O(g); (d) g = (f); 3. Big-Omega notation expresses (a) tight bounds; (b) upper bounds; (c) lower bounds; (d) worst cases; (e) none of these 4. Big-O notation expresses (a) tight bounds; (b) upper bounds; (c) lower bounds; (d) best cases; (e) none of these 5. Theta notation expresses (a) tight bounds; (b) upper bounds; (c) lower bounds; (d) worst cases; (e) none of these 7. The theorem, T1(n) (g1(n)) T2(n) (g2(n))

T1(n) + T2(n) (max{g1(n), g2(n)}) says that (a) the slower and faster parts of an algorithm together set its running time; (b) the faster part of an algorithm dominates in determining running time; (c) the slower part of an algorithm dominates in determining running time; (d) Algorithm T computes functions g1 and g2; (e) Algorithm T finds the maximum of g1 and g2 11. Quadratic time is faster than (a) (1); (b) (lg n); (c) (n2); (d) (n3); (e) none of these Best case for an algorithm (a) takes the same time for all data; (b) assumes the data that the algorithm handles in the greatest time; (c) assumes the data that the algorithm handles in the least time; (d) is the expected time considering all possible input data; (e) none of these 14. Worst case for an algorithm (a) takes the same time for all data; (b) assumes the data that the algorithm handles in the greatest time; (c) assumes the data that the algorithm handles in the least time; (d) is the expected time considering all possible input data; (e) none of these 15. Average case for an algorithm (a) takes the same time for all data; (b) assumes the data that the algorithm handles in the greatest time; (c) assumes the data that the algorithm handles in the least time; (d) is the expected time considering all possible input data; (e) none of these 16. Bubble sorts worst-case running time function is determined by (a) a single loop; (b) nested loops; (c) a series of loops; (d) the input data; (e) none of these 17. A loop nested to two levels, each with roughly n iterations, has running time (a) O(1); (b) O(n); (c) O(n2); (d) O(n lg n); (e) O(2n) 19. The running time function of an algorithm is determined by (a) the number of operations in a sequence structure; (b) the number of branches in a selection structure; (c) the time of the slowest of a series of loops; (d) the data; (e) none of these

Recurrence relations enable us to use _____ to obtain running time (a) empirical tests; (b) loop nesting; (c) base-case running time; (d) depth of recursion; (e) base-case running time and depth of recursion Brute-force algorithms make use of (a) a straightforward solution to a problem by examining all possible solutions; (Brute-force) (b) the fact that an optimal solution can be constructed by adding the cheapest next component, one at a time (Greedy) ; (c) the fact that data is arranged so that at each step, half the remaining input data can disposed of; (Divede and conquer) (d) effort can be saved by saving the results of previous effort in a table; (dynamic programming); The linear search is what kind of algorithm? (a) divide and conquer; (b) greedy; (c) brute force; (d) dynamic programming; (e) probabilistic The Binary search is what kind of algorithm? (a) divide and conquer; (b) greedy; (c) brute force; (d) dynamic programming; (e) probabilistic Inserting an element in a sorted array so that it stays sorted is (a) O(1); (b) O(lg n); (c) O(n); (d) O(n lg n); (e) O(n2) Linear search is O(___) (a) 1; (b) lg n; (c) n; (d) n2; (e) 2n 14. A recursive-case running time of (1 + T(n1)) indicates ____ time (a) constant; (b) logarithmic; (c) linear; (d) quadratic; (e) exponential 15. What is the running time of a brute-force algorithm that tells whether an array is sorted? (a) O(1); (b) O(lg n); (c) O(n); (d) O(n lg n); (e) O(n2) 16. What is the running time of a brute-force algorithm that finds the minimum element of an array? (a) O(1); (b) O(lg n); (c) O(n); (d) O(n lg n); (e) O(n2) 17. What is the running time of a brute-force algorithm that number of occurrences of 1 in an array?

(a) O(1); (b) O(lg n); (c) O(n); (d) O(n lg n); (e) O(n2) 18. What is the running time of a brute-force algorithm that finds the intersection of two arrays? (a) O(1); (b) O(lg n); (c) O(n); (d) O(n lg n); (e) O(n2) 19. What is the running time of a brute-force algorithm that tells whether all element of an array are the same? (a) O(1); (b) O(lg n); (c) O(n); (d) O(n lg n); (e) O(n2)

The BST search uses which approach to algorithm design? (a) divide and conquer; (b) greedy; (c) brute force; (d) dynamic programming; (e) probabilistic After each step of the BST search, the quantity of remaining data to be searched is on average (a) 1; (b) lg n; (c) n 2; (d) n; (e) 2n The height of a BST is on average O(__) (a) 1; (b) lg n; (c) n; (d) n lg n; (e) n2 Hill climbing is a(n) ____ approach (a) divide-andconquer; (b) brute-force; (c) greedy; (d) dynamicprogramming; (e) probabilistic

The Ford-Fulkerson method finds Maximum Flow

Potrebbero piacerti anche