×

Close

- Design And Analysis Of Algorithm - DAA
- Note
**Guru govind Singh indraprastha university - GGSIPU**- Computer Science Engineering
- 8 Topics
**276 Views**- 5 Offline Downloads
- Uploaded 1 year ago

Touch here to read

Page-2

Topic:

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

1 Introduction Overview. The main topics to be covered in this course are Meetings. We meet twice a week, on Tuesdays and Thursdays, from 1:15 to 2:30pm, in room D106 LSRC. I Design Techniques; II Searching; III Prioritizing; Communication. The course material will be delivered in the two weekly lectures. A written record of the lectures will be available on the web, usually a day after the lecture. The web also contains other information, such as homework assignments, solutions, useful links, etc. The main supporting text is IV Graph Algorithms; V Topological Algorithms; VI Geometric Algorithms; VII NP-completeness. TARJAN . Data Structures and Network Algorithms. SIAM, 1983. The emphasis will be on algorithm design and on algorithm analysis. For the analysis, we frequently need basic mathematical tools. Think of analysis as the measurement of the quality of your design. Just like you use your sense of taste to check your cooking, you should get into the habit of using algorithm analysis to justify design decisions when you write an algorithm or a computer program. This is a necessary step to reach the next level in mastering the art of programming. I encourage you to implement new algorithms and to compare the experimental performance of your program with the theoretical prediction gained through analysis. The book focuses on fundamental data structures and graph algorithms, and additional topics covered in the course can be found in the lecture notes or other texts in algorithms such as K LEINBERG AND TARDOS . Algorithm Design. Pearson Education, 2006. Examinations. There will be a final exam (covering the material of the entire semester) and a midterm (at the beginning of October), You may want to freshen up your math skills before going into this course. The weighting of exams and homework used to determine your grades is homework midterm final 35%, 25%, 40%. Homework. We have seven homeworks scheduled throughout this semester, one per main topic covered in the course. The solutions to each homework are due one and a half weeks after the assignment. More precisely, they are due at the beginning of the third lecture after the assignment. The seventh homework may help you prepare for the final exam and solutions will not be collected. Rule 1. The solution to any one homework question must fit on a single page (together with the statement of the problem). Rule 2. The discussion of questions and solutions before the due date is not discouraged, but you must formulate your own solution. Rule 3. The deadline for turning in solutions is 10 minutes after the beginning of the lecture on the due date. 3

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

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

## Leave your Comments