efficiency of algorithms l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Efficiency of Algorithms PowerPoint Presentation
Download Presentation
Efficiency of Algorithms

Loading in 2 Seconds...

play fullscreen
1 / 20

Efficiency of Algorithms - PowerPoint PPT Presentation


  • 119 Views
  • Uploaded on

Efficiency of Algorithms. February 11th. Efficiency of an algorithm. worst case efficiency is the maximum number of steps that an algorithm can take for any collection of data values. Best case efficiency

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Efficiency of Algorithms' - idola


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
efficiency of an algorithm
Efficiency of an algorithm

worst case efficiency

is the maximum number of steps that an algorithm can take for any collection of data values.

Best case efficiency

is the minimum number of steps that an algorithm can take any collection of data values.

Average case efficiency

- the efficiency averaged on all possible inputs

- must assume a distribution of the input

- we normally assume uniform distribution (all keys are equally probable)

If the input has size n, efficiency will be a function of n

order of magnitude
Order of Magnitude
  • Worst-case of sequential search:
    • 3n+5 comparisons
    • Are these constants accurate? Can we ignore them?
  • Simplification:
    • ignore the constants, look only at the order of magnitude
    • n, 0.5n, 2n, 4n, 3n+5, 2n+100, 0.1n+3 ….are all linear
    • we say that their order of magnitude is n
      • 3n+5 is order of magnitude n: 3n+5 = (n)
      • 2n +100 is order of magnitude n: 2n+100=(n)
      • 0.1n+3 is order of magnitude n: 0.1n+3=(n)
      • ….
context
Context
  • Last time
    • Efficiency of sequential search
    • Order of magnitude (n)
  • Today
    • efficiency of data cleanup algorithms
      • Copy-over, shuffle-left, converging pointers
    • Order of magnitude (n2)
    • New search algorithm: binary search
slide5
Copy-Over Algorithm (Fig 3.2)
  • Get values for n and the list of n values A1, A2, …, An
  • Set left to 1
  • Set newposition to 1
  • While left <= n do
    • If Aleft is non-zero
      • Copy A left into B newposition

