Lecture 6: Analysis of Algorithms - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Lecture 6: Analysis of Algorithms

Description:

Analysis of Algorithms. News and Reminders. The first Exam is ... We discussed how algorithms (for the same task!) have widely varying resource requirements. ... – PowerPoint PPT presentation

Number of Views:58
Avg rating:3.0/5.0
Slides: 13
Provided by: BruceP53
Category:

less

Transcript and Presenter's Notes

Title: Lecture 6: Analysis of Algorithms


1
Lecture 6 Analysis of Algorithms
2
News and Reminders
  • The first Exam is Wednesday, September 28th. It
    will cover the lectures, the first 3 labs, the
    practice exercises and chapters 1-3 of the
    textbook.
  • There will be no lab on Friday, although the TA's
    will be available to meet with you just send
    email to your TA.

3
Recall that last time
  • We discussed how algorithms (for the same task!)
    have widely varying resource requirements.
  • We discussed how poorly sequential search would
    perform for realistically large databases.
  • We discussed an algorithm that does much better.
    Binary search requires time O(log2n).
  • But, its only useful when the data is sorted.
  • How can we sort data, and can we do it
    efficiently?

4
Sorting a list
First, an algorithm thats easy to write, but is
badly inefficient...
5
The Simple Sort Algorithm
given a list of positive numbers, unsorted1, ,
unsortedn and another list, sorted1, , sortedn,
with all values initially set to zero
  • 1. set i to 1
  • 2. repeat until i gt n
  • set indexForSmallest to the index of the
    smallest
  • positive value in unsorted
  • 4. set sortedi to unsortedindexForSmallest
  • 5. set unsortedindexForSmallest to 0
  • 6. increment i

6
This algorithm is expensive!
7
A much better sorting algorithm Quicksort
Given an unsorted list L1, , Ln 1. if n lt1
then do nothing 2. else 3. choose a
member x of L 4. Set D1 to the list of
those members of L that are less than x 5.
Set D2 to the list of those members of L that are
greater than or equal to x (but
not including x itself) 6. rearrange L so
that D1 is before x and D2 is after x 7.
sort D1 to obtain R1 8. sort D2 to obtain
R2 9. the final sorted list is R1 followed
by x followed by R2
time requirements O(n log2n) space requirements
O(n)
lets look at an example...
8
Example of Quicksort
9
Algorithms with horrendous time requirements
four cities connected by roads
Q Is it possible to start at A, visit every city
exactly once, and return to A? A Obvious to
anyone who looks at the entire map. Not so
obvious to an algorithm that sees, at any
one time, only one city and its roads
start at A, visit B, C, and D in some
order, then return to A
One algorithm to answer the question 1.
generate all possible routes of length 5 2.
check each path to determine whether it meets the
requirement
what is the time requirement? ...
10
All Paths from A of length 5
Number of paths to generate and check is 24
16 The algorithms time requirement is O(2n).
This gets real bad, real fast!
11
Comparing the time requirements
n
work
2n
n2
order 10 50 100
1,000
35 30 25 20 15 10 5 0
log2n .0003 .0006 .0007
.001 n .001 .005 .01
.1 n2 .01 .25
1 1.67 min 2n .1024 3570
4x1016 forget it!
years centuries
n
time requirements for algorithms of various
orders of magnitude. Time is in seconds, unless
stated otherwise Assume the computer can do one
unit of work in 0.0001 seconds
log2n
n
0 5 10 15
12
Exercises
  • Expressed as an order of magnitude,what is the
    time requirement for the algorithms for these
    tasks?
  • (from lab1) find the largest number in a list.
  • (from lab2) calculate the cost of a cement
    driveway
  • (from lab3) find all the printers in a users
    price range
  • Given a list of people and their birth dates,
    find all the people born on July 4th.
  • Given a list of people and their birth dates,
    find all the people who share a birth date with
    someone else on the list.
Write a Comment
User Comments (0)
About PowerShow.com