Download presentation
Presentation is loading. Please wait.
1
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song
2
Divide-and-Conquer It is an algorithm design technique Example: find the max of n elements Iteration Method: Sequential So, we need n-1 comparison Divide-and-conquer Find out the max of each half, then compare Let the number of comparisons is C(n), and n = 2 k then C(n) = { 0, (n=1) 2 C(n/2) + 1 (n>1) This way to represent C(n) is called Recurrence Relation.
3
Recurrence Relation We need the recurrence relation to measure the complexity of the algorithm. How to solve it? Repeated Substitution Guess it, then verify the solution by induction proof. One more example: Binary Search
4
Binary Search Binary Search is to search some element from a SORTED list. It works by repeatedly checking the middle point of this list. If the element is bigger than the middle point, the first half of the list will be dumped, otherwise dump the second half. The algorithm gets its name from the way we always deal with half of the remaining list. The recurrence relation is: (number of comparisons) C(n) = 1 (when n=1) C(n) = C(n/2) + 1 (when n>1, to simplify the question, we make n = 2 k ) With the Repeated Substitution, we figure out the exact solution is C(n) = 1+log 2 n (which means the time complexity is O(log 2 n) Make some extra points with induction proof of this solution.
5
Bubble Sort Bubble sort is a simple sorting algorithm. It works by repeatedly stepping through the list to be sorted, comparing two items at a time 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. It gets its name from the way smaller elements "bubble" to the top of the list. Example: sort a list [5, 1, 4, 2, 3] The recurrence relation is: (number of comparisons) C(n) = 0 (when n=1) C(n) = C(n-1) + (n-1) (when n>1) Worst case is O(n 2 ). Solve it with repeated substitution and verify the solution with induction proof?
6
Merge Sort Merge sort preserves the input order of equal elements in the sorted output. It is an example of the divide and conquer technique. It was invented by John Von Neumann in 1945. Example: sort a list [5, 2, 8, 1, 7, 4, 6, 3] Basic Steps: Divide the unsorted list into two sublists of about half the size Divide each of the two sublists recursively until we have list sizes of length 1, in which case the list itself is returned Merge the two sublists back into one sorted list.
7
Merge Sort (Cont.) The recurrence relation is: (number of comparisons) C(n) = 0 (when n=1) C(n) = 2*C(n/2) + (n-1) (when n>1) For time complexity, no best or worst case It is always about O(n*log n ). Solve it with repeated substitution and verify with induction proof?
8
More Sorting Algorithms Heap Sort Quick Sort Integer Sort Bucket sort Radix sort
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.