+ All Categories
Home > Documents > New CSE101: Discussion #10 - University of California, San...

New CSE101: Discussion #10 - University of California, San...

Date post: 14-Oct-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
48
CSE101: Discussion #10
Transcript
Page 1: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

CSE101: Discussion #10

Page 2: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Agenda1. Diversity, Equity, and Inclusion: Today’s Guest2. Final Exam Concept Review:

a. Path Finding Algorithmsb. Divide and Conquerc. Greedy Algorithmsd. Dynamic Programming

3. Final Exam Prep Problem

Page 3: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Today’s Guest: Philip Emeagwali- b. 1954 in Akure, Nigeria- Early schooling halted due

to Nigerian civil war- 13yo child-soldier in

Biafran army- Never attended

high-school (self-study)- B.S. Math from OSU- PhD from UMich- 1989 Gordon Bell prize for

high-performance computing

Page 4: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Concept Review: Path Finding AlgorithmsContext

● Assume all graphs are simple i.e. no self-loops● Take note of whether the question specifies:

○ Directed or undirected ○ Acyclic or potentially cyclic○ Positive or non-negative edge weights

● Use any algorithm that was covered explicitly in lecture○ Assume algorithms given in lecture are correct without proof○ Assume runtimes of algorithms given in lecture are correct without proof

Page 5: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Concept Review: Path Finding AlgorithmsDFS

● LIFO i.e. “stack”● O(|V| + |E|)● Solves graph search● Finds SCCs

BFS

● FIFO i.e. “queue”● O(|V| + |E|)● Solves graph search● Finds shortest path i.e. DIJKSTRA (if non-negative edge weights)

Page 6: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Concept Review: Divide and ConquerHow to use Divide and Conquer?

1. Build recursion tree to break big problem into sub-problems2. Solve each sub-problem at leaf node3. Combine sub-problem solutions for big problem solution

How to estimate time complexity of Divide and Conquer?

1. Write recurrence relation2. Apply Master Theorem (if admissible equation)3. Draw recursion tree and use relationships among depth, number of leaf

nodes, branching factor to estimate time complexity without Master Theorem

Page 7: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Concept Review: Divide and ConquerMaster Theorem

Page 8: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Concept Review: Divide and ConquerWhen does the Master Theorem fail?

● a i.e. number of children at each node is not fixed

● a < 1 i.e. each node can not have less than 1 sub-problem

Page 9: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Concept Review: Greedy AlgorithmsHow to use Greedy Algorithms?

1. Break large problem into smaller problems2. Pick best option for first small problem...this is “set in stone”3. Use constraints to limit choices for next small problem given what you have

already “set in stone”4. Keep going until you have solved all the small problems5. Combine solutions to solve big problem

Page 10: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Concept Review: Greedy AlgorithmsHow to prove Greedy Algorithms are optimal?

Exchange Argument or Greedy Stays Ahead

● You are given a problem instance I● Call GS the greedy algorithm solution to I● Call OS the optimal solution to I ● Must show

○ For every I ○ Value of GS >= Value of OS○ Cost GS <= Cost of OS

● *Remember*: You can still do this proof even if you know very little about some imaginary OS

Page 11: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Concept Review: Greedy AlgorithmsUsing Exchange Arguments to prove optimality

1. Define your solutions i.e. GS and OS2. Compare GS and OS

a. Are they different because GS has elements that are not in OS?b. Are they different because GS has elements in a different order than OS?

3. Exchange one of pieces that are different in OS for the pieces in GS4. Show that the value / cost of OS did not change5. Repeat until OS is exactly GS6. Use induction to show that this repetition / iteration doesn’t change the

optimality of the OS and at the end you have GS!

Page 12: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Concept Review: Greedy AlgorithmsUsing Greedy Stays Ahead to prove optimality

1. Define your solutions i.e. GS and OS2. Define a measure (this is the hardest part) that relates to each choice that the

greedy algorithm makes to construct GS and OS (e.g. class scheduling)3. For each step, use induction to show that the cost / value of the GS is always

at least as good if not better than the cost / value of the OS at that step

Page 13: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

What is it?

● Back-tracking + memoization

How do I create a DP algorithm?

1. Create recursive, back-tracking algorithm2. Define sub-problems in recursion tree3. Create matrix to store answers to sub-problems4. Assemble matrix entries into larger solution

Concept Review: Dynamic Programming

Page 14: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Be careful!

● Clearly state each sub-problem● Clearly define the entries in your matrix

