168
A l i dD i f Al ith Analysis and Design of Algorithms Deepak John Department Of Computer Applications , SJCET-Pala

Analysis and design of algorithms part2

Embed Size (px)

DESCRIPTION

Analysis of searching and sorting. Insertion sort, Quick sort, Merge sort and Heap sort. Binomial Heaps and Fibonacci Heaps, Lower bounds for sorting by comparison of keys. Comparison of sorting algorithms. Amortized Time Analysis. Red-Black Trees – Insertion & Deletion.

Citation preview

Page 1: Analysis and design of algorithms part2

A l i d D i f Al ithAnalysis and Design of Algorithms

Deepak JohnDepartment Of Computer Applications , SJCET-Pala

Page 2: Analysis and design of algorithms part2

Analysis of searching and sorting. Insertion sort,Analysis of searching and sorting. Insertion sort,Quick sort, Merge sort and Heap sort. BinomialHeaps and Fibonacci Heaps, Lower bounds forsorting by comparison of keys. Comparison of sortingalgorithms. Amortized Time Analysis. Red-BlackT I ti d D l tiTrees – Insertion and Deletion.

Page 3: Analysis and design of algorithms part2

Approachpp

Step I:Choose the criteria (for natural numbers, criteria can beascending or descending order).

Step II:Step II:How to put data in order using the criterion selected.

Page 4: Analysis and design of algorithms part2

AnalysisAnalysis• Final ordering of data can be obtained in a variety of ways.• Some are meaningful and efficient.g• Meaningful and efficient ways depend on many aspects of an

application- type of data, randomness of data, run time constraints,i f th d t t f it i tsize of the data, nature of criteria, etc.

• To make comparisons, certain properties of sorting algorithmsshould be defined.

• Properties which are used to compare algorithms withoutdepending on the type and speed of the machines are:– number of comparisons– number of comparisons.– number of data movements.– Use of auxiliary storage.

Page 5: Analysis and design of algorithms part2

SortinggSorting is the process of arranging a group of items

into a defined order based on particular criteriainto a defined order based on particular criteriaThere are many, different types of sorting algorithms,

but the primary ones are:1. insertion sort1. insertion sort2. Quick sort3. Merge sort 4. Heap sort.

Page 6: Analysis and design of algorithms part2

Insertion sortInsertion sort

th iti t i t th t l t i f d b• the proper position to insert the current element is found bycomparing the current element with the elements in the sortedsub-array.which is an efficient algorithm for sorting a small

b f l tnumber of elements.

Page 7: Analysis and design of algorithms part2
Page 8: Analysis and design of algorithms part2
Page 9: Analysis and design of algorithms part2

Analyzing Algorithmy g g

O t l (li 1 8) tl 1 ti ( ith l th(A))Outer loop (lines 1–8) runs exactly n − 1 times (with n = length(A))

Page 10: Analysis and design of algorithms part2

Best case•The best case for insertion sort is when the input array is already•The best case for insertion sort is when the input array is alreadysorted, in which case the while loop never executes (but thecondition must be checked once).•tj=1, and line 6 and 7 will be executed 0 times.•T(n) = c1n + c2(n - 1) + c4(n - 1) + c5(n - 1) + c8(n - 1)

( 1 2 4 5 8) ( 2 4 5 8)= (c1 + c2 + c4 + c5 + c8)n - (c2 + c4 + c5 + c8)= an + b= Θ(n)= Θ(n)

Deepak John,Department Of IT,CE Poonjar

Page 11: Analysis and design of algorithms part2

worst case for insertion sort is when the input array is in reversesorted order, in which case the while loop executes the maximumnumber of times.•the inner loop is executed exactly j − 1 times for every iteration of theouter loop.outer loop.

=an2+bn-c (consider only leading terms of formula, since lower orderterms are insignificant for large n Ignore the leading terms constantterms are insignificant for large n.Ignore the leading terms constantcoefficient ,since constant factors are less significant than the rate ofgrowth in determine computational efficiency of large inputs)Θ( 2)=Θ(n2)

Page 12: Analysis and design of algorithms part2

Average case• when random data is sorted, insertion sort is usually closer to the

worst caseworst case.

• in average, tj = j/2. T(n) will still be in the order of n2, same as theworst case.

Order of GrowthThe order of a running-time function is the fastest growing term,g g g ,discarding constant factorsBest case: an + b →Θ(n)

2 2Worst case : an2 + bn - c →Θ(n2)Average case: Θ(n2)

Page 13: Analysis and design of algorithms part2

• AdvantagegThe advantage of Insertion Sort is that it is relatively simple andeasy to implement.

• DisadvantageThe disadvantage of Insertion Sort is that it is not efficient tooperate with a large list or input sizeoperate with a large list or input size.

Page 14: Analysis and design of algorithms part2

Quick-SortQ• Quick-sort is a randomized

ti l ith b d thsorting algorithm based on thedivide-and-conquer paradigm:

– Divide: pick a randomx

Divide: pick a randomelement x (called pivot)and partition S into xp

• L elements less than x• E elements equal x

x

L GE• G elements greater than x

– Recur: sort L and G

L GE

x

– Conquer: join L, E and G

Page 15: Analysis and design of algorithms part2

Choice Of PivotChoice Of PivotThree ways to choose the pivot:• Median-of-Three - from the leftmost, middle, and rightmostg

elements of the list to be sorted, select the one with mediankey as the pivot

• Pi ot is rightmost element in list that is to be sorted• Pivot is rightmost element in list that is to be sorted– When sorting A[6:20], use A[20] as the pivot

• Randomly select one of the elements to be sorted as the pivotRandomly select one of the elements to be sorted as the pivot– When sorting A[6:20], generate a random number r in the

range [6, 20]– Use A[r] as the pivot

Page 16: Analysis and design of algorithms part2

AlgorithmGiven an array of n elements (e.g.,

integers):• If array only contains one element,

return• Else• Else

– pick one element to use as pivot.– Partition elements into two sub-arrays:

• Elements less than or equal to pivot• Elements greater than pivot

– Quick sort two sub-arraysQuick sort two sub arrays– Return results

Page 17: Analysis and design of algorithms part2

The Pseudo-Code

Page 18: Analysis and design of algorithms part2

P titi iPartitioning

• The key to the algorithm is the PARTITION procedure, whichrearranges the sub-array in place.g y p

• Given a pivot, partition the elements of the array such that theresulting array consists of:1 One sub array that contains elements > pivot1. One sub-array that contains elements >= pivot2. Another sub-array that contains elements < pivot

• The sub-arrays are stored in the original data array.y g y

Page 19: Analysis and design of algorithms part2

Analysisy

The running time of quick sort depends on whether the partitioning isb l d t A th t k d if lbalanced or not. Assume that keys are random, uniformlydistributed.

Best caseBest caseRecursion:

1. Partition splits array in two sub-arrays of size n/22. Quicksort each sub-array

the depth of the recursion is log2nAt each level of the recursion, the work done in all the partitions at

that level is =O(n)O(log n) * O(n) = O(n log n)O(log2n) O(n) O(n log2n)Best case running time: O(n log2n)

Deepak John,Department Of IT,CE Poonjar

Page 20: Analysis and design of algorithms part2
Page 21: Analysis and design of algorithms part2

W tWorst case• Data is sorted already

Recursion:– Recursion:1. Partition splits array in two sub-arrays:

• one sub-array of size 0• the other sub-array of size n-1

2. Quick sort each sub-arrayRec rring on the length n 1 part req ires rec rring to depth n 1Recurring on the length n-1 part requires recurring to depth n-1

• recursion is O(n) levels deep (for an array of size n).• the partitioning work done at each level is O(n)the partitioning work done at each level is O(n).• O(n) * O(n) = O(n2)

Worst case running time: O(n2)

Page 22: Analysis and design of algorithms part2

Average-case• If the pivot element is randomly chosen we expect the split of the

input array to be reasonably well balanced on average .• Assuming random input, average-case running time is much closer to

(n lg n) than (n2)• T(n)=O(n lgn)• T(n)=O(n lgn)

Improved Pivot Selection

