Docsity

Discrete Structures Lecture 9: Algorithms and Complexity, Study notes of Discrete Structures and Graph Theory

Partial preview of the text

Download Discrete Structures Lecture 9: Algorithms and Complexity and more Study notes Discrete Structures and Graph Theory in PDF only on Docsity!

9/16/

CSci 243

Discrete Structures

Lecture 9 Algorithms 2:Complexity

9/16/

Outline^ • Motivation: Linear vs. Binary Search• Introduction to Complexity• Characterizing Growth of Functions• Back to Complexity• P and NP

9/16/

Algorithmic Complexity^ •

Computational complexity: a measure of thetime or space performance

  • Must be calibrated to be meaningful:
    • To relevant criteria: e.g. number of comparisons• To size of input: input of size

n elements

  • To trivial differences: define equivalence classes

Complexity analysis is useless if the algorithm isincorrect!

9/16/

Calibration of Search Algorithms^ • Relevant criteria: number of comparisons

between list elements and element to find

  • Size of input: list of

n elements

  • To trivial differences: +5 comparisons is okay.

Is x5 comparisons okay too?

We need a precise metric for comparingalgorithms and identifying similar ones.

9/16/

Characterizing Function Growth^ • Can write functions which characterize the

behavior of the algorithms

  • Example: linear search is like f(n)=n, because

we do one comparison for each of the n items

  • Then classify long term behavior of functions—

asymptotic behavior

  • Note: Sometimes we do care about short term

9/16/

Big-O Notation^ • f(x) is O( g(x) ) if there exist constants C and k

such that |f(x)|

C|g(x)|

  • C: Don’t care how many times faster!

(Plus need to compare x

2

and x

  • k: The “eventually” point for x• Algorithm F (f(x)) performs no

worse than

algorithm G (g(x)) for large inputs

9/16/

Quick Question^ • Is 5(

x^ ) O(

x)?

9/16/

  • x

3

is

not O(1000000 x

  • Assume |x

C|1000000 x

  • x

3

1000000 C x

2

(positive for positive x)

  • x

1000000 C

  • There is no value of C and k, because 1000000C

is a constant, and x can be more than thatconstant.

Example

9/16/

Big-Omega Notation^ • f(x) is

( g(x) ) if there exist constants C and k

such that |f(x)|

C|g(x)|

  • Just flipped comparison• Algorithm F (f(x)) performs no

better than

algorithm G (g(x)) for large inputs

9/16/

14

Big-Theta Notation^ • f(x) is

( g(x) ) if f(x) is O( g(x) ) and f(x) is

g(x) )

  • Algorithm F (f(x)) performs

the same as

algorithm G (g(x)) for large inputs

  • Lots of people say “big-oh” when they mean

“big-theta”.

  • Defines an equivalence class.

9/16/

Next up… Analysis of Algorithms

9/16/

Analysis Example 1^ • foreach location in list

list[location] += 1; foreach location in list

list[location] *= 2;

  • Both loops are

(n)

  • Do one, then the next, so f(x) + g(x)• So algorithm is

( max(n,n) ) =

(n)

9/16/

Analysis Example 3^ • foreach location

1

in list

foreach location

2

in list

list[location

] *= list[location 1

]; 2

foreach location in list

list[location] += 3;

  • QC: Double loop is
  • QC: Single loop is
  • QC: Combined complexity is
  • QC: Polynomial rule says it is

9/16/

Common Terminology^ • f(x) is

(g(x)): “f(x) is

of order g(x)”

  • f(x) is

(k): “a constant time algorithm”

  • f(x) is

(log(n)): “a logarithmic time algorithm”

  • f(x) is

(n): “a linear time algorithm”

  • f(x) is

(n

2 ): “a quadratic time algorithm”

  • f(x) is

(n

k^ ): “a polynomial time algorithm”

  • f(x) is

(k

n^ ): “a exponential time algorithm”

  • f(x) is

(n!): “a factorial time algorithm”