11
Câu hỏi ôn tập môn PTTKGT Tài liệu tham khảo : 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ú ý những recurrence nào không 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/2 h+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:

Pttkgt Final

Embed Size (px)

Citation preview

Page 1: Pttkgt Final

Câu hỏi ôn tập môn PTTKGT

Tài liệu tham khảo :

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ú ý những recurrence nào không thể sử dung Master method

Solution to Problem 4-1 : page 55, 56, 57Solution to Problem 4-4 : page 57 den 61 HeapsortExercises 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)

Page 2: Pttkgt Final

1 if heap-size[A] < 12 then error "heap underflow"3 max ← A[1]4 A[1] ← A[heap-size[A]]5 heap-size[A] ← heap-size[A] - 16 MAX-HEAPIFY(A, 1)7 return max

Hỏi : 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] ← key4 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] + 12 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 i ← p - 13 for j ← p to r - 14 do if A[j] ≤ x5 then i ← i + 16 exchange A[i] ↔ A[j]7 exchange A[i + 1] ↔ A[r]8 return i + 1

Hỏi : The running time of PARTITION on a subarray of size n is Θ(n).

Exercises 7.4-1

Page 3: Pttkgt Final

Show that in the recurrence (xem đề trong sách)

Exercises 7.4-2

Show that quicksort's best-case running time is Ω(n lg n).

Chương 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= (√ 5-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 truea. No two reds in a row on a simple path from the root to a leafb. If a node is black, then both its children are redc. 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-4After absorbing each red node into its black parent, the degree of each node blacknode 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 câu )

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

Page 4: Pttkgt Final

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)!)

Page 5: Pttkgt Final

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 row-major 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 = yj10 then c[i, j] ← c[i - 1, j - 1] + 111 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 sách)

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 sách)

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 sách)

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)) ,

Page 6: Pttkgt Final

((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ự làm tìm thứ tự tính toán và tổng số phép nhân)

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 ???

Page 7: Pttkgt Final

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 skAn 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] ← 0for l ← 1 to n do for i ← 1 to nl + 1 do j ←i + l1 e[i, j ]←∞ 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.The running time of Optimal Binary Search Tree is O(n3)

CM : Độ phức tạp của thuật toán nhân ma trận Strassen : O(nlg7)

Chương 5 : Graph AlgorithmExercises 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′.

Page 8: Pttkgt Final

MINIMUM SPANNING TREE.Độ phức tạp của Kruskal and Prim's algorithm

1. Big-O notation and algorithm analysis1. 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 these4. Big-O notation expresses (a) tight bounds; (b) upper bounds; (c) lower bounds; (d) best cases; (e) none of these5. Theta notation expresses (a) tight bounds; (b) upper bounds; (c) lower bounds; (d) worst cases; (e) none of these7. The theorem, T1(n) ∈ Θ(g1(n)) ∧ T2(n) ∈ Θ(g2(n)) ⇒T1(n) + T2(n) ∈ Θ(maxg1(n), g2(n)) says that(a) the slower and faster parts of an algorithm togetherset its running time; (b) the faster part of an algorithmdominates in determining running time; (c) the slowerpart of an algorithm dominates in determining runningtime; (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 theseBest case for an algorithm (a) takes the same time forall data; (b) assumes the data that the algorithmhandles in the greatest time; (c) assumes the data thatthe algorithm handles in the least time; (d) is theexpected time considering all possible input data;(e) none of these14. Worst case for an algorithm (a) takes the same time forall data; (b) assumes the data that the algorithmhandles in the greatest time; (c) assumes the data thatthe algorithm handles in the least time; (d) is theexpected time considering all possible input data;(e) none of these15. Average case for an algorithm (a) takes the same timefor all data; (b) assumes the data that the algorithmhandles in the greatest time; (c) assumes the data thatthe algorithm handles in the least time; (d) is theexpected time considering all possible input data;(e) none of these16. Bubble sort’s worst-case running time function isdetermined by (a) a single loop; (b) nested loops;(c) a series of loops; (d) the input data; (e) none ofthese17. A loop nested to two levels, each with roughly niterations, 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 isdetermined by (a) the number of operations in asequence structure; (b) the number of branches in aselection structure; (c) the time of the slowest of aseries of loops; (d) the data; (e) none of these

Page 9: Pttkgt Final

Recurrence relations enable us to use _____ to obtainrunning 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) dynamicprogramming; (e) probabilistic

The Binary search is what kind of algorithm? (a) divide and conquer; (b) greedy; (c) brute force; (d) dynamicprogramming; (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(n−1)) indicates____ time (a) constant; (b) logarithmic; (c) linear;(d) quadratic; (e) exponential15. What is the running time of a brute-force algorithmthat tells whether an array is sorted? (a) O(1); (b) O(lgn); (c) O(n); (d) O(n lg n); (e) O(n2)16. What is the running time of a brute-force algorithmthat 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 algorithmthat 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 algorithmthat 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 algorithmthat 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 algorithmdesign? (a) divide and conquer; (b) greedy; (c) bruteforce; (d) dynamic programming; (e) probabilistic

After each step of the BST search, the quantity ofremaining 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

Page 10: Pttkgt Final

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