Docsity

Issues in Algorithm Design and Analysis - Handout #4 | CS 231, Study notes of Algorithms and Programming

Partial preview of the text

Download Issues in Algorithm Design and Analysis - Handout #4 | CS 231 and more Study notes Algorithms and Programming in PDF only on Docsity!

Issues In AlgorithmDesign and Analysis

CS231 Lecture 1

September 11, 2001

Handout

What is CS231 About?

Designing algorithms (recipes) to solve problems. Proving that an algorithm correctly solves a problem. Analyzing the resources (time, space) required by analgorithm. Exploring techniques for algorithm design and analysis

divide/conquer/glue and recurrence equations induction and loop invariants greediness dynamic programming probabilistic algorithms and analyses amortized analysis Learning standard algorithms in a variety of domains.

Sock-Matching Algorithm 1

Idea:

Scan

from left to right. For each non-matching pair,

swap 2nd element of pair with match for 1st element foundby scanning rightward. CLRS-style Psuedo-code:

1

match(A) 2

for

i

1

to^

(length[A]/2)

do

3

if^

A[2i-1]

A[2i]

then

4

swap(A, 2i,

nextIndex(A, A[2i-1],

2i)

5

nextIndex(A, e, k) 6

for

h

k^

to^

length[A]

do

7

if^

e^

=^

A[h]

then

8

return

h

9

swap(A, i, j) 10

temp

A[i]

11

A[i]

A[j]

12

A[j]

temp

Efficiency

Want to measure the time and space resourcerequirements of algorithms as a function of the problemsize. As shown in the following slides, there are manyissues to consider when doing this.

Measuring Input Size

Standard assumptions:

Size of numerical input is the input itself. Size of array input is length of array. Not always obvious:

Size of tree may be number of nodes or height. Size of number

may be

,^

, or number of bits

(^

Can have more than one size:

Searching for string of length

in text of length

Processing a graph with

vertices and

edges.

In sock matching, could distinguish number colors from number of pairs

CS231 Lec. 1, Sep. 11, 2001 – p.7/

Model of Computation

Typically assume that numerical operations takeconstant time. Addition would take linear time if model only supportedincrement operations. In practice, operations take time proportional to numberof bits (lg n). We will generally assume sequential rather than parallelmodel. (See CS331 for Parallel algorithms.)

17

8

23

42

6

37

19

91

+^

+^

+^

+

+^

+

+

Asymptotics

Want to avoid tedious and overwhelming bean-counting. We will use

asymptotic notation

to abstract over details of

running times. For two functions

and

means “

is way smaller than

means “

is at most

means “

is about the same as

means “

is at least

means “

is way bigger than

You may be familiar with some of these from CS230; wewill study all of them in the next lecture.

Analyzing Time Behavior of Algorithm 1

What is the best-case input for Algorithm 1?What is the asymptotic best-case running time

for

Algorithm 1?What is the worst-case input for for Algorithm 1?What is the asymptotic worst-case running time

for

Algorithm 1?

CS231 Lec. 1, Sep. 11, 2001 – p.11/

Analyzing Time Behavior of Algorithm 2

Does the running time of Algorithm 2 depend on the input?What is the asymptotic running time

for Algorithm 2?

What About Space?

Counting space

An integer variable takes one space unit. A variable containing an array takes one space unit(a pointer) plus the space for the array. An array of length

takes the space of

variables.

A function parameter counts as a variable. Non-tail calls create a “call frame”; tail calls do not. Typically distinguish “working space” from space forinput and output. What is the asymptotic space

for Algorithm 1?

What is the asymptotic space

for Algorithm 2?

Pop Quiz

What is the main topic of this course?1. Quotes of the previous Vice President.2. Patterns of growth in water flora.3. Recipes and resources.4. Habits of accompanying male friends.