Pick median value of three elements from data array : data[0],y [ ],data[n/2], and data[n-1].Use this median value as pivot.

Page 23: Analysis and design of algorithms part2

Merge sortgMerge-sort on an input sequence S with n elements consists of threesteps:

Divide: partition S into two sequences S1 and S2 of about n/2elements eachRecur: recursively sort S1 and S2y 1 2Conquer: merge S1 and S2 into a unique sorted sequence

A L G O R I T H M S

divideA L G O R I T H M S

sortA G L O R H I M S T

Deepak John,Department Of IT,CE Poonjar

mergeA G H I L M O R S T

Page 24: Analysis and design of algorithms part2

AlgorithmMERGE-SORT (A, p, r)

1 IF p < r // Check for base case

Algorithm

1. IF p < r // Check for base case2. THEN q = (p + r)/2 // Divide step3. MERGE-SORT (A, p, q // Conquer step.4 MERGE SORT(A + 1 ) // C t4. MERGE-SORT(A, q + 1, r) // Conquer step.5. MERGE (A, p, q, r) // Conquer step.

Page 25: Analysis and design of algorithms part2

( )1 2 3 4 5 6 7 8

p rq

MERGE(A, p, q, r)1. Compute n1 and n22 Copy the first n1 elements into

63217542

n1 n22. Copy the first n1 elements intoL[1 . . n1 + 1] and the next n2 elements into R[1 . . n2 + 1]

3. L[n1 + 1] ← �; R[n2 + 1] ← �4 i 1 j 1 p q

1 2

4. i ← 1; j ← 15. for k ← p to r6. do if L[ i ] ≤ R[ j ]

p q

7542

rq + 1

L �

6. do if L[ i ] ≤ R[ j ]7. then A[k] ← L[ i ]8. i ←i + 1

6321R �

9. else A[k] ← R[ j ]10. j ← j + 1

Page 26: Analysis and design of algorithms part2

Analysis• For simplicity assume that n is a power of 2 so that each divide step• For simplicity, assume that n is a power of 2 so that each divide step

yields two subproblems, both of size exactly n/2.• The base case occurs when n = 1.When n > 1, time for merge sort

steps:Divide: Just compute q as the average of p and r, which takes constanttime i e Θ(1)time i.e. Θ(1).

Conquer: Recursively solve 2 sub problems, each of size n/2, which is2T(n/2).

Combine: MERGE on an n-element sub array takes Θ(n) time.• Summed together they give a function ,the recurrence for merge sort

i i irunning time isT(n) = Θ(1) if n=1

= 2T(n/2)+ Θ (n)+Θ(1) If n>1= 2T(n/2)+ Θ (n)+Θ(1). If n>1T(n)=Θ(n lg2n)

Page 27: Analysis and design of algorithms part2

Analysis of MergeSort

O(n log n) best-, average-, and worst-case complexity because themerging is always linear

Analysis of MergeSort

g g y―Extra O(n) temporary array for merging data―Extra copying to the temporary array and backUseful only for external sorting

Deepak John,Department Of IT,CE Poonjar

Page 28: Analysis and design of algorithms part2

HeapspDefinitions of heap:

1. A balanced, left-justified binary tree in which no node has a, j yvalue greater than the value in its parent.

Example min heapY>=XZ>=X

Page 29: Analysis and design of algorithms part2

Heap• The binary heap data structures is an array that can be

viewed as a complete binary tree. Each node of the binarytree corresponds to an element of the array. The array iscompletely filled on all levels except possibly lowest.

19

12 16

41 7

1619 1 412 7Array A 1619 1 412 7Array A

Page 30: Analysis and design of algorithms part2

Max Heap Example Min Heap Example

19

1

12 16

4 16

41 7

127 19

1619 1 412 7

41 7

127 191641

Array A Array A

Page 31: Analysis and design of algorithms part2

Heap Property

Heap Proceduresp

Page 32: Analysis and design of algorithms part2

• Algorithm1. Add the new element to the next available position at the

lowest level2. Restore the max-heap property if violated

• General strategy is percolate up (or bubble up): if the parent• General strategy is percolate up (or bubble up): if the parentof the element is smaller than the element, then interchangethe parent and child.

ORORRestore the min-heap property if violated• General strategy is percolate up (or bubble up): if the parentgy p p ( p) p

of the element is larger than the element, then interchangethe parent and child.

Page 33: Analysis and design of algorithms part2

19 19

12 16 12 16

41 7 41 7 17

19 Insert 17

12 17

swap

41 7 16

Percolate up to maintain the heap property

Page 34: Analysis and design of algorithms part2

• Delete max– Copy the last number to the root ( overwrite the maximum

l t t d th )element stored there ).– Restore the max heap property by percolate down.

• Delete min– Copy the last number to the root ( overwrite the minimumpy (

element stored there ).– Restore the min heap property by percolate down.

Page 35: Analysis and design of algorithms part2

Maintaining the Heap PropertyMaintaining the Heap Property

• Suppose a node is smaller than a childpp– Left and Right subtrees of i are max-

heaps• To eliminate the violation:

– Exchange with larger childMove down the tree– Move down the tree

– Continue until node is not smaller thanchildren

Page 36: Analysis and design of algorithms part2

Maintaining the Heap Property• Assumptions:

– Left and RightAlg: MAX-HEAPIFY(A, i, n)1 l ← LEFT(i)Left and Right

subtrees of i aremax-heaps

1. l ← LEFT(i)2. r ← RIGHT(i)3. if l ≤ n and A[l] > A[i]

– A[i] may besmaller than itschildren

[ ] [ ]4. then largest ←l5. else largest ←i6. if r ≤ n and A[r] > A[largest]7. then largest ←r8 if l ≠ i8. if largest ≠ i9. then exchange A[i] ↔A[largest]10 MAX HEAPIFY(A largest n)10. MAX-HEAPIFY(A, largest, n)

Page 37: Analysis and design of algorithms part2

ExampleMAX-HEAPIFY(A 2 10)MAX HEAPIFY(A, 2, 10)

A[2] → A[4]

A[2] violates the heap property A[4] violates the heap property

Page 38: Analysis and design of algorithms part2

A[4] → A[9]

Heap property restored

Page 39: Analysis and design of algorithms part2

T(n)=O(lg n )•Best Case Occurs when no swap is performed, T(n)=O(1)p p , ( ) ( )•Worst case occurs when we swap all elements

Page 40: Analysis and design of algorithms part2

BUILD-MAX-HEAPProduces a max-heap from an unordered input array

BUILD MAX HEAP

•O(n) calls( )• Each call takes O(lg n) time for max haepify ,so O(n lg n) be thetotal time.

Page 41: Analysis and design of algorithms part2

Heap sortThe heapsort algorithm consists of two phases:

- build a heap from an arbitrary array- use the heap to sort the datause the heap to sort the data

• To sort the elements in the decreasing order, use a min heap• To sort the elements in the increasing order, use a max heap

11

Page 42: Analysis and design of algorithms part2

Example Heap SortLet us look at this example: we must convert the unordered arraywith n = 10 elements into a max-heapwith n 10 elements into a max heap

we start with position 10/2 = 5

Page 43: Analysis and design of algorithms part2

We compare 3 with its child and swap them

W 17 ith it t hild d it ith thWe compare 17 with its two children and swap it with themaximum child (70)

Page 44: Analysis and design of algorithms part2

We compare 28 with its two children, 63 and 34, and swap it withthe largest child

We compare 52 with its children, swap it with the largestRec rsing no f rther s aps are needed– Recursing, no further swaps are needed

Page 45: Analysis and design of algorithms part2

Finally, we swap the root with its largest child, and recurse,i 46 i i h 81 d h i i h 70swapping 46 again with 81, and then again with 70

Page 46: Analysis and design of algorithms part2

We have now converted the unsortedarray

into a max-heap:

Page 47: Analysis and design of algorithms part2

Suppose we pop the maximum element of this heap

This leaves a gap at the back of the array:

Page 48: Analysis and design of algorithms part2

This is the last entry in the array, so why not fill it with the largestelement?element?

Repeat this process: pop the maximum element, and then insert it atthe end of the array:

Page 49: Analysis and design of algorithms part2

Repeat this process– Pop and append 70Pop and append 70

– Pop and append 63

Page 50: Analysis and design of algorithms part2

We have the 4 largest elements in order– Pop and append 52

– Pop and append 46

Page 51: Analysis and design of algorithms part2

Continuing...– Pop and append 34

– Pop and append 28

Page 52: Analysis and design of algorithms part2

Finally we can pop 17 insert it into the 2nd location and theFinally, we can pop 17, insert it into the 2nd location, and theresulting array is sorted

Page 53: Analysis and design of algorithms part2

Analysisy• The call to BuildHeap() takes O(n) time • Each of the n - 1 calls to Heapify() takes O(lg n) timep y() ( g )• Thus the total time taken by HeapSort()

= O(n) + (n - 1) O(lg n)O( ) + O( l )= O(n) + O(n lg n)

= O(n lg n)There are no best-case and worst-case scenarios for heap sortp

Page 54: Analysis and design of algorithms part2
Page 55: Analysis and design of algorithms part2

Binomial trees•Is an ordered tree defined recursively•Binomial tree properties:

Page 56: Analysis and design of algorithms part2

Examples

B00

B1

B2

B2B1

B0

Bk-1

Bk-22

Bk

Page 57: Analysis and design of algorithms part2

Binomial heaps A binomial heap is a linked list of binomial trees with the following

properties:1 The binomial trees are linked in increasing order of size1. The binomial trees are linked in increasing order of size.2. There is at most one binomial tree of each size.3. Each binomial tree has the heap structure: the value in each node is ≤

5 1h d[H]

3. Each binomial tree has the heap structure: the value in each node is ≤the values in its children.

5 1

1210

head[H]

7

2

13103

15 151210

1616

Page 58: Analysis and design of algorithms part2
Page 59: Analysis and design of algorithms part2

Binomial Heap ImplementationE h d h th f ll i fi ld• Each node has the following fields:

p: parentchild: leftmost childchild: leftmost childsiblingDegreegKey

•Roots of the trees are connected using linked list.•Each node x also contains the field degree[x] , which is the number ofchildren of x.

Page 60: Analysis and design of algorithms part2

Binomial Heap Implementation

a) c)keyp

