Algorithmic Complexity 2 Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.

Slides:



Advertisements
Similar presentations
Discrete Structures CISC 2315
Advertisements

Lecture: Algorithmic complexity
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.
Algorithm Strategies Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 1 – Basic Concepts
Algorithmic Complexity Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Introduction to Analysis of Algorithms
Algorithmic Complexity 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 2 Elements of complexity analysis Performance and efficiency Motivation: analysis.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 2. Types of Complexities.
Algorithmic Complexity Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
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)
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Aaron Bauer Winter 2014.
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
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.
1 ©2008 DEEDS Group Introduction to Computer Science 2 - SS 08 Asymptotic Complexity Introduction in Computer Science 2 Asymptotic Complexity DEEDS Group.
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Introduction to complexity. 2 Analysis of Algorithms Why do we need to analyze algorithms? –To measure the performance –Comparison of different algorithms.
Mathematics Review and Asymptotic Notation
CS 3343: Analysis of Algorithms
DISCRETE MATHEMATICS I CHAPTER 11 Dr. Adam Anthony Spring 2011 Some material adapted from lecture notes provided by Dr. Chungsim Han and Dr. Sam Lomonaco.
Algorithm Analysis An algorithm is a clearly specified set of simple instructions to be followed to solve a problem. Three questions for algorithm analysis.
Design and Analysis of Algorithms - Chapter 21 Analysis of Algorithms b Issues: CorrectnessCorrectness Time efficiencyTime efficiency Space efficiencySpace.
Analysis of Algorithms
1 COMP3040 Tutorial 1 Analysis of algorithms. 2 Outline Motivation Analysis of algorithms Examples Practice questions.
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.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program.
Asymptotic Analysis (based on slides used at UMBC)
CSC – 332 Data Structures Generics Analysis of Algorithms Dr. Curry Guinn.
Introduction to Analysis of Algorithms CS342 S2004.
Algorithmic Analysis Charl du Plessis and Robert Ketteringham.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.
Asymptotic Analysis CSE 331. Definition of Efficiency An algorithm is efficient if, when implemented, it runs quickly on real instances Implemented where?
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.
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.
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.
Big O David Kauchak cs302 Spring Administrative Assignment 1: how’d it go? Assignment 2: out soon… Lab code.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
Analysis of Algorithms & Recurrence Relations. Recursive Algorithms Definition –An algorithm that calls itself Components of a recursive algorithm 1.Base.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Ch03-Algorithms 1. Algorithms What is an algorithm? An algorithm is a finite set of precise instructions for performing a computation or for solving a.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
LECTURE 2 : fundamentals of analysis of algorithm efficiency Introduction to design and analysis algorithm 1.
Algorithm Analysis 1.
Chapter 2 Algorithm Analysis
Introduction to Analysis of Algorithms
Analysis of Algorithms
Analysis of Algorithms
Analysis of Algorithms
Algorithm Analysis (not included in any exams!)
CS 201 Fundamental Structures of Computer Science
Analysis of Algorithms
Fundamentals of the Analysis of Algorithm Efficiency
Searching, Sorting, and Asymptotic Complexity
At the end of this session, learner will be able to:
David Kauchak cs161 Summer 2009
Analysis of Algorithms
Algorithm Analysis How can we demonstrate that one algorithm is superior to another without being misled by any of the following problems: Special cases.
Presentation transcript:

Algorithmic Complexity 2 Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park

Overview Critical sections Recurrence relations Comparing complexity Types of complexity analysis

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

Recursive Algorithms Definition An algorithm that calls itself Components of a recursive algorithm 1. Base cases Computation with no recursion 2. Recursive cases Recursive calls Combining recursive results

Recursive Algorithm Example 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) recursive cases base case

Recurrence Relations Definition Value of a function at a point is given in terms of its value at other points Examples T(n) = T(n-1) + k T(n) = T(n-1) + n T(n) = T(n-1) + T(n-2) T(n) = T(n/2) + k T(n) = 2  T(n/2) + k

Recurrence Relations Base case Value of function at some specified points Also called boundary values / boundary conditions Base case example T(1) = 0 T(1) = 1 T(2) = 1 T(2) = k

