Top Banner
CSE 830: Design and Theory of Algorithms Dr. Eric Torng
35

CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

CSE 830:Design and Theory of Algorithms

Dr. Eric Torng

Page 2: CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

Overview

• Administrative stuff…

• Lecture schedule overview

• What is an algorithm? What is a problem?

• How we study algorithms

• Some examples

Page 3: CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

What is an Algorithm?

According to the Academic American Encyclopedia:

An algorithm is a procedure for solving a usually complicated problem by carrying out a precisely determined sequence of simpler, unambiguous steps. Such procedures were originally used in mathematical calculations (the name is a variant of algorism, which originally meant the Arabic numerals and then "arithmetic") but are now widely used in computer programs and in programmed learning.

Page 4: CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

What is an Algorithm?

Algorithms are the ideas behind computer programs.

An algorithm is the thing that stays the same whether the program is in C++ running on a Cray in New York or is in BASIC running on a Macintosh in Katmandu!

To be interesting, an algorithm has to solve a general, specified problem.

Page 5: CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

What is a problem?

• Definition– A mapping/relation between a set of input instances

(domain) and an output set (range)• Problem Specification

– Specify what a typical input instance is– Specify what the output should be in terms of the input

instance• Example: Sorting

– Input: A sequence of N numbers a1…an

– Output: the permutation (reordering) of the input sequence such that a1 a2 … an .

Page 6: CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

Types of Problems

Search: find X in the input satisfying property Y

Structuring: Transform input X to satisfy property Y

Construction: Build X satisfying Y

Optimization: Find the best X satisfying property Y

Decision: Does X satisfy Y?

Adaptive: Maintain property Y over time.

Page 7: CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

Two desired properties of algorithms

• Correctness– Always provides correct output when presented

with legal input

• Efficiency– What does efficiency mean?

Page 8: CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

Example: Odd or Even?

What is the best way to determine if a number is odd or even? Some of the algorithms we can try are:

• Count up to that number from one and alternate naming each number as odd or even.

• Factor the number and see if there are any twos in the factorization.

• Keep a lookup table of all numbers from 0 to the maximum integer.

• Look at the last bit (or digit) of the number.

Page 9: CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

Example: TSP

• Input: A sequence of N cities with the distances dij between each pair of cities

• Output: a permutation (ordering) of the cities <c1’, …, cn’> that minimizes the expression {j =1 to n-1} dj’,j’+1 + dn’,1’

Page 10: CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

Example Circuit

Page 11: CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

Not Correct!

Page 12: CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

A better algorithm?

What if we always connect the closest pair of points that do not result in a cycle or a three-way branch? We finish when we have a single chain.

Page 13: CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

Still Not Correct!

Page 14: CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

A Correct Algorithm

We could try all possible orderings of the points, then select the ordering which minimizes the total length:

d = For each of the n! permutations, Pi of the n points,

if cost(Pi) < d thend = cost(Pi)Pmin = Pi

return Pmin

Page 15: CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

Areas of study of algorithms

1. How to devise correct and efficient algorithms for solving a given problem

2. How to express algorithms3. How to validate/verify algorithms4. How to analyze algorithms5. How to prove (or at least indicate) no

correct, efficient algorithm exists for solving a given problem

Page 16: CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

How to devise algorithms

• Something of an art form

• Cannot be fully automated

• We will describe some general techniques and try to illustrate when each is appropriate

• Major focus of course

Page 17: CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

Expressing Algorithms

• Implementations

• Pseudo-code

• English

• NOT our point of emphasis in this course

Page 18: CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

Verifying algorithm correctness

• Proving an algorithm generates correct output for all inputs

• One technique covered in textbook– Loop invariants

• We will do some of this in the course, but it is not emphasized as much as algorithm design or algorithm analysis

Page 19: CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

Analyzing algorithms

• The “process” of determining how much resources (time, space) are used by a given algorithm