20

NIL

h d[H]

12

NIL

)keydegreechild sibling

NILhead[H] NIL

1210b)

2120NIL NIL

head[H] 1

1210

101

15 150NIL NIL

Page 61: Analysis and design of algorithms part2

Binomial Heap OperationsBinomial Heap Operations1 Make-Heap()1. Make-Heap().2. Insert(H, x), where x is a node .3. Minimum(H).( )4. Extract-Min(H).5. Union(H1, H2): merge H1 and H2, creating a new heap.6. Decrease-Key(H, x, k): decrease x.key (x is a node in

H) to k. (It’s assumed that k � x.key.)

Page 62: Analysis and design of algorithms part2

Make-Heap():p()•Make an empty binomial heap. Creating all of the pointers can bedone in O(1) time.

Th ti i l t i t d t it t NILThe operation simply creates a new pointer and sets it to NIL.

Binomial-Heap-Create()1 head[H] <- NIL2 return head[H]

Page 63: Analysis and design of algorithms part2

Minimum(H):•To do this we find the smallest key among those stored at the rootsTo do this we find the smallest key among those stored at the rootsconnected to the head of H.•The minimum must be in some root in the top list.•If there are n nodes in the heap there are at most lg n roots at the top at•If there are n nodes in the heap there are at most lg n roots at the top, atmost one each of degree 0, 1, 2, . . . , lg n , so this can be found in O(lgn) time. Binomial-Heap-Minimum(H)

1 y <- NIL2 x <- head[H]3 min <- ∞4 while x is not NIL5 do if key[x] < min then6 min < key[x]6 min <- key[x]7 y <- x8 x <- sibling[x]9 return y

Page 64: Analysis and design of algorithms part2

Find Minimum Key Example

5 1head[ 2 5 1head[ 2

a) b)

1210

15

H]7 1210

15

H]7

15 15

5 1head[ 2 5 1head[ 2

c) d)

1210

head[H]

7 1210

head[H]

7

15 15

Deepak John,Department Of IT,CE Poonjar

Page 65: Analysis and design of algorithms part2

Binomial-Link(y,z)Link binomial trees with the same degree. Note that z, the secondargument to BL(), becomes the parent, and y becomes the child.

Link(y,z)p[y] := z;ibli [ ] hild[ ]

Link(y,z)p[y] := z;ibli [ ] hild[ ]sibling[y] := child[z];

child[z] := y;degree[z] := degree[z] + 1

sibling[y] := child[z];child[z] := y;degree[z] := degree[z] + 1g [ ] g [ ]g [ ] g [ ]

y yz

z

Bk-1

Bk-1Bk-1 Bk-1

yLink

Deepak John,Department Of IT,CE Poonjar

Page 66: Analysis and design of algorithms part2

Union(H1,H2)•is the most sophisticated of the binomial heap operationsis the most sophisticated of the binomial heap operations.•It’s used in many other operations.The running time will be O(log n).g ( g )

UnionH1, H2 H1 H2

H1 = H2 =

Union traverses the new root list like this:prev-x x next-x

Union traverses the new root list like this:

Deepak John,Department Of IT,CE Poonjar

Page 67: Analysis and design of algorithms part2

Starting with the following two binomial heaps:1880602

58 19

18

93

8060

32 63

2

69

M t li t b t 2 188060

53

Merge root lists, butnow we have twotrees of same degree

53

32 63

2

69

58 19

18

93

8060

53 69

Combine trees of same28060

Combine trees of samedegree using binomiallink, make smaller keythe root of the

53

32 63

58 19

1893

the root of thecombined tree 69

Page 68: Analysis and design of algorithms part2

Casesprev-x x next-x sibling[next-x] prev-x x next-x

a b c dp g[ ]

Bk Bl

Case 1 a b c d

p x next-x

Bk Bl

Case 1:occurs when degree[x] ≠ degree[next-x], that is, when x is theroot of a Bk-tree and next-x is the root of a Bl-tree for some l > k.k l

prev-x x next-x sibling[next-x] prev-x x next-x

a b c d

p g[ ]

BB

Case 2 a b c dprev x x

BBBBkBkBk BkBkBk

Case 2: occurs when x is the first of three roots of equal degree, that is when

Deepak John,Department Of IT,CE Poonjar

is, whendegree[x] = degree[next-x] = degree[sibling[next-x]].

Page 69: Analysis and design of algorithms part2

a b c dprev-x x next-x sibling[next-x]

Case 3 a b d

prev-x x next-x

BkBk Blkey[x] key[next[x]]

cBk

Bk

Bl

prev-x x next-x sibling[next-x] C 4

Bk+1

prev-x x next-xa b c d

prev x x next x sibling[next x]

Bk BkBl

Case 4 a

bc d

Bk Bl

prev x x next x

kkey[x] > key[next[x]]

k

Bk

Bk+1

Case 3 and 4: occur when x is the first of two roots of equal degree, that is, when

d [ ] d [ t ] ≠d [ ibli [ t ]]degree[x] = degree[next-x] ≠degree[sibling[next-x]].

Page 70: Analysis and design of algorithms part2

Union(H1, H2)Union(H1, H2)( 1, 2)H := new heap;head[H] := merge(H1, H2); /* simple merge of root lists */if head[H] = NIL then return H fi;

( 1, 2)H := new heap;head[H] := merge(H1, H2); /* simple merge of root lists */if head[H] = NIL then return H fi;if head[H] NIL then return H fi;prev-x := NIL;x := head[H];next-x := sibling[x];

if head[H] NIL then return H fi;prev-x := NIL;x := head[H];next-x := sibling[x];next x : sibling[x];while next-x NIL do

if (degree[x] degree[next-x]) or(sibling[next-x] NIL and degree[sibling[next-x]] = degree[x]) then

next x : sibling[x];while next-x NIL do

if (degree[x] degree[next-x]) or(sibling[next-x] NIL and degree[sibling[next-x]] = degree[x]) then(sibling[next-x] NIL and degree[sibling[next-x]] = degree[x]) then

