final exam information - princeton university

57
Final exam information Final exam. Check out between 4:30pm on 5/18 and 4:30pm on 5/20. 3 hours to complete. Gradescope platform. Rules. Honor Code. Closed book, closed note. Strongly emphasizes material since the midterm. Electronic devices are prohibited (except to take the exam). 8.5-by-11 cheatsheet (both sides, in your own handwriting). Final exam preparation. Practice by doing old exams. Ask questions via Zoom office hours or Ed. 1 see course website for exam archive including associated readings and assignments (but no serious Java programming) see course website for schedule

Upload: others

Post on 13-May-2022

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Final exam information - Princeton University

Final exam information

Final exam.

・Check out between 4:30pm on 5/18 and 4:30pm on 5/20.

・3 hours to complete.

・Gradescope platform.

Rules.

・Honor Code.

・Closed book, closed note.

・Strongly emphasizes material since the midterm.

・Electronic devices are prohibited (except to take the exam).

・8.5-by-11 cheatsheet (both sides, in your own handwriting).

Final exam preparation.

・Practice by doing old exams.

・Ask questions via Zoom office hours or Ed.

1

see course website for exam archive

including associated readings and assignments

(but no serious Java programming)

see course website for schedule

Page 2: Final exam information - Princeton University

ROBERT SEDGEWICK | KEVIN WAYNE

F O U R T H E D I T I O N

Algorithms

Algorithms ROBERT SEDGEWICK | KEVIN WAYNE

Last updated on 4/29/20 10:07 AM

ALGORITHM DESIGN

‣ analysis of algorithms

‣ greedy

‣ network flow

‣ dynamic programming

‣ divide-and-conquer

‣ randomized algorithmshttps://algs4.cs.princeton.edu

Page 3: Final exam information - Princeton University

3

Algorithm design

Algorithm design patterns.

・Analysis of algorithms.

・Greedy.

・Network flow.

・Dynamic programming.

・Divide-and-conquer.

・Randomized algorithms.

Want more? See COS 340, COS 343, COS 423, COS 445, COS 451, COS 488, .…

Page 4: Final exam information - Princeton University

4

Interview questions

Page 5: Final exam information - Princeton University

ROBERT SEDGEWICK | KEVIN WAYNE

Algorithms

‣ analysis of algorithms

‣ greedy

‣ network flow

‣ dynamic programming

‣ divide-and-conquer

‣ randomized algorithms

ALGORITHM DESIGN

https://algs4.cs.princeton.edu

Page 6: Final exam information - Princeton University

Goal. Find T using fewest number of tosses.

6

Egg drop

n

.

.

.

T

.

.

.

.

3

2

1

breaks

does notbreak

Page 7: Final exam information - Princeton University

Goal. Find T using fewest number of tosses.

Variant 0. 1 egg.

Variant 1. ∞ eggs.

Variant 2. 2 eggs.

7

Egg drop

n

.

.

.

T

.

.

.

.

3

2

1

breaks

does notbreak

Page 8: Final exam information - Princeton University

Goal. Find T using fewest number of tosses.

Variant 0. 1 egg.

Solution. Use sequential search: drop on floors

1, 2, 3, …, T until egg breaks.

Analysis. 1 egg and T tosses.

Egg drop

8

n

.

.

.

T

.

.

.

.

3

2

1

breaks

does notbreak

running time depends upon a parameter that you don’t no a priori

Page 9: Final exam information - Princeton University

Egg drop

Goal. Find T using fewest number of tosses.

Variant 1. ∞ eggs.

Solution. Binary search for T.

・Initialize [lo, hi] = [0, n].

・Repeat until length of interval is 1: – drop on floor mid = (lo + hi) / 2. – if it breaks, update hi = mid. – if it doesn’t break, update lo = mid.

Analysis. ~ log2 n eggs, ~ log2 n tosses.

9

n

.

.

.

T

.

.

.

.

3

2

1

breaks

does notbreak

Suppose T is much smaller than n. Can you guarantee Θ(log T) tosses?

Page 10: Final exam information - Princeton University

Egg drop

Goal. Find T using fewest number of tosses.

Variant 1′. ∞ eggs and Θ(log T) tosses.

Solution. Use repeated doubling; then binary search.

・Drop on floors 1, 2, 4, 8, 16, …, x to find a floor

x such that T ≤ x < 2T.

・Binary search in interval [½ x, x].

Analysis. ~ log2 T eggs, ~ 2 log2 T tosses.

・Repeated doubling: 1 egg and 1 + log2 x tosses.

・Binary search: ~ log2 x eggs and ~ log2 x tosses.

・Recall: T ≤ x < 2T.

10

n

.

.

.

T

.

.

.

.

3

2

1

breaks

does notbreak

Page 11: Final exam information - Princeton University

Goal. Find T using fewest number of tosses.