○ Dimensions: parameters that change during recursion

● Make sure that the answers in your matrix can actually be combined to give you the answer to your original question

● Time complexity○ # of cells the matrix * number of cases needed to evaluate to compute each cell

Concept Review: Dynamic Programming

Page 15: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Final Exam Prep Problem

Page 16: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Discussion 10 Part 2

Page 17: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem
Page 18: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Review: Selection algorithm from class

Page 19: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Input: A = [(x1, f1), …(xn, fn)], kReturns: value of kth smallest elementKthSmallest(A, k)

if A has only 1 tuple (x1, f1):return x1

Page 20: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Input: A = [(x1, f1), …(xn, fn)], kReturns: value of kth smallest elementKthSmallest(A, k)

if A has only 1 tuple (x1, f1):return x1

let xm = the median of x1...xn

Page 21: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Input: A = [(x1, f1), …(xn, fn)], kReturns: value of kth smallest elementKthSmallest(A, k)

if A has only 1 tuple (x1, f1):return x1

let xm = the median of x1...xnSplit the list into sets L (less than xm) and R (greater than xm)let freq = sum of frequencies of elements in L

Page 22: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Input: A = [(x1, f1), …(xn, fn)], kReturns: value of kth smallest elementKthSmallest(A, k)

if A has only 1 tuple (x1, f1):return x1

let xm = the median of x1...xnSplit the list into sets L (less than xm), R (greater than xm)let freq = sum of frequencies of elements in L

Case where the kth smallest element is the medianif freq < k < freq + fm:

return xm

Page 23: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Input: A = [(x1, f1), …(xn, fn)], kReturns: value of kth smallest elementKthSmallest(A, k)

if A has only 1 tuple (x1, f1):return x1

let xm = the median of x1...xnSplit the list into sets L (less than xm), R (greater than xm)let freq = sum of frequencies of elements in L

Case where the kth smallest element is the medianif freq < k < freq + fm:

return xm

Case where the kth smallest element is in the left halfif k < freq:

return KthSmallest(L, k)

Page 24: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Input: A = [(x1, f1), …(xn, fn)], kReturns: value of kth smallest elementKthSmallest(A, k)

if A has only 1 tuple (x1, f1):return x1

let xm = the median of x1...xnSplit the list into sets L (less than xm), R (greater than xm)let freq = sum of frequencies of elements in L

Case where the kth smallest element is the medianif freq < k < freq + fm:

return xm

Case where the kth smallest element is in the left halfif k < freq:

return KthSmallest(L, k)

Otherwise, the kth smallest element is in the right half. This means the recursion eliminates a bunch of smaller elements, so we subtract from k accordinglyreturn KthSmallest(R, k - freq - fm)

Page 25: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Input: A = [(x1, f1), …(xn, fn)], kReturns: value of kth smallest elementKthSmallest(A, k)

if A has only 1 tuple (x1, f1):return x1

let xm = the median of x1...xnSplit the list into sets L (less than xm), R (greater than xm)let freq = sum of frequencies of elements in L

Case where the kth smallest element is the medianif freq < k < freq + fm:

return xm

Case where the kth smallest element is in the left halfif k < freq:

return KthSmallest(L, k)

Otherwise, the kth smallest element is in the right half. This means the recursion eliminates a bunch of smaller elements, so we subtract from k accordinglyreturn KthSmallest(R, k - freq - fm)

Runtime:Algorithm gets rid of half the list every iteration

T(n) = T(n/2) + O(n) → runtime is O(n)

Page 26: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

(and the goal is to minimize the penalty)

Page 27: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem
Page 28: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Constraints: ● Words must be printed in order:

● Each line has max M characters: (sum of lengths of words is at most M)

Page 29: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Constraints: ● Words must be printed in order: i1 < i2 < … < ik

● Each line has max M characters: (sum of lengths of words is at most M)

Page 30: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Constraints: ● Words must be printed in order: i1 < i2 < … < ik

● Each line has max M characters:

Objective function: ● minimize the penalty

(sum of lengths of words is at most M)

Page 31: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Constraints: ● Words must be printed in order: i1 < i2 < … < ik

● Each line has max M characters:

Objective function: ● minimize the penalty: minimize sum of

(sum of lengths of words is at most M)

Page 32: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem
Page 33: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

G: Places words on the earliest line possible

Suppose OS ends line 1 on a different word than G

i1

j1

[i1 will be somewhere here]

= end of line

Page 34: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

G: Places words on the earliest line possible

