ADSA: IntroAlgs/1 1 241-423 Advanced Data Structures and Algorithms Objective –introduce algorithm design using basic searching and sorting, and remind.

Slides:



Advertisements
Similar presentations
Recursion Chapter 14. Overview Base case and general case of recursion. A recursion is a method that calls itself. That simplifies the problem. The simpler.
Advertisements

MATH 224 – Discrete Mathematics
Garfield AP Computer Science
CSE Lecture 3 – Algorithms I
CMPT 225 Sorting Algorithms Algorithm Analysis: Big O Notation.
Algorithm Complexity Analysis: Big-O Notation (Chapter 10.4)
ADSA: Sorting/ Advanced Data Structures and Algorithms Objective –examine popular sorting algorithms, with an emphasis on divide and conquer.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 7 Sorting.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
1 CSE1301 Computer Programming Lecture 31: List Processing (Search)
Cmpt-225 Sorting. Fundamental problem in computing science  putting a collection of items in order Often used as part of another algorithm  e.g. sort.
Lecture 3 Aug 31, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis discussion of lab – permutation generation.
Main Index Contents 11 Main Index Contents Selection Sort Selection SortSelection Sort Selection Sort (3 slides) Selection Sort Alg. Selection Sort Alg.Selection.
Analysis of Algorithm.
Lecture 3 Feb 7, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis Image representation Image processing.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Sort, Search, and Running Time.
1 Divide and Conquer Binary Search Mergesort Recurrence Relations CSE Lecture 4 – Algorithms II.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Abstract Data Types (ADTs) Data Structures The Java Collections API
COMP s1 Computing 2 Complexity
Searching – Linear and Binary Searches. Comparing Algorithms Should we use Program 1 or Program 2? Is Program 1 “fast”? “Fast enough”? P1P2.
Week 2 CS 361: Advanced Data Structures and Algorithms
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
1 Sorting Algorithms (Basic) Search Algorithms BinaryInterpolation Big-O Notation Complexity Sorting, Searching, Recursion Intro to Algorithms Selection.
CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,
Chapter 19 Searching, Sorting and Big O
1 CSC 222: Object-Oriented Programming Spring 2012 Searching and sorting  sequential search  algorithm analysis: big-Oh, rate-of-growth  binary search.
1 Programming with Recursion. 2 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making.
Analysis of Algorithms
CSE1301 Computer Programming: Lecture 26 List Processing (Search)
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Big Oh Algorithms are compared to each other by expressing their efficiency in big-oh notation Big O notation is used in Computer Science to describe the.
CSC 211 Data Structures Lecture 13
Data Structure Introduction.
ADSA: Generics/ Advanced Data Structures and Algorithms Objective –to describe basic forms of generic classes, interfaces, and methods for searching.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 5 Generic.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Introduction to Analysis of Algorithms CS342 S2004.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
Algorithm Analysis (Big O)
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
تصميم وتحليل الخوارزميات عال311 Chapter 3 Growth of Functions
Complexity Analysis. 2 Complexity The complexity of an algorithm quantifies the resources needed as a function of the amount of input data size. The resource.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 4 Introduction.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Searching Topics Sequential Search Binary Search.
Algorithm Analysis Lakshmish Ramaswamy. Formal Definitions Big-Oh: T(N) is O(F(N)) if there exists positive constants N 0 and c such that T(N) N 0 Big-Omega:
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
1 Algorithms Searching and Sorting Algorithm Efficiency.
Algorithm Complexity Analysis (Chapter 10.4) Dr. Yingwu Zhu.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Complexity Analysis (Part I)
Algorithm Analysis: Big O Notation
Introduction to Search Algorithms
CSC 222: Object-Oriented Programming
Towers of Hanoi Move n (4) disks from pole A to pole C
COMP 53 – Week Seven Big O Sorting.
Searching & Sorting "There's nothing hidden in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting.
Building Java Programs
Data Structures for Java William H. Ford William R. Topp
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
Complexity Analysis (Part I)
Complexity Analysis (Part I)
Presentation transcript:

ADSA: IntroAlgs/ Advanced Data Structures and Algorithms Objective –introduce algorithm design using basic searching and sorting, and remind students about T() and Big-Oh running time. Semester 2, Intro. to Algorithms

ADSA: IntroAlgs/1 2 Contents 1.Selection Sort 2. An Array Sublist 3.Sequential Search 4.Binary Search 5.System/Memory Efficiency 6.Running Time Analysis 7.Big-Oh Analysis 8.The Timing Class

ADSA: IntroAlgs/ Selection Sort Go through an array position by position, starting at index 0. At the current position, select the smallest element from the rest of the array. Swap it with the value in the current position.

ADSA: IntroAlgs/1 4 An Example

ADSA: IntroAlgs/1 5 Number of passes = size of array – 1 –e.g. made 4 passes over the 5-element arr[] –stopped sorting when finshed comparing arr[3] and arr[4]

ADSA: IntroAlgs/1 6 selectionSort() public static void selectionSort(int[] arr) { // index of smallest elem in sublist int smallIndex; int idx; int n = arr.length; // idx has range 0 to n-2 for (idx = 0; idx < n-1; idx++) { // scan sublist starting at idx smallIndex = idx; :

ADSA: IntroAlgs/1 7 /* j goes through sublist from arr[idx+1] to arr[n-1] */ for (int j = idx+1; j < n; j++) /* if smaller element found, assign smallIndex to that posn */ if (arr[j] < arr[smallIndex]) smallIndex = j; //* swap next smallest elem into arr[idx] int temp = arr[idx]; arr[idx] = arr[smallIndex]; arr[smallIndex] = temp; } } // end of selectionSort()

ADSA: IntroAlgs/1 8 Usage Example // an integer array int[] arr = {66, 20, 33, 55, 53, 57, 69, 11, 67, 70}; // use selectionSort() to order array selectionSort(arr); System.out.print("Sorted: "); for (int i=0; i < arr.length; i++) System.out.print(arr[i] + " "); Sorted:

ADSA: IntroAlgs/ An Array Sublist An array sublist is a sequence of elements whose indices begin at index first and go up to, but not including, last. Uses the notation: [first, last).

ADSA: IntroAlgs/ Sequential Search Begin with a target value and index range [first, last). Go through sublist item by item, looking for target. Return the index position of the match or -1 if target is not in sublist.

ADSA: IntroAlgs/1 11 Examples

ADSA: IntroAlgs/1 12 seqSearch() public static int seqSearch(int[] arr, int first, int last, int target) { /* scan first <= i < last; return index for position if a match occurs */ for (int i = first; i < last; i++) if (arr[i] == target) return i; return -1; // target not found }

ADSA: IntroAlgs/ Binary Search Binary search requires an ordered list, so large sections of the list can be skipped during the search. Calculate midpoint of the current sublist [first,last). If target matches midpoint value, then the search is finished. continued

ADSA: IntroAlgs/1 14 If target is less than midpoint value, look in the lower sublist; otherwise, look in the upper sublist. Continue until target is found or sublist size is 0.

ADSA: IntroAlgs/1 15 Binary Search Case 1 target == midpoint value. The search is complete –mid is the index of the midpoint value

ADSA: IntroAlgs/1 16 Binary Search Case 2 target < midValue –so search in lower sublist Index range becomes [ first, mid ). Set index last to be end of lower sublist ( last = mid ).

ADSA: IntroAlgs/1 17 Binary Search Case 3 target > midValue –so search upper sublist Index range becomes [mid+1,last), because the upper sublist starts to the right of mid Set index first to be front of the upper sublist (first = mid+1).

ADSA: IntroAlgs/1 18 Binary Search Finish The binary search stops when a match is found, or when the sublist is 'empty'. –an empty sublist for [first,last) is when first >= last.

ADSA: IntroAlgs/1 19 A Successful Search: Step 1 target = 23 continued

ADSA: IntroAlgs/1 20 Step 2 target = 23 The sublist is roughly halved. continued

ADSA: IntroAlgs/1 21 Step 3 target = 23 The sublist is roughly halved. continued

ADSA: IntroAlgs/1 22 Failure Example Step 1 target = 4 continued

ADSA: IntroAlgs/1 23 Step 2 target = 4 The sublist is roughly halved. continued

ADSA: IntroAlgs/1 24 Step 3 target = 4 The sublist is roughly halved. continued

ADSA: IntroAlgs/1 25 Step 4 Index range [2,2). first ≥ last, so search fails. The return value is -1.

ADSA: IntroAlgs/1 26 binSearch() public static int binSearch(int arr[], int first, int last, int target) { int mid; // index of midpoint int midValue; // value from arr[mid] // test for nonempty sublist while (first < last) { mid = (first+last)/2; midValue = arr[mid]; :

ADSA: IntroAlgs/1 27 if (target == midValue) return mid; // have a match // determine which sublist to search else if (target < midValue) // search lower sublist; set last last = mid; else // search upper sublist; set first first = mid+1; } return -1; // target not found } // end of binSearch()

ADSA: IntroAlgs/ System/Memory Efficiency System efficiency is how fast an algorithm runs on a particular machine. Memory efficiency is the amount of memory an algorithm uses –if an algorithm uses too much memory, it can be too slow, or may not execute at all, on a particular system.

ADSA: IntroAlgs/ Running Time Analysis Machine-independent algorithm efficiency is measured in terms of the number of operations used in the code. The complexity of the algorithm usually depends on some size measure –usually the size of the input data

ADSA: IntroAlgs/1 30 min() public static int min(int[] arr) // return the smallest elem. in arr[] { int n = arr.length; if (n == 0) { System.out.println("Array has 0 size"); return 0; } else { int min = arr[0]; for (int i = 1; i < n; i++) if (arr[i] < min) min = arr[i]; return min; } input data is the array, arr[]

ADSA: IntroAlgs/1 31 Running Time The number of comparison operations, T(n), required to find the smallest element in an n-element array. T(n) = n-1 T() was explained in "Discrete Maths", part 4 Could count all operations, but T() would still be linear in n.

ADSA: IntroAlgs/1 32 Running Time: Selection Sort Count the number of comparison operations used to sort an array of size n –there are n-1 passes altogether –in the first pass there are n-1 comparisons –in the 2 nd pass, n-2 comparisons,... T(n) = (n-1) + (n-2) = n(n-1)/2 = n 2 /2 - n/2

ADSA: IntroAlgs/1 33 Running Time: seqSearch() Best case: Find target at index 0. T(n) = 1 Worst case: Find target at index n-1 or not finding it. T(n) = n Average case: Average of the number of comparisons to find a target at any position. T(n) = ( n)/n = n(n+1)/2 * (1/n) = (n+1)/2

ADSA: IntroAlgs/1 34 Running Time: binSearch() Best case: Target found at first midpoint. T(n) = 1 Worst case: Length of sublists halves at each iteration. T(n) = (int) log 2 n + 1 Average case: A fancy analysis shows: T(n) = (int) log 2 n

ADSA: IntroAlgs/ Big-Oh Notation Big-Oh, O(n) is a simpler version of T(n) that only uses the 'biggest' term of the T(n) equation, without constants. –e.g. if T(n) = 8n 3 +5n 2 -11n+1, then T(n) is O(n 3 ). –Selection sort is O(n 2 ). –The average case for seqSearch() is O(n). –The worst case for binSearch() is O(log 2 n). O() was explained in "Discrete Maths", part 4

ADSA: IntroAlgs/1 36 Common Big-Oh's Constant time: T(n) is O(1) when its running time is independent of the n value. –e.g. find the smallest value in an ordered n-element array continued

ADSA: IntroAlgs/1 37 Linear: T(n) is O(n) when running time is proportional to n, the size of the data. If n doubles, T() doubles. –e.g. find the smallest value in an unordered n-element array, as in min() continued

ADSA: IntroAlgs/1 38 Quadratic: T(n) is O(n 2 ). If n doubles, T() increases by a factor of 4 –e.g. selection sort Cubic: T(n) is O(n 3 ). Doubling n increases T() by a factor of 8 –e.g. multiplication of two n*n matricies continued

ADSA: IntroAlgs/1 39 Logarithmic: T() is O(log 2 n) or O(n log 2 n). –occurs when the algorithm repeatedly subdivides the data into sublists whose sizes are 1/2, 1/4, 1/8,... of the original size n –e.g. binary search is O(log 2 n) –e.g. quicksort is O(n log 2 n) continued

ADSA: IntroAlgs/1 40 Exponential: T(n) is O(a n ). –These algorithms deal with problems that require searching through a large number of potential solutions before finding an answer. –e.g. the traveling salesman problem mentioned in the Discrete Maths subject

ADSA: IntroAlgs/1 41 T() Graphs

ADSA: IntroAlgs/1 42 T() Equations

ADSA: IntroAlgs/ The Timing Class A class in the Ford&Topp ds.time package.

ADSA: IntroAlgs/1 44 Timing Class Example Timing sortTimer = new Timing(); sortTimer.start();// start timing selectionSort(arr);// sort double timeInSec = sortTimer.stop(); // get sorting time in secs

ADSA: IntroAlgs/1 45 Search Time Comparison Compare sequential and binary search on the folllowing problem: ,999 target list , listSeq (listBin after sorting) search for each inside

ADSA: IntroAlgs/1 46 SearchTimes import java.util.Random; import java.text.DecimalFormat; import ds.util.Arrays; // Ford & Topp packages import ds.time.Timing; public class SearchTimes { public static void main(String[] args) { int ARRAY_SIZE = ; int TARGET_SIZE = 50000; // arrays for searches int[] listSeq = new int[ARRAY_SIZE], listBin = new int[ARRAY_SIZE], targetList = new int[TARGET_SIZE]; :

ADSA: IntroAlgs/1 47 // use Timing object t to compute times Timing t = new Timing(); // random number object Random rnd = new Random(); // format real numbers with 3 dps DecimalFormat fmt = new DecimalFormat("#.000"); :

ADSA: IntroAlgs/1 48 // initialize arrays with random numbers for (int i = 0; i < ARRAY_SIZE; i++) listSeq[i]=listBin[i]= rnd.nextInt( ); // initialize targetList with random numbers for (int i=0; i < TARGET_SIZE; i++) targetList[i] = rnd.nextInt( ); // time seq. search for targets in listSeq t.start(); for (int i = 0; i < TARGET_SIZE; i++) Arrays.seqSearch(listSeq, 0, ARRAY_SIZE, targetList[i]); :

ADSA: IntroAlgs/1 49 double seqTime = t.stop(); System.out.println("Sequential Search takes " + fmt.format(seqTime) + " seconds."); // sort listBin Arrays.selectionSort(listBin); // time binary search for targets in listBin t.start(); for (int i = 0; i < TARGET_SIZE; i++) Arrays.binSearch(listBin, 0, ARRAY_SIZE, targetList[i]); :

ADSA: IntroAlgs/1 50 double binTime = t.stop(); System.out.println("Binary Search takes " + fmt.format(binTime) + " seconds."); System.out.println("Ratio of sequential to binary search time is " + fmt.format(seqTime/binTime)); } // end of main() } // end of SearchTimes class

ADSA: IntroAlgs/1 51 Compilation and Execution Ford and Topp libraries Must add in sorting time.