CS 2430 Day 30. Announcements Quiz #5: 4/19 Agenda Big O Searching –Linear search.

Slides:



Advertisements
Similar presentations
Growth-rate Functions
Advertisements

Introduction to Computer Science Theory
Analysis of Algorithms CS Data Structures Section 2.6.
Algorithmic Complexity Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
Introduction to Analysis of Algorithms
1 CSE1301 Computer Programming Lecture 31: List Processing (Search)
1 Algorithm Efficiency, Big O Notation, and Role of Data Structures/ADTs Algorithm Efficiency Big O Notation Role of Data Structures Abstract Data Types.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Analysis of Algorithm.
Analysis of Algorithms CPS212 Gordon College. Measuring the efficiency of algorithms There are 2 algorithms: algo1 and algo2 that produce the same results.
CS Discrete Mathematical Structures Mehdi Ghayoumi MSB rm 132 Ofc hr: Thur, 9:30-11:30a.
Abstract Data Types (ADTs) Data Structures The Java Collections API
1 MT258 Computer Programming and Problem Solving Unit 9.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Searching – Linear and Binary Searches. Comparing Algorithms Should we use Program 1 or Program 2? Is Program 1 “fast”? “Fast enough”? P1P2.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
Week 2 CS 361: Advanced Data Structures and Algorithms
Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
{ CS203 Lecture 7 John Hurley Cal State LA. 2 Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Analysis of Algorithms
Complexity of algorithms Algorithms can be classified by the amount of time they need to complete compared to their input size. There is a wide variety:
Algorithm Evaluation. What’s an algorithm? a clearly specified set of simple instructions to be followed to solve a problem a way of doing something What.
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
CSE1301 Computer Programming: Lecture 26 List Processing (Search)
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
L. Grewe.  An array ◦ stores several elements of the same type ◦ can be thought of as a list of elements: int a[8]
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.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
SortingBigOh ASFA AP Computer Science A. Big-O refers to the order of an algorithm runtime growth in relation to the number of items I. O(l) - constant.
Data Structure Introduction.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
1 Algorithms  Algorithms are simply a list of steps required to solve some particular problem  They are designed as abstractions of processes carried.
Sorting.
1 5. Abstract Data Structures & Algorithms 5.6 Algorithm Evaluation.
CS 206 Introduction to Computer Science II 09 / 18 / 2009 Instructor: Michael Eckmann.
Foundations of Algorithms, Fourth Edition
Week 12 - Friday.  What did we talk about last time?  Finished hunters and prey  Class variables  Constants  Class constants  Started Big Oh notation.
تصميم وتحليل الخوارزميات عال311 Chapter 3 Growth of Functions
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 4 Introduction.
Searching Topics Sequential Search Binary Search.
Big O David Kauchak cs302 Spring Administrative Assignment 1: how’d it go? Assignment 2: out soon… Lab code.
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
Section 1.7 Comparing Algorithms: Big-O Analysis.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
1 Algorithms Searching and Sorting Algorithm Efficiency.
Algorithm Complexity Analysis (Chapter 10.4) Dr. Yingwu Zhu.
Section 1: Problem solving AQA Computing A2 © Nelson Thornes 2009 Examples of problems with different time complexities Section 1.2: Comparing algorithms.
GC 211:Data Structures Algorithm Analysis Tools
Lesson Objectives Aims Understand the following: The big O notation.
Sorting by Tammy Bailey
Building Java Programs
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Introduction to Data Structures
Chapter 2.
Programming and Data Structure
Analyzing an Algorithm Computing the Order of Magnitude Big O Notation
Intro to Computer Science CS1510 Dr. Sarah Diesburg
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
CMPT 120 Lecture 33 – Unit 5 – Internet and Big Data
COMPUTER 2430 Object Oriented Programming and Data Structures I
Algorithm Analysis How can we demonstrate that one algorithm is superior to another without being misled by any of the following problems: Special cases.
Data Structures & Programming
Presentation transcript:

CS 2430 Day 30

Announcements Quiz #5: 4/19

Agenda Big O Searching –Linear search

Computational complexity and big O notation

Algorithm efficiency Need a way to determine how “efficient” an algorithm so we can compare algorithms Could try timings, but they can vary a lot depending on the input and/or hardware Another approach is to count the number of steps required to process N elements

Critical steps To get a feel for the behavior of the algorithm, we just count the dominant instructions –for a sort, it would be the comparison of array elements –for a search, it would be the comparison of an array element to the item being searched for The count will be some function of N, i.e., the number of items being processed

Big O The “Big O” of an algorithm is the order of the fastest growing term of the number of critical steps performed (as a function of N) Examples: –2N / = O(N) –42 = 42N 0 = O(1) –100N 2 + 7N / = O(N 2 ) –Log N + 31 = O(Log 2 N), where “Log 2 N” is the base-2 logarithm of N

Big O examples Search an unsorted array? O(N) Push onto a Stack ? O(1) Remove from a Bag ? O(N)

Matrix multiplication

public class Matrix { private double[][] data; // matrix entries private int n; // dimension of matrix, assume square... public Matrix times(Matrix rhs) { Matrix lhs = this; Matrix res = new Matrix(lhs.n); // creates empty square Matrix for (int i = 0; i < res.n; i++) for (int j = 0; j < res.n; j++) for (int k = 0; k < lhs.n; k++) res.data[i][j] += lhs.data[i][k] * rhs.data[k][j]; return res; } } Let N denote the matrix dimension, i.e., n What is the big O of times() ? O(N 3 )

Is it possible to do better than O(N 3 ) for matrix multplication?

Yes! Middle school matrix multiplication algorithmO(N 3 ) Strassen algorithm (1969)O(N ) Coppersmith–Winograd algorithm (1987, 1990) O(N ) Williams algorithm (2011)O(N ) (20??)O(N 2 ) – conjectured to be possible!

Bigger big O and beyond! Agrawal–Kayal–Saxena primality test algorithm (2002) O(N 10 ) –The exponent continues to decrease Traveling Salesman problem (NP-complete) O(N!) – brute force O(2 N ) – “Dynamic programming” Halting problem (undecidable)Cannot be solved by any computer given any amount of time. No big O! Cannot even be solved by humans (in general)

Searching

Linear search private static int lsearch(Comparable a[], int n, E x) { for (int i = 0; i < n; i++) if (a[i].compareTo(x) == 0) return i; return -1; } Can put this method in any container class that needs it What is the worst case big O? O(N)

Linear search on average What is the average case efficiency of linear search? Given a randomly sorted array that contains the target element X, how many comparisons needed? Answer = 1*Pr[X is in position 0] + 2*Pr[X is in position 1] + ∙ ∙ ∙ + N*Pr[X is in position N – 1] = ???

How to get the answer! ???= 1*Pr[X is in position 0] + 2*Pr[X is in position 1] + ∙ ∙ ∙ + N*Pr[X is in position N – 1] = 1 / N + 2 / N + ∙ ∙ ∙ + N / N = (1 / N) * N*(N+1) / 2 = (N + 1) / 2 = O(N)

Can we do better if the array is sorted?