Download s01.1 - UBC ECE

Document related concepts

Network tap wikipedia , lookup

PC/104 wikipedia , lookup

Recursive InterNetwork Architecture (RINA) wikipedia , lookup

Airborne Networking wikipedia , lookup

List of wireless community networks by region wikipedia , lookup

Transcript
Discrete Structures
&
Algorithms
EECE 320 : UBC : Spring 2009
Matei Ripeanu
http://www.ece.ubc.ca/~matei/EECE320/
1
• URL: http://www.ece.ubc.ca/~matei/EECE320/
• Instructor: Matei Ripeanu
•
•
•
matei@...
Office: KAIS 4033
Office hours: after class (Thu 8:00pm -- …)
• Teaching assistants
•
Sarah Motiee <hghasemi@>
Sorting pancakes
3
The chefs at ubchop are sloppy: when they
prepare pancakes, they come out all
different sizes.
When the waiter delivers them to a customer, he
rearranges them (so that smallest is on top, and so
on, down to the largest at the bottom).
He does this by grabbing several from
the top and flipping them over, repeating
this (varying the number he flips) as
many times as necessary.
4
Treating pancakes as numbers
5
5
2
3
4
1
2
3
4
1
5
How do we sort this stack?
How many flips do
we need?
5
2
3
4
1
6
4 flips suffice
5
2
3
4
1
1
4
3
2
5
2
3
4
1
5
7
4
3
2
1
5
1
2
3
4
5
What is the best way to sort?
X = Smallest number of
flips required to sort:
Lower
Bound
5
2
3
4
1
?X?
4
Upper
Bound
8
4 flips are necessary
5
2
3
4
1
1
4
3
2
5
4
1
3
2
5
If we could do it in three flips:
Flip 1 has to put 5 on bottom
Flip 2 must bring 4 to top (if it didn’t,
we would spend more than 3)
9
X=4
4X4
Lower
Bound
Upper
Bound
X=4
10
Pancake numbers
P5 =
Number of flips required to sort the
worst case stack of 5 pancakes
1
2
3
1
4
5
5
4
32
2
1
X1
X2
P5 =
...
X3
5
2
3
4
1
4
...
2
3
1
4
5
X119
1
3
5
2
4
X120
MAX over all s { MIN # of flips to
sort s }; s is a stack of 5 pancakes
11
The 5th pancake number
Lower
Bound
4?  P5  ?
Upper
Bound
12
Defining the pancake number
• Two ways to define P
n
•
•
Pn = maximum over all s { minimum number of
flips needed to sort stack s }; where s is one of
n! stacks
Pn = the number of flips required to sort the
worst-case stack of n pancakes
13
Pn for small n
• How do we
reason about Pn
for n=0, 1, 2, 3?
14
Initial values of Pn
n
0
1
Pn
15
2
3
Initial values of Pn
n
0
1
2
3
Pn
0
0
1
3
16
P3 = 3
1
3
2
requires 3 flips, hence P3 ≥ 3
ANY stack of 3 can be done by getting the
big one to the bottom (≤ 2 flips), and then
using ≤ 1 flips to handle the top two.
17
The
Pn =
Lower
Bound
th
n
pancake number
Number of flips required to sort the
worst case stack of n pancakes.
?  Pn  ?
Upper
Bound
18
?  Pn  ?
Try to find upper
and lower bounds
on Pn, for n > 3.
19
Bracketing (bounding)
[
≤ f(x) ≤
20
]
Procedure: Bring to top
Bring biggest to top
Place it on bottom.
Bring next largest to top
Place second from
bottom.
And so on…
21
Upper bound on Pn
• Use the bring to top method for n pancakes.
•
•
If n=1: no work is needed
Else: flip pancake n to the top and then flip it to
position n.
•
Then: use the bring to top method for the
remaining n-1 pancakes.
• Number of flips: 2(n-1)
22
A better upper bound
• Using the bring to top method, we noted that
we need at most 2(n-1) flips.
• When n=2, however, we need only 1 flip.
• Therefore, we need 2(n-2) flips for the biggest
n-2 items and then 1 flip.
• Total number of flips: 2(n-2)+1 = 2n-3.
23
?  Pn  2n-3
Bring to top is not optimal
5
2
3
4
1
1
4
3
2
5
4
1
3
2
5
2
3
1
4
5
• Bring-to-top takes 5 flips,
• but we need only 4 flips.
25
3
2
1
4
5
Obtaining a lower bound
• The breaking apart argument.
• Suppose a stack S has a pair of
adjacent pancakes that will not be
adjacent in the sorted stack.
• Any sequence of flips that sorts
stack S must have one flip that
inserts the spatula between that
pair and breaks them apart.
• Furthermore, this is true of the
“pair” formed by the bottom
pancake of S and the26plate.
9
16
S
2
4
6
8
.
.
n
1
3
5
.
.
n-1
n  Pn
Suppose n is even:
S contains n pairs that will need
to be broken apart during any
sequence that sorts it.
Detail: This
construction only
works when n>2
27
2
1
S
1
3
5
7
.
.
n
2
4
6
.
.
n-1
n  Pn
Suppose n is odd:
S contains n pairs that will need
to be broken apart during any
sequence that sorts it.
Detail: This
construction only
works when n>3
28
1
3
2
n  Pn  2n – 3 for n > 3
Bring-to-top is
within a factor of
2 of the optimal
sorting scheme!
29
n  Pn  2n – 3 (for n  3)
Starting from
ANY stack we
can get to the
sorted stack
using no more
than Pn flips.
30
From ANY stack to the sorted stack in · Pn.
Pn ?
From the sorted stack to ANY stack in ·…
(((
)))
How?
Reverse the
sequences we use
to sort.
31
Q: From ANY stack to ANY stack in … ?
From ANY stack to sorted stack in · Pn.
From sorted stack to ANY stack in · Pn.
Hence,
From ANY stack to ANY stack in · 2Pn.
32
From ANY stack to ANY stack in · 2Pn.
(((
)))
Can you find a
faster way
than 2Pn flips
to go from
ANY to ANY?
33
From ANY Stack S to ANY stack T in · Pn
Rename the pancakes in S to be 1,2,3,..,n. Rewrite T
using the new naming scheme that you used for S. T
will be some list: p(1),p(2),..,p(n). The sequence of
flips that brings the sorted stack to p(1),p(2),..,p(n)
will bring S to T.
S: 4,3,5,1,2
T: 5,2,4,3,1
1,2,3,4,5
3,5,1,2,4
34
Recap: n  Pn  2n – 3 (for n  3)
Starting from
ANY stack we
can get to the
sorted stack
using no more
than Pn flips.
35
Known pancake numbers
n
1
2
3
4
5
6
7
8
9
10
11
12
13
36
Pn
0
1
3
4
5
7
8
9
10
11
13
14
15
P14 is unknown
• 1 x 2 x 3 x ... x 14 = 14! orderings for 14
pancakes.
• 14! = 87,178,291,200.
37
Computer engineering?
38
Sorting by prefix reversal
Posed in American Mathematical Monthly,
Vol. 82 (1), 1975,
“Harry Dweighter” a.k.a. Jacob Goodman
39
(17/16)n  Pn  (5n+5)/3
William Gates and Christos Papadimitriou.
Bounds For Sorting By Prefix Reversal.
Discrete Mathematics, vol 27, pp 47-57, 1979.
40
(15/14)n  Pn  (5n+5)/3
H. Heydari and H. I. Sudborough. On the
Diameter of the Pancake Network. Journal
of Algorithms, vol 25, pp 67-94, 1997.
41
Pancake networks
• Assume a network with n! nodes:
•
•
For each node, assign it the name of one of the
n! stacks of n pancakes.
Put a wire between two nodes if they are one
flip apart.
42
Pancake network for n=3
1
2
3
3
2
1
2
1
3
2
3
1
43
3
1
2
1
3
2
Pancake network for n=4
44
Pancake network: routing
What is the maximum distance between
two nodes in the pancake network?
Pn
45
Pancake network: reliability
If up to k nodes get hit by lightning does the
network remain connected?
The pancake network is optimally reliable
for its number of edges and nodes.
46
Mutation distance
47
One “simple problem”
A host of problems
and applications at the
frontiers of science.
48
Motivating this course
•Computer science and
engineering is not merely
about computers and
programming …
•
… it is about
(mathematically) modeling our
world and about finding better
ways to solve problems
• Today’s example is a
microcosm of this exercise.
49
Mechanics for the course
50
Mailing list
•
•
•
[email protected]
For questions and general discussion
No discussion about assignments other than to
clarify problem statements
51
Assessment & grading
•
20% Assignments
•
20% Programming project
•
30% Quizzes
•
30% Final exam
•
Participation bonus: up to 5%
52
Homework & Quizzes
• 6 homework assignments over the
course of the term
•
•
•
•
Work in groups of three
Typically assigned on a Thursday, due in a week
Late policy: 33% penalty per late day
Each homework is worth about 3.5%
• 4 quizzes over the term
•
•
•
Individual
30 minutes or so, in class
Each quiz is worth about 6%
53
Ask questions
•Participation bonus: up to 5%
54
54
(((
)))
Summary so far
•
Pancake sorting
•
•
•
•
•
A problem with many applications
Bracketing (bounding a function)
Proving bounds for pancake sorting
You can make money solving such problems (Bill Gates!)
Illustrated many concepts that we will learn in this course
•
•
•
•
Proofs
Sets
Counting
Performance of algorithms
55