Algorithmic Complexity 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms
Advertisements

Spring 2015 Lecture 5: QuickSort & Selection
Analysis of Algorithms
Algorithmic Complexity Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Chapter 3 Growth of Functions
Chapter 10 Algorithm Efficiency
Sorting Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CSE 326 Asymptotic Analysis David Kaplan Dept of Computer Science & Engineering Autumn 2001.
Introduction to Analysis of Algorithms
© 2006 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 Algorithm Efficiency and Sorting CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck.
Sorting Algorithms Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Eleg667/2001-f/Topic-1a 1 A Brief Review of Algorithm Design and Analysis.
20-Jun-15 Analysis of Algorithms II. 2 Basics Before we attempt to analyze an algorithm, we need to define two things: How we measure the size of the.
Lecture 3 Aug 31, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis discussion of lab – permutation generation.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Data Structures Review Session 1
Algorithmic Complexity 2 Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Data Structure Algorithm Analysis TA: Abbas Sarraf
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Elementary Data Structures and Algorithms
Lecture 3 Feb 7, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis Image representation Image processing.
Algorithmic Complexity Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS2420: Lecture 11 Vladimir Kulyukin Computer Science Department Utah State University.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Algorithmic Complexity 3 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Algorithm Analysis (Big O)
Spring2012 Lecture#10 CSE 246 Data Structures and Algorithms.
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Aaron Bauer Winter 2014.
SIGCSE Tradeoffs, intuition analysis, understanding big-Oh aka O-notation Owen Astrachan
Algorithm Analysis & Complexity We saw that a linear search used n comparisons in the worst case (for an array of size n) and binary search had logn comparisons.
Program Performance & Asymptotic Notations CSE, POSTECH.
Week 2 CS 361: Advanced Data Structures and Algorithms
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.
CSC 201 Analysis and Design of Algorithms Lecture 04: CSC 201 Analysis and Design of Algorithms Lecture 04: Time complexity analysis in form of Big-Oh.
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
1 COMP3040 Tutorial 1 Analysis of algorithms. 2 Outline Motivation Analysis of algorithms Examples Practice questions.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
ASYMPTOTIC COMPLEXITY CS2111 CS2110 – Fall
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
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.
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.
RUNNING TIME 10.4 – 10.5 (P. 551 – 555). RUNNING TIME analysis of algorithms involves analyzing their effectiveness analysis of algorithms involves analyzing.
Introduction to Analysis of Algorithms CS342 S2004.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
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.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
DS.A.1 Algorithm Analysis Chapter 2 Overview Definitions of Big-Oh and Other Notations Common Functions and Growth Rates Simple Model of Computation Worst.
21-Feb-16 Analysis of Algorithms II. 2 Basics Before we attempt to analyze an algorithm, we need to define two things: How we measure the size of the.
Analysis of Algorithms & Recurrence Relations. Recursive Algorithms Definition –An algorithm that calls itself Components of a recursive algorithm 1.Base.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
Big-O. Speed as function Function relating input size to execution time – f(n) = steps where n = length of array f(n) = 4(n-1) + 3 = 4n – 1.
Algorithm Analysis 1.
Design and Analysis of Algorithms
Introduction to Analysis of Algorithms
Introduction to Analysis of Algorithms
Introduction to Analysis of Algorithms
Algorithm design and Analysis
Complexity Analysis of Algorithms
Data Structures Review Session
Analysis of Algorithms II
Analysis of Algorithms
Presentation transcript:

Algorithmic Complexity 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park

Overview Big-O notation Analysis cases Critical section

Big-O Notation Represents Upper bound on number of steps in algorithm Intrinsic efficiency of algorithm for large inputs f(n) O(…) input size # steps

Formal Definition of Big-O Function f(n) is  ( g(n) ) if For some positive constants M, N 0 M  g(N 0 )  f(n), for all n  N 0 Intuitively For some coefficient M & all data sizes  N 0 M  g(n) is always greater than f(n)

Big-O Examples 5n  O(n) Select M = 6, N 0 = 1000 For n  n  5n+1000 is always true Example  for n = 

Big-O Examples 2n n  O(n 2 ) Select M = 4, N 0 = 100 For n  100 4n 2  2n n is always true Example  for n = 

Types of Case Analysis Can analyze different types (cases) of algorithm behavior Types of analysis Best case Worst case Average case

Types of Case Analysis Best case Smallest number of steps required Not very useful Example  Find item in first place checked

Types of Case Analysis Worst case Largest number of steps required Useful for upper bound on worst performance Real-time applications (e.g., multimedia) Quality of service guarantee Example  Find item in last place checked

Types of Case Analysis Average case Number of steps required for “typical” case Most useful metric in practice Different approaches 1. Average case 2. Expected case 3. Amortized

Approaches to Average Case 1. Average case Average over all possible inputs Assumes uniform probability distribution 2. Expected case Weighted average over all inputs Weighted by likelihood of input 3. Amortized Examine common sequence of operations Average number of steps over sequence

