Title: Notes
1Notes
- Office hours for jfh Tu/Th 230-4, but will
extend to 430 by request (send email).
2Seam Carving Demo
3Seam Carving Algorithm
- Find cost/path of best seam from bottom row to
bottom (easy)? - Foreach higher row
- foreach pixel
- Find least cost of three pixels beneath
- Mark it as next step in path
- add its cost to importance of this pixel record
this cost - At top row
- Find lowest-cost pixel
- Follow its path
4Dynamic Programming
- Start from the end and work back
- Cache previously computed results
- Instance of inductive thinking
5ltInductiongt
6Mapping the internet
- You can use a tool called ping to find out the
round-trip time to any point in the internet - Can you figure out the topology of the internet
that way (whos connected to whom directly)? - In practice, no
- Topology keeps changing
- Network has cycles multiple ways to get from
here to there
7Reduced problem
- In an acyclic (no cycles) network, if you know
all pairwise distances, can you find the
topology? - Example
- Note symmetry AB BA
8Harder problem
9Harder problem
10Generic Solution
- If we could just reduce the size by one, wed be
in good shape, because that 2x2 problem was
really easy!
11(No Transcript)
12Stacks
- Saw them last semester Java stack-traces.
- The stack trace shows how you got here when you
have an error - The stack tells the program how to continue when
its done with this procedure invocation! - Stacks are great as breadcrumbs (Hansel and
Gretel)? - Good for looking back for recent extreme events
- Great for checking clean code (matching parens,
matching tags in HTML, etc.)?
13StacksThe Stack Interface
public int size()? public boolean
isEmpty()? public E top()? public void push(E
element)? public E pop()?
14Array Based StacksFinite Size Implementation
Constructor public ArrayStack(int capacity)
S ? (E) new Objectcapacity // array t
? -1 // top of stack
15Style Notes
- Note missing from both definitions are the
class-definitions. For Java, it might look like
this - public class ArrayStack
- public ArrayStack(int capacity)
- S ? (E) new
- For Python, its this
- class ArrayStack()
- """Array-based implementation of finite
stack""" - def __init__(self, capacity)
- self.S None for x in range(0,
- Note doc string just after class name, and
indentation.
16Array Based Stacks
public int size() return t 1 public
boolean isEmpty() return (t lt 0) public E
top() if (isEmpty())? throw
EmptyStackException return St public E
pop() if (isEmpty())? throw
EmptyStackException --t return St 1
17Array Based StacksFinite Size Implementation
public void push(E obj) if (t S.length -
1)? throw FullStackException t St ?
obj
18Array Based StacksGrowable Implementation
Constructor public ArrayStack(int capacity)
S ? (E) new Object1 // array t ? -1 //
top of stack
19Array Based Stacks Grow-by-One Implementation
public void push(E obj) if (t S.length() -
1) A ? (E) new
Object S.length 1 for i ? 0 to
S.length - 1 Ai ? Si S ?
A t St ? obj
20Array Based Stacks Array-Doubling Implementation
public void push(E obj) if (t S.length() -
1) A ? (E) new
Object S.length 2 for i ? 0 to
S.length - 1 Ai ? Si S ?
A t St ? obj
21Array Based Stacks
public static void testStack() ArrayStackltInteg
ergt s ? new ArrayStackltIntegergt()? s.push(3)?
s.push(4)? s.push(5)? print(s.pop()) //
'5' print(s.top()) // '4' s.push(8)? s.push(9)
? print(s.size()) // '4' // push a lot of elts
to test // array doubling s.push(0)? print(s.po
p()) // '0' s.push(1)? s.push(6)? s.push(7)?
s.push(9)? s.push(10)? print(s.size()) //
'9' while (!s.isEmpty()) print(s.pop() '
')? // '10 9 7 6 1 9 8 4 3' print(newline)? s
.top() // EmptyStackEx testStack() invoke
test code!
def test_stack() s ArrayStack()? s.push(3)?
s.push(4)? s.push(5)? print s.pop()
'5' print s.top() '4' s.push(8)? s.push(9)?
print len(s) '4' push a lot of elts to
test array doubling s.push(0)? print s.pop()
'0' s.push(1)? s.push(6)? s.push(7)? s.pus
h(9)? s.push(10)? print len(s) '9' while
not s.is_empty() print s.pop(), '10 9 7 6
1 9 8 4 3' print print newline s.top()
EmptyStackEx test_stack() invoke test code!
22Python Exceptions
23Printing in Python
To tell Python what to print when the user tries
to print your stack, you need to implement the
__str__ method. This method constructs a string
and then returns it to be printed by Python.
This is like overriding toString() in Java)?
24Notes on Python Style
def __init__(self, capacity) self.S None
for x in range(0, capacity) self.t -1 top
of stack
- Good practice indicate private variables with
underscore (or even two see python doc) - Problem harder to read, uses more space on
slides. - Solution YOU should do this I wont.
def __init__(self, capacity) self._S None
for x in range(0, capacity) self._t -1 top
of stack