Dijkstra's Algorithm & Dynamic Programming (Udemy Sections 28-29)
Name at least three common uses of Dijkstra's Algorithm still used today.
1. GPS (finding fastest route) 2. Network Routing (finding open shortest path for data) 3. Biology (modeling the spread of virus among humans) 4. Airline tickets (finding cheapest route to your destination) 5. Many others!
In JavaScript... 1. fibMemoized(10000) -- what will happen? 2. fibTabulated(10000) -- what will happen?
1. Stack overflow (too many recursive calls for the stack to handle) 2. Infinity (no error, but JS isn't capable of that large of numbers)
Dijkstra's Algorithm finds the ________________ path between two vertices on a graph. A) shortest B) longest
A
Which type of graph does Dijkstra's Algorithm search upon? A) weighted B) unweighted
A
A foundational concept behind dynamic programming is __________________. A) sorting B) recursion C) searching D) stacks/queues
B
Dijkstra's Algorithm searches upon a(n) _____________. A) tree B) graph C) either of these D) neither of these
B
Which can be achieved when using tabulation instead of memoization in dynamic programming? A) better time complexity B) better space complexity C) both
B (Time complexity is roughly the same - O(n).)
The top-down approach to dynamic programming is called memoization. What is the opposite (bottom-up) called? A) Formulation B) Elevator Pattern C) Tabulation D) Methodical
C
Using memoization (i.e., dynamic programming) within a recursive solution for the Fibonacci Sequence takes the time complexity from O(2^n) to what? A) O(1) B) O(logn) C) O(n) D) O(nlogn)
C (As n grows, the time it takes to execute the algorithm grows (roughly) in proportion to n. This is a massive improvement!)
True or False: Most problems can be solved using dynamic programming.
False (It's actually a pretty small subset.)
True or False: Merge sort solutions are a classic example of overlapping subproblems.
False Merge sort is more "divide and conquer" but it's not overlapping subproblems, because you're not repeating the same solution over and over.
True or False: Solutions using recursions always involve overlapping subproblems.
False Recursive solutions involve subproblems, but it doesn't mean they overlap.
True or False: Using plain old recursion to find the value of the nth number in the Fibonacci Sequence is the best solution.
False This is an ideal candidate for dynamic programming because of the overlapping subproblems. Plain old recursion ends up running the exact same code over and over again for something like fib(2) => 1.
True or False: Finding the cheapest flight between two cities is an example of optimal substructure.
False You'd *think* it would be, but going from SF -> SEA -> FAI does NOT necessarily mean that the cheapest way from SF -> SEA is flying direct. (i.e., airlines can be weird with their pricing, so this is NOT optimal substructure.)
What is one classic example of overlapping subproblems?
Fibonacci Sequence 0-1-2-3-5-8-13 Each new solution is an overlapping subproblem of previous solutions.
The Big O of plain old recursion to find the value of the nth number in the Fibonacci Sequence is ________.
O(2^n) It's exponential complexity, which is even worse than quadratic (n^2).
What is the time complexity of a tabulated approach to a Fibonacci Sequence solution?
O(n)
What are the two conditions a problem must have in order to be solved via dynamic programming?
Optimal Substructure & Overlapping Subproblems
True or False: Dynamic programming was "invented" in the 1940s to optimize military scheduling, and was given the name "dynamic" because "it sounded impressive."
True
True or False: Using Dijkstra's Algorithm to find the shortest path between two points in a graph is an example of optimal substructure.
True Because solving shortest path between two points involves *also* finding the shortest path between any two points along the way.
Dijkstra's Algorithm implements a ___________ ____________.
binary heap (priority queue)
Memoization can also be thought of as ______________.
caching
Dynamic programming is defined as "a method for solving a(n) __________ problem by breaking it down into a collection of ____________ subproblems, solving each one of those subproblems just once, and storing their solutions."
complex simpler
Instead of recursion (used in memoization), tabulation uses ________________.
iteration
What is the term in dynamic programming defined as "storing the results of expensive function calls and returning the cached result when the same inputs occur again?"
memoization
Dynamic programming can be described as using ______________ knowledge to make solving a(n) _______________ problem easier.
past future
A problem is said to have "optimal substructure" if an optimal solution can be constructed from optimal solutions of its ___________________.
subproblems
The bottom-down approach to dynamic programming is called tabulation because the previous results are stored in a(n) _______________.
table (usually an array)
Dijkstra claims he came up with his algorithm in 20 minutes by trying to figure out the shortest path by _____________ between two cities in the Netherlands.
train