算法导论 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)

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/nocooldown/archive/2010/07/14/1776738.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值