Basic ideas of Efficiency - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Basic ideas of Efficiency

Description:

First Part of Chapter 9 1. Basic Ideas Concerning Efficiency 2 ... unction looks much better for small numbers, but we. don't care. ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 22
Provided by: kurbanni
Category:

less

Transcript and Presenter's Notes

Title: Basic ideas of Efficiency


1
First Part of Chapter 9 1
  • Basic ideas of Efficiency
  • Efficiency of Search Algorithms
  • Selection Sort
  • Bubble Sort
  • Insertion Sort

2
Basic Ideas Concerning Efficiency 2
  • The speed of a program depends at least on
  • On what computer are you running?
  • What data are you running it on?
  • How good is the algorithm that you are
  • using?

3
  • We are studying algorithms, so we need to compare
  • algorithms, to find out which ones are good and
    which ones
  • are bad.
  • But we cannot compare algorithms without
    eliminating the other two influences
  • hardware
  • data
  • How do we eliminate the influence of the
    computer?
  • We dont use a computer at all. We perform a
    mathematical analysis.

3
4
  • How do we eliminate the influence of the data?
  • We perform the analysis with the worst possible
  • data so that we get the worst possible case.
  • Basic idea of the mathematical analysis
  • How many comparisons are performed by the
  • algorithm, if we have N input data items?
  • How many data items are moved by the
  • algorithm if we have N input data items?

4
5
  • Example How many comparisons are
  • performed when we are performing a sequential
    search in
  • an array with Nnumbers?
  • We are looking for a number in this array, say
    99. Now
  • we have to compare 99 with 29, with 10, with 14,
    etc.
  • Altogether we have to compare it with N
    numbers. Note
  • that 99 does not exist in the array. Clearly that
    defines
  • a worst case.

3
4
0
1
2
6
  • Are N comparisons good for a search algorithm?
  • Imagine you have 1,000,000 N numbers.
  • 1,000,000 comparisons would take a while. If you
  • would use Binary Search (discussed previously)
    you could
  • get away with about 320 comparisons.
  • 60 is certainly much, much, MUCH, butter
  • than a 1,000,000. So, we can conclude that
  • binary search is MUCH better than sequential
  • search. In practice we would even ignore the
    3.
  • (Well return to this in more detail, later.)

7
  • Do we really have to worry about this
    complexity
  • business? Lets take a look at a couple of common
  • mathematical functions
  • N 10 100 1,000 1,000,000
  • log n 3 6 9 19
  • N 10 100 1,000 1,000,000
  • N log N 30 664 9,965 10,000,000
  • s qr (N) 100 10,000 1,000,000 1012
  • c u b e (N) 1,000 106 109 1018
  • 2N 103 1030 10301 10301,030

8
  • Just to keep the perspective
  • Lets say we have a computer running at 100
  • MHz. That means I have 108 cycles per second.
  • 1 Year has 31,536,000 seconds, thats about
  • another 108 . In one year I can have at best
    1016 Cycles
  • If I use a program that processes 100 numbers at
    2 N speed
  • I would need 10 YEARS to process 100 numbers.
    Thats
  • 100,000 BILLION YEARS. Thats much longer than
    even
  • generous estimates about how old the universe is.

9
  • SO We must care.
  • We are ONLY interested in large problems.
  • Nobody cares about the speed of searching an
  • array of 10 numbers.
  • In fact, we are only interested in the growth
    -rate
  • of the run time, and not in proportionality
  • factors.

10
  • That means, if we have a choice between a program
    with
  • run time 100N and a program with 0.01 N2, the
    square
  • unction looks much better for small numbers, but
    we
  • dont care. For large N, it is still the square
    that is worse.
  • Now comes the mathematical formulation
  • DEFINITION Order of an Algorithm. An algorithm A
    is
  • of order f(N) if there is some constant c, such
    that for all
  • but a finite number of exceptions, A can solve a
    problem
  • of size N in time less than or equal to c f(N)

