Vous êtes sur la page 1sur 7

4.

6 Fibonacci Heaps
4.6.1 Structure of Fibonacci heaps Like a binomial heap, a Fibonacci heap is a collection of min-heap-ordered trees. The trees in a Fibonacci heap are not constrained to be binomial trees, however. Figure 4.3(a) shows an example of a Fibonacci heap. Unlike trees within binomial heaps, which are ordered, trees within Fibonacci heaps are rooted but unordered. As Figure 4.3(b) shows, each node x contains a pointer p [x] to its parent and a pointer child [x] to any one of its children. The children of x are linked together in a circular, doubly linked list, which we call the child list of x. Each child y in a child list has pointers left [y] and right [y] that point to ys left and right siblings, respectively. If node y is an only child, then left [y] = right [y] = y. The order in which siblings appear in a child list is arbitrary

Figure 4.3(a) A Fibonacci heap consisting of five min-heap-ordered trees and 14 nodes. The dashed line indicates the root list. The minimum node of the heap is the node containing the key 3. The three marked nodes are blackened. The potential of this particular Fibonacci heap is 5+2.3=11. (b) A more complete representation showing pointers p (up arrows), child (down arrows), and left and right (sideways arrows).

Two other fields in each node will be of use. The number of children in the child list of node x is stored in degree[x]. The Boolean-valued field mark[x] indicates whether node x has lost a child since the last time x was made the child of another node. Newly created nodes are unmarked, and a node x

becomes unmarked whenever it is made the child of another node. A given Fibonacci heap H is accessed by a pointer min [H] to the root of a tree containing a minimum key; this node is called the minimum node of the Fibonacci heap. If a Fibonacci heap H is empty, then min [H] = NIL. The roots of all the trees in a Fibonacci heap are linked together using their
left and right pointers into a circular, doubly linked list called the root list of the Fibonacci heap. The pointer min [H] thus points to the node in the root list whose key is minimum. The order of the trees within a root list is

arbitrary.
We rely on one other attribute for a Fibonacci heap H : the number of nodes

currently in H is kept in n[H].

4.5 Binomial Heaps


Unit 4

A binomial heap H of binomial trees

is set that

nt. We say that each such tree is min-heap-ordered. 2. For any nonnegative integer k, there is at most one binomial tree in

satisfies the following binomial heap propertie s. 1. Each binomial tree in node is great er than or equa l the key of its pare to

whose root has degree k.

H obeys the min-heap property:

the key of a

If f(n) = n3 / 2 and g(n) = 37n2 + 120n + 17. Show that g O( f ), And f O( g ). Answer Since for n 78, g(n) < 1 f(n), it follows that g O( f ). We could have come to the same conclusion from:
2

lim(g(n)/f(n)= lim 37n +120n+17/n 3/2 = lim(74/n + 240/n2 +34/n3) =0 We can show that f O( g ) by observing that the limit of f / g = . Here is an alternative method. We assume that f O( g ) and derive a contradiction. If f O( g ), then there exist constants c and n0 such that for all n n0,
n3/2 37 cn2+120 cn+17 c.
n/237c+120c/n+17c/n2174c
n n n

Since c is a constant and n may be arbitrarily large, it is impossible to have n/2 174c for all n n0.

Bubble sort Bubble sort, also known as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list. Because it only uses comparisons to operate on elements, it is a comparison sort. Although the algorithm is simple, it is not efficient for sorting large lists; other algorithms are better.

Bubble sort has worst-case and average complexity both (n2), where n is the number of items being sorted. There exist many sorting algorithms with substantially better worst-case or average complexity of O(n log n). Even other (n2) sorting algorithms, such as insertion sort, tend to have better performance than bubble sort. Therefore, bubble sort is not a practical sorting algorithm when n is large. The only significant advantage that bubble sort has over most other implementations, even quicksort, but not insertion sort, is that the ability to detect that the list is sorted is efficiently built into the algorithm. Performance of bubble sort over an already-sorted list (best-case) is O(n). By contrast, most other algorithms, even those with better average-case complexity, perform their entire sorting process on the set and thus are more complex. However, not only does insertion sort have this mechanism too, but it also performs better on a list that is substantially sorted (having a small number of inversions).

Step-by-step example
Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using bubble sort algorithm. In each step, elements written in bold are being compared. Three passes will be required
First Pass: (51428) (15428) (14528) ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps them. ( 1 4 5 2 8 ), Swap since 5 > 4 ( 1 4 2 5 8 ), Swap since 5 > 2

(14258) ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them. Second Pass: (14258) (14258) (14258) ( 1 2 4 5 8 ), Swap since 4 > 2 (12458) (12458) (12458) (12458) Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted. Third Pass: (12458) (12458) (12458) (12458) (12458) (12458) (12458) (12458)

The algorithm can be expressed as:


procedure bubbleSort( A : list of sortable items ) repeat swapped = false for i = 1 to length(A) - 1 inclusive do: if A[i-1] > A[i] then swap( A[i-1], A[i] ) swapped = true end if end for until not swapped end procedure

The bubble sort algorithm can be easily optimized by observing that the n-th pass finds the n-th largest element and puts it into its final place. So, the inner loop can avoid looking at the last n-1 items when running for the n-th time:
procedure bubbleSort( A : list of sortable items ) n = length(A) repeat swapped = false for i = 1 to n-1 inclusive do if A[i-1] > A[i] then swap(A[i-1], A[i]) swapped = true end if end for n = n - 1 until not swapped end procedure

More generally, it can happen that more than one element is placed in their final position on a single pass. In particular, after every pass, all elements after the last swap are sorted, and do not need to be checked again. This allows us to skip over a lot of the elements, resulting in about a worst case 50% improvement in comparison count (though no improvement in swap counts), and adds very little complexity because the new code subsumes the "swapped" variable: To accomplish this in pseudocode we write the following:
procedure bubbleSort( A : list of sortable items ) n = length(A) repeat newn = 0 for i = 1 to n-1 inclusive do if A[i-1] > A[i] then

swap(A[i-1], A[i]) newn = i end if end for n = newn until n = 0 end procedure

Alternate modifications, such as the cocktail shaker sort attempt to improve on the bubble sort performance while keeping the same idea of repeatedly comparing and swapping adjacent items.

Theorem
If n 1 , then for any n key B tree T of height h and minimum degree

T2,hlogt n+1/2 Proof If a B tree has height h, the root contains at least one key and all
other nodes contai n least at

t keys. Thus, there are at least 2 nodes at If a B tree has height h, the root contains at least one key and alother nodes contain at least t

Vous aimerez peut-être aussi