Solving Recurrence Equations Back substitution (iteration method) Iteratively substitute recurrence into formula Example T(1) = 5 T(n) = T(n-1) + 1 = ( T(n-2) + 1 ) + 1 = ( ( T(n-3) + 1 ) + 1 ) + 1 = ( ( ( T(n-4) + 1 ) + 1 ) + 1 ) + 1 = … = (…( T(1) + 1 ) + … ) + 1 = (…( ) + … ) + 1 = n + 4

Example Recurrence Solutions Examples T(n) = T(n-1) + k  O(n) T(n) = T(n-1) + n  O(n 2 ) T(n) = T(n-1) + T(n-2)  O(n!) T(n) = T(n/2) + k  O(log(n)) T(n) = 2  T(n/2) + k  O(n) T(n) = 2  T(n-1) + k  O(2 n ) Many additional issues, solution methods Take CMSC 351 – Introduction to Algorithms

Asymptotic Complexity Categories Complexity NameExample O(1)ConstantArray access O(log(n))LogarithmicBinary search O(n)LinearLargest element O(n log(n))N log NOptimal sort O(n 2 )Quadratic2D Matrix addition O(n 3 )Cubic2D Matrix multiply O(n k )PolynomialLinear programming O(k n )ExponentialInteger programming From smallest to largest For size n, constant k > 1

Comparing Complexity Compare two algorithms f(n), g(n) Determine which increases at faster rate As problem size n increases Can compare ratio If , f() is larger If 0, g() is larger If constant, then same complexity lim n   f(n) g(n)

Complexity Comparison Examples log(n) vs. n ½ n vs. n 1000 lim n   f(n) g(n) lim n   log(n) n ½ 0 lim n   f(n) g(n) lim n   n n 1000 ?? Not clear, use L’Hopital’s Rule

L’Hopital’s Rule If ratio is indeterminate 0 / 0 or  /  Ratio of derivatives computes same value Can simplify ratio by repeatedly taking derivatives of numerator & denominator lim n   f(n) g(n) lim n   f'(n) g'(n) =

Using L’Hopital’s Rule n vs. n 1000 lim n   f(n) g(n) lim n   n n 1000 lim n   n n n 999 lim n   n (n-1) n  999 n 998  …

Additional Complexity Measures Upper bound Big-O   (…) Represents upper bound on # steps Lower bound Big-Omega   (…) Represents lower bound on # steps Combined bound Big-Theta   (…) Represents combined upper/lower bound on # steps Best possible asymptotic solution

2D Matrix Multiplication Example Problem C = A * B Lower bound  (n 2 ) Required to examine 2D matrix Upper bounds O(n 3 )Basic algorithm O(n )Strassen’s algorithm (1969) O(n )Coppersmith & Winograd (1987) Improvements still possible (open problem) Since upper & lower bounds do not match  =

Additional Complexity Categories NameDescription NPNondeterministic polynomial time (NP) PSPACEPolynomial space EXPSPACEExponential space DecidableCan be solved by finite algorithm UndecidableNot solvable by finite algorithm Mostly of academic interest only Quadratic algorithms usually too slow for large data Use fast heuristics to provide non-optimal solutions

NP Time Algorithm Polynomial solution possible If make correct guesses on how to proceed Required for many fundamental problems Boolean satisfiability Traveling salesman problem (TLP) Bin packing Key to solving many optimization problems Most efficient trip routes Most efficient schedule for employees Most efficient usage of resources

NP Time Algorithm Properties of NP Can be solved with exponential time Not proven to require exponential time Currently solve using heuristics NP-complete problems Representative of all NP problems Solution can be used to solve any NP problem Examples Boolean satisfiability Traveling salesman

P = NP? Are NP problems solvable in polynomial time? Prove P=NP Show polynomial time solution exists for any NP-complete problem Prove P  NP Show no polynomial-time solution possible The expected answer Important open problem in computer science $1 million prize offered by Clay Math Institute

Algorithmic Complexity Summary Asymptotic complexity Fundamental measure of efficiency Independent of implementation & computer platform Learned how to Examine program Find critical sections Calculate complexity of algorithm Compare complexity