Algorithms and Complexity - Lecture Slides | CS 200, Study notes of Computer Science

Material Type: Notes; Professor: Howe; Class: Algorithms and Data Structures; Subject: Computer Science; University: Colorado State University; Term: Fall 2008;

Typology: Study notes

Pre 2010

Uploaded on 03/18/2009

koofers-user-hsm
koofers-user-hsm 🇺🇸

5

(2)

10 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Algorithms and Complexity
Rosen Ch. 3.2: Growth of Functions
Rosen Ch. 3.3: Complexity of Algorithms
Walls Ch. 10.1: Efficiency of Algorithms
Measuring the efficiency of
algorithms
We have two algorithms: alg1 and alg2
that solve the same problem. Our
application needs a fast running time.
How do we choose between the
algorithms?
Measuring the efficiency of
algorithms
Implement the two algorithms in Java and
compare their running times
Issues with this approach:
How are the algorithms coded? We want to compare
the algorithms, not the implementations.
What computer should we use? Results may be
sensitive to this choice.
What data should we use?
Measuring the efficiency of
algorithms
Goal:
techniques that analyze algorithms independent of
specifics such as implementation, hardware, or data.
Solution:
analyze and model the number of operations the
algorithm will perform as a function of input
Example:
copying an array with n elements requires ??
operations.
pf3
pf4
pf5
pf8

Partial preview of the text

Download Algorithms and Complexity - Lecture Slides | CS 200 and more Study notes Computer Science in PDF only on Docsity!

Algorithms and Complexity

 Rosen Ch. 3.2: Growth of Functions

 Rosen Ch. 3.3: Complexity of Algorithms

 Walls Ch. 10.1: Efficiency of Algorithms

Measuring the efficiency of

algorithms

 We have two algorithms: alg1 and alg

that solve the same problem. Our

application needs a fast running time.

 How do we choose between the

algorithms?

Measuring the efficiency of

algorithms

 Implement the two algorithms in Java and

compare their running times

 Issues with this approach:

 How are the algorithms coded? We want to compare
the algorithms, not the implementations.
 What computer should we use? Results may be
sensitive to this choice.
 What data should we use?

Measuring the efficiency of

algorithms

 Goal:

 techniques that analyze algorithms independent of
specifics such as implementation, hardware, or data.

 Solution:

 analyze and model the number of operations the
algorithm will perform as a function of input

 Example:

 copying an array with n elements requires ??
operations.

Growth rates

 Algorithm A requires n^2 / 2 operations to solve

a problem of size n

 Algorithm B requires 5n+10 operations to

solve a problem of size n

 So… which do we pick?

Order of magnitude

growth analysis

 A function f(x) is O(g(x)) if and only if

there exist two positive constants, c and k,

such that f(x) ≤ c*g(x) ∀ x > k

c*g(x)
f(x)

n

k

Big-O

 focus is only on growth

 shape of g(x) matters

 not the multiplicative constant c

 focus is on large x

 k allows us to ignore shapes for smaller x

Classic Shapes: constant

 O(1)

 examples?

Other Shapes: Superlinear

Polynomial (x^2 ), exponential (cx)

Growth rates

 Algorithm A requires n^2 / 2 operations to solve a

problem of size n

 Algorithm B requires 5n+10 operations to solve a

problem of size n

 For large enough problem size algorithm B is more

efficient

 We focus on the growth rate:

 Algorithm A requires time proportional to n^2
 Algorithm B requires time proportional to n

Simplifying Big-O

Theorem: Let

where are real numbers.

Then is

f ( x ) = an x

n

+ an " 1 x

n " 1

+ ...+ a 1 x + a 0

an , an " 1 ..., a 1 , a 0

f ( x )

O ( x

n

Combinations of Functions

 Additive Theorem:

!^ ^ Multiplicative Theorem:

Suppose that f 1 ( x ) is O ( g 1 ( x )) and f 2 ( x ) is O ( g 2 ( x )).

Then ( f 1 + f 2 )( x ) is O (max(| g 1 ( x ) |,| g 2 ( x ) |).

Suppose that f 1 ( x ) is O ( g 1 ( x )) and f 2 ( x ) is O ( g 2 ( x )).

Then ( f 1 f 2 )( x ) is O ( g 1 ( x ) g 2 ( x )).

Other Notations

 Big-Omega ( ): lower bound

 Big-Theta ( ): tight upper bound

Best, Worst or Average

Time Complexity

 Best case
 shortest “time” possible to execute an algorithm
 “time” measured in “steps”: O(1) time operations
 Average case
 amount of time expected “usually”
 In this course we will hand wave a lot
 Worst case
 just how bad can it get??
 What is the maximal #steps this will take
 Example: linear search

Practical Analysis - Loops

1 public void insertElementAt(Object obj, int index) {
2 for (i = elementCount; i > index; i--) {
3 elementData[i] = elementData[i-1];
How many times will line 3 repeat?
On what does the number depend?

Practical Analysis - Recursion

 f(n) depends on :

 number of calls

 work done in each call

 Examples:

 factorial: how many recursive calls?

 binary search

Selection Sort - Code

public static void selectionSort(Comparable[] theArray, int n) { // Sorts the items in an array into ascending order. // Precondition: theArray is an array of n items. // Postcondition: theArray is sorted into ascending order. // last = index of the last item in the subarray of items yet to be sorted // largest = index of the largest item found for (int last = n-1; last >= 1 ; last--) { // Invariant: theArray[last+1..n-1] is sorted and > theArray[0..last] // select largest item in theArray[0..last] int largest = indexOfLargest(theArray, last+1); // swap largest item theArray[largest] with theArray[last] Comparable temp = theArray[largest]; theArray[largest] = theArray[last]; theArray[last] = temp; }}

indexOfLargest

private static int indexOfLargest(Comparable[] theArray, int size) { // Finds the largest item in an array. // Precondition: theArray is an array of size items; size >= 1. // Postcondition: Returns the index of the largest item in the array. int indexSoFar = 0 ; // index of largest item found so far // Invariant: theArray[indexSoFar]>=theArray[0..currIndex-1] for (int currIndex = 1 ; currIndex < size; ++currIndex) { if (theArray[currIndex].compareTo(theArray[indexSoFar])> 0 ) { indexSoFar = currIndex; } // end if } // end for return indexSoFar;}

selectionSort –

Algorithm Complexity

 How many compares are done?

 How many swaps are done?

 How much space?

selectionSort –

Algorithm Complexity

 How many compares are done?

(n-1)+(n-2)+...+1, or O(n^2 )

 How many swaps are done?

 Note swap is run even if already in position

 n, or O(n)

 How much space?

 In-place algorithm

Examples

 Copying an array

 Linear search in a list for a particular value

 Enumerate all lowercase char strings of length n

 Insert Element in Vector class

Overall Comments

 Order-of-magnitude analysis focuses on large
problems
 If the problem size is always small, you can probably
ignore an algorithm’s efficiency
 Weigh the trade-offs between an algorithm’s time
requirements, its memory requirements, expense of
programming/maintenance, …
 Compare algorithms for both style and efficiency