Algorithms & Complexity

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

The Efficiency of Algorithms Chapter 4 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
CSCE 2100: Computing Foundations 1 Running Time of Programs
Analysys & Complexity of Algorithms Big Oh Notation.
CS 3610/5610N data structures Lecture: complexity analysis Data Structures Using C++ 2E1.
Asymptotic Growth Rate
Cutler/HeadGrowth of Functions 1 Asymptotic Growth Rate.
Introduction to Analysis of Algorithms
CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
The Efficiency of Algorithms
Asymptotic Analysis Motivation Definitions Common complexity functions
The Efficiency of Algorithms
CS 206 Introduction to Computer Science II 01 / 28 / 2009 Instructor: Michael Eckmann.
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.
Algorithm Analysis (Big O)
1 Chapter 2 Program Performance – Part 2. 2 Step Counts Instead of accounting for the time spent on chosen operations, the step-count method accounts.
Program Performance & Asymptotic Notations CSE, POSTECH.
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.
Mathematics Review and Asymptotic Notation
Analysis of Algorithms
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.
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
Coursenotes CS3114: Data Structures and Algorithms Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright ©
Complexity A decidable problem is computationally solvable. But what resources are needed to solve the problem? –How much time will it require? –How much.
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
Algorithm Analysis (Algorithm Complexity). Correctness is Not Enough It isn’t sufficient that our algorithms perform the required tasks. We want them.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Growth of Functions. 2 Analysis of Bubble Sort 3 Time Analysis – Best Case The array is already sorted – no swap operations are required.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Time Complexity of Algorithms
Foundations of Algorithms, Fourth Edition
Algorithm Analysis (Big O)
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.
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
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
Complexity of Algorithms Fundamental Data Structures and Algorithms Ananda Guna January 13, 2005.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
1 ADT Implementation: Recursion, Algorithm Analysis Chapter 10.
Lecture 3COMPSCI.220.S1.T Running Time: Estimation Rules Running time is proportional to the most significant term in T(n) Once a problem size.
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
CMPT 438 Algorithms.
Introduction to Data Structures
Design and Analysis of Algorithms
Analysis of Algorithms
GC 211:Data Structures Week 2: Algorithm Analysis Tools
Reasons to study Data Structures & Algorithms
Scalability for Search
Asymptotic Notations Algorithms perform f(n) basic operations to accomplish task Identify that function Identify size of problem (n) Count number of operations.
GC 211:Data Structures Algorithm Analysis Tools
Courtsey & Copyright: DESIGN AND ANALYSIS OF ALGORITHMS Courtsey & Copyright:
Time Complexity Analysis Neil Tang 01/19/2010
Algorithm Analysis Neil Tang 01/22/2008
GC 211:Data Structures Algorithm Analysis Tools
CSCI 2670 Introduction to Theory of Computing
Asymptotic Notations Algorithms perform f(n) basic operations to accomplish task Identify that function Identify size of problem (n) Count number of operations.
Algorithm Efficiency Chapter 10.
Analysys & Complexity of Algorithms
Programming and Data Structure
Reasons to study Data Structures & Algorithms
Asymptotic Notations Algorithms perform f(n) basic operations to accomplish task Identify that function Identify size of problem (n) Count number of operations.
Introduction to Algorithm and its Complexity Lecture 1: 18 slides
The Efficiency of Algorithms
The Efficiency of Algorithms
Estimating Algorithm Performance
Algorithm Efficiency and Sorting
Presentation transcript:

Algorithms & Complexity Dr. Ranette Halverson CMPS 2433 – Discrete Systems & Analysis Chapter 1: Introduction, 1.4

Solving a Problem Existence of a Solution How many Solutions Optimal Solution Sample problems on page 1 of book.

Algorithm Step-by-Step instructions to accomplish a task or solve a problem Computer Program Assembly instructions Driving Directions Recipe Format & Detail level depends on user

Efficiency of Algorithms Different algorithms for a single task may have different efficiency Driving Directions – short route vs. long route Searching Algorithms Sorting Algorithms Must be able to evaluate efficiency (speed) of computer algorithms

Comparing Algorithms Want to choose the “best” algorithm for tasks Generally, best = fastest But there are other considerations Hardware (e.g. LaTex) Size of data set Data Structure Need a standard measurement

Complexity = Speed = Efficiency NOT Really – but sort of *Complexity is the number of basic operations required by an algorithm Will 2 algorithms with same complexity take the same actual amount of time to run?? Why or Why Not? * Means memorize

Complexity Examples Read x, y, z x = y + z Print x How many operations? Read x, y, z x = y / z Print x Are these algorithms the same complexity? The same speed?

