Computability O(n) exercises. Searching. Shuffling Homework: review examples. Research other shuffling.

Slides:



Advertisements
Similar presentations
Efficiency of Algorithms Csci 107 Lecture 6-7. Topics –Data cleanup algorithms Copy-over, shuffle-left, converging pointers –Efficiency of data cleanup.
Advertisements

Analysis of Algorithms
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
Analysys & Complexity of Algorithms Big Oh Notation.
Computability Start complexity. Motivation by thinking about sorting. Homework: Finish examples.
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.
Chapter 1 – Basic Concepts
CS 206 Introduction to Computer Science II 12 / 09 / 2009 Instructor: Michael Eckmann.
Algorithmic Complexity Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Chapter 3 Growth of Functions
1 CSE1301 Computer Programming: Lecture 28 List Sorting.
Lecture 25 Selection sort, reviewed Insertion sort, reviewed Merge sort Running time of merge sort, 2 ways to look at it Quicksort Course evaluations.
Object (Data and Algorithm) Analysis Cmput Lecture 5 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this.
CS 206 Introduction to Computer Science II 12 / 03 / 2008 Instructor: Michael Eckmann.
1 Data Structures A program solves a problem. A program solves a problem. A solution consists of: A solution consists of:  a way to organize the data.
 Last lesson  Arrays for implementing collection classes  Performance analysis (review)  Today  Performance analysis  Logarithm.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Algorithm Analysis (Big O)
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Aaron Bauer Winter 2014.
COMP s1 Computing 2 Complexity
Asymptotic Notations Iterative Algorithms and their analysis
For Wednesday Read Weiss chapter 3, sections 1-5. This should be largely review. If you’re struggling with the C++ aspects, you may refer to Savitch, chapter.
Analysis CS 367 – Introduction to Data Structures.
CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
Analysis of Algorithms
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Data Structure Introduction.
Complexity, etc. Homework. Comparison to computability. Big Oh notation. Sorting. Classwork/Homework: prepare presentation on specific sorts. Presentation.
CS 206 Introduction to Computer Science II 04 / 22 / 2009 Instructor: Michael Eckmann.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
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.
Asymptotic Behavior Algorithm : Design & Analysis [2]
Foundations of Algorithms, Fourth Edition
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
ADS 1 Algorithms and Data Structures 1 Syllabus Asymptotical notation (Binary trees,) AVL trees, Red-Black trees B-trees Hashing Graph alg: searching,
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Algorithm Analysis (Big O)
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Sorting Algorithms Written by J.J. Shepherd. Sorting Review For each one of these sorting problems we are assuming ascending order so smallest to largest.
Computability Sort homework. Formal definitions of time complexity. Big 0. Homework: Exercises. Searching. Shuffling.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Sorting – Lecture 3 More about Merge Sort, Quick Sort.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
Analysis of Algorithms
COP 3503 FALL 2012 Shayan Javed Lecture 15
DATA STRUCTURES Introduction: Basic Concepts and Notations
Teach A level Computing: Algorithms and Data Structures
Searching CSCE 121 J. Michael Moore.
Algorithm design and Analysis
Complexity Present sorting methods. Binary search. Other measures.
CS 201 Fundamental Structures of Computer Science
Analysys & Complexity of Algorithms
Searching CLRS, Sections 9.1 – 9.3.
Programming and Data Structure
Estimating Algorithm Performance
Big O notation f = O(g(n)) c g(n)
Presentation transcript:

Computability O(n) exercises. Searching. Shuffling Homework: review examples. Research other shuffling.

Recall O() notation has <= –A function f(n) is of order Big Oh g(n) if there exists a constant c and a number n 0 such that f(n) = n o() notation has strictly < –A function f(n) is of order little oh g(n) if there exists a constant c and a number n 0 such that f(n) = n. This is equivalent to limit f(n)/g(n) going to 0 as n goes to infinity.

O(n) and o(n) homework Prove or disprove: 100*n = O(n) n 3 = O(n) 100*n = o(n) e n = o(3 n )

Searching Given an array of n items, with keys numbers, arranged in order by keys, what is a method for finding the item with key k? –NOTE: assumption that array is in order! How do you look up a key? How do you look up a name in a list?

Linear search check the first one, then the second, etc. This COULD take n steps. –Probably claim that on average, it takes n/2 steps. So, linear search is O(n). –Average n/2 is still O(n). Remember bounds are including a coefficient. Can we do better?

Binary search Idea is to do a test that will halve the search space –best strategy if the answers are equally likely. Compare value to key of item in middle of array –if equal, done –if less, search lower part –if greater, search upper part

iterative binary search function binsearch(arr,value,low,high) { while (low<=high) { var mid:int; mid = (low+high) / 2; // will take floor if (arr[mid]>value) { high = mid-1; } else if (arr[mid]<value) {low = mid+1;} else {return mid;} } return -1; }

recursive binary search function binsearch(arr,value,low, high) { var mid:int; if (low>high) return -1; mid = (low+high)/2; if (arr[mid]>value) return binsearch(arr,value,low,mid-1) else if (arr[mid]<value) return binsearch(arr,value,mid+1,high); else return mid; }

Costs of recursion In any programming language, a recursive function call, or any function call, takes some steps –set up the call, environment, place block of data on a stack –return: pops the stack: replaces the block of data with a value

Analysis Focus on number of compare steps. –recursive versus iteration could be significant, but it is a constant factor in terms of problem size. Problem size [at least] halved each time. –n  n/2  n/4… If n = 2 m, there would be m steps Steps (compares) are bounded by log 2 (n)

Geography game Three categories –man-made –natural –political Ask true-false questions Strategy: reduce the [remaining] search space along various/multiple dimensions. –Bifurcate the search space

Shuffling How to shuffle a set of n items? Definition: a good shuffle is one in which any element in the original array could end up in any position in the shuffled array with equal probability.

Fisher-Yates-Knuth Look at end of the array. Pick slot randomly from the rest of the array. Swap. Shrink array down 1 position. Repeat: that is, pick slot randomly from the rest of the array. Swap with element at top-1 position. Continue.

Fisher Yates Knuth function fyk(arr) { i=arr.length – 1; while(i>0) { s = Math.floor(Math.random()*(i+1)); // random int 0 to i swap(arr,s,i); i--; } } function swap(arr,a,b) { hold = arr[a]; arr[a] = arr[b]; arr[b] = hold; }

Complexity of FYK shuffle Number of steps = n O(n) Note also done in place –use one extra hold value, but space n versus space n+1 is considered the same. Constants and coefficents do not matter. Note: assumes Math.random takes a constant amount of time.

Homework Research on-line and report more information on Fisher-Yates-Knuth –order of compares, average, worst case Other ways of shuffling cards? –7 is enough (Kruskal). What is the measure of goodness? Other ways to search? Search other spaces?