prev-x := x;x := next-x;

else

(sibling[next-x] NIL and degree[sibling[next-x]] = degree[x]) thenprev-x := x;x := next-x;

else

Cases 1,2

elseelse

Deepak John,Department Of IT,CE Poonjar

Page 71: Analysis and design of algorithms part2

if key[x] key[next-x] thenif key[x] key[next-x] thensibling[x] := sibling[next-x];

Link(next-x, x)else

if NIL th h d[H] l ibli [ ] fi

sibling[x] := sibling[next-x];Link(next-x, x)

elseif NIL th h d[H] l ibli [ ] fi

Case 3

if prev-x = NIL then head[H] := next-x else sibling[prev-x] := next-x fiLink(x, next-x);x := next-x

fi

if prev-x = NIL then head[H] := next-x else sibling[prev-x] := next-x fiLink(x, next-x);x := next-x

fi

Case 4

fifi;next-x := sibling[x]

od;

fifi;next-x := sibling[x]

od;od;return Hod;return H

Deepak John,Department Of IT,CE Poonjar

Page 72: Analysis and design of algorithms part2

Union Example12

3328

15

25

7head[H1] 18

37

3

441029

6

8

head[H2]

41

50

31 1748

32 2445

222330

55

Merge

18

37

3

4410

612

3328

15

25

7head[H]x next-x

37

50

31 1748

441029

32 24

222330

833

41

2825

50

55

32 2445

Page 73: Analysis and design of algorithms part2

t18

37

3

441029

6

8

12

3328

15

25

7head[H]x next-x

50

31 1748

32 2445

22233041

55Case 3

37

3

441029

6

83328

15

25

7head[H]

18

12x next-x

50

31 1748

1029

32 2445

222330

841

55

3245

Page 74: Analysis and design of algorithms part2

t

37

3

441029

6

83328

15

25

7head[H]

18

12x next-x

50

31 1748

32 2445

22233041

Case 2 55

37

3

441029

6

83328

15

25

7head[H]

18

12prev-x x next-x

50

31 1748

32 2445

22233041

55

Page 75: Analysis and design of algorithms part2

37

3

441029

6

83328

15

25

7head[H]

18

12prev-x x next-x

50

31 1748

1029

32 2445

222330

841

Case 455

3245

37

3

441029

6

3328

15

7

head[H]

18

12prev-x x next-x

50

31 1748

441029

32 2445

222330

84125

7

55

32 2445

Page 76: Analysis and design of algorithms part2

prev x next x

37

3

441029

6

83328

15

7

head[H]

18

12prev-x x next-x

50

31 1748

32 2445

2223304125

Case 3 55

37

3

441029

6

815 7

head[H]

18

12prev-x x next-x

50

31 1748

32 2445

222330

8

33

41

28 25

55

Page 77: Analysis and design of algorithms part2

37

3

441029

6

815 7

head[H]

18

12prev-x x next-x

50

31 1748

32 2445

22233033

41

28 25

55Case 1

37

3

441029

6

815 7

head[H]

18

12prev-x x next-x = NIL

terminates

50

31 1748

29

32 2445

222330

8

33

41

28

5

25

Note: Union isO(l )

55

345 O(lg n).

Page 78: Analysis and design of algorithms part2

insertInsert(H, x)

H’ := Make-B-H();Insert(H, x)

H’ := Make-B-H();();p[x] := NIL;child[x] := NIL;sibling[x] := NIL;

();p[x] := NIL;child[x] := NIL;sibling[x] := NIL;sibling[x] := NIL;degree[x] := 0;head(H’) := x;

i ( ’)

sibling[x] := NIL;degree[x] := 0;head(H’) := x;

i ( ’)H := Union(H, H’)H := Union(H, H’)

Deepak John,Department Of IT,CE Poonjar

Page 79: Analysis and design of algorithms part2

Extract Node With Minimum KeyThis operation is started by finding and removing the node x withThis operation is started by finding and removing the node x withminimum key from the binomial heap H. Create a new binomial heapH’ and set to the list of x’s children in the reverse order. Unite H and H’to get the resulting binomial heap.to get the resulting binomial heap.Pseudocode

Binomial-Heap-Extract-Min(H)1 find the root x with the minimum key in the root list of H,

and remove x from the root list of H.2 H’ <- Make-Binomial-Heap()2 H <- Make-Binomial-Heap()3 reverse the order of the linked list of x’s children,and set

head[H’] to point to the head of the resulting list.4 H <- Binomial-Heap-Union(H,H’)5 Return x

Run time: O(log n)Run time: O(log n)

Deepak John,Department Of IT,CE Poonjar

Page 80: Analysis and design of algorithms part2

Extract Minimum Key Exampley p

5 1head[H] 2

12107

2

12103

15 151210

15

5 1head[H] 2

1210

15

7 1210

15

3

1210

Deepak John,Department Of IT,CE Poonjar15

Page 81: Analysis and design of algorithms part2

5 12 10head[H] 2 head[H’]

157 1210

15

2

151210

15

5

7

2

12102

head[H]

10

12

15

2

121015

Deepak John,Department Of IT,CE Poonjar

15

Page 82: Analysis and design of algorithms part2

Decreasing a keyThe current key is replaced with a new key To maintain the min-heapThe current key is replaced with a new key. To maintain the min-heapproperty, it is then compared to the key of the parent. If its parent’s key isgreater then the key and data will be exchanged. This process continues untilthe new key is greater than the parent’s key or the new key is in the root.y g p y yPseudocode:

Binomial-Heap-Decrease-Key(H,x,k)1 if k > key[x]1 if k > key[x]2 then error “new key is greater than current key”3 key[x] <-k4 y <-x5 z <-p[y]6 while z not NIL and key[y] < key[z]6 while z not NIL and key[y] key[z]7 do exchange key[y] <-> key[z]8 if y and z have satellite fields, exchange them, too.9 <9 y <- z10 z <- p[y]

Deepak John,Department Of IT,CE Poonjar

Page 83: Analysis and design of algorithms part2

Decreasing a keyExecution time: This procedure takes O(log n) since the maximumExecution time: This procedure takes O(log n) since the maximumdepth of x is log n.Example:p

5 2h d[H] 5 2

1210

head[H] 5 2

1210

head[H]

15 1

5 2head[H] 5 15 2

121

head[H] 5 1

122

head[H]

Deepak John,Department Of IT,CE Poonjar

10 10

Page 84: Analysis and design of algorithms part2

Delete a NodeWith assumption that there is no node in H has a key of -∞.

h k f d l i d i fi d dThe key of deleting node is first decreased to -∞.This node is then deleted using extracting min procedure.Pseudocode:

Binomial-Heap-Delete(H,x)1 Binomial-Heap-Decrease-Key(H,x,-∞)2 Binomial-Heap-Extract-Min(H)2 Binomial-Heap-Extract-Min(H)

Run time: O(log n) since the run time of both Binomial-Heap-Decrease-K d Bi i l H E t t Mi d i d f O(l )Key and Binomial-Heap-Extract-Min procedures are in order of O(log n).

Deepak John,Department Of IT,CE Poonjar

Page 85: Analysis and design of algorithms part2

Delete a Node Examplep

a) b)

5 2head[H] 5 2head[H]

a) b)

1210

15

12-∞

1515

5 -∞head[H] 5head[H]

c) d)

122

1

12 2head[H’]

Deepak John,Department Of IT,CE Poonjar

15 15

Page 86: Analysis and design of algorithms part2

e) f)

5 12 2

15

head[H] 5

12

2

15

head[H]

)

15 12 15

g)

5

2

15

head[H]

12

Deepak John,Department Of IT,CE Poonjar

Page 87: Analysis and design of algorithms part2

Fibonacci heap

•A Fibonacci heap is Set of min heap ordered trees

Fibonacci heap

•A Fibonacci heap is Set of min-heap ordered trees.•Each node x has pointed p[x] to its parent & child [x] to one of itschildren•Represent trees using left-child, right sibling pointers and circular,doubly linked list.Child li k d t th i d bl li k d i l li t•Children are linked together in a doubly-linked circular list.

