# Assignment 3: Recursion and Big-O

## Preliminaries

Assigned: Monday, 4 October 1999
Due: 10:00 a.m., Monday, 11 October 1999

Summary: In this assignment, you will further ground your study of recursion and Big-O analysis.

Turning it in: Send me a printed copy of anthing you do.

Collaboration: You should work on this individually. You may discuss ideas with fellow students and request help debugging, but each of you should write up your own solutions.

## Problems from Java Plus Data Structures

Do problems 24, 27, 28, and 29 of Chapter 5.

## Big O

Determine the running time (Big-O notation) of each of the following algorithms. Note that you may have to come up with an appropriate metric for the size of each problem.

### (a) Shortest Path

```Suppose you have N locations connected by sidewalks.
To find the shortest path from location A to location B ...
List all the paths from A to B
Find the distance of each path.
Take the smallest of all those values.
```

### (b) Smallest Value

```To find the smallest value in a collection ...
Set smallestSoFar to one value in the collection
While unchecked values remain in the collectoin
Pick one
If that value is smaller than guess then
Set smallestSoFar to that value
Return smallestSoFar
```

### (c) Smallest Value, Revisited

```To find the smallest value in a collection ...
If the collection has only one element
Return that element
Otherwise
Split the collection into two equal halves
Find the smallest value in each half
Return the smaller of those two values
```

### (d) Least Difference

```To find the smallest difference between any two
different numbers in a collection ...
Set estimate to ``infinity''
For each value, v, in the collection
For each value, u, not equal to v
If |u-v| < estimate then
Set estimate to |u-v|
Return estimate
```

## Extra Credit

It's probably not worth your time, but you can obtain extra credit by implementing any of the algorithms in part 2 of the assignment.

## History

Monday, 4 October 1999

• Created

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.