算法导论 chapter6


 

build a max heap, use it to do heap sort and resolve problem6.1

 

there are two ways to build a max heap. 

1. for each node from 0 to size/2, do maxheapify. maxheapfy is to assume a node's left subtree and right subtree are max heaps,  then adjust the tree(left subtree+ this node + right subtree) to a max heap according to this node's value.

2. to build a new heap form empty using insertKey(value). insert value to the end of the heap. Then keep swapping with the parent if it's larger than the parent.

 

heap sort: with a max heap, extract the root(max value) by swap root with the last node. remove the last node and then do maxheapfy on root

c++ code/Files/nocooldown/6.zip

 

problem6.3

 

An m × n Young tableau is an m × n matrix such that the entries of each row are in sorted order from left to right and the entries of each column are in sorted order from top to bottom. Some of the entries of a Young tableau may be ∞, which we treat as nonexistent elements. Thus, a Young tableau can be used to hold rmn finite numbers.

  1. Draw a 4×4 Young tableau containing the elements {9, 16, 3, 2, 4, 8, 5, 14, 12}.

  2. Argue that an m × n Young tableau Y is empty if Y[1, 1] = ∞. Argue that Y is full (contains mn elements) if Y[m, n] < ∞.

  3. Give an algorithm to implement EXTRACT-MIN on a nonempty m × n Young tableau that runs in O(m + n) time. Your algorithm should use a recursive subroutine that solves an m × n problem by recursively solving either an (m - 1) × n or an m × (n - 1) subproblem. (Hint: Think about MAX-HEAPIFY.) Define T(p), where p = m + n, to be the maximum running time of EXTRACT-MIN on any m × n Young tableau. Give and solve a recurrence for T(p) that yields the O(m + n) time bound.

  4. Show how to insert a new element into a nonfull m × n Young tableau in O(m + n) time.

  5. Using no other sorting method as a subroutine, show how to use an n × n Young tableau to sort n2 numbers in O(n3) time.

  6. Give an O(m+n)-time algorithm to determine whether a given number is stored in a given m × n Young tableau

answer:

 

 apparenetly this problem is like a min heap. pass a,b

c. EXTRAC-MIN :

    min <- Y[1]

     i <- length[Y]

    exchange Y[1] <-> Y[i]

    length[Y] <- legnth[Y] - 1

    tableaufy(1)

    return max

tableaufy i:

  right <- i+1

  bottom <- i + m

  if Y[right] > Y[i] and (right/m != i/m):

        largest = right

   else

        largest = i

  if Y[bottom] > Y[largest] and (i < length[Y]):

        largest = bottom

  if largest != i

        exchange Y[i] <-> Y[largest]

        tableaufy(largest)

 d. INSERT value

     length[Y] <- length[Y] + 1

     pos <- length[Y]

     Y[pos] <- value

     while True:

        if (value < Y[left(pos)])

             exchange Y[pos] <-> Y[left(pos)]

              pos <- left(pos)

        else if (value < Y[top(pos)])

             exchange Y[pos] <-> Y[top(pos)]

              pos <- top(pos)

 

        else break

e.  use extract-min, which is O(n), length is n2 , so O(n3)

f. 

SEARCH(Y [1 ...m; 1 ...n], x)
if Y [m; 1] == x
    return (m,1)
else if x < Y [m; 1]
    return SEARCH(Y [1 ...m-1, 1...n], x)

else if x > Y[m;1]

     return SEARCH(Y [1...m, 2...n], x)

 

 

 

 

 

 

 

posted on 2010-07-14 21:01  freestyleking  阅读(362)  评论(0编辑  收藏  举报

导航