CSC 172 DATA STRUCTURES. THEORETICAL BOUND  Many good sorting algorithms run in O(nlogn) time.  Can we do better?  Can we reason about algorithms not.

Slides:



Advertisements
Similar presentations
Garfield AP Computer Science
Advertisements

AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
CS4413 Divide-and-Conquer
Algorithm Design Techniques: Divide and Conquer. Introduction Divide and conquer – algorithm makes at least two recursive calls Subproblems should not.
Divide and Conquer. Recall Complexity Analysis – Comparison of algorithm – Big O Simplification From source code – Recursive.
Recursion & Merge Sort Introduction to Algorithms Recursion & Merge Sort CSE 680 Prof. Roger Crawfis.
Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search.
Divide and Conquer Chapter 6. Divide and Conquer Paradigm Divide the problem into sub-problems of smaller sizes Conquer by recursively doing the same.
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
Recursion CSC 220: Data Structure Winter Introduction A programming technique in which a function calls itself. One of the most effective techniques.
CSC2100B Quick Sort and Merge Sort Xin 1. Quick Sort Efficient sorting algorithm Example of Divide and Conquer algorithm Two phases ◦ Partition phase.
© 2004 Goodrich, Tamassia QuickSort1 Quick-Sort     29  9.
Quicksort CSC 172 SPRING 2002 LECTURE 13. Quicksort The basic quicksort algorithm is recursive Chosing the pivot Deciding how to partition Dealing with.
CSC 2300 Data Structures & Algorithms March 27, 2007 Chapter 7. Sorting.
General Announcements Project Due Friday, 1/30 Labs start Wednesday & Thursday – Java review – Weiss 1.19, 1.20 – You may show up & hand in Workshops.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
DATA STRUCTURES INTRODUCTION CSC 172 SPRING 2002.
CHAPTER 11 Sorting.
CS2420: Lecture 10 Vladimir Kulyukin Computer Science Department Utah State University.
Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders  Project 2 (polynomial linked-list) is due, tomorrow  Wed, 18 th 5PM  Computer Science Office –
5 - 1 § 5 The Divide-and-Conquer Strategy e.g. find the maximum of a set S of n numbers.
TTIT33 Algorithms and Optimization – Dalg Lecture 2 HT TTIT33 Algorithms and optimization Lecture 2 Algorithms Sorting [GT] 3.1.2, 11 [LD] ,
Radix Sort CSC 172 SPRING 2004 LECTURE 25. Theoretical bound? A * n logn = t A* 524,288 * 19 = 111 A = 1.1*10^-5 1.1*10^-5 * 134,217,728 * 27 = 40,380.
CSC 2300 Data Structures & Algorithms January 30, 2007 Chapter 2. Algorithm Analysis.
Design and Analysis of Algorithms – Chapter 51 Divide and Conquer (I) Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
1 Divide and Conquer Binary Search Mergesort Recurrence Relations CSE Lecture 4 – Algorithms II.
ALGORITHM ANALYSIS AND DESIGN INTRODUCTION TO ALGORITHMS CS 413 Divide and Conquer Algortihms: Binary search, merge sort.
Algorithm Analysis. Algorithm An algorithm is a clearly specified set of instructions which, when followed, solves a problem. recipes directions for putting.
1 More Sorting radix sort bucket sort in-place sorting how fast can we sort?
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
1 Lecture 16: Lists and vectors Binary search, Sorting.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
1 Designing algorithms There are many ways to design an algorithm. Insertion sort uses an incremental approach: having sorted the sub-array A[1…j - 1],
File Organization and Processing Week 13 Divide and Conquer.
Sorting Fun1 Chapter 4: Sorting     29  9.
Bucket & Radix Sorts. Efficient Sorts QuickSort : O(nlogn) – O(n 2 ) MergeSort : O(nlogn) Coincidence?
Sorting What makes it hard? Chapter 7 in DS&AA Chapter 8 in DS&PS.
Searching and Sorting Recursion, Merge-sort, Divide & Conquer, Bucket sort, Radix sort Lecture 5.
CSE 421 Algorithms Lecture 15 Closest Pair, Multiplication.
Divide and Conquer Strategy
QUICKSORT 2015-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
Algorithm Design Techniques An Example The Problem Algorithm 1: Cubic Time Algorithm 2: Quadratic Time Algorithm 3: O(n log n) Time Algorithm 4: Linear.
Towers of Hanoi Move n (4) disks from pole A to pole B such that a larger disk is never put on a smaller disk A BC ABC.
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
Quicksort CSC 172. Quicksort The basic quicksort algorithm is recursive Chosing the pivot Deciding how to partition Dealing with duplicates Wrong decisions.
Sorting Lower Bounds n Beating Them. Recap Divide and Conquer –Know how to break a problem into smaller problems, such that –Given a solution to the smaller.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Sorting and Runtime Complexity CS255. Sorting Different ways to sort: –Bubble –Exchange –Insertion –Merge –Quick –more…
CS6045: Advanced Algorithms Sorting Algorithms. Sorting Input: sequence of numbers Output: a sorted sequence.
Merge Sort 1/12/2018 5:48 AM Merge Sort 7 2   7  2  2 7
Advanced Sorting 7 2  9 4   2   4   7
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
CSC 172 DATA STRUCTURES.
Quick-Sort 9/12/2018 3:26 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Algorithm Design Methods
Objectives Introduce different known sorting algorithms
CS 3343: Analysis of Algorithms
Unit-2 Divide and Conquer
Punya Biswas Lecture 15 Closest Pair, Multiplication
Sub-Quadratic Sorting Algorithms
Merge Sort 2/23/ :15 PM Merge Sort 7 2   7  2   4  4 9
Quick-Sort 2/25/2019 2:22 AM Quick-Sort     2
Quick-Sort 4/8/ :20 AM Quick-Sort     2 9  9
Divide-and-Conquer 7 2  9 4   2   4   7
Quick-Sort 4/25/2019 8:10 AM Quick-Sort     2
Divide & Conquer Sorting
Merge Sort 5/30/2019 7:52 AM Merge Sort 7 2   7  2  2 7
Divide-and-Conquer Divide: the problem into subproblems
Presentation transcript:

CSC 172 DATA STRUCTURES

THEORETICAL BOUND  Many good sorting algorithms run in O(nlogn) time.  Can we do better?  Can we reason about algorithms not yet invented?

THEORETICAL BOUND  We can think of any comparison based sorting algorithm as a decision tree

THEORETICAL BOUND  A binary tree of depth d has at most 2^d leaves.  A binay tree with L leaves must have depth at lease log(L)  There are n! Arrangements of n items  A tree with n! Leaves must have depth at least log(n!)  log(n!) is at least Ω (n log n)

THEORETICAL BOUND log(n!) = log(n(n-1)(n-2)...(2)(1)) = log(n) + log(n-1) log(2) + log(1) >= log(n) + log(n-1) log(n/2) >= (n/2) log(n/2) >=(n/2) log(n) – (n/2) = Ω (n log n)

EXAMPLE A post office routes mail letters are sorted into separate bags for different geographical areas, each of these bags is itself sorted into batches for smaller sub-regions, and so on until they are delivered. Radix Sort

RADIX SORT  Radix sort considers the structure of the keys  Assume keys are represented in base M  Sorting is done by comparing bits in the same position

RADIX EXCHANGE SORT  Examine bits from left to right  Sort array with the respect to the leftmost bit 1XXX 0XXX 1XXX

RADIX EXCHANGE SORT  Scanning Partition REPEAT ; scan top down to find a “1” scan bottom up to find a “0” exchange UNTIL: scans cross 1XXX 0XXX 1XXX 0XXX 1XXX 0XXX 1XXX

RADIX EXCHANGE SORT  Examine bits from left to right  Sort array with the respect to the leftmost bit 1XXX 0XXX 1XXX 0XXX 1XXX

RADIX EXCHANGE SORT  Examine bits from left to right  Sort array with the respect to the leftmost bit 1XXX 0XXX 1XXX 0XXX 1XXX TIME: O(b N)

DIVIDE AND CONQUER Mergesort Quicksort Maximum Subsequence Fast Fourier Transform

DIVIDE AND CONQUER Divide: Solve a subproblem by recursively calling on a subset of the data Conquer: The solution to the larger problem is formed from the solutions to the sub problems.

Example  One dimensional pattern recognition  Input: a vector x of n floating point numbers  Output: the maximum sum found in any contiguous subvector of the input.  X[2..6] or

Obvious solution //check all pairs int sum; int maxsofar = 0; for (int i = 0; i<x.length;i++) for (int j = i; j<x.length;j++){ sum = 0; for (int k = i;k<=j;k++) sum += x[k]; maxsofar = max(sum,maxsofar); }

A better solution // check all pairs int sum; int maxsofar = 0; for (int i = 0; i<x.length;i++) { sum = 0; for (int j = i; j<x.length;j++){ sum += x[k]; // the sum of x[i..j] maxsofar = max(sum,maxsofar); }

Divide & Conquer  To solve a problem of size n, recursively solve two sub-problems of size of size n/2, and combine their solutions to yield a solution to the complete problem.

D&C for LCS ab x mama mbmb m a, m b or: mcmc

Recursive D&D LCS public int LCS(int[] x){ return LCS(x,0,x.length-1); } public int LCS(int[] x, int low, high ){ // the hard part

Recursive D&C LCS public int LCS(int[] x, int low, int high){ if (low>high) return 0; if (low == high) return max(0,x[low]); int mid = (low + high) /2; return max(LCS(x,low,mid), LCS(x,mid+1,high)); }// still need to do “m c ”

How to find m c ?  Note that mc consists of two parts  The part starting at the boundary and reaching up  The part ending at the boundary and reaching down  The sum of these is m c mcmc m clower m cup

public int LCS(int[]x,int low,int high){ if (low>high) return 0; if (low == high) return max(0,x[low]); int mid = (low + high) /2; int umax = findUmax(x,mid+1,upper); int lmax = findLmax(x,low,middle); return max(LCS(x,lower,middle), LCS(middle+1,upper), lmax + umax); }

findLmax int findLmax(int[]x,int low,int mid){ double lmax = 0, sum = 0; for (int j = mid;j>=low;j--){ sum+=x[j]; lmax = max(lmax,sum); } return lmax; } // Run Time? In terms of middle-lower?

findUmax int findLmax(int[]x, int mid1,int high){ int umax = 0, sum = 0; for (int j = midd1;j<=high;j++){ sum+=x[j]; umax = max(lmax,sum); } return umax; } // Run Time? In terms of high-mid1?

Runtime of Div&Conq Please : Read p 449 & 450 of Weiss

RUNTIME T(N) = 2T(N/2) + O(N) is O(N log N)

In General

by telescoping because