•The entire heap is accessed by a pointer min [H] which points to theminimum-key rootu ey oo

Deepak John,Department Of IT,CE Poonjar

Page 88: Analysis and design of algorithms part2
Page 89: Analysis and design of algorithms part2

min[ H ]

(a) 23 7 24

30

17

26

3

4139

303818 52 26 46

3541

min[ H ]

(b) 23 7 24

30

17

3818 52 26

3

46

4139

303818 52 26 46

35

Page 90: Analysis and design of algorithms part2

Deepak John,Department Of IT,CE Poonjar

Page 91: Analysis and design of algorithms part2

• Potential function:

Number of marked nodes in H

Fibonacci heapNumber of trees in the rooted list of H

Number of marked nodes in H (H) = t(H) + 2m(H)

�(H) = 5 + 2�3 = 11 minHeap H trees(H) = 5 marks(H) = 3

72317 24 3

30

35

26 464118 52

3539 44marked

Deepak John,Department Of IT,CE Poonjar

Page 92: Analysis and design of algorithms part2

Fibonacci Heap Operationsp pCreateInsertInsertFind-MinUnionU oDeleteDelete-Min

•Make-Fib-Heap(H):Allocate and return the Fibonacci heap object H with n[H]=0 andmin[H]=nil, t(H)=0 , m(H)=0 so (H)=0

The cost of Make-Fib-Heap is O(1)The cost of Make-Fib-Heap is O(1).

Deepak John,Department Of IT,CE Poonjar

Page 93: Analysis and design of algorithms part2

Fibonacci Heaps: InsertInsert.

Create a new singleton tree.Add to left of min pointer.Update min pointer.

i t 21

21

insert 21

min

72317 24 3

30 26 464118 52

Deepak John,Department Of IT,CE Poonjar

3539 44Heap H

Page 94: Analysis and design of algorithms part2

min

41

723

18 52

3

30

17

26 46

24 21

39

4118 52

3544

Heap H

Insert Analysisse t a ys s

Actual cost. O(1)

Change in potential. +1

Amortized cost O(1)Amortized cost. O(1)

Page 95: Analysis and design of algorithms part2

Fib-Heap-Insert(H x)Fib Heap Insert(H, x){ degree[x] 0

P[x] NIL[ ]child[x] NILleft[x] x ; right[x] xmark[x] FALSEconcatenate the root list containing x with root list Hif i [H] NIL k [ ] k [ i [H]]if min[H] = NIL or key[x]<key[min[H]]

then min[H] xn[H] n[H]+1n[H] n[H]+1

}

Page 96: Analysis and design of algorithms part2

Fibonacci Heaps: UnionUnion.

Concatenate two Fibonacci heaps.Root lists are circular, doubly linked lists.

min min

717 323 24 21

39

4118 5230

35

26 46

44Heap H' Heap H''

39 44

Deepak John,Department Of IT,CE Poonjar

Page 97: Analysis and design of algorithms part2

min

717 323 24 21

4118 5230 26 46

3935

44Heap H

Actual cost. O(1)

Change in potential. 0

Amortized cost. O(1)( )

Page 98: Analysis and design of algorithms part2

Fib-Heap-Union(H1, H2){ k ib []{ H Make-Fib-Heap[]

min[H] min[H1]concatenate the root list of H with the root list of Hconcatenate the root list of H2 with the root list of Hif (min[H1]=NIL) or (min[H2] NIL and

min[H2]<min[H1])[ 2] [ 1])then min[H] min[H2]

n[H] n[H1]+n[H2]free the objects H1 and H2

return H}

Deepak John,Department Of IT,CE Poonjar

Page 99: Analysis and design of algorithms part2

Extract min()Fib-Heap-Extract-Min(H)

{ z min[H]if z NILif z NIL

then { for each child x of zdo { add x to the root list of H

P[x] NIL }P[x] NIL }remove z from the root list of Hif z = right[z]

then min[H] NILthen min[H] NILelse min[H] right[z]

Consolidate(H)n[H] n[H] – 1n[H] n[H] – 1

}return z

}

Deepak John,Department Of IT,CE Poonjar

}

Page 100: Analysis and design of algorithms part2