Variant 2. 2 eggs.

In worst case, how many tosses needed as afunction of n?

A. Θ(1)

B. Θ(log n)

C. Θ( )

D. Θ(n)

Algorithm design: quiz 1

n

.

.

.

T

.

.

.

.

3

2

1

breaks

does notbreak

pn

<latexit sha1_base64="19RJT8B+LATTAWXFMhiGPyaBiaI=">AAACMHicbVDLTsJAFJ36RHwBLt00EhNXpPURWZK4cYmJPBJoyHR6CxOm0zpzayANP+FWv8Kv0ZVx61dYShcCnmSSk3PunXvvcSPBNVrWp7GxubW9s1vYK+4fHB4dl8qVtg5jxaDFQhGqrks1CC6hhRwFdCMFNHAFdNzx3dzvPIPSPJSPOI3ACehQcp8ziqnU7esnhYmcDUpVq2ZlMNeJnZMqydEclI1K3wtZHIBEJqjWPduK0EmoQs4EzIr9WENE2ZgOoZdSSQPQTpItPDPPU8Uz/VClT6KZqX87EhpoPQ3ctDKgONKr3lz8z+vF6NedhMsoRpBsMciPhYmhOb/e9LgChmKaEsoUT3c12YgqyjDNaGlK9ncEbOmSZBJLzkIPVlSBE1R0nqK9mtk6aV/W7KvazcN1tVHP8yyQU3JGLohNbkmD3JMmaRFGBHkhr+TNeDc+jC/je1G6YeQ9J2QJxs8vvDGrAA==</latexit>

Page 12: Final exam information - Princeton University

Egg drop (asymmetric search)

Goal. Find T using fewest number of tosses.

Variant 2. 2 eggs.

Solution. Use gridding; then sequential search.

・Toss at floors

until first egg breaks, say at floor .

・Sequential search in interval .

Analysis. At most tosses.

・First egg: ≤ tosses.

・Second egg: ≤ tosses.

Signing bonus 1. Use 2 eggs and at most tosses.

Signing bonus 2. Use 3 eggs and at most 3 n1/3 tosses.12

n

.

.

.

T

.

.

.

.

3

2

1

breaks

does notbreak

p2n

<latexit sha1_base64="Ar6eiC74UkVQbsb3vW7Do6Zen0I=">AAACMXicbVDLTsJAFJ36RHwBLt00EhNXpEWNLEncuMREHgYaMh0uMGE6rTO3BtLwFW71K/wadsatP2FbuhDwJJOcnHPv3HuPGwiu0bIWxtb2zu7efu4gf3h0fHJaKJZa2g8Vgybzha86LtUguIQmchTQCRRQzxXQdif3id9+BaW5L59wFoDj0ZHkQ84oxtJzT78ojKpy3i+UrYqVwtwkdkbKJEOjXzRKvYH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oyhh7EO4AQu0vLtZnAz0e6AxDvx/uAj56/xFYNRwxJv9kK2+EM8JxEc9IScxz3Nxqb8aDAKifDqKVzvSgsOamlZYWaJmtx5aiUeCGH1PPUyJxcUs/2nMB3rwwgK6w/hmGmPu2oZe7cOE99ZS555Ja9qfiS16s46yS1MmXFZPBhUFZp4AKmocFAWULWY08kWuX/CjiSViL7aBemzN4uCRc2qa8ro7AY0JKq+ZqtnPgUo+XMnpPTX+1ot935s9s67MzzXBFfxTfxQ0RiTxyKI3EsugLFX3EjbsVd43+wHmwFXx5Kg8a857NYQLB9D3epuog=</latexit>

Page 13: Final exam information - Princeton University

ROBERT SEDGEWICK | KEVIN WAYNE

Algorithms

‣ analysis of algorithms

‣ greedy

‣ network flow

‣ dynamic programming

‣ divide-and-conquer

‣ randomized algorithms

ALGORITHM DESIGN

https://algs4.cs.princeton.edu

Page 14: Final exam information - Princeton University

14

Greedy algorithms

Make locally optimal choices at each step.

Familiar examples.

・Huffman coding.

・Prim’s algorithm.

・Kruskal’s algorithm.

・Dijkstra’s algorithm.

More classic examples.

・U.S. coin changing.

・Activity scheduling.

・Gale–Shapley stable marriage.

・...

Caveat. Greedy algorithm rarely leads to globally optimal solution.

(but is often used anyway, especially for intractable problems)

Page 15: Final exam information - Princeton University

15

Document search

Given a document that is a sequence of n words, and a query that

is a sequence of m words, find the smallest range in the document

that includes the m query words (in the same order).

Ex. Query = “textbook programming computer”

