Docsity

Algorithmic Analysis Final Exam - Data Structure - Handout | CS 261, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
1
CS 261 – Data Structures
Execution Time
Algorithmic Analysis
• How do we determine an algorithm’s execution time?
• Benchmarking:
– Same machine, 2 (or more) different algorithms
– Depends on processor, compiler, time of day
– Also, relative performance often depends on problem size
• Analysis of algorithms: Big-Oh notation
– Abstract away small variations and concentrate on how input
size affects execution time
– Independent of any platform, compiler, or language
– Asymptotic analysis: look at execution “in the limit”
• Look at performance in terms of the size of the problem
• Smallest function g(n) producing an upper bound on the execution
pf3
pf4
pf5

Partial preview of the text

Download Algorithmic Analysis Final Exam - Data Structure - Handout | CS 261 and more Exams Data Structures and Algorithms in PDF only on Docsity!

CS 261 – Data Structures

Execution Time

Algorithmic Analysis

• How do we determine an algorithm’s execution time?

• Benchmarking:

  • Same machine, 2 (or more) different algorithms
  • Depends on processor, compiler, time of day
  • Also, relative performance often depends on problem size

• Analysis of algorithms: Big-Oh notation

  • Abstract away small variations and concentrate on how input

size affects execution time

  • Independent of any platform, compiler, or language
  • Asymptotic analysis: look at execution “in the limit”
    • Look at performance in terms of the size of the problem
    • Smallest function g ( n ) producing an upper bound on the execution

Constant Time: O(1)

  • Operations on primitives:
    • Arithmetic, assignment, equality  constant time
    • Fixed sequence of constant time operation  constant time
  • Conditional expression (if statement):
    • Time to test + max time of either alternative (worst case)
    • If test and both alternatives are constant  constant time public class Card { public final int suit, rank; public static final int diamond = 1, club = 2, spade = 3, heart = 4; Card(int is, int ir) { suit = is; rank = ir; } public Color color() { if (suit==diamond || suit==heart) return Color.red; else return Color.black; } }

Constant Loop: O(1)

  • Both the number of iterations and the (sequence of)

operations within loop are constant:

public class CardDeck { CardDeck() { int topCard = 0; for(int i=0; i < 13; i++) { cards[topCard++] = new Card(Card.diamond, i); cards[topCard++] = new Card(Card.club, i); cards[topCard++] = new Card(Card.spade, i); cards[topCard++] = new Card(Card.heart, i); } } }

13 * 4 constant operations  constant time  O(1)

Nested Loops: Independent

  • Nested loops multiply:

void matrixMult(double [][] a, double [][] b, double [][] c){ int n = a.length; for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { c[i][j] = 0.0; for(int k = 0; k < n; k++) { c[i][j] += a[i][k] * b[k][j]; } } } }

n

n

n

= n^3

O( n^3 )

Nested Loops: Dependent

  • Nested loops multiply:

void bubbleSort(double [] v) { int n = v.length;

// Find largest remaining value and place in v[i]. for (int i = n - 1; i > 0; i--) {

// Move large values to the top. for (int j = 0; j < i; j++) { if (v[j] > v[j+1]) { // If out of order, double temp = v[j]; // then swap. v[j] = v[j + 1]; v[j + 1] = temp; } } }

} ( n – 1) + ( n – 2) + … + 1 = ( n^2 – n ) / 2  O( n^2 )

n - 1 times

While Loops: Simple Case

  • Simple linear traversal over a range of values:

void insertionSort(double [] v) { int n = v.length; for(int i = 1; i < n; i++) {

// Move element v[i] into place. double element = v[i]; int j = i - 1; while (j >= 0 && element < v[j]) { v[j+1] = v[j]; j--; // Slide old value up. } v[j+1] = element; } }

Worst case (reverse order): 1 + 2 + … + ( n -1) = ( n^2 – n ) / 2  O( n^2 )

  • This can easily be rewritten as a dependent nested ‘for’ loop

n - 1 times

While Loops: Binary Subdivision

  • Search for a value using binary subdivision:

int binarySearch(double [] data, double val) { int mid, low = 0, high = data.length;

// Iteratively subdivide the search area until only one value left. while (low < high) { mid = (low + high) / 2; if (data[mid] < val) low = mid + 1; else high = mid; } return low; }

  • Similar to the way you would look up a word in a dictionary
  • The search area ( n = high – low ) is cut in half in each iteration - n can be cut in half log 2 n times  O(log 2 n ) – or just O(log n )

Benchmarking

  • Algorithmic analysis is the first and best way, but not the

final word

  • What if two algorithms are both of the same complexity?
  • Example: bubble sort and insertion sort are both O( n^2 )
    • So, which one is the “faster” algorithm?
    • Benchmarking: run both algorithms on the same machine
    • Often indicates the constant multipliers and other “ignored”

components

  • Still, different implementations of the same algorithm often

exhibit different execution times – due to changes in the constant

multiplier or other factors (such as adding an early exit to bubble sort)