Analyzing algorithms using big-O, omega, theta First, we analyze some easy algorithms. Most of them have the same running time for all inputs of length.

Slides:



Advertisements
Similar presentations
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Advertisements

Analysis of Algorithms
Intro to Analysis of Algorithms. Algorithm “A sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any.
Dependency Analysis We want to “parallelize” program to make it run faster. For that, we need dependence analysis to ensure correctness.
CIT 596 Complexity. Tight bounds Is mergesort O(n 4 )? YES! But it is silly to use that as your measure More common to hear it as….? What about quicksort?
11.2 Complexity Analysis. Complexity Analysis As true computer scientists, we need a way to compare the efficiency of algorithms. Should we just use a.
CS 3610/5610N data structures Lecture: complexity analysis Data Structures Using C++ 2E1.
Chapter 1 – Basic Concepts
1 ICS 353 Design and Analysis of Algorithms Spring Semester (062) King Fahd University of Petroleum & Minerals Information & Computer Science.
Simple Sorting Algorithms
CS421 - Course Information Website Syllabus Schedule The Book:
How should a computer shuffle?. Intro - 2 Comp 122, Goal  Input: Given n items to shuffle (cards, …)  Output: Return some list of exactly those n items;
Lecture 4 Sept 4 Goals: chapter 1 (completion) 1-d array examples Selection sorting Insertion sorting Max subsequence sum Algorithm analysis (Chapter 2)
CS 280 Data Structures Professor John Peterson. Example: log(N) This is where things get hairy! How would you compute Log 10 (N) in a very approximate.
Professor John Peterson
Fall 2008 Insertion Sort – review of loop invariants.
CS 280 Data Structures Professor John Peterson. Big O Notation We use a mathematical notation called “Big O” to talk about the performance of an algorithm.
CS Main Questions Given that the computer is the Great Symbol Manipulator, there are three main questions in the field of computer science: What kinds.
Data Structures and Algorithms1 Basics -- 2 From: Data Structures and Their Algorithms, by Harry R. Lewis and Larry Denenberg (Harvard University: Harper.
Data Structure Algorithm Analysis TA: Abbas Sarraf
Analysis of Algorithm.
Elementary Data Structures and Algorithms
Simple Sorting Algorithms. 2 Bubble sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them.
Simple Sorting Algorithms. 2 Outline We are going to look at three simple sorting techniques: Bubble Sort, Selection Sort, and Insertion Sort We are going.
Insertion Sort for (int i = 1; i < a.length; i++) {// insert a[i] into a[0:i-1] int t = a[i]; int j; for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1]
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
Introduction to complexity. 2 Analysis of Algorithms Why do we need to analyze algorithms? –To measure the performance –Comparison of different algorithms.
CS 3343: Analysis of Algorithms
MS 101: Algorithms Instructor Neelima Gupta
10/13/20151 CS 3343: Analysis of Algorithms Lecture 9: Review for midterm 1 Analysis of quick sort.
2IL50 Data Structures Fall 2015 Lecture 2: Analysis of Algorithms.
Jessie Zhao Course page: 1.
Section 3.1: Proof Strategy Now that we have a fair amount of experience with proofs, we will start to prove more difficult theorems. Our experience so.
1 COMP3040 Tutorial 1 Analysis of algorithms. 2 Outline Motivation Analysis of algorithms Examples Practice questions.
Asymptotic Analysis-Ch. 3
Coursenotes CS3114: Data Structures and Algorithms Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright ©
Tonga Institute of Higher Education Design and Analysis of Algorithms IT 254 Lecture 2: Mathematical Foundations.
Asymptotic Notation (O, Ω, )
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Data Structure Introduction.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
Probabilistic Analysis and Randomized Algorithm. Average-Case Analysis  In practice, many algorithms perform better than their worse case  The average.
September 9, Algorithms and Data Structures Lecture II Simonas Šaltenis Nykredit Center for Database Research Aalborg University
The Growth of Functions Rosen 3.2. What affects runtime of a program? the machine it runs on the programming language the efficiency of the compiler the.
Sorting. Sorting Terminology Sort Key –each element to be sorted must be associated with a sort key which can be compared with other keys e.g. for any.
Quicksort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
COP 3530 Spring2012 Data Structures & Algorithms Discussion Session Week 5.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Asymptotic Performance. Review: Asymptotic Performance Asymptotic performance: How does algorithm behave as the problem size gets very large? Running.
Spring 2015 Lecture 2: Analysis of Algorithms
CS 150: Analysis of Algorithms. Goals for this Unit Begin a focus on data structures and algorithms Understand the nature of the performance of algorithms.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
CS 162 Intro to Programming II Insertion Sort 1. Assume the initial sequence a[0] a[1] … a[k] is already sorted k = 0 when the algorithm starts Insert.
2IL50 Data Structures Spring 2016 Lecture 2: Analysis of Algorithms.
CSC317 1 So far so good, but can we do better? Yes, cheaper by halves... orkbook/cheaperbyhalf.html.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
CSC263 Tutorial 1 Big-O, Ω, ϴ Trevor Brown
Help Session 3: Induction and Complexity Ezgi, Shenqi, Bran.
CSCI 6212 Design and Analysis of Algorithms Which algorithm is better ? Dr. Juman Byun The George Washington University Please drop this course if you.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting Input: sequence of numbers Output: a sorted sequence.
QuickSort QuickSort Best, Worst Average Cases K-th Ordered Statistic
CS 3343: Analysis of Algorithms
CS 2210 Discrete Structures Algorithms and Complexity
Linear and Binary Search
CS 3343: Analysis of Algorithms
Big-O, Ω, ϴ Trevor Brown CSC263 Tutorial 1 Big-O, Ω, ϴ Trevor Brown
Ack: Several slides from Prof. Jim Anderson’s COMP 202 notes.
Print the following triangle, using nested loops
Quicksort Quick sort Correctness of partition - loop invariant
CS 2210 Discrete Structures Algorithms and Complexity
Presentation transcript:

Analyzing algorithms using big-O, omega, theta First, we analyze some easy algorithms. Most of them have the same running time for all inputs of length n.

Example 1 char A[100] for i = print A[i] Running time: 100*c = Theta(1) (why?) O(1), Omega(1) => Theta(1)

Example 2 char A[n] for i = 1..n print A[i] Running time: n*c O(n), Omega(n) => Theta(n)

Example 3 int A[n] for i = 1..n binarySearch(A, i) Remember: binarySearch is O(lg n). O(n lg n)

Example 4 char A[n] for i = 1..n for j = 1..n print A[i] O(n^2), Omega(n^2) => Theta(n^2)

Example 5 char A[n] char A[n] for i = 1..n >= for i = n/2..n for j = 1..i for j = 1..n/2 print A[j] print A[j] Big-O: i is always <= n, so j always iterates up to at most n, so this is less work than n*n, which is O(n^2). Big-Omega: – Useful trick: consider the iterations of the first loop for which i >= n/2. – In these iterations, the second loop iterates from j=1 to at least j=n/2. – Therefore, the time to perform just these iterations is at least n/2*n/2 = (1/4) n^2, which is Omega(n^2). – The time to perform ALL iterations is even more than this so, of course, the whole algorithm must take Omega(n^2) time.

char A[n] for i = 1..n >= for i = n/2..n for j = 1..i for j = n/4..n/2 for k = 1..j for k = 1..n/4 for l = 1..7 for l = 1..7 print A[k] print A[k] Big-O: i <= n, and j <= n, so running time < n*n*n*7 = c*n^3 = O(n^3). Big-Omega: – Consider only the iterations of the first loop from n/2 up to n. – For these values of i, the second loop always iterates up to at least n/2! – Consider only the iterations of the second loop from n/4 up to n/2. – For these values of j, the third loop always iterates up to at least n/4! – Consider only the iterations of the third loop up to n/4. – The fourth loop always iterates up to 7. – Total iterations: n/2 * n/4 * n/4 * 7 = (7/32) n^3 = Omega(n^3) Example 6

Analyzing algorithms using big-O, omega, theta Now, we are going to perform some slightly different analysis. The next two algorithms’ running times vary widely for different inputs of length n. For some length n inputs, they terminate very quickly. For other inputs n inputs, they can be slow.

Example 1 LinearSearch(A[1..n], key) for i = 1..n if A[i] = key then return true return false Might take 1 iteration… might take n... Big-O: at most n iterations, constant work per iteration, so O(n) Big-Omega: can you find a bad input that makes the algorithm take Omega(n) time?

Example 2 Your boss tells you that your group needs to develop a sorting algorithm for the company's web server that runs in O(n lg n) time. If you can't prove that it can sort any input of length n in O(n lg n) time, it's no good to him. He doesn't want to lose orders because of a slow server. Your colleague tells you that he's been working on this piece of code, which he believes should fit the bill. He says he thinks it can sort any input of length n in O(n lg n) time, but he doesn't know how to prove this.

Example 2 continued WeirdSort(A[1..n]) last := n sorted := false while not sorted do sorted := true for j := 1 to last-1 do if A[j] > A[j+1] then swap A[j] and A[j+1] sorted := false end if end for last := last-1 end while [Discuss… result is:] O(n^2), Omega(n^2) => Theta(n^2)!