This book is intended to survey the most important computer algorithms in use today, and to teach fundamental techniques to the growing number of people in need of knowing them. It is intended for use as a textbook for a second course in computer science, after students have acquired basic programming skills and familiarity with computer systems. The book also may be useful for self-study or as a reference for people engaged in the development of computer systems or applications programs, since it contains implementations of useful algorithms and detailed information on performance characteristics and clients.

This book is intended to survey the most important computer algorithms in use today, and to teach fundamental techniques to the growing number of people in need of knowing them. It is intended for use as a textbook for a second course in computer science, after students have acquired basic programming skills and familiarity with computer systems. The book also may be useful for self-study or as a reference for people engaged in the development of computer systems or applications programs, since it contains implementations of useful algorithms and detailed information on performance characteristics and clients.

19 words

Page 16: Final exam information - Princeton University

16

Document search

Given a document that is a sequence of n words, and a query that

is a sequence of m words, find the smallest range in the document

that includes the m query words (in the same order).

Solution.

・For each query word, maintain a queue of positions where it occurs.

textbook: 130 200 250 345

programming: 100 180 184 300 315

computer: 120 150 190 290 320 400

how?

Page 17: Final exam information - Princeton University

17

Document search

Given a document that is a sequence of n words, and a query that

is a sequence of m words, find the smallest range in the document

that includes the m query words (in the same order).

Solution.

・For each query word, maintain a queue of positions where it occurs.

・Maintain smallest range containing m query words, with first word at i.

textbook: 130 200 250 345

programming: 100 180 184 300 315

computer: 120 150 190 290 320 400

range: 130 to ???

Page 18: Final exam information - Princeton University

18

Document search

Given a document that is a sequence of n words, and a query that

is a sequence of m words, find the smallest range in the document

that includes the m query words (in the same order).

Solution.

・For each query word, maintain a queue of positions where it occurs.

・Maintain smallest range containing m query words, with first word at i.

textbook: 130 200 250 345

programming: 100 180 184 300 315

computer: 120 150 190 290 320 400

range: 130 to ???

Page 19: Final exam information - Princeton University

19

Document search

Given a document that is a sequence of n words, and a query that

is a sequence of m words, find the smallest range in the document

that includes the m query words (in the same order).

Solution.

・For each query word, maintain a queue of positions where it occurs.

・Maintain smallest range containing m query words, with first word at i.

textbook: 130 200 250 345

programming: 100 180 184 300 315

computer: 120 150 190 290 320 400

range: 130 to ???

Page 20: Final exam information - Princeton University

20

Document search

Given a document that is a sequence of n words, and a query that

is a sequence of m words, find the smallest range in the document

that includes the m query words (in the same order).

Solution.

・For each query word, maintain a queue of positions where it occurs.

・Maintain smallest range containing m query words, with first word at i.

textbook: 130 200 250 345

programming: 100 180 184 300 315

computer: 120 150 190 290 320 400

range: 130 to 190

Page 21: Final exam information - Princeton University

21

Document search

Given a document that is a sequence of n words, and a query that

is a sequence of m words, find the smallest range in the document

that includes the m query words (in the same order).

Solution.

・For each query word, maintain a queue of positions where it occurs.

・Maintain smallest range containing m query words, with first word at i.

textbook: 130 200 250 345

programming: 100 180 184 300 315

computer: 120 150 190 290 320 400

range: 200 to ???

Page 22: Final exam information - Princeton University

22

Document search

Given a document that is a sequence of n words, and a query that

is a sequence of m words, find the smallest range in the document

that includes the m query words (in the same order).

Solution.

・For each query word, maintain a queue of positions where it occurs.

・Maintain smallest range containing m query words, with first word at i.

textbook: 130 200 250 345

programming: 100 180 184 300 315

computer: 120 150 190 290 320 400

range: 200 to ???

Page 23: Final exam information - Princeton University

23

Document search

Given a document that is a sequence of n words, and a query that

is a sequence of m words, find the smallest range in the document

that includes the m query words (in the same order).

Solution.

・For each query word, maintain a queue of positions where it occurs.

・Maintain smallest range containing m query words, with first word at i.

textbook: 130 200 250 345

programming: 100 180 184 300 315

computer: 120 150 190 290 320 400

range: 200 to ???

Page 24: Final exam information - Princeton University

24

Document search

Given a document that is a sequence of n words, and a query that

is a sequence of m words, find the smallest range in the document

that includes the m query words (in the same order).

Solution.

・For each query word, maintain a queue of positions where it occurs.

・Maintain smallest range containing m query words, with first word at i.

textbook: 130 200 250 345

programming: 100 180 184 300 315

computer: 120 150 190 290 320 400

range: 200 to ???

Page 25: Final exam information - Princeton University

25

Document search

Given a document that is a sequence of n words, and a query that

is a sequence of m words, find the smallest range in the document

that includes the m query words (in the same order).

Solution.

・For each query word, maintain a queue of positions where it occurs.

