×
Study like there’s no tomorrow because if you keep putting off your studies for tomorrow, you’ll probably be too late.

# Note for Design And Analysis Of Algorithm - DAA By HIMANSHU GULATI

• Design And Analysis Of Algorithm - DAA
• Note
• Guru govind Singh indraprastha university - GGSIPU
• Computer Science Engineering
• 8 Topics
• 320 Views
Page-2

#### Note for Design And Analysis Of Algorithm - DAA By HIMANSHU GULATI

Topic: / 95

0 User(s)

#### Text from page-2

CPS 230 Fall Semester of 2008 Table of Contents 1 I 2 3 4 5 II 6 7 8 9 III 10 11 12 Introduction 3 D ESIGN T ECHNIQUES 4 Divide-and-Conquer Prune-and-Search Dynamic Programming Greedy Algorithms First Homework Assignment 5 8 11 14 17 S EARCHING 18 Binary Search Trees Red-Black Trees Amortized Analysis Splay Trees Second Homework Assignment 19 22 26 29 33 P RIORITIZING 34 Heaps and Heapsort Fibonacci Heaps Solving Recurrence Relations Third Homework Assignment 35 38 41 44 IV 13 14 15 16 V 17 18 19 VI 20 21 22 VII 23 24 25 2 G RAPH A LGORITHMS 45 Graph Search Shortest Paths Minimum Spanning Trees Union-Find Fourth Homework Assignment 46 50 53 56 60 T OPOLOGICAL A LGORITHMS 61 Geometric Graphs Surfaces Homology Fifth Homework Assignment 62 65 68 72 G EOMETRIC A LGORITHMS 73 Plane-Sweep Delaunay Triangulations Alpha Shapes Sixth Homework Assignment 74 77 81 84 NP-C OMPLETENESS 85 Easy and Hard Problems NP-Complete Problems Approximation Algorithms Seventh Homework Assignment 86 89 92 95

#### Text from page-4

I D ESIGN T ECHNIQUES 2 3 4 5 Divide-and-Conquer Prune-and-Search Dynamic Programming Greedy Algorithms First Homework Assignment 4

#### Text from page-5

2 Divide-and-Conquer 7 We use quicksort as an example for an algorithm that follows the divide-and-conquer paradigm. It has the reputation of being the fasted comparison-based sorting algorithm. Indeed it is very fast on the average but can be slow for some input, unless precautions are taken. 3 5 4 2 9 4 2 1 i j 7 3 5 4 2 1 4 2 9 i 2 The algorithm. Quicksort follows the general paradigm of divide-and-conquer, which means it divides the unsorted array into two, it recurses on the two pieces, and it finally combines the two sorted pieces to obtain the sorted array. An interesting feature of quicksort is that the divide step separates small from large items. As a consequence, combining the sorted pieces happens automatically without doing anything extra. 3 5 4 2 j 1 4 7 9 m Figure 1: First, i and j stop at items 9 and 1, which are then swapped. Second, i and j cross and the pivot, 7, is swapped with item 2. Special cases (i) and (iii) are ok but case (ii) requires a stopper at A[r + 1]. This stopper must be an item at least as large as x. If r < n − 1 this stopper is automatically given. For r = n − 1, we create such a stopper by setting A[n] = +∞. void Q UICK S ORT (int ℓ, r) if ℓ < r then m = S PLIT(ℓ, r); Q UICK S ORT (ℓ, m − 1); Q UICK S ORT (m + 1, r) endif. Running time. The actions taken by quicksort can be expressed using a binary tree: each (internal) node represents a call and displays the length of the subarray; see Figure 2. The worst case occurs when A is already sorted. We assume the items are stored in A[0..n − 1]. The array is sorted by calling Q UICK S ORT (0, n − 1). Splitting. The performance of quicksort depends heavily on the performance of the split operation. The effect of splitting from ℓ to r is: 9 7 1 1 • x = A[ℓ] is moved to its correct location at A[m]; 5 2 2 1 • no item in A[ℓ..m − 1] is larger than x; • no item in A[m + 1..r] is smaller than x. 1 Figure 2: The total amount of time is proportional to the sum of lengths, which are the numbers of nodes in the corresponding subtrees. In the displayed case this sum is 29. Figure 1 illustrates the process with an example. The nine items are split by moving a pointer i from left to right and another pointer j from right to left. The process stops when i and j cross. To get splitting right is a bit delicate, in particular in special cases. Make sure the algorithm is correct for (i) x is smallest item, (ii) x is largest item, (iii) all items are the same. In this case the tree degenerates to a list without branching. The sum of lengths can be described by the following recurrence relation:   n X n+1 i = . T (n) = n + T (n − 1) = 2 i=1 int S PLIT(int ℓ, r) x = A[ℓ]; i = ℓ; j = r + 1; repeat repeat i++ until x ≤ A[i]; repeat j-- until x ≥ A[j]; if i < j then S WAP(i, j) endif until i ≥ j; S WAP(ℓ, j); return j. The running time in the worst case is therefore in O(n2 ). In the best case the tree is completely balanced and the sum of lengths is described by the recurrence relation   n−1 . T (n) = n + 2 · T 2 5