Intro To Algorithms

  • Uploaded by: sri
  • 0
  • 0
  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Intro To Algorithms as PDF for free.

More details

  • Words: 592
  • Pages: 15
ECE 242 Fall 2007

Data Structures and Algorithms in Java Analysis of Algorithms

ECE242 Fall 2007

Data Structures in Java, Prof. Mettu

Analysis of Algorithms • How do we compare two implementations of the same interface? • We need to account for all possible inputs. • Our performance measure should be conservative.

ECE242 Fall 2007

Data Structures in Java, Prof. Mettu

“Asymptotic” Running Time How do we characterize the running time of a method? public boolean contains(int x) { int i = 0; while ((i < next) && (data[i] != x)) { i++; } return (i != next); }

Just count number of time units utilized; here the parameter is the size of the collection. ECE242 Fall 2007

Data Structures in Java, Prof. Mettu

“Worst-case” Analysis • Which implementation is better? • Any single measure of running time should describe performance over all possible inputs.

• Worst-case reasoning is conservative, and can serve as a baseline measure for comparing implementations.

ECE242 Fall 2007

Data Structures in Java, Prof. Mettu

Defining Running Time We can parameterize the running time of a method by its input. public int sum(int n) { int sum = 0; for (int i = 1; i <= n; i++) sum += i; return sum;

public int sum(int n) { return (int) n*(n+1)/2; }

}

ECE242 Fall 2007

Data Structures in Java, Prof. Mettu

Defining Running Time We can parameterize the running time of a method by its input. public int sum(int n) { int sum = 0; for (int i = 1; i <= n; i++) sum += i;

public int sum(int n) { return (int) n*(n+1)/2; }

return sum; }

n+n+n ECE242 Fall 2007

Data Structures in Java, Prof. Mettu

Defining Running Time We can parameterize the running time of a method by its input. public int sum(int n) { int sum = 0; for (int i = 1; i <= n; i++) sum += i;

public int sum(int n) { return (int) n*(n+1)/2; }

return sum; }

n+n+n ECE242 Fall 2007

Data Structures in Java, Prof. Mettu

3

Big-O Notation We would like to ignore platform-specific details.

r(n) = O(f (n))



Asymptotic Runtime

Allows for slack in bound

∃c, n0 : ∀n :: n > n0 → r(n) ≤ c · f (n) ECE242 Fall 2007

Data Structures in Java, Prof. Mettu

Why Big-O Notation? 2.55n

runtime

7

n

.75n

5 1 input size ECE242 Fall 2007

Data Structures in Java, Prof. Mettu

Characterizing an Algorithm public void some_method() { <statement 1>; <statement 2>; ...

characterize space/running time

r(n)

}

prove upper bound

r(n) = O(f (n)) ECE242 Fall 2007

Data Structures in Java, Prof. Mettu

Static vs. Dynamic Collections Which is faster? Why? Method

Array-based Dynamic Implementation Implementation

add() remove() contains() size() ECE242 Fall 2007

Data Structures in Java, Prof. Mettu

Static vs. Dynamic Collections Which is faster? Why? Method

add()

Array-based Dynamic Implementation Implementation

O(n)

remove() contains() size() ECE242 Fall 2007

Data Structures in Java, Prof. Mettu

O(1)

Static vs. Dynamic Collections Which is faster? Why? Method

Array-based Dynamic Implementation Implementation

add()

O(n)

O(1)

remove()

O(n)

O(n)

contains() size() ECE242 Fall 2007

Data Structures in Java, Prof. Mettu

Static vs. Dynamic Collections Which is faster? Why? Method

Array-based Dynamic Implementation Implementation

add()

O(n)

O(1)

remove()

O(n)

O(n)

contains()

O(n)

O(n)

size() ECE242 Fall 2007

Data Structures in Java, Prof. Mettu

Static vs. Dynamic Collections Which is faster? Why? Method

Array-based Dynamic Implementation Implementation

add()

O(n)

O(1)

remove()

O(n)

O(n)

contains()

O(n)

O(n)

size()

O(1)

O(1)

ECE242 Fall 2007

Data Structures in Java, Prof. Mettu

Related Documents

Intro To Algorithms
June 2020 20
Intro To Algorithms 2
June 2020 6
Algorithms
May 2020 19
Algorithms
June 2020 23
Algorithms
October 2019 26
Algorithms
November 2019 26

More Documents from "Anonymous 0U9j6BLllB"

Snuping Spyware
December 2019 79
Ngantuk Ngedite.docx
April 2020 15
Field Bus Guide
June 2020 17
10.pdf
December 2019 27
09 (3).pdf
December 2019 27