・Maintain smallest range containing m query words, with first word at i.

textbook: 130 200 250 345

programming: 100 180 184 300 315

computer: 120 150 190 290 320 400

range: 200 to 320

Page 26: Final exam information - Princeton University

26

Document search

Given a document that is a sequence of n words, and a query that

is a sequence of m words, find the smallest range in the document

that includes the m query words (in the same order).

Solution.

・For each query word, maintain a queue of positions where it occurs.

・Maintain smallest range containing m query words, with first word at i.

textbook: 130 200 250 345

programming: 100 180 184 300 315

computer: 120 150 190 290 320 400

range: 250 to 320

Page 27: Final exam information - Princeton University

27

Document search

Given a document that is a sequence of n words, and a query that

is a sequence of m words, find the smallest range in the document

that includes the m query words (in the same order).

Solution.

・For each query word, maintain a queue of positions where it occurs.

・Maintain smallest range containing m query words, with first word at i.

textbook: 130 200 250 345

programming: 100 180 184 300 315

computer: 120 150 190 290 320 400

range: 345 to ???

Page 28: Final exam information - Princeton University

28

Document search

Given a document that is a sequence of n words, and a query that

is a sequence of m words, find the smallest range in the document

that includes the m query words (in the same order).

Solution.

・For each query word, maintain a queue of positions where it occurs.

・Maintain smallest range containing m query words, with first word at i.

textbook: 130 200 250 345

programming: 100 180 184 300 315

computer: 120 150 190 290 320 400

range: 345 to ???

Page 29: Final exam information - Princeton University

29

Document search

Given a document that is a sequence of n words, and a query that

is a sequence of m words, find the smallest range in the document

that includes the m query words (in the same order).

Solution.

・For each query word, maintain a queue of positions where it occurs.

・Maintain smallest range containing m query words, with first word at i.

textbook: 130 200 250 345

programming: 100 180 184 300 315

computer: 120 150 190 290 320 400

range: 345 to ???

Page 30: Final exam information - Princeton University

What is running time as a function of the number of words n in the input and the number of words m in the query? Assume m ≤ n and that each word is at most, say, 20 characters.

A. Θ(log n)

B. Θ(n)

C. Θ(n log n)

D. Θ(n 2)

Algorithm design: quiz 2

Page 31: Final exam information - Princeton University

ROBERT SEDGEWICK | KEVIN WAYNE

Algorithms

‣ analysis of algorithms

‣ greedy

‣ network flow

‣ dynamic programming

‣ divide-and-conquer

‣ randomized algorithms

ALGORITHM DESIGN

https://algs4.cs.princeton.edu

Page 32: Final exam information - Princeton University

32

Network flow

Classic problems on graphs and digraphs.

Familiar examples.

・Shortest paths.

・Bipartite matching.

・Maxflow and mincut.

・Minimum spanning tree.

Other classic examples.

・Minimum-cost arborescence.

・Non-bipartite matching.

・Assignment problem.

・Minimum-cost flow.

・...

Applications. Many many problems can be modeled using network flow.

“reduction”

Page 33: Final exam information - Princeton University

33

Shortest path with orange and black edges

Goal. Given a digraph, where each edge has a positive weight and is orange

or black, find shortest path from s to t that uses at most k orange edges.

s

2 3

1G

t

8

21

4 3

910

7

k = 0: s→1→t (17) k = 1: s→3→t (13)k = 2: s→2→3→t (11)k = 3: s→2→1→3→t (10)

Page 34: Final exam information - Princeton University

34

Shortest path with orange and black edges

Goal. Given a digraph, where each edge has a positive weight and is orange

or black, find shortest path from s to t that uses at most k orange edges.

Solution. Create k+1 copies of the digraph G0, G1, …, Gk. For each edge v→w

・Black: add edge from vertex v in graph Gi to vertex w in Gi.

・Orange: add edge from vertex v in graph Gi to vertex w in Gi+1.

s

2 3

1G

t

s

2 3

1

t

s′

2′ 3′

1′

t′

s"

2" 3"

1"G0 G1 G2

t"

8

8 8 8

2

2 2

1

4 3

9

7

10

Page 35: Final exam information - Princeton University

35

Shortest path with orange and black edges

Goal. Given a digraph, where each edge has a positive weight and is orange

or black, find shortest path from s to t that uses at most k orange edges.

Solution. Create k+1 copies of the digraph G0, G1, …, Gk. For each edge v→w

・Black: add edge from vertex v in graph Gi to vertex w in Gi.

・Orange: add edge from vertex v in graph Gi to vertex w in Gi+1.

・Find shortest path from s to every copy of t (and choose best).

s

2 3

1

t

s′

2′ 3′

1′

t′

s"

2" 3"

1"

t"

8 8 8

2 2

G0 G1 G2