11
  • Big O Notation
  • To express that an algorithm is of order f(N)
  • we write that it is O(f(N)).
  • The function f(N) is called the growth-rate
  • function of the algorithm.
  • Mathematical Properties of the Big O
  • notation.
  • (1) You can ignore lower order functions.
  • O(N3) O(N3 N2)

12
  • (2) You can ignore proportionality factors.
  • O(4 N) O(N)
  • (3) You can combine growth-rate functions.
  • O(f(N)) O(g(N)) O(f(N)g(N))
  • If f(N) g(N) N, then the above becomes by
  • property (2))
  • O(N)

13
searching algorithms 13
  • Back to searching algorithms.
  • Sequential Search In the worst case, you have to
    look at all N elements. Therefore, it is O(N). On
    average, if the number is in the array, you need
    N/2 comparisons. Thats also O(N).
  • Binary Search
  • I compare the middle number with the number I am
    looking for, and if they are different, I divide
    the array in half. How many times can I divide an
    array in half, until I have only one element?

14
  • In other words, what is the solution of
  • 2k N
  • for k? By definition it is
  • k log2N (binary logarithm!)
  • If N is a power of 2, it follows that the
    complexity of Binary Search is O(log2N).
  • What if N is not a power of 2?
  • You can easily find the smallest k such that
  • 2k-1 lt n lt 2k
  • Then we would get
  • k 1 log2 N (with N rounded down)
  • This is still O(log2N). Check your book for the
    derivation.

15
Selection Sort 15
  • Basic Idea
  • Select the largest element of an array.
  • Swap the last element with the largest element.
  • Now do the same thing again, for an array that
    has
  • one less element.
  • Keep doing this, until you are down to an
  • array of 2 elements.
  • Example Sort this array
  • 0 1 2 3 4
  • Advice If you want to be sure you avoid certain
    mistakes, draw the array frame.

16
  • The largest element is at position 3.
  • Swap the elements at 3 and at 4.
  • 0 1 2 3 4
  • 29 10 14 13 37
  • Now do this again, but without the last element
    which can NEVER move out of its place.
  • 0 1 2 3 4
  • 29 10 14 13 37
  • Largest element is at position 0.
  • Swap elements at 0 and 3.

17
  • 0 1 2 3 4
  • 13 10 14 29 37
  • Now 29 is at its final position. It can NEVER
    move away.
  • So we can ignore it.
  • 0 1 2 3 4
  • 13 10 14 29 37
  • Now that we have the idea we drop the array
    frame.
  • The largest number is at position 2.
  • Swap the largest number with the last number. (It
    is
  • already at the last position, but the computer
    does not
  • test for that.)

18
  • 13 10 14 29 37
  • Select largest number. It is at position 0.
  • Swap the largest number with the last number.
  • 10 13 14 29 37
  • We are done. If there is only one number left
    (10) then
  • it must be the smallest number.
  • Therefore, the whole array is sorted.
  • 0 1 2 3 4
  • 10 13 14 29 37

19
  • void SelectionSort(dataType A, int N)
  • int L
  • for(int Last N-1 Last gt 1 --Last)
  • L IndexOfLargest(A, Last1)
  • swap(AL, ALast)
  • int IndexOfLargest(const dataType A, int size)
  • int IndexSoFar 0
  • for (int CurrIndex 1 CurrIndex lt size
    CurrIndex)
  • if (ACurrIndex gt AIndexSoFar)
  • IndexSoFar CurrIndex
  • return IndexSoFar

20
  • void Swap(dataType X, dataType Y)
  • dataType Temp X
  • X Y
  • Y Temp
  • 1st call N-1 times
  • 2nd call N-2 times
  • 3rd call N-3 times
  • last call 1 time
  • There is 1 comparison per run through the loop.
  • (N-1)(N-2)...1 N(N-1)/2

21
  • There are N-1 calls to swap. Swap performs 3 data
  • moves.
  • 3 (N-1)
  • The total number of operations is then
  • N2/2 5N/2 -3
  • According to our previous rules, we can neglect
    the
  • linear and constant components. Selection sort is
  • therefore O(NN).
Write a Comment
User Comments (0)
About PowerShow.com