Fib-Heap-Link(H, y, x){ remove y from

the root list of H;Consolidate(H) the root list of H;make y a child of x;degree[x]degree[x]+1;mark[y] FALSE;

Consolidate(H){ for i 0 to D(n[H]) do A[i]=NIL

for each node w in the root list of Hdo { x w ; d degree[x] ; mark[y] FALSE;

}do { x w ; d degree[x] ;

while A[d] NILdo { yA[d]

if key[x]>key[y] then exchange xyif key[x]>key[y] then exchange xyFib-Heap-Link(H, y, x)A[d] NIL ; d d+1 }

A[d] }A[d] x }min[H] NIL

for i 0 to D(n[H]) doif A[i] NIL h { dd A[i] h li f Hif A[i] NIL then { add A[i] to the root list of H ;

if min[H]=NIL or key[A[i]]<key[min[H]]then min[H] A[i] }

Deepak John,Department Of IT,CE Poonjar

}

Page 101: Analysis and design of algorithms part2

Fibonacci Heaps: Delete MinFibonacci Heaps: Delete Min• Delete min.

Delete min; meld its children into root list; update min– Delete min; meld its children into root list; update min.– Consolidate trees so that no two roots have same rank.

min

317237 24

39

4118 52

44

30

35

26 46

39 4435

Page 102: Analysis and design of algorithms part2

411723 18 527 24

min

3930 26 46 44

35

min current

411723 18 527 24

3930

35

26 46 44

35

Page 103: Analysis and design of algorithms part2

0 1 2 3rank

411723 18 527 24

currentmin

3930 26 46 44

35 0 1 2 3

411723 18 527 24

min current

39

411723 18 52

30

7

26 46

24

4430

35

26 46

Page 104: Analysis and design of algorithms part2

0 1 2 3rank

411723 18 527 24

min

3930 26 46 44currentrank

350 1 2 3

rank

411723 18 527 24

min

3930 26 46 44current

35link 23 into 17

Page 105: Analysis and design of algorithms part2

0 1 2 3rank

min

4117 18 527 24

392330

35

26 46 44current

35

link 17 into 7

Page 106: Analysis and design of algorithms part2

0 1 2 3rank

0 1 2 3

current

417 18 5224min

39301726 46 44

35 23

link 24 into 7

Page 107: Analysis and design of algorithms part2

0 1 2 3rank

mincurrent

39

417

30

18 52

1724 443930

23

17

26 46

24 44

35

Page 108: Analysis and design of algorithms part2

rank0 1 2 3

a

417 18 52min

current

39301724 44

23

35

26 46

35

Page 109: Analysis and design of algorithms part2

0 1 2 3rank

417 18 52min

current

39

417

30

18 52

1724 44

2326 46

35

Page 110: Analysis and design of algorithms part2

0 1 2 3rank

417 18 52min

current

39

417

30

18 52

1724 44

2326 46

35link 41 into 18

Page 111: Analysis and design of algorithms part2

0 1 2 3rank

7 1852min

current

3941

7

30

1852

1724

2326 46 44

35

Page 112: Analysis and design of algorithms part2

0 1 2 3rank

min

current

7

30

52

1724 3941

18

30

23

17

26 46

24 3941

44

35

Page 113: Analysis and design of algorithms part2

7 52min

18

301724 3941

23

35

26 46 44

stop

Page 114: Analysis and design of algorithms part2

Fibonacci Heaps: Decrease KeyDecrease key of element x to k.

Case 0: min-heap property not violated.Case 0: min heap property not violated.•decrease key of x to k•change heap min pointer if necessary

7 18 38min

24 17 23 21 39 41

46 3026 5245

Deepak John,Department Of IT,CE Poonjar

88Decrease 46 to 45.

7235

Page 115: Analysis and design of algorithms part2

Case 1: parent of x is unmarked.d k f t k•decrease key of x to k

•cut off link between x and its parent•mark parent•mark parent•add tree rooted at x to root list, updating heap min pointer

7 18 38min

24 17 23 21 39 41

45 3026 52

Decrease 45 to 15

15

Deepak John,Department Of IT,CE Poonjar

88Decrease 45 to 15.

7235

Page 116: Analysis and design of algorithms part2

7 18 38min

24 17 23 21 39 4124

15 3026 52

Decrease 45 to 15.88

ec ease 5 to 5.7235

7 18 38min

15

24 17 23 21 39 412472

3026 52

88Decrease 45 to 15.

35

Deepak John,Department Of IT,CE Poonjar

Page 117: Analysis and design of algorithms part2

Case 2: parent of x is marked.•decrease key of x to k•cut off link between x and its parent p[x], and add x to root list•cut off link between p[x] and p[p[x]], add p[x] to root list

If p[p[x]] unmarked, then mark it.If p[p[x]] marked, cut off p[p[x]], unmark, and repeat.

15 7 18 38min

24 17 23 21 39 4172 24

3026 52

Decrease 35 to 535

Deepak John,Department Of IT,CE Poonjar

88Decrease 35 to 5.

5

Page 118: Analysis and design of algorithms part2

7 18 38515min

24 17 23 21 39 412472

3026 52

D 35 5

parent marked

Decrease 35 to 5.88

26 7 18 38515min

24 17 23 21 39 4188 2472

30 52

Decrease 35 to 5.parent marked

Deepak John,Department Of IT,CE Poonjar

Page 119: Analysis and design of algorithms part2

26 7 18 38515 24min

17 23 21 39 418872

30 52

Decrease 35 to 5.

Deepak John,Department Of IT,CE Poonjar

Page 120: Analysis and design of algorithms part2

Deepak John,Department Of IT,CE Poonjar

Page 121: Analysis and design of algorithms part2

Deepak John,Department Of IT,CE Poonjar

Page 122: Analysis and design of algorithms part2

Amortized Analysis techniquesy q• In amortized analysis we average the time required for a sequence

of operations over all the operations performed.• A ti d l i t t f h• Amortized analysis guarantees an average worst case for each

operation.– No involvement of probability

• The amortized cost per operation is therefore T(n)/n. The aggregate method The Accounting method The Accounting method. The potential method

Page 123: Analysis and design of algorithms part2

Aggregate analysisAggregate analysis

– The total amount of time needed for the n operations iscomputed and divided by n.

– Treat all operations equally.– Compute the worst-case running time of a sequence of n

operationsoperations.– Divide by n to get an amortized running time.– We aggregate the cost of a series of n operations to T(n), thengg g p ( ),

each operation has the same amortized cost of T(n)/n

Page 124: Analysis and design of algorithms part2

The Accounting methodThe Accounting method

• Principles of the accounting methodp g– 1. Associate credit accounts with different parts of the

structure– 2. Associate amortized costs with operations and show

how they credit or debit accounts• Different costs may be assigned to different operations• Different costs may be assigned to different operations.operations are assigned an amortized cost. Objects of the

data structure are assigned a credit

Page 125: Analysis and design of algorithms part2

Accounting Method vs. Aggregate Method

• Aggregate method:gg g– first analyze entire sequence– then calculate amortized cost per operation

• Accounting method:– first assign amortized cost per operation– check that they are valid (never go into the red)– then compute cost of entire sequence of operations

Page 126: Analysis and design of algorithms part2

The Potential method• Similar to accounting method• Similar to accounting method• Amortized costs are assigned in a more complicated way

– based on a potential functionbased on a potential function– and the current state of the data structure

• Must ensure that sum of amortized costs of all operations in thesequence is at least the sum of the actual costs of all operations in thesequence.

• Define potential function which maps any state of the data• Define potential function which maps any state of the data structure to a real number

• Notation:– D0 - initial state of data structure– Di - state of data structure after i-th operation

t l t f i th ti– ci - actual cost of i-th operation– mi - amortized cost of i-th operation

Page 127: Analysis and design of algorithms part2

Red-Black TreesA red-black tree can also be defined as a binary search tree that satisfiesthe following properties:1.A node is either red or black.2.The root is ALWAYS black.3 All leaves are black3.All leaves are black.4.Both Children of a node that is red, are black. (no red node can havea red child).5 E h f i d d d d l f i h5.Every path from a given node down to any descendant leaf contains thesame number of black nodes. The number of black nodes on such a path(not including the initial node but including leaves) is called the black-height (bh) of the node.

The red-black tree has O(lg n) height

Deepak John,Department Of IT,CE Poonjar

Page 128: Analysis and design of algorithms part2

Red-Black Tree

■ Root Property: the root is black■ External Property: every leaf is blackp y y■ Internal Property: the children of a red node are black■ Depth Property: all the leaves have the same black depth

Deepak John,Department Of IT,CE Poonjar

Page 129: Analysis and design of algorithms part2

Rotations•Rotations are the basic tree-restructuring operation for almost allbalanced search trees.R t ti t k d bl k t d d•Rotation takes a red-black-tree and a node,

•Changes pointers to change the local structure, and Won’t violate thebinary-search-tree property.•Left rotation and right rotation are inverses.

y

Left-Rotate(T, x)x

x

y

Right-Rotate(T, y)

Deepak John,Department Of IT,CE Poonjar

Page 130: Analysis and design of algorithms part2

An example of LEFT-ROTATE(T,x)

Deepak John,Department Of IT,CE Poonjar

Page 131: Analysis and design of algorithms part2

Left and Right RotationLeft Rotate (T x)Left Rotate (T x)Left-Rotate (T, x)1. y right[x] // Set y.2. right[x] left[y] //Turn y’s left subtree into x’s right subtree.

Left-Rotate (T, x)1. y right[x] // Set y.2. right[x] left[y] //Turn y’s left subtree into x’s right subtree.3. if left[y] nil[T ]4. then p[left[y]] x5 [ ] [ ] // Li k ’ t t

3. if left[y] nil[T ]4. then p[left[y]] x5 [ ] [ ] // Li k ’ t t5. p[y] p[x] // Link x’s parent to y.6. if p[x] = nil[T ]7. then root[T ] y

5. p[y] p[x] // Link x’s parent to y.6. if p[x] = nil[T ]7. then root[T ] y

•The code for RIGHT-ROTATE is symmetric.

[ ] y8. else if x = left[p[x]]9. then left[p[x]] y10 l i h [ [ ]]

[ ] y8. else if x = left[p[x]]9. then left[p[x]] y10 l i h [ [ ]] •Both LEFT-ROTATE

and RIGHT-ROTATErun in O(1) time

10. else right[p[x]] y11. left[y] x // Put x on y’s left.12. p[x] y

10. else right[p[x]] y11. left[y] x // Put x on y’s left.12. p[x] y

Deepak John,Department Of IT,CE Poonjar

run in O(1) time.12. p[x] y12. p[x] y

Page 132: Analysis and design of algorithms part2

Ri h iRight rotation:1. x=left[y];2. left[y]=right[x];[y] g [ ];3. If(right[x]!=nil)4. then p[right[x]]=y;5 p[x]=p[y];5. p[x] p[y];6. if(p[y]==nil)7. then root=x;8 El If(l f [ [ ]] )8. Else If(left[p[y]]=y)9. then left[p[y]]=x;10. else right[p[y]]=x;g [p[y]]11. right[x]=y;12. p[y]=x;

Page 133: Analysis and design of algorithms part2

RotationTh d d f L f R h• The pseudo-code for Left-Rotate assumes that– right[x] nil[T ], and

root’s parent is nil[T ]– root s parent is nil[T ].• Left Rotation on x, makes x the left child of y, and the left subtree

of y into the right subtree of x.• Pseudocode for Right-Rotate is symmetric: exchange left and right

everywhere.Ti O(1) f b h L f R d Ri h R i• Time: O(1) for both Left-Rotate and Right-Rotate, since a constantnumber of pointers are modified.

Operations on RB TreesOperations on RB Trees• All operations can be performed in O(lg n) time.• Insertion and Deletion are not straightforward.

Page 134: Analysis and design of algorithms part2

When Inserting a NodegRemember:1. Insert nodes one at a time, and after every Insertionbalance the treebalance the tree.2. Every node inserted starts as a Red node.3. Consult the cases, for rebalancing the tree.

•Basic steps:1. Use Tree-Insert from BST (slightly modified) to insert a node

x into Tx into T.-Procedure RB-Insert(x).-Color the node x red.Color the node x red.

2. Fix the modified tree by re-coloring nodes and performingrotation to preserve RB tree property.

Deepak John,Department Of IT,CE Poonjar

-Procedure RB-Insert-Fixup.

Page 135: Analysis and design of algorithms part2

Red-Black fixup• y = z’s “uncle”y• Three cases:

– y is red– y is black and z is a right child– y is black and z is a left child.

Page 136: Analysis and design of algorithms part2

Case 1 – Z’s uncle y is redC C

new zp[p[z]]

A D

y

C

A D

p[z]

B

z

A D

B

B

B

z is a right child here.Similar steps if z is a left child.

• y.Color = black• z.Parent.Color = black• z.Parent.Parent.Color = red• z = z.Parent.Parent

R fi• Repeat fixup

Page 137: Analysis and design of algorithms part2

11

2 14

71 15

5 8

4

y

4zy.Color = blackz.Parent.Color = blackNew . a e .Co o b acz.Parent.Parent.Color = redz = z.Parent.Parent

NewNode

repeat fixup

Page 138: Analysis and design of algorithms part2

1111

2 14

71 15

5 8 y

4z y.Color = blackz.Parent.Color = blackz.Parent.Parent.Color = redz = z.Parent.Parent

fi

NewNode

repeat fixup

Page 139: Analysis and design of algorithms part2

11

2 142 14

71 15

5 8 yC l bl k

4zy.Color = blackz.Parent.Color = blackz Parent Parent Color = redNew z.Parent.Parent.Color redz = z.Parent.Parentrepeat fixup

NewNode

p p

Page 140: Analysis and design of algorithms part2

1111

2 14

71 15

5 8 y

4z y.Color = blackz.Parent.Color = blackz Parent Parent Color redz.Parent.Parent.Color = redz = z.Parent.Parentrepeat fixup

NewNode

repeat fixup

Page 141: Analysis and design of algorithms part2

1111

2 14 y

71 15z

5 8y.Color = black

4 z.Parent.Color = blackz.Parent.Parent.Color = red

P PNew z = z.Parent.Parentrepeat fixup

NewNode

Page 142: Analysis and design of algorithms part2

Case 2 – y is black, z is a right childC C

p[z] p[z]A

z

y B y

B

A

z

• z = z.Parent• Left-Rotate(T, z)• Do Case 3

N t th t C 2 i b t f C 3• Note that Case 2 is a subset of Case 3

Page 143: Analysis and design of algorithms part2

1111

2 14 y

71 15z

5 8 z = z.Parent

4Left-Rotate(T,z)Do Case 3

Page 144: Analysis and design of algorithms part2

1111

2 14z y

71 15

5 8 z = z.ParentLeft-Rotate(T,z)

4( , )

Do Case 3

Page 145: Analysis and design of algorithms part2

11

147 y

2

1

15

5

8z

1 5

44

z = z.ParentLeft-Rotate(T,z)Do Case 3

Page 146: Analysis and design of algorithms part2

Case 3 – y is black, z is a left child

BC

p[z]

AB y

p[z]

C

z

A z

• z.Parent.Color = black• z.Parent.Parent.Color = red• Right-Rotate(T, z.Parent.Parent)

Page 147: Analysis and design of algorithms part2

1111

147 y

2 158z

1 5

4 z.Parent.Color = blackz.Parent.Parent.Color = redRight-Rotate(T, z.Parent.Parent)

Page 148: Analysis and design of algorithms part2

1111

147 y

2 158z

1 5

z Parent Color = black4 z.Parent.Color = blackz.Parent.Parent.Color = redRight-Rotate(T, z.Parent.Parent)Right Rotate(T, z.Parent.Parent)

Page 149: Analysis and design of algorithms part2

11

147 y

2 158z

1 5

4 z.Parent.Color = black4z.Parent.Parent.Color = redRight-Rotate(T, z.Parent.Parent)

Page 150: Analysis and design of algorithms part2

7

112

7

z

141 5 8 y

154

z.Parent.Color = blackz.Parent.Parent.Color = redRight-Rotate(T, z.Parent.Parent)

Page 151: Analysis and design of algorithms part2

RB-Insert(T, z)1. y nil[T]2 x root[T]2. x root[T]3. while x nil[T]4. do y x5 if key[z] < key[x]5. if key[z] < key[x]6. then x left[x]7. else x right[x]8 [ ] 8. p[z] y9. if y = nil[T]10. then root[T] z11 l if k [ ] k [ ]11. else if key[z] < key[y]12. then left[y] z13. else right[y] z14. left[z] nil[T]15. right[z] nil[T]16 color[z] RED16. color[z] RED17. RB-Insert-Fixup (T, z)

Page 152: Analysis and design of algorithms part2

RB-Insert-Fixup (T, z)1. while color[p[z]] = RED2 d if [ ] l ft[ [ [ ]]]2. do if p[z] = left[p[p[z]]]3. then y right[p[p[z]]]4. if color[y] = RED5. then color[p[z]] BLACK // Case 16. color[y] BLACK // Case 17. color[p[p[z]]] RED // Case 1[p[p[ ]]]8. z p[p[z]] // Case 19. else if z = right[p[z]] // color[y] RED10 then z p[z] // Case 210. then z p[z] // Case 211. LEFT-ROTATE(T, z) // Case 212. color[p[z]] BLACK // Case 313 color[p[p[z]]] RED // Case 313. color[p[p[z]]] RED // Case 314. RIGHT-ROTATE(T, p[p[z]]) // Case 315. else (if p[z] = right[p[p[z]]])(same as 10-1416 ith “ i ht” d “l ft” h d)16. with “right” and “left” exchanged)17. color[root[T ]] BLACK

Page 153: Analysis and design of algorithms part2

Correctness

Loop invariant:• At the start of each iteration of the while loop,

– z is red.– If p[z] is the root, then p[z] is black.– There is at most one red-black violation:

• Property 2: z is a red root or• Property 2: z is a red root, or• Property 4: z and p[z] are both red.

Page 154: Analysis and design of algorithms part2

• Termination: The loop terminates only if p[z] is black. Hence,property 4 is OK. The last line ensures property 2 always holds.p p y p p y y

• Maintenance: We drop out when z is the root (since then p[z] issentinel nil[T ], which is black). When we start the loop body, the

l i l ti i f t 4only violation is of property 4.– There are 6 cases, 3 of which are symmetric to the other 3. We

consider cases in which p[z] is a left child.p[ ]– Let y be z’s uncle (p[z]’s sibling).

Page 155: Analysis and design of algorithms part2

Algorithm AnalysisAlgorithm Analysis• O(lg n) time to get through RB-Insert up to theO(lg n) time to get through RB Insert up to the

call of RB-Insert-Fixup.• Within RB-Insert-Fixup:• Within RB-Insert-Fixup:

– Each iteration takes O(1) time.Each iteration but the last moves up 2 levels– Each iteration but the last moves z up 2 levels.

– O(lg n) levels O(lg n) time.Th i ti i d bl k t t k O(l ) ti– Thus, insertion in a red-black tree takes O(lg n) time.

– Note: there are at most 2 rotations overall.

Page 156: Analysis and design of algorithms part2

Deletion

• Find• Swap

– Moves entry to node with one external node (left)• Remove entry• Reattach right child

Page 157: Analysis and design of algorithms part2

DeletionDeletion from a red black tree, is similar to deletion for a binarysearch tree, with a few exception:

•Always set the parent of a deleted node, to be the parent of oneof the deleted nodes children.•Red black fix-up method called if removed node is black.p

After a deletion of a red node (no violations occur):N bl k h i h h b ff d•No black-heights have been affected.

•No red nodes have been made adjacent (parent and child bothred).)•Deleted node is not the root since the root is black.

