Beruflich Dokumente
Kultur Dokumente
References :
Cormen, Leiserson, Rivest, Stein, Introduction To Algorithms, 2nd Ed., McGraw Hill / The MIT Press, 2001
Ellis Horowitz, Sartaj Sahni, Fundamentals Of Data Structures In C++ A.V. Aho, J.E. Hopcroft, J.D. Ullman, Data Structures And Algorithms, 1983 U. Manber, Introduction To Algorithms: A Creative Approach, Addison Wesley, 1989
1
Topics
Trees 1. Definitions 2. Implementing methods 3. Traversing methods 4. Expression tree 5. Threaded tree 6. Huffman tree and coding 7. Heap 8. Binary search tree 9. Selection tree Hashing Algorithm Analysis Methods 1. Definitions 2. Complexity computation 3. Methods for solving recursive problems Recursive Algorithms Basic Data Structures 1. Abstract Data Types 2. Array 3. Stack 4. Queue 5. Link list
Topics (contd.)
Problem solving methods 1. Divide and conquer 2. Dynamic programming 3. Greedy 4. Backtracking Trees (contd) 1. B-tree 2. AVL 3. 2-3 Tree 4. 2-3-4 tree Advanced Data Structures 1. Red-Black tree 2. Binomial heap 3. Fibonacci heap 4. Disjoint set Sorting Algorithms Based on compare 1. Bubble sort 2. Selection sort 3. Quick sort 4. Heap sort 5. Merge sort 6. Shell sort Not based on compare 1. Count sort 2. Radix sort 3. Bucket sort Graphs 1. Definitions 2. Implementing methods 3. Traversing methods 4. Famous problems
3
Algorithm Analysis
Example : Sorting
Sorting an array with n items ascending : a[0]<=a[1]<=a[2]<=..<=a[n]
Insertion Sort
Start with a single-size subset Each time Insert the next item into the sorted subset
Third
Last
3
3
6
5
6
6
9
9
14
14
7 * n-1
+ 5
=2(
1)+6
=2(
1)+7 (n1)
9
= 1 + 2 1 + 3 1 + 4
=2
+ 5
( 1)+6
=2
( 1)+7 (n1)
=2
= 1 + 2 + 3 + 7 1 + 4
=2
+ (5 + 6 )
( 1)
=2
Best Situation : = 1 :
= 1 + 2 + 3 + 7 1 + 4 1 = 1 + (2 +3 + 4 + 7 ) 1 = 1 + 1
Worst Situation : = k :
=
=2 =2
2 = 1 2
1 =
=2 =1
1 2 2
= 1 + 2 + 3 + 7 1 + 4 = 2 2 + 2 + 2
1 1 + 5 + 6 2
1 2 2
Average Situation :
=
=1
+ + =
=1
+ + ===== = 3 2 + 3 + 3
=1
10
()
100
10
(2 )
52
14
45
3.2
31.94
(3 )
3 2 2
12
27
2.3
10.50
(2 )
10
13
1.3
2.00
11
12
13
14
log
2 3 2 Quadratic Cubic Exponential
15
Comparison of N, log
() 16 64 256 1,024 16,384 131,072 262,144 (log ) 4 6 8 10 14 17 18 (2 ) 256 4K 64K 1M 256M 16G 6.87E+10
524,188
1,048,576 1,073,741,824
19
20 30
2.74E+11
1.09E+12 1.15E+18
16
2 = 2 , 10003 = (10 )
17
18
19
Example
f(n) cg(n) f(n) 0
cg(n)
0 =
f(n) = O(g(n))
2 () f(n) 1 () 0 = ( )
20
21
Some Properties
Transitivity
() = = = = = = = = = = = =
= = =
Reflexivity
=
=
Symmetry
= ( = ( )
22
Some Properties(contd)
Transpose Symmetry
=
> 0 =
Analogy
= = = = =
Runtime Analysis
Simple Sentences : assign read- write O(1) (constant) Simple Rules : + - * / == > >= < <= O(1) (constant) Sequence of simple rules/sentences : O(1) Loops : for do while loop body cost* time loop executes Conditional sentences : if O(max(T(body1),T(body2)) A part of program that executes many times has the most cost
24
a=b;
Constant time => 1
sum=0;
for (i=1;i<=n ; i++) sum+=n; Asymptotic Complexity : O(n)
25
1=
=1 =1 =1
1
+ 1 2
1 =
=0
1=
=1 =1 =1
= 2
Asymptotic Complexity : 2
26
27
Binary Search
How Many Items Are Checked? //return position of element in sorted array of size n with value k int BS(int array[],int n,int k) { int l=-1; int r=n; //l and r are beyond array bounds while(r!=l+1) // stop when l and r meet { int i= (l+r)/2; if (k<array[i]) r=i; //left half; if (k==array[i]) return i; //found if (k>array[i]) l=i; //right half; } return n; // not found; }
29
Example : -2,11,-4,13,-5,-2 Answer : A[2]+A[3]+A[4] = 20 We check four different algorithms with , log , 2 , (3 ) Complexities.
30
Algorithm 1
1 , , , +1 , ,
int maxSum=0; for (int i=0;i<a.size();i++) (
=0 =
O(1)
1 1
)
1
for (int j=i;j<a.size();j++) int thisSum=0; for (int k=i;k<=j;k++) thisSum+=a[k]; if(thisSum > maxSum) maxSum=thisSum; return maxSum; Time Complexity : (3 )
(
=
( ))
O(1) O(j-i) O(1) O(1) O(1) O(1)
31
Algorithm 2
Main Idea : if we have sum of numbers from i to j-1,we can have i to j in constant time so we can omit the inner loop and the complexity decreases to (2 ) int maxSum=0; for (int i=0;i<a.size();i++) { int thissum=0; for (int j=i;j<=a.size();j++) { thisSum+=a[j]; if (thisSum>maxSum) maxSum=thisSum; } } return maxSum;
32
Algorithm 3
Use divide and conquer method. We divide the input into 2 nearly equal parts. The answer can be found either in right half or left half or a part of it is in left half and a part is in right half. Example : Left Half ||| Right Half
1 -3 5 2 Left Max: 7 ||| MAX:14 -1 2 6 -2 Right Max: 8
We examine each part with recursion. For finding the maximum which is a part of both halves, we find the maximum in the end of left half and the beginning of right half. ( O(n) ) T(n) =
1 2T(n/2)+n n=1 n>1
Algorithm 4
int maxSum=0,thisSum=0; for (int j=0;j<a.size();j++) { thisSum+=a[j]; if (thisSum>maxSum) maxSum=thisSum; else if (thisSum<0) thisSum=0; } return maxSum; The sequence cant start with a negative number. The sequence cant contain a negative prefix. So if the sum of is less than 0 we can move i forward to j+1(and change thisSum to 0) So the complexity is O(n)
34
Summary
We gave 4 algorithms for a problem,with these complexities : 3 2 log For a problem with n = 106 the first algorithm wont answer in our lifetime, but the last one will answer in less than 1 second.
35
36
37
Merge Sort
MergeSort(A[i..j]) if (i<j) { mid=(i+j)/2; MergeSort(A[i..mid]); MergeSort(A[mid+1j]); Merge(A[i..mid],A[mid+1,j]); } T(n)=2T(n/2)+n
38
Proving
Guess : T(n) = O(n log n) We should prove there exists a c such that T(n) <= cn log n T(n) = 2T(n/2)+n<=2(c(n/2)log(n/2))+n T(n) <=cn logn(n/2)+n = cn log(n) cn log 2 +n = cn log n cn+n => T(n)<= cn log n prove =
2
+T
Wrong guess : T(n) <= cn Right guess : T(n) <= cn-b b>=1
39
+ 1 is O(n)
Changing Variables
= 2
2 = = 2
2 2 2
+ log
40
41
Factorial
int factorial(int n){ if(n<=1) return 1; else return n*factorial(n-1); }
Hanoi Tower
void tower(int n, char A,char B,char C) { If(n==1) move(A,B); else { tower(n-1,A,C,B); move (A,B); tower(n-1,C,B,A); } } Hanoi(n,A,B,C):: Hanoi(n-1,A,C,B) Hanoi(1,A,B,C) Hanoi(n-1,C,B,A) T(n)=2T(n-1)+c =4T(n-2)+2c+c =8T(n-3)+4c+2c+c = =21 1 + 22 + + 2 + 1 = (2 1) =(2 )
43
Example
2 2 2 2 4 2 8 = 2 (1 + 1 1 1 + + + log ) 2 22 2
= (2 )
44
Master Theorem
T(n) = a T(n/b)+f(n) a>=1 , b>1 > 0
() = (log )
= (log )
= (log )
= (log log )
= log + = ( )
()
(c<1)
45
Examples
+ 1 = 3 = 9, = 3, = , log = 2 = 2 = (2 ) = 9 First state
Second state
Third state
46
Example
= 7 + 2 2 = 4 + log 2 = 4 + 2 2 = 3 + 3 2 = 7(/3) + 2
+ 2 3 = 3 + 3 = 1 + = 8
47
Example
= 7 + 2 2 = 4 + log 2 = 4 + 2 2 = 3( ) + 3 2 = 7 + 2 3 = 8 + 2 3 = 3 + 3 = 1 + First state First state (log 7 ) (2 )
Second State ( )
Third State Third State (2 ) (2 )
48
Example : Combination
Comb(m,n) = Comb(m-1,n-1)+Comb(m,n-1) Comb(0,n)=Comb(i,i)=1 int Comb(int m,int n) { if(m==n || m==0 ) return 0; return Comb(m-1,n-1)+Comb(m,n-1); }
49
Example : Combination
T(m,n)=T(m-1,n-1)+T(m,n-1) T(n,n) = T(n,0)=1
??????
50
Examples
2 = + + , 1 = 3 3 = + + , 1 = = + + , 1 =
51