Page 36: Final exam information - Princeton University

What is worst-case running time of algorithm as a function of k, number of vertices V, and number of edges E? Assume E ≥ V.

A. Θ(E log V)

B. Θ(k E)

C. Θ(k E log V)

D. Θ(k2 E log V)

Algorithm design: quiz 3

(k E) log (k V)

number of edges

number of vertices

Dijkstra: E log V

Page 37: Final exam information - Princeton University

ROBERT SEDGEWICK | KEVIN WAYNE

Algorithms

‣ analysis of algorithms

‣ greedy

‣ network flow

‣ dynamic programming

‣ divide-and-conquer

‣ randomized algorithms

ALGORITHM DESIGN

https://algs4.cs.princeton.edu

Page 38: Final exam information - Princeton University

38

Dynamic programming

・Break up problem into a series of overlapping subproblems.

・Build up solutions to larger and larger subproblems.

(caching solutions to subproblems in a table for later reuse)

Familiar examples.

・Shortest paths in DAGs.

・Seam carving.

・Bellman–Ford.

More classic examples.

・Unix diff.

・Viterbi algorithm for hidden Markov models.

・Smith–Waterman for DNA sequence alignment.

・CKY algorithm for parsing context-free grammars.

...

THE THEORY OF DYNAMIC PROGRAMMING RICHARD BELLMAN

1. Introduction. Before turning to a discussion of some representa-tive problems which will permit us to exhibit various mathematical features of the theory, let us present a brief survey of the funda-mental concepts, hopes, and aspirations of dynamic programming.

To begin with, the theory was created to treat the mathematical problems arising from the study of various multi-stage decision processes, which may roughly be described in the following way: We have a physical system whose state at any time / is determined by a set of quantities which we call state parameters, or state variables. At certain times, which may be prescribed in advance, or which may be determined by the process itself, we are called upon to make de-cisions which will affect the state of the system. These decisions are equivalent to transformations of the state variables, the choice of a decision being identical with the choice of a transformation. The out-come of the preceding decisions is to be used to guide the choice of future ones, with the purpose of the whole process that of maximizing some function of the parameters describing the final state.

Examples of processes fitting this loose description are furnished by virtually every phase of modern life, from the planning of indus-trial production lines to the scheduling of patients at a medical clinic ; from the determination of long-term investment programs for universities to the determination of a replacement policy for ma-chinery in factories; from the programming of training policies for skilled and unskilled labor to the choice of optimal purchasing and in-ventory policies for department stores and military establishments.

I t is abundantly clear from the very brief description of possible applications tha t the problems arising from the study of these processes are problems of the future as well as of the immediate present.

Turning to a more precise discussion, let us introduce a small amount of terminology. A sequence of decisions will be called a policy, and a policy which is most advantageous according to some preassigned criterion will be called an optimal policy.

The classical approach to the mathematical problems arising from the processes described above is to consider the set of all possible

An address delivered before the Summer Meeting of the Society in Laramie on September 3, 1953 by invitation of the Committee to Select Hour Speakers for An-nual and Summer meetings; received by the editors August 27,1954.

503

Page 39: Final exam information - Princeton University

39

House coloring problem

Goal. Paint a row of n houses red, green, or blue so that

・No two adjacent houses have the same color.

・Minimize total cost, where cost(i, color) is cost to paint i given color.

A B C D E F

7 6 7 8 9 20

3 8 9 22 12 8

16 10 4 2 5 7

cost to paint house i the given color(3 + 6 + 4 + 8 + 5 + 8 = 34)

Page 40: Final exam information - Princeton University

40

House coloring problem

Goal. Paint a row of n houses red, green, or blue so that

・No two adjacent houses have the same color.

・Minimize total cost, where cost(i, color) is cost to paint i given color.

Subproblems.

・R[i] = min cost to paint houses 1, …, i with i red.

・G[i] = min cost to paint houses 1, …, i with i green.

・B[i] = min cost to paint houses 1, …, i with i blue.

・Optimal cost = min { R[n], G[n], B[n] }.

Dynamic programming recurrence.

・R[i + 1] = cost(i+1, red) + min { G[i], B[i] }

・G[i + 1] = cost(i+1, green) + min { B[i], R[i] }

・B[i + 1] = cost(i+1, blue) + min { R[i], G[i] }

overlapping subproblems

Page 41: Final exam information - Princeton University

What is running time of algorithm as a function of n?

A. Θ(n)

B. Θ(n log n)

C. Θ(n log2 n)

D. Θ(n 2)

Algorithm design: quiz 4

int[] r = new int[n+1]; int[] g = new int[n+1]; int[] b = new int[n+1];

for (int i = 1; i <= n; i++) { r[i+1] = cost[i+1][RED] + Math.min(g[i], b[i]); g[i+1] = cost[i+1][GREEN] + Math.min(b[i], r[i]); b[i+1] = cost[i+1][BLUE] + Math.min(r[i], g[i]); }

