cpsc 171 introduction to computer science more efficiency of algorithms

17
CPSC 171 Introduction to Computer Science More Efficiency of Algorithms

Upload: alan-mcdonald

Post on 18-Dec-2015

216 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: CPSC 171 Introduction to Computer Science More Efficiency of Algorithms

CPSC 171 Introduction to Computer Science

More Efficiency of Algorithms

Page 2: CPSC 171 Introduction to Computer Science More Efficiency of Algorithms

Quick Quiz

1. In the sequential search algorithm, the minimum amount of work is done if NAME is the ____ name in the list.

2. The ____ case of an algorithm requires the minimum amount of work.

3. The ____ case of an algorithm requires the maximum amount of work.

4. The selection sort algorithm does the same amount of work no matter how the numbers are initially arranged. True or False?

5. n grows at a much faster rate than n2. True or False?

Answer: first

Answer: best

Answer: worst

Answer: True

Answer: false

Page 3: CPSC 171 Introduction to Computer Science More Efficiency of Algorithms

QUICKSORT

Get a list of n elements to sort.

Partition the list with the smallest elements in the first part and the largest elements in the second part.

Sort the first part using Quicksort.

Sort the second part using Quicksort.

Stop.

High level description of quicksort:

Page 4: CPSC 171 Introduction to Computer Science More Efficiency of Algorithms

Two Problems to Deal With:

1) What is the partitioning and how do we accomplish it?2) How do we sort the two parts?

Let’s deal with (2) first: To sort a sublist, we will use the same

strategy as on the entire list- i.e. Partition the list with the smallest elements in

the first part and the largest elements in the second part.

Sort the first part using Quicksort. Sort the second part using Quicksort.

Obviously when a list or sublist has length 1, it is sorted.

Page 5: CPSC 171 Introduction to Computer Science More Efficiency of Algorithms

The First Quicksort Problem

Question (1): What is the partitioning and how do we accomplish it?An element from the list called pivot is used to divide list into two sublists We follow common practice of using the

first element of list as the pivot.

We use the pivot to create A left sublist contains those elements ≤

the pivot A right sublist contains those elements

> the pivot.

Page 6: CPSC 171 Introduction to Computer Science More Efficiency of Algorithms

Partitioning Example

The left pointer moves right until a value > 3 is foundNext, right pointer moves left until a value ≤ 3 is foundThese two values are swapped, and process repeats

3 4 5 1 6 8 7 3 0

3 4 5 1 6 8 7 3 0

3 0 5 1 6 8 7 3 4

3 0 5 1 6 8 7 3 4

3 0 3 1 6 8 7 5 4

3 0 3 1 6 8 7 5 4

Page 7: CPSC 171 Introduction to Computer Science More Efficiency of Algorithms

Partitioning Example (cont)

3 0 3 1 6 8 7 5 4

1 0 3 3 6 8 7 5 4

≤ pivot pivot > pivot

Partitioning stops when the left (white) pointer ≥ the right (blue) pointer. At this point, the list items at the pivot and right pointer are swapped.

Page 8: CPSC 171 Introduction to Computer Science More Efficiency of Algorithms

Partitioning Algorithm1. Set the pivot to the first element in list2. Set the left marker L to the first element of the list3. Set the right marker R to the last element (nth) of

the list4. While L is less than R, do Steps 5-95. While element at L is not larger than pivot and

L≤n6. Move L to the right one position7. While element at R is larger than pivot and R≥18. Move R to the left one position9. If L is left of R then exchange elements at L and R.10. Exchange the pivot with element at R.11. Stop

Page 9: CPSC 171 Introduction to Computer Science More Efficiency of Algorithms

Quicksort Complexity

Best case time complexity (n lg n)

Average case time complexity (n lg n)

Worst case running time (n2)

Worst case examples??? A list that is already sorted A list that is reverse sorted (largest to smallest)

Page 10: CPSC 171 Introduction to Computer Science More Efficiency of Algorithms

Figure 3.22Order-of-Magnitude Time Efficiency Summary

Page 11: CPSC 171 Introduction to Computer Science More Efficiency of Algorithms

Quick Quiz

1. In the shuffle-left algorithm, the best case occurs when the list has no 0 values. True or False?

2. The best case for the converging-pointers algorithm is a list of all 0 entries. True or False?

3. The binary search algorithm works only on a list that has already been ____.

Answer: True

Answer: False

Answer: sorted

Page 12: CPSC 171 Introduction to Computer Science More Efficiency of Algorithms

Figure 3.25Comparisons of lg n, n, n2 , and 2n

Page 13: CPSC 171 Introduction to Computer Science More Efficiency of Algorithms

Figure 3.27A Comparison of Four Orders of Magnitude

Page 14: CPSC 171 Introduction to Computer Science More Efficiency of Algorithms

When Things Get Out Of Hand: Hamiltonian Circuits

A path that begins and ends at the same node and goes through all other nodes exactly once following the edges.

A B

C D

Page 15: CPSC 171 Introduction to Computer Science More Efficiency of Algorithms

Satisfiability Problem

(A or B) and (¬B or ¬C)

(A or B) and (B or C) and (¬A or ¬B)

(A or B) and (¬B or ¬C) and (¬A)

(A or B) and (¬A or ¬C) and (¬B or C) and (A or B or ¬C)

Page 16: CPSC 171 Introduction to Computer Science More Efficiency of Algorithms

Bin Packing Problem

Given: an unlimited number of bins of

volume 1 unit N objects of volume between 0.0 and

1.0

Find: The minimum number of bins to store

the n objects

Page 17: CPSC 171 Introduction to Computer Science More Efficiency of Algorithms

Approximation Algorithms

How would you go about solving the bin-packing problem quickly even if it is the best solution?

(Ever see how luggage is packed on an airplane?)