new cse101: discussion #10 - university of california, san...
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/1.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/2.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/3.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/4.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/5.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/6.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/7.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/8.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/9.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/10.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/11.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/12.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/13.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/14.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/15.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/16.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/17.jpg)
![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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/18.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/19.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/20.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/21.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/22.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/23.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/24.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/25.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/26.jpg)
(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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/27.jpg)
![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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/28.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/29.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/30.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/31.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/32.jpg)
![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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/33.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/34.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/35.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/36.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/37.jpg)
![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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/38.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/39.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/40.jpg)
![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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/41.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/42.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/43.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/44.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/45.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/46.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/47.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081407/604cece397c4bd532d1a3eb4/html5/thumbnails/48.jpg)