HW 3
Possible invariants:
left * right + result = x * y
result = product of y and the
number represented by the
rightmost k bits of the
binary representation of x
Stating these in combination
with the exit condition yields
the correct answer.
left * ri
Questions
Observations
Once an algorithm has been found,
is it correct?
how much time does it take?
can we do better?
Section 0.2 raises two key points.
some functions grow much more quickly that others
exponential algorithms can take impractically l
HW 2
2. basic steps
HW 2
- keep it basic!
Establishing the loop invariant often requires proper
initialization be sure to state what that initialization is!
strategy: compare element to maxsofar
basic step: compare two numbers
e.g. maxsofar is the maxim
Iterative Algorithms.
Developing an Iterative Algorithm
.do repetition using loops.
Two main concerns:
figuring out what the repeated step is
convincingly arguing correctness
.solve problems by moving towards the solution one
iteration at a time.
Our st
HW 2
HW 4
Program captioning: assign employees to programs so that
no employees are assigned overlapping programs and the
fewest number of employees are used.
f(n) = (log n)(log n), g(n) = n/log(n)
Tips/tricks/strategies:
more of the input
simplify alg
HW 7
Developing Recursive Algorithms
Is the number of English words a fair measure of input
size?
establishing the problem
1. specifications
2. size
sums
brainstorming ideas
3. basic steps
i from 0 to n: 7 i3 300 i2 + 16
i from 1 to log n: n i 2
i fr
Patterns
Example
Graph coloring problem d+1 colors, at most d neighbors
Algorithms utilizing a particular approach (e.g. more of the
input) often have similar forms for some of the steps.
input elements
nodes
3. measure of progress
the number of nodes c
HW 5
Dealing With Sums
The asymptotically faster algorithm takes longer when the
program is run. Why?
Sums arise in algorithm analysis when the amount of work
done increases or decreases with each loop iteration.
small n
(and) constant factors, differen
Example
Example
A sorted matrix is a 2D array of numbers such that the
numbers increase across each row and down each column.
(Assume all of the numbers are distinct no duplicates.)
Monster problem.
most obvious measure of progress
distance to shore
Loc
Homework #4
due Mon 2/3
1. Exercise 0.1, pages 8-9. Do parts (a)-(p). (Part (q) is optional.) Justify each of your
answers using the definitions of O, , and , and show your work. If you aren't familiar
with the functions, try plotting them (just be carefu
Homework #3
due Fri 1/31
For each problem, develop an iterative algorithm to solve the problem using the 15-step process.
Write down each of the steps. (You can skip the implementation details and running time.) Also
identify which approach you are using
Homework #5
due Wed 2/5
1. Alice and Bob each implement different algorithms for solving a particular problem.
When they run their programs, they find that the one with the asymptotically faster
algorithm takes longer. What could be going on?
Homework #1
due Mon 1/27
Choose ONE of the following problems.
For your chosen problem, develop an iterative algorithm to solve the problem using the 15-step
process. Write down each of the steps. If you try several approaches or consider multiple
possibi
Homework #2
due Wed 1/29
For each problem, develop an iterative algorithm to solve the problem using the 15-step process.
Write down each of the steps. (You can skip the implementation details and running time.) Also
identify which approach you are using
Tools for Reasoning About Correctness
Reasoning About Correctness
A precondition is a statement about what must be true in
order for the following code to work correctly.
This kind of reasoning about what you know about the
state at various points in the
Lab 5 Test Cases
Lab 5 Test Cases
give specific starting states, inputs, expected outputs
don't confuse implementation with concept
not specific
LinkedSet set = null;
descriptive name: contains
starting state: a LinkedSet containing some elements
input
Lab 2
Sequence in the Java Collections classes
The concept of sequence goes by many names, and has
many variations in how it is realized.
including null
java.util.List
abstract class
supports rank-based operations
only sequential access operations are
Recursion
Implementing Recursion
A recursive definition is one which defines a problem in
terms of another instance of the same kind of problem.
Always via a function/method what makes it recursive is
calling itself.
e.g. Fibonacci numbers
Body always con
Lab 1
Arrays
avoid spaces in filenames
put in line breaks before 80 characters
naming conventions
Arrays are good for lots of things.
fast access to a particular slot by index
can resize as needed with dynamic arrays
instance variables should end wi
Implementing Priority Queue
Other Stack Applications
In the lab, you are using a linked list with the elements
stored in order by priority.
switch box routing
if start pin, push
if end pin, start pin must be the one on top of the stack
But is this the
Generic Programming
Any Kind of Element - Old Style
Why? The Lazy Programmer's Principle:
We don't want to write anything twice, and we especially
don't want to debug anything twice.
Old style: (still valid)
Object is the superclass of all object types
Solitaire Encryption
Solitaire Encryption
Things only done once at the beginning or the end of a
bunch of iterations do not belong in the loop.
Things only done once at the beginning or the end of a
bunch of iterations do not belong in the loop.
ListNode
Trees
Applications of Trees
So far we've only seen linear structures.
Applications of trees.
directory hierarchy
arrays, linked lists
Sequence, Stack, Queue, Priority Queue
Trees are hierarchical structures.
root
child/parent, ancestor/descendant, sibl
Algorithm Analysis
Key Ideas
You have more than one way of doing something.
We are interested in:
how the running time depends on the input size T(n)
the growth rate of T(n) rather than its actual value
e.g. implementing a collection using an array or
Implementing Stacks and Queues
Other Stack Applications
Stack insert/remove at top.
reversing a string
array insert/remove at end
linked list insert/remove at head
paren balancing
Queue insert at end, remove at beginning.
linked list with tail pointe
Correctness and Robustness
Addressing the Problem
A correct program does what it is supposed to do, in all
legal situations.
Programming language features can help prevent problems
or at least detect them when they occur.
e.g. a program to add positive
Abstract Data Types
ADT Sequence
An abstract data type (ADT) refers to
a set of possible values, and
a set of operations on those values
without any specification of how those values are to be
represented or how the operations are to be implemented.
Typ
Efficiency
Variations
Compare arrays to linked lists:
tail pointers
speed up insert at end by maintaining both head and tail pointers
array
insert at tail
insert at head
insert after
remove at tail
remove at head
remove
at
space
linked list
possibly gro
Examples
Examples
Gray code successive values differ by only one bit
Sometimes it is easier to figure out a recursive solution for a
problem.
1-bit gray code: 0, 1
n-bit gray code:
generate (n-1)-bit gray code
prepend 0 to each element
prepend 1 to each