int minCost = min3(r[n], g[n], b[n]);

Page 42: Final exam information - Princeton University

ROBERT SEDGEWICK | KEVIN WAYNE

Algorithms

‣ analysis of algorithms

‣ greedy

‣ network flow

‣ dynamic programming

‣ divide-and-conquer

‣ randomized algorithms

ALGORITHM DESIGN

https://algs4.cs.princeton.edu

Page 43: Final exam information - Princeton University

43

Divide and conquer

・Break up problem into two or more independent subproblems.

・Solve each subproblem recursively.

・Combine solutions to subproblems to form solution to original problem.

Familiar examples.

・Mergesort.

・Quicksort.

More classic examples.

・Closest pair.

・Convolution and FFT.

・Matrix multiplication.

・Integer multiplication.

Prototypical usage. Turn brute-force Θ(n2) algorithm into Θ(n log n) one.

needs to take COS 226?

Page 44: Final exam information - Princeton University

44

Music site tries to match your song preferences with others.

・Your ranking of songs: 0, 1, …, n−1.

・My ranking of songs: a0, a1, …, an−1.

・Music site consults database to find people with similar tastes.

Kendall-tau distance. Number of inversions between two rankings.

Inversion. Songs i and j are inverted if i < j, but ai > aj.

Personalized recommendations

A B C D E F G H

you 0 1 2 3 4 5 6 7

me 0 2 3 1 4 5 7 6

3 inversions: 2-1, 3-1, 7-6

Page 45: Final exam information - Princeton University

Problem. Given a permutation of length n, count the number of inversions.

Brute-force n2 algorithm. For each i < j, check if ai > aj .

A bit better. Run insertion sort; return number of exchanges.

Goal. n log n time (or better).

45

Counting inversions

0 2 3 1 4 5 7 6

3 inversions: 2-1, 3-1, 7-6

Page 46: Final exam information - Princeton University

46

Counting inversions: divide-and-conquer

0 4 3 7 9 1 5 8 2 6input

count inversions in left subarray 0 4 3 7 9 1 5 8 2 6

count inversions in right subarray 0 4 3 7 9 1 5 8 2 6

count inversionswith one elementin each subarray

0 4 3 7 9 1 5 8 2 6

4-3

1

5-2 8-2 8-6

3

3-1 3-2 4-1 4-2 7-1 7-2 7-5 7-6 9-1 9-2 9-5 9-6 9-8

13

1 + 3 + 13 = 17output 0 4 3 7 9 1 5 8 2 6

this step seems to require n 2 time

Page 47: Final exam information - Princeton University

47

Counting inversions: divide-and-conquer

0 4 3 7 9 1 5 8 2 6input

count inversions in left subarray

and sort0 3 4 7 9 1 5 8 2 6 1

count inversions in right subarray

and sort

0 3 4 7 9 1 2 5 6 8 3

count inversionswith one element in

each sorted subarray0 3 4 7 9 1 2 5 6 8 13

1 + 3 + 13 = 17and merge intosorted whole

0 1 2 3 4 5 6 7 8 9

Page 48: Final exam information - Princeton University

What is running time of algorithm as a function of n?

A. Θ(n)

B. Θ(n log n)

C. Θ(n log2 n)

D. Θ(n 2)

Algorithm design: quiz 5

Page 49: Final exam information - Princeton University

ROBERT SEDGEWICK | KEVIN WAYNE

Algorithms

‣ analysis of algorithms

‣ greedy

‣ network flow

‣ dynamic programming

‣ divide-and-conquer

‣ randomized algorithms

ALGORITHM DESIGN

https://algs4.cs.princeton.edu

Page 50: Final exam information - Princeton University

50

Randomized algorithms

Algorithm whose performance (or output) depends on the results

of random coin flips.

Familiar examples.

・Quicksort.

・Quickselect.

More classic examples.

・Rabin–Karp substring search.

・Miller–Rabin primality testing.

・Polynomial identity testing.

・Volume of convex body.

・Universal hashing.

・Global min cut.

Page 51: Final exam information - Princeton University

51

Nuts and bolts

Problem. A disorganized carpenter has a mixed pile of n nuts and n bolts.

・The goal is to find the corresponding pairs of nuts and bolts.

・Each nut fits exactly one bolt and each bolt fits exactly one nut.

・By fitting a nut and a bolt together, the carpenter can see which one is

bigger (but cannot directly compare either two nuts or two bolts).

Brute-force algorithm. Compare each bolt to each nut: Θ(n2) compares.

Challenge. Design an algorithm that makes O(n log n) compares.

Page 52: Final exam information - Princeton University

Shuffle. Shuffle the nuts and bolts.

Partition.

