Heap & Graph Search Algorithms

Heap/Priority Queue

Properties

  1. 任意节点小于它的所有后裔,最小元素在root上
  2. heap总是complete tree. 可以将所有元素放在一个array里。 complete tree: 每一层都fully filled, 除了最后一层,如果最后一层有缺,尽量往左靠。
  3. 将根结点最大的heap叫max heap,根结点最小的heap叫min heap。
  4. index of lChild = index of parent*2+1
  5. index of rChild = index of parent*2 +2
  6. unsorted but follow rules above

Operations

  1. insert: 先放在最后,持续往上swap。时间复杂度O(log(n))
  2. update: 将新元素提升使其符合heap的性质,如果新元素比原来的元素大,和更小的child互换。时间复杂度O(log(n))
  3. get/top
  4. pop: pop之后,把最后一个元素放在上面
  5. heapify: 使得一个unsorted array变成一个heap。时间复杂度O(n)。

经典考题

Find smallest k elements from an unsorted array of size n.

Assumption: What is the relationship between k and n?

Solution 1:

sort it and return the first k elements. O(log(n).

Solution 2:

  1. Build a min-heap: heapify it -> O(n).
  2. Keep popping out k element O(klogn). 因为pop完还要重新组合。

Solution 3:

  1. use a max-heap of size k -> O(logk),先heapify前k个elements
  2. from the k+1th element to the nth element, and for the current element x
    Case1: if x < max-heap.top(), max-heap.pop(), and max-heap.insert(x);
    Case2: else, do nothing
  3. O(k) + O((n-k)log(k))

Compare Solution 2 and Solution 3

  1. Case1: k<<<<<<<n, it depends, how small is k
  2. Case2: k = n, it depends, hard to say, depending on the value of k versus n

猜你喜欢

转载自blog.csdn.net/weixin_44343532/article/details/85453132