Deepak John,Department Of IT,CE Poonjar

Page 158: Analysis and design of algorithms part2

• After Deletion of a Black node a restore function must be called tofix red-black properties that might be violated. There are 3possible initial violations.

If d l t d d th t d hild i ht b th t– If deleted node was the root, a red child might now be the root,Violation of property 2.

– If both the parent of removed node, and a child of removedIf both the parent of removed node, and a child of removednode are red, we have a violation of property 4.

– The removal of a black node can cause the black-height of oneh b h (b 1) i l i 5path to be shorter (by 1), violating property 5.

– We correct the problem of rule 5 by adding an extra “black” tothe node passed into the fix-up procedure. This leads tothe node passed into the fix up procedure. This leads toviolations in rules 1 since this node is now neither red or black.

Deepak John,Department Of IT,CE Poonjar

Page 159: Analysis and design of algorithms part2

Delete PossibilitiesDelete Possibilities1:Delete Red node

• No problem2:Delete black node with red child

• Color red child black3:Delete black node with black child

C l hild “D bl Bl k”• Color child “Double Black”• 3 possibilities depending on neighboring nodes

X’s sibling is black with at least one red child– X s sibling is black with at least one red child– X’s sibling is black with no red children– X’s sibling is reds s b g s ed

Page 160: Analysis and design of algorithms part2