・Pick leftmost bolt i and compare against all nuts; divide nuts smaller

than i from those that are larger than i.

・Let i ʹ be the nut that matches bolt i. Compare i ʹ against all bolts;

divide bolts smaller than i ʹ from those that are larger than i ʹ.

Divide-and-conquer. Recursively solve two subproblems.52

Nuts and bolts

3 0 1 4 2 5 6 9 8 7bolts

2′ 1′ 4′ 0′ 3′ 5′ 7′ 8′ 9′ 6′nuts

5 3 6 0 9 1 4 8 2 7

7′ 2′ 8′ 1′ 5′ 9′ 4′ 0′ 6′ 3′

bolts

nuts

smaller nuts larger nuts

smaller bolts larger bolts

Page 53: Final exam information - Princeton University

What is the expected running time of algorithm as a function of n?

A. Θ(n)

B. Θ(n log n)

C. Θ(n log2 n)

D. Θ(n 2)

Algorithm design: quiz 6

same analysis as quicksort (but ~ 2n compares per partition instead of ~ n)

Page 54: Final exam information - Princeton University

Hiring bonus. Algorithm that takes O(n log n) time in the worst case.

54

Nuts and bolts

Chapter 27 Matching Nuts and Bolts in O(nlogn) Time

(Extended Abstract)

Jinos KomMs ‘t4 Yuan Ma 2 Endre Szemerkdi 3y4

Abstract

Given a set of n nuts of distinct widths and a set of n bolts such that each nut corresponds to a unique bolt of the same width, how should we match every nut with its correspond- ing bolt by comparing nuts with bolts (no comparison is allowed between two nuts or between two bolts)? The prob- lem can be naturally viewed as a variant of the classic sort- ing problem as follows. Given two lists of n numbers each such that one list is a permutation of the other, how should we sort the lists by comparisons only between numbers in different lists? We give an O(n log n)-time deterministic al- gorithm for the problem. This is optimal up to a constant factor and answers an open question posed by Alon, Blum, Fiat, Kannan, Naor, and Ostrovsky [3]. Moreover, when copies of nuts and bolts are allowed, our algorithm runs in optimal O(logn) t ime on n processors in Valiant’s parallel comparison tree model. Our algorithm is based on the AKS sorting algorithm with substantial modifications.

1 Introduction

Given a set of n nuts of distinct widths and a set of n bolts such that each nut corresponds to a unique bolt of the same width, how should we match every nut with

‘Department of Mathematics, Rutgers University, P&&away, NJ 08855. Email: komlos&nath.rutgem.edu.

‘Department of Computer Science, Stanford University, CA 94305. Supported by an NSF Mathematical Sciences Post- doctoral Research Fellowship. Part of the work was done while the author was visiting DIMACS, and part of work was done while he was at MIT and supported by DARPA Contracts N00014-91-J-1698 and N00014-92-J-1799. Email: yuanOcs.stanford.edu.

3Department of Computer Science, Rutgers University, Pis- CataWay, NJ 08855. Part of the work was done while the au- thor was at University of Paderborn, Germany. Email: sse- [email protected].

‘The work presented here is part of the “Hypercomputing & Design” (HPCD) project; and it is supported (partly) by ARPA under contract DABT-63-93-C-0064. ‘The content of the infor- mation herein does not necessarily reflect the position of the Government and official endorsement should not be inferred.

its corresponding bolt by comparing nuts with bolts (no comparison is allowed between two nuts or between two bolts)?

This problem can be naturally viewed as a variant of the classic sorting problem as follows. Given two lists of n numbers each such that one list is a permu- tation of the other, how should we sort the lists by comparisons only between numbers in different lists? In fact, the following simple reasoning illustrates that the problem of matching nuts and bolts and the prob- lem of sorting them have the same complexity, up to a constant factor. On one hand, if the nuts and bolts are sorted, then a nut and a bolt at the same position in the sorted order certainly match with each other. On the other hand, if the nuts and bolts are matched, we can sort them by any optimal sorting algorithm in O(n log n) time. Hence, the complexity equivalence of sorting and matching them follows from the simple in- formation lower bound of R(nlogn) on the matching problem, which can be easily derived from the fact that there are n! possible ways to match the nuts and bolts. So in this paper, we will consider the problem of how to sort the nuts and bolts, instead of matching them.

The problem of sorting nuts and bolts has a sim- ple randomized algorithm (e.g., a simple variant of the QUICKSORT algorithm) that runs in the opti- mal O(n logn) expected time [8]. However, finding a nontrivial (say, o(n2)-time) deterministic algorithm has appeared to be highly nontrivial. Alon, Blum, Fiat, Kannan, Naor, and Ostrovsky [3] designed an O(n log4 n)-time deterministic algorithm based on ex- pander graphs, and they posed the open question of de- signing an optimal deterministic algorithm to the prob- lem. Recently, Bradford and Fleischer [6] improved the running time to O(n log’ n), but the question remains open if O(n log n) can be achieved.