Suppose OS ends line 1 on a different word than GOS’: take words from later lines of OS (up to i1) and move them to end of line 1

i1

j1

[i1 will be somewhere here] OS’

= end of line

Page 35: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

G: Places words on the earliest line possible

Suppose OS ends line 1 on a different word than GOS’: take words from later lines of OS (up to i1) and move them to end of line 1

Lines 2+ are still validLine 1 is still valid because G is valid

i1

j1

OS’[i1 will be somewhere here]

= end of line

Page 36: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

G: Places words on the earliest line possible

Suppose OS ends line 1 on a different word than GOS’: take words from later lines of OS (up to i1) and move them to end of line 1

Lines 2+ are still validLine 1 is still valid because G is valid

Net penalty is the same: if d = total length of words between i1 and j1, then Line 1 is +d and Lines 2+ are -d

i1

j1

OS’[i1 will be somewhere here]

= end of line

Page 37: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem
Page 38: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Greedy Optimal

Page 39: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Greedy: 3^2 = 9 Optimal: 1^2 + 1^2 + 1^2 = 3

w(1) = 4, w(2) = 1, w(3) = 3, w(4) = 2, w(5) = 2M = 5

Page 40: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem
Page 41: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Algorithms from class (for directed, unweighted graphs):● DFS● BFS● Topological sort (pre/post orders)● SCC algorithm● Finding sources/sinks in DAGs● Turning directed graph into DAG of SCCs● ...

Page 42: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Algorithms from class (for directed, unweighted graphs):● DFS● BFS● Topological sort (pre/post orders)● SCC algorithm● Finding sources/sinks in DAGs● Turning directed graph into DAG of SCCs● ...

Page 43: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Algorithms from class (for directed, unweighted graphs):● DFS● BFS● Topological sort (pre/post orders)● SCC algorithm● Finding sources/sinks in DAGs● Turning directed graph into DAG of SCCs● ...

Notice that:● If there’s an SCC, we can always find a path that passes through every

vertex within the SCC

Page 44: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Algorithms from class (for directed, unweighted graphs):● DFS● BFS● Topological sort (pre/post orders)● SCC algorithm● Finding sources/sinks in DAGs● Turning directed graph into DAG of SCCs● ...

Notice that:● If there’s an SCC, we can always find a path that passes through every

vertex within the SCC● If G has a source vertex, we need to start at that vertex (since we can’t

come back to it later)

Page 45: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Algorithms from class (for directed, unweighted graphs):● DFS● BFS● Topological sort (pre/post orders)● SCC algorithm● Finding sources/sinks in DAGs● Turning directed graph into DAG of SCCs● ...

Notice that:● If there’s an SCC, we can always find a path that passes through every

vertex within the SCC● If G has a source vertex, we need to start at that vertex (since we can’t

come back to it later)● If G has 2+ source vertices, there can’t be a path

Page 46: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Algorithms from class (for directed, unweighted graphs):● DFS● BFS● Topological sort (pre/post orders)● SCC algorithm● Finding sources/sinks in DAGs● Turning directed graph into DAG of SCCs● ...

Notice that:● If there’s an SCC, we can always find a path that passes through every

vertex within the SCC● If G has a source vertex, we need to start at that vertex (since we can’t

come back to it later)● If G has 2+ source vertices, there can’t be a path

Algorithm description:- Turn G into a DAG of SCCs. - Find the source SCC. If there is more than one, return false- Run explore on the DAG, starting from the source SCC. - If every SCC node is visited, return true. - Otherwise, return false.

Page 47: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Algorithms from class (for directed, unweighted graphs):● DFS● BFS● Topological sort (pre/post orders)● SCC algorithm● Finding sources/sinks in DAGs● Turning directed graph into DAG of SCCs● ...

Notice that:● If there’s an SCC, we can always find a path that passes through every

vertex within the SCC● If G has a source vertex, we need to start at that vertex (since we can’t

come back to it later)● If G has 2+ source vertices, there can’t be a path

Algorithm description:- Turn G into a DAG of SCCs. - Find the source SCC. If there is more than one, return false- Run explore on the DAG, starting from the source SCC. - If every SCC node is visited, return true. - Otherwise, return false.

Runtime is O(|V|+|E|)

Page 48: New CSE101: Discussion #10 - University of California, San Diegocseweb.ucsd.edu/.../cse101-a/Discussion/discussion-10.pdf · 2020. 6. 5. · Build recursion tree to break big problem

Recommended