(Copy it into position newposition in new list

      • Increase left by 1
      • Increase newposition by 1
    • Else increase left by 1
  • Stop
efficiency of copy over
Efficiency of Copy-Over
  • Best case:
    • all values are zero: no copying, no extra space
  • Worst-case:
    • No zero value: n elements copied, n extra space
    • Time: (n)
    • Extra space: n
shuffle left algorithm fig 3 1
Shuffle-Left Algorithm (Fig 3.1)
  • Get values for n and the listof n values A1, A2, …, An
  • Set legit to n
  • Set left to 1
  • Set right to 2
  • Repeat steps 6-14 until left > legit

6 if Aleftt ≠ 0

7 Increase left by 1

8 Increase right by 1

9 else

10 Reduce legit by 1

      • Repeat 12-13 until right > n
        • Copy Aight into Aright-1
        • Increase right by 1

14 Set right to left + 1

15 Stop

efficiency of shuffle left
Efficiency of Shuffle-Left
  • Space:
    • no extra space (except few variables)
  • Time
    • Best-case
      • No zero value:
      • no copying ==> order of n = (n)
    • Worst case
      • All zero values:
        • every element thus requires copying n-1 values one to the left
      • n x (n-1) = n2 - n = order of n2 = (n2) (why?)
    • Average case
      • Half of the values are zero
      • n/2 x (n-1) = (n2 - n)/2 = order of n2 = (n2)
converging pointers algorithm fig 3 3
Converging Pointers Algorithm (Fig 3.3)
  • Get values for n and the listof n values A1, A2,…,An
  • Set legit to n
  • Set left to 1
  • Set right to n
  • Repeat steps 6-10 until left ≥ right
    • If the value of Aleft≠0 then increase left by 1
    • Else
      • Reduce legit by 1
      • Copy the value of Aright to Aleft

10 Reduce right by 1

  • if Aleft=0 thenreduce legit by 1.
  • Stop
efficiency of converging pointers algorithm
Efficiency of Converging Pointers Algorithm
  • Space
    • No extra space used (except few variables)
  • Time
    • Best-case
      • No zero value
      • No copying => order of n = (n)
    • Worst-case
      • All values zero:
      • One copy at each step => n-1 copies
      • order of n = (n)
    • Average-case
      • Half of the values are zero:
      • n/2 copies
      • order of n = (n)
data cleanup algorithms
Data Cleanup Algorithms
  • Copy-Over
    • worst-case: time (n), extra space n
    • best case: time (n), no extra space
  • Shuffle-left
    • worst-case: time (n2), no extra space
    • Best-case: time (n), no extra space
  • Converging pointers
    • worst-case: time (n), no extra space
    • Best-case: time (n), no extra space
order of magnitude n 2
Order of magnitude (n2)
  • Any algorithm that does cn2 work for any constant c
    • 2n2 is order of magnitude n2 : 2n2=(n2)
    • .5n2 is order of magnitude n2 : .5n2=(n2)
    • 100n2 is order of magnitude n2: 100n2=(n2)
another example
Another example
  • Problem: Suppose we have n cities and the distances between cities are stored in a table, where entry [i,j] stores the distance from city i to city j
    • How many distances in total?
    • An algorithm to write out these distances
      • For each row 1 through n do
        • For each column 1 through n do
          • Print out the distance in this row and column
    • Analysis?
comparison of n and n 2
Comparison of (n) and (n2)
  • (n): n, 2n+5, 0.01n, 100n, 3n+10,..
  • (n2): n2, 10n2, 0.01n2,n2+3n, n2+10,…
  • We do not distinguish between constants..
    • Then…why do we distinguish between n and n2 ??
    • Compare the shapes: n2 grows much faster than n
      • Anything that is order of magnitude n2 will eventually be larger than anything that is of order n, no matter what the constant factors are
      • Fundamentally n2 is more time consuming than n
    • (n2) is larger (less efficient) than (n)
      • 0.1n2 is larger than 10n (for large enough n)
      • 0.0001n2 is larger than 1000n (for large enough n)
the tortoise and the hare
The Tortoise and the Hare

Does algorithm efficiency matter??

    • …just buy a faster machine!

Example:

  • Pentium Pro
    • 1GHz (109 instr per second), $2000
  • Cray computer
    • 10000 GHz(1013 instr per second), $30million
  • Run a (n) algorithm on a Pentium
  • Run a (n2) algorithm on a Cray
  • For what values of n is the Pentium faster?
    • For n > 10000 the Pentium leaves the Cray in the dust..
searching
Searching
  • Problem: find a target in a list of values
  • Sequential search
    • Best-case : (1) comparison
      • target is found immediately
    • Worst-case: (n) comparisons
      • Target is not found
    • Average-case: (n) comparisons
      • Target is found in the middle
  • Can we do better?
    • No…unless we have the input list in sorted order
searching a sorted list
Searching a sorted list
  • Problem: find a target in a sorted list
    • How do we search in a phone book?
    • Can we come up with an algorithm?
      • Check the middle value
      • If smaller than target, go right
      • Otherwise go left
binary search
Binary search
  • Get values for list, A1, A2, ….An, n , target
  • Set start =1, set end = n
  • Set found = NO
  • Repeat until ??
    • Set m = middle value between start and end
    • If target = m then
      • Print target found at position m
      • Set found = YES
    • Else if target < Am then end = m

Else start = m

  • If found = NO then print “Not found”
  • End
efficiency of binary search
Efficiency of binary search
  • What is the best case?
  • What is the worst case?
    • Initially the size of the list in n
    • After the first iteration through the repeat loop, if not found, then either start = m or end = m ==> size of the list on which we search is n/2
    • Every time in the repeat loop the size of the list is halved: n, n/2, n/4,….
    • How many times can a number be halved?
orders of magnitude
Orders of magnitude
  • Order of magnitude ( lg n)
    • Worst-case efficiency of binary search: ( lg n)
  • Comparing order of magnitudes

(1) << (lg n) << (n) << (n2)