Since the classic sorting problem has been inten- sively studied, it is natural to ask if any existing O(n log n)-time deterministic sorting algorithm can be easily adapted to sort nuts and bolts. In a certain sense,

232

Chapter 27 Matching Nuts and Bolts in O(nlogn) Time

(Extended Abstract)

Jinos KomMs ‘t4 Yuan Ma 2 Endre Szemerkdi 3y4

Abstract

Given a set of n nuts of distinct widths and a set of n bolts such that each nut corresponds to a unique bolt of the same width, how should we match every nut with its correspond- ing bolt by comparing nuts with bolts (no comparison is allowed between two nuts or between two bolts)? The prob- lem can be naturally viewed as a variant of the classic sort- ing problem as follows. Given two lists of n numbers each such that one list is a permutation of the other, how should we sort the lists by comparisons only between numbers in different lists? We give an O(n log n)-time deterministic al- gorithm for the problem. This is optimal up to a constant factor and answers an open question posed by Alon, Blum, Fiat, Kannan, Naor, and Ostrovsky [3]. Moreover, when copies of nuts and bolts are allowed, our algorithm runs in optimal O(logn) t ime on n processors in Valiant’s parallel comparison tree model. Our algorithm is based on the AKS sorting algorithm with substantial modifications.

1 Introduction

Given a set of n nuts of distinct widths and a set of n bolts such that each nut corresponds to a unique bolt of the same width, how should we match every nut with

‘Department of Mathematics, Rutgers University, P&&away, NJ 08855. Email: komlos&nath.rutgem.edu.

‘Department of Computer Science, Stanford University, CA 94305. Supported by an NSF Mathematical Sciences Post- doctoral Research Fellowship. Part of the work was done while the author was visiting DIMACS, and part of work was done while he was at MIT and supported by DARPA Contracts N00014-91-J-1698 and N00014-92-J-1799. Email: yuanOcs.stanford.edu.

3Department of Computer Science, Rutgers University, Pis- CataWay, NJ 08855. Part of the work was done while the au- thor was at University of Paderborn, Germany. Email: sse- [email protected].

‘The work presented here is part of the “Hypercomputing & Design” (HPCD) project; and it is supported (partly) by ARPA under contract DABT-63-93-C-0064. ‘The content of the infor- mation herein does not necessarily reflect the position of the Government and official endorsement should not be inferred.

its corresponding bolt by comparing nuts with bolts (no comparison is allowed between two nuts or between two bolts)?

This problem can be naturally viewed as a variant of the classic sorting problem as follows. Given two lists of n numbers each such that one list is a permu- tation of the other, how should we sort the lists by comparisons only between numbers in different lists? In fact, the following simple reasoning illustrates that the problem of matching nuts and bolts and the prob- lem of sorting them have the same complexity, up to a constant factor. On one hand, if the nuts and bolts are sorted, then a nut and a bolt at the same position in the sorted order certainly match with each other. On the other hand, if the nuts and bolts are matched, we can sort them by any optimal sorting algorithm in O(n log n) time. Hence, the complexity equivalence of sorting and matching them follows from the simple in- formation lower bound of R(nlogn) on the matching problem, which can be easily derived from the fact that there are n! possible ways to match the nuts and bolts. So in this paper, we will consider the problem of how to sort the nuts and bolts, instead of matching them.

The problem of sorting nuts and bolts has a sim- ple randomized algorithm (e.g., a simple variant of the QUICKSORT algorithm) that runs in the opti- mal O(n logn) expected time [8]. However, finding a nontrivial (say, o(n2)-time) deterministic algorithm has appeared to be highly nontrivial. Alon, Blum, Fiat, Kannan, Naor, and Ostrovsky [3] designed an O(n log4 n)-time deterministic algorithm based on ex- pander graphs, and they posed the open question of de- signing an optimal deterministic algorithm to the prob- lem. Recently, Bradford and Fleischer [6] improved the running time to O(n log’ n), but the question remains open if O(n log n) can be achieved.

Since the classic sorting problem has been inten- sively studied, it is natural to ask if any existing O(n log n)-time deterministic sorting algorithm can be easily adapted to sort nuts and bolts. In a certain sense,

232

Page 55: Final exam information - Princeton University

Faculty lead preceptors

Undergraduate graders and lab TAs. Apply to be one next semester!

Ed tech. Several developed here at Princeton!

Credits

55

and graduate student AIs.

Page 56: Final exam information - Princeton University

A final thought

56

A farewell video (from P04, Fall 2018)

Page 57: Final exam information - Princeton University

A final thought

“ Algorithms and data structures are love.

Algorithms and data structures are life. ”

— anonymous COS 226 student