CSC 172 DATA STRUCTURES.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Algorithm Design Techniques: Divide and Conquer. Introduction Divide and conquer – algorithm makes at least two recursive calls Subproblems should not.
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.
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.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
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 –
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.
1 Divide and Conquer Binary Search Mergesort Recurrence Relations CSE Lecture 4 – Algorithms II.
1 More Sorting radix sort bucket sort in-place sorting how fast can we sort?
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
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.
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.
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.
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.
Sorting and Runtime Complexity CS255. Sorting Different ways to sort: –Bubble –Exchange –Insertion –Merge –Quick –more…
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Merge Sort 1/12/2018 5:48 AM Merge Sort 7 2   7  2  2 7
Advanced Sorting.
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.
Merge Sort 1/12/2018 9:44 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Sorts, CompareTo Method and Strings
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Quick Sort and Merge Sort
Lecture 4 Divide-and-Conquer
Chapter 4 Divide-and-Conquer
Quicksort The basic quicksort algorithm is recursive Chosing the pivot
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,
Divide-And-Conquer-And-Combine
Divide and Conquer.
Algorithm Design Methods
Objectives Introduce different known sorting algorithms
Sorting Chapter 13 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Growth Functions Algorithms Lecture 8
CS 3343: Analysis of Algorithms
Visit for more Learning Resources
Unit-2 Divide and Conquer
Punya Biswas Lecture 15 Closest Pair, Multiplication
Richard Anderson Lecture 13 Divide and Conquer
CSE 373 Data Structures and Algorithms
CS200: Algorithms Analysis
Sub-Quadratic Sorting Algorithms
Merge Sort 2/23/ :15 PM Merge Sort 7 2   7  2   4  4 9
Divide-and-Conquer The most-well known algorithm design strategy:
CS 3343: Analysis of Algorithms
Divide-and-Conquer 7 2  9 4   2   4   7
CSE 373 Data Structures and Algorithms
Merge Sort 4/10/ :25 AM Merge Sort 7 2   7  2   4  4 9
Algorithms: Design and Analysis
CSC 380: Design and Analysis of Algorithms
Quick-Sort 4/25/2019 8:10 AM Quick-Sort     2
Divide & Conquer Sorting
CSC 380: Design and Analysis of Algorithms
Algorithms Recurrences.
Merge Sort 5/30/2019 7:52 AM Merge Sort 7 2   7  2  2 7
CS203 Lecture 15.
Divide and Conquer Merge sort and quick sort Binary search
Divide-and-Conquer 7 2  9 4   2   4   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 1XXX 0XXX 1XXX 0XXX 0XXX 1XXX 1XXX 1XXX 0XXX 1XXX Examine bits from left to right Sort array with the respect to the leftmost bit 1XXX 0XXX 1XXX 0XXX 0XXX 1XXX 1XXX 1XXX 0XXX 1XXX

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

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

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

Mergesort Quicksort Maximum Subsequence Fast Fourier Transform 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 187 84 -23 -93 97 58 -53 26 59 -41 31

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 x a b ma mb ma , mb or: mc

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 “mc”

How to find mc? 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 mc mc mclower mcup

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 𝑇 𝑁 =𝑎𝑇 𝑁 𝑏 +𝑂 𝑁 𝑘 ;𝑎≥1,𝑏>1 𝑇 𝑁 =𝑎𝑇 𝑁 𝑏 +𝑂 𝑁 𝑘 ;𝑎≥1,𝑏>1 𝑇 𝑁 = 𝑂 𝑁 log 𝑏 𝑎 𝑖𝑓𝑎> 𝑏 𝑘 𝑂 𝑁 𝑘 log𝑁 𝑖𝑓𝑎= 𝑏 𝑘 𝑂 𝑁 𝑘 𝑖𝑓𝑎< 𝑏 𝑘

because by telescoping 𝑇 𝑁 =𝑎𝑇 𝑁 𝑏 +𝑂 𝑁 𝑘 𝑇 𝑁 =𝑇 𝑏 𝑚 = 𝑎 𝑚 𝑖=0 𝑚 𝑏 𝑘 𝑎 𝑖 𝑇 𝑁 = 𝑂 𝑁 log 𝑏 𝑎 𝑖𝑓𝑎> 𝑏 𝑘 𝑂 𝑁 𝑘 log 𝑏 𝑁 𝑖𝑓𝑎= 𝑏 𝑘 𝑂 𝑁 𝑘 𝑖𝑓𝑎< 𝑏 𝑘 by telescoping