Complexity Examples Do 10 times Read x, y, z x = y + z Print x

Complexity Examples Do n times Read x, y, z x = y + z Print x Do 10 times Read x, y, z x = y + z Print x

Big Oh Notation O(f(n)) – “Big Oh of f of n” n represents size of data set Examples O(1) or O(c) O(n) O(n2) Big Oh is an upper bound.

Constant Complexity An algorithm with the same number of operations regardless of the data set is said to have CONSTANT COMPLEXITY O(1) or 0(c) See previous algorithms Most significant algorithms are NOT constant complexity

Complexity Examples Do n times Read x, y, z x = y + z Print x Complexity? Depends on value of n! NOT Constant 6 * n basic operations O(6n)  O(n) Do 10 times Read x, y, z x = y + z Print x Complexity? O(c) or O(1) - constant

Algorithm Complexity Number of operations in terms of the data set size Do 10 times Read x, y, z x = y + z Print x Constant Time Complexity – O(1) or O(c)

Algorithm Complexity Do n times Read x, y, z x = y + z Print x Time Complexity is dependent upon n 6n Operations - O(6n)  O(n)

*Big Oh (yes, memorize) O(f(n)) – Big Oh of f of n A function g(n) = O(f(n)) if there exist 2 constants K and n0 such that |g(n)| <= K|f(n)| for all n >=n0 Big Oh is an upper bound.

Evaluating xn Operations??? P = x k = 1 While k < n P = P * x k = k + 1 Print P Operations???

Evaluating xn Total =3 + n + 2(n-1) + 2(n-1) 3 + n + 2n -2 + 2n – 2 P = x k = 1 While k < n P = P * x k = k + 1 Print P Operations 1 n 2(n-1) Total =3 + n + 2(n-1) + 2(n-1) 3 + n + 2n -2 + 2n – 2 5n – 1 = O(5n -1)  O(n)

Trace: xn  54  P = x k = 1 While k < n P = P * x k = k + 1 Print P (n=4, x=5) P = 5, k = 1 P = 5*5 (25); k= 2 P = 25*5 (125); k = 3 P = 125*5 (625); k=4 Print 625

Complexity & Rate of Growth Complexity measures growth rate of algorithm time in terms of data size O(1) Constant O(n) Linear O(n2) Polynomial (any constant power) O(5n) Exponential (any constant base) What do these functions look like when graphed? How big are real world data sets? (name some)

Polynomial Evaluation P(x) = anxn + an-1xn-1 + … +a1x + a0 Given values for x, a0, a1,…an How many ops for anxn ? Operations to calculate each term in poly? n + n-1 + n-2 +…+ 1 + 0 = (n*(n+1))/2 Additions to combine the n+1 terms = n Total Ops = (n2+n)/2 + n  O(n2) What did we omit from analysis???

Polynomial Evaluation (p.26) P(x) = anxn + an-1xn-1 + … +a1x + a0 Given values for x, a0, a1,…an S = a0 , k = 1 while k <=1 S = S + ak xk k = k+ 1 Print S Can you spot any inefficiencies?

Polynomial Evaluation Algorithm (p Polynomial Evaluation Algorithm (p.26) P(x) = anxn + an-1xn-1 + … +a1x + a0 Given values for n, x, a0, a1,…an S = a0 , k = 1 while k <=n S = S + ak xk k = k+ 1 Print S *Slide 17: xk = 5k + 1 2 n + 1 #n(2+5(k+1)+1) 2n 1 #Total = 2n +5kn + 5n + n 5kn + 8n

Complexity of Poly. Evaluation Total = (by line on previous slide) =2 + (n + 1) + (5kn + 8n) + 2n + 1 = 5kn + 11n + 4 Worst case for k?? = 5n2 + 11 n + 4 O(n2) Note: this is for inserting code for xk Function call adds additional overhead.

Analysis of Polynomial Compare Slides 20 and 22/23 Same Big Oh  O(n2) “Different details” but same results

Horner’s Rule (Polynomial Evaluation) P(x) = anxn + an-1xn-1 + … +a1x + a0 S = an, k = 1 While k <= n S = Sx + an-k k = k = 1 Print s 2 n + 1 n * 3 n *2 1 Total = 2 + n+1 + 3n + n2 + 1 = 6n + 4  O(n)

Summary & Homework See table on page 31 In general, an algorithm is considered “good” if its complexity is no more than some polynomial in n For small n, some non-polynomial algorithms may be “acceptable” Homework: Page 33+ 1 – 10, 23 – 26 – will discuss in class 27 – 30 (detail for each step as on slides, state Total & Big Oh) – Turn in 27-30 for grading