Quicksort Example Quicksort One of the fastest comparison sorts Frequently used in practice Quicksort algorithm Pick pivot value from list Partition list into values smaller & bigger than pivot Recursively sort both lists

Quicksort Example Quicksort properties Average case = O(nlog(n)) Worst case = O(n 2 ) Pivot  smallest / largest value in list Picking from front of nearly sorted list Can avoid worst-case behavior Attempt to select random pivot value

Amortization Example Adding numbers to end of array of size k If array is full, allocate new array Allocation cost is O(size of new array) Copy over contents of existing array Two approaches Non-amortized If array is full, allocate new array of size k+1 Amortized If array is full, allocate new array of size 2k Compare their allocation cost

Amortization Example Non-amortized approach Allocation cost as table grows from 1..n Total cost  ½ (n+1) 2 Case analysis Best case  allocation cost = k Worse case  allocation cost = k Average case  allocation cost = k = n/2 Size (k) Cost

Amortization Example Amortized approach Allocation cost as table grows from 1..n Total cost  2 (n – 1) Case analysis Best case  allocation cost = 0 Worse case  allocation cost = 2(k – 1) Average case  allocation cost = 2 Worse case takes more steps, but faster overall Size (k) Cost

Analyzing Algorithms Goal Find asymptotic complexity of algorithm Approach Ignore less frequently executed parts of algorithm Find critical section of algorithm Determine how many times critical section is executed as function of problem size

Critical Section of Algorithm Heart of algorithm Dominates overall execution time Characteristics Operation central to functioning of program Contained inside deeply nested loops Executed as often as any other part of algorithm Sources Loops Recursion

Critical Section Example 1 Code (for input size n) 1. A 2. for (int i = 0; i < n; i++) 3. B 4. C Code execution A  B  C  Time 

Critical Section Example 1 Code (for input size n) 1. A 2. for (int i = 0; i < n; i++) 3. B 4. C Code execution A  once B  n times C  once Time  1 + n + 1 = O(n) critical section

Critical Section Example 2 Code (for input size n) 1. A 2. for (int i = 0; i < n; i++) 3. B 4. for (int j = 0; j < n; j++) 5. C 6. D Code execution A  B  Time  C  D 

Critical Section Example 2 Code (for input size n) 1. A 2. for (int i = 0; i < n; i++) 3. B 4. for (int j = 0; j < n; j++) 5. C 6. D Code execution A  once B  n times Time  1 + n + n = O(n 2 ) critical section C  n 2 times D  once

Critical Section Example 3 Code (for input size n) 1. A 2. for (int i = 0; i < n; i++) 3. for (int j = i+1; j < n; j++) 4. B Code execution A  B  Time 

Critical Section Example 3 Code (for input size n) 1. A 2. for (int i = 0; i < n; i++) 3. for (int j = i+1; j < n; j++) 4. B Code execution A  once B  ½ n (n-1) times Time  1 + ½ n 2 = O(n 2 ) critical section

Critical Section Example 4 Code (for input size n) 1. A 2. for (int i = 0; i < n; i++) 3. for (int j = 0; j < 10000; j++) 4. B Code execution A  B  Time 

Critical Section Example 4 Code (for input size n) 1. A 2. for (int i = 0; i < n; i++) 3. for (int j = 0; j < 10000; j++) 4. B Code execution A  once B  n times Time  n = O(n) critical section

Critical Section Example 5 Code (for input size n) 1. for (int i = 0; i < n; i++) 2. for (int j = 0; j < n; j++) 3. A 4. for (int i = 0; i < n; i++) 5. for (int j = 0; j < n; j++) 6. B Code execution A  B  Time 

Critical Section Example 5 Code (for input size n) 1. for (int i = 0; i < n; i++) 2. for (int j = 0; j < n; j++) 3. A 4. for (int i = 0; i < n; i++) 5. for (int j = 0; j < n; j++) 6. B Code execution A  n 2 times B  n 2 times Time  n 2 + n 2 = O(n 2 ) critical sections

Critical Section Example 6 Code (for input size n) 1. i = 1 2. while (i < n) 3. A 4. i = 2  i 5. B Code execution A  B  Time 

Critical Section Example 6 Code (for input size n) 1. i = 1 2. while (i < n) 3. A 4. i = 2  i 5. B Code execution A  log(n) times B  1 times Time  log(n) + 1 = O( log(n) ) critical section

Critical Section Example 7 Code (for input size n) 1. DoWork (int n) 2. if (n == 1) 3. A 4. else 5. DoWork(n/2) 6. DoWork(n/2) Code execution A  DoWork(n/2)  Time(1)  Time(n) =

Critical Section Example 7 Code (for input size n) 1. DoWork (int n) 2. if (n == 1) 3. A 4. else 5. DoWork(n/2) 6. DoWork(n/2) Code execution A  1 times DoWork(n/2)  2 times Time(1)  1 Time(n) = 2  Time(n/2) + 1 critical sections