Deletion – Fixup p• Idea: Move the extra black up the tree until x points to a red &

black node turn it into a black node,• x points to the root just remove the extra black, or• We can do certain rotations and recolorings and finish.

Withi th hil l• Within the while loop:– x always points to a nonroot doubly black node.– w is x’s siblingw is x s sibling.– w cannot be nil[T ], since that would violate property 5 at

p[x].

Page 161: Analysis and design of algorithms part2

Case 1 – w is redp[x]

B

A D Bx w

D

E

p[ ]

A D

C E

B

A C

E

x new wC E

w

•w must have black children.•Make w black and p[x] red.•Th l ft t t [ ]•Then left rotate on p[x].•New sibling of x was a child of w before rotation must be black.Go immediately to case 2, 3, or 4.

Deepak John,Department Of IT,CE Poonjar

Page 162: Analysis and design of algorithms part2

Case 2 – w is black, both w’s children are blackp[x] black

B

A Dx w

Bnew xc

cp[x]

A D

C E

A D

C E

•Take 1 black off x ( singly black) and off w ( red)

C E

C E

•Take 1 black off x ( singly black) and off w ( red).•Move that black to p[x].•Do the next iteration with p[x] as the new xDo the next iteration with p[x] as the new x.•If entered this case from case 1, then p[x] was red new x is red &black color attribute of new x is RED loop terminates. Then newx is made black in the last line.

Deepak John,Department Of IT,CE Poonjar

Page 163: Analysis and design of algorithms part2

Case 3 – w is black, w’s left child is red, w’s right child is blackw s right child is black

Bx w

Bc

c

A D

x w

A C

D

new wx

C E

D

E

•Make w red and w’s left child black.

Make w red and w s left child black.•Then right rotate on w.•New sibling w of x is black with a red right child case 4.

Deepak John,Department Of IT,CE Poonjar

Page 164: Analysis and design of algorithms part2

Case 4 – w is black, w’s right child is redB

A D Bx w

D

E

c

A D

C E

B

A C

E

xc’C E

•Make w be p[x]’s color (c).•Make p[x] black and w’s right child black.Th l ft t t [ ]•Then left rotate on p[x].

•Remove the extra black on x ( x is now singly black) withoutviolating any red-black properties.g y p p•All done. Setting x to root causes the loop to terminate.

Deepak John,Department Of IT,CE Poonjar

Page 165: Analysis and design of algorithms part2

RB-Delete(T, z)1. if left[z] = nil[T] or right[z] = nil[T]2. then y z

RB-Delete(T, z)1. if left[z] = nil[T] or right[z] = nil[T]2. then y z3. else y TREE-SUCCESSOR(z)4. if left[y] = nil[T ]5. then x left[y]

3. else y TREE-SUCCESSOR(z)4. if left[y] = nil[T ]5. then x left[y]6. else x right[y]7. p[x] p[y] // Do this, even if x is nil[T]6. else x right[y]7. p[x] p[y] // Do this, even if x is nil[T]8. if p[y] = nil[T ]8. if p[y] = nil[T ]9. then root[T ] x10. else if y = left[p[y]]11. then left[p[y]] x

9. then root[T ] x10. else if y = left[p[y]]11. then left[p[y]] x11. then left[p[y]] x12. else right[p[y]] x13. if y = z14 then key[z] key[y]

11. then left[p[y]] x12. else right[p[y]] x13. if y = z14 then key[z] key[y]14. then key[z] key[y]15. copy y’s satellite data into z16. if color[y] = BLACK17 th RB D l t Fi (T )

14. then key[z] key[y]15. copy y’s satellite data into z16. if color[y] = BLACK17 th RB D l t Fi (T )

Deepak John,Department Of IT,CE Poonjar

17. then RB-Delete-Fixup(T, x)18. return y17. then RB-Delete-Fixup(T, x)18. return y

Page 166: Analysis and design of algorithms part2

RB D l Fi (T )RB D l Fi (T )RB-Delete-Fixup(T, x)1. while x root[T ] and color[x] = BLACK2 do if x = left[p[x]]

RB-Delete-Fixup(T, x)1. while x root[T ] and color[x] = BLACK2 do if x = left[p[x]]2. do if x = left[p[x]]3. then w right[p[x]]4. if color[w] = RED

2. do if x = left[p[x]]3. then w right[p[x]]4. if color[w] = RED[ ]5. then color[w] BLACK // Case 16. color[p[x]] RED // Case 1

[ ]5. then color[w] BLACK // Case 16. color[p[x]] RED // Case 17. LEFT-ROTATE(T, p[x]) // Case 18. w right[p[x]] // Case 17. LEFT-ROTATE(T, p[x]) // Case 18. w right[p[x]] // Case 1

Deepak John,Department Of IT,CE Poonjar

Page 167: Analysis and design of algorithms part2

/* x is still left[p[x]] */9. if color[left[w]] = BLACK and color[right[w]] = BLACK

l //

/* x is still left[p[x]] */9. if color[left[w]] = BLACK and color[right[w]] = BLACK

l //10. then color[w] RED // Case 211. x p[x] // Case 212. else if color[right[w]] = BLACK

10. then color[w] RED // Case 211. x p[x] // Case 212. else if color[right[w]] = BLACK12. else if color[right[w]] BLACK13. then color[left[w]] BLACK // Case 314. color[w] RED // Case 3

12. else if color[right[w]] BLACK13. then color[left[w]] BLACK // Case 314. color[w] RED // Case 315. RIGHT-ROTATE(T,w) // Case 316. w right[p[x]] // Case 317 color[w] color[p[x]] // Case 4

15. RIGHT-ROTATE(T,w) // Case 316. w right[p[x]] // Case 317 color[w] color[p[x]] // Case 417. color[w] color[p[x]] // Case 418. color[p[x]] BLACK // Case 419. color[right[w]] BLACK // Case 4

17. color[w] color[p[x]] // Case 418. color[p[x]] BLACK // Case 419. color[right[w]] BLACK // Case 420. LEFT-ROTATE(T, p[x]) // Case 421. x root[T ] // Case 422 else (same as then cla se ith “right” and “left” e changed)

20. LEFT-ROTATE(T, p[x]) // Case 421. x root[T ] // Case 422 else (same as then cla se ith “right” and “left” e changed)22. else (same as then clause with “right” and “left” exchanged)23. color[x] BLACK22. else (same as then clause with “right” and “left” exchanged)23. color[x] BLACK

Page 168: Analysis and design of algorithms part2

Delete AnalysisO(lg n) time to get through RB-Delete up to the call of RB-Delete-Fixup.Within RB-Delete-Fixup:

Case 2 is the only case in which more iterations occur.x moves up 1 levelx moves up 1 level.Hence, O(lg n) iterations.

Each of cases 1, 3, and 4 has 1 rotation 3 rotations in all.Hence, O(lg n) time.

Deepak John,Department Of IT,CE Poonjar