• We want to be able to make quantitative assessments about the value (goodness) of one algorithm compared to another

• We want to do this WITHOUT implementing and running an executable version of an algorithm

• Major point of emphasis of course– Question: How can we study the time complexity of an

algorithm if we don’t run it or even choose a specific machine to measure it on?

Page 20: CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

The RAM Model

• Each "simple" operation (+, -, =, if, call) takes exactly 1 step.

• Loops and subroutine calls are not simple operations, but depend upon the size of the data and the contents of a subroutine. We do not want “sort” to be a single step operation.

• Each memory access takes exactly 1 step.

Page 21: CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

Measuring Complexity

• The worst case complexity of the algorithm is the function defined by the maximum number of steps taken on any instance of size n.

• The best case complexity of the algorithm is the function defined by the minimum number of steps taken on any instance of size n.

• The average-case complexity of the algorithm is the function defined by an average number of steps taken on any instance of size n.

Page 22: CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

Best, Worst, and Average Case

Page 23: CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

Case Study: Insertion SortOne way to sort an array of n elements is to start with an empty list, then successively insert new elements into their proper position, scanning from the “right end” back to the “left end”

Loop invariant used to prove correctness

How efficient is insertion sort?

Page 24: CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

Correctness Analysis

What is the loop invariant? That is, what is true before each execution of the loop?

for i = 2 to nkey = A[i]j = i - 1while j > 0 AND A[j] > key

A[j+1] = A[j] j = j -1

A[j+1] = key

Page 25: CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

Exact Analysis

Count the number of times each line will be executed:

Num Exec.for i = 2 to n (n-1) + 1

key = A[i] n-1j = i - 1 n-1while j > 0 AND A[j] > key ?

A[j+1] = A[j] ?j = j -1 ?

A[j+1] = key n-1

Page 26: CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

Best Case

Page 27: CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

Worst Case

Page 28: CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

Average Case

Page 29: CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

Average Case (Zoom Out)

Page 30: CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

1: for i = 2 to n2: key = A[i] 3: j = i - 14: while j > 0 AND A[j] > key5: A[j+1] = A[j]6: j = j -17: A[j+1] = key

[3] [7] [4] [9] [4]

1: i = 32: key = 43: j = 24: (while true!)5: A[3] = A[2]

[3] [7] [7] [9] [4]

6: j = 14: (while false!)7: A[2] = 4

[3] [4] [7] [9] [4]

1: i = 42: key = 93: j = 34: (while false!)7: A[4] = 9

[3] [7] [4] [9] [4]

1: i = 22: key = 73: j = 14: ( while false! )7: A[2] = 7

[3] [4] [7] [9] [4]

1: i = 52: key = 43: j = 44: (while true!)5: A[5] = A[4]

[3] [4] [7] [9] [9]

6: j = 34: (while true!)5: A[4] = A[3]

[3] [4] [7] [7] [9]

6: j = 24: (while false!)7: A[3] = 4

[3] [4] [4] [7] [9]

Page 31: CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

Measuring Complexity

What is the best way to measure the time complexity of an algorithm?

- Best case run time?

- Worst case run time?

- Average case run time?

Which should we try to optimize?

Page 32: CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

Best Case Analysis

How can we modify almost any algorithm to have a good best-case running time?

Solve one instance of it efficiently.

Page 33: CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

Average case analysis

• Based on a probability distribution of input instances

– Distribution may not be accurate

• Often more complicated to compute than worst case analysis

• Often worst case analysis is a pretty good indicator for average case

– Though not always true: quicksort, simplex method

Page 34: CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

Best, Worst, and Average Case

Page 35: CSE 830: Design and Theory of Algorithms Dr. Eric Torng.

Worst case analysis

• Need to find and understand input that causes worst case performance

• Typically much simpler to compute as we do not need to “average” out performance

• Provides guarantee that is independent of any assumptions about the input

• The standard analysis performed