Lecture Notes on 19 Apr 2023 * Test 3 is on Monday, 24 April 2023. * Do the quiz on that day. It is the honor code. * There is NO class on Monday (24 Apr 2023) and no office hours next week. * Directed Acyclic Graphs (DAGs) https://www.cs.utexas.edu/users/mitra/csSpring2023/cs313/notes/DAG.pdf Topological Sort (Topo Sort) Works on directed graphs that do not have cycles (DAGs) 0. Determine the in_degree for all vertices. The in_degree is the number of edges that are incident on that vertex. 1. Remove the vertices that have an in_degree of 0 to a list and remove the out going edges from those vertices. Sort the list in a given order. Enqueue the vertices into a Queue and then update the in_degree of all remaining vertices. 2. Repeat step 1 until there are no more vertices in the Graph. 3. Dequeue the vertices and print. * Dijkstra's Single Source Shortest Path Algorithm - Choose a starting vertex. - Create a table with all the other vertices in the graph. - From the starting vertex fill the table with the cost of visiting all the other vertices. - Go to the vertex with the lowest cost and update the cost of visiting all the other vertices from the starting vertex. - Go to all the other vertices in order of increasing cost and update the costs of visiting the other vertices from the starting vertex. - When you have visited all the vertices that you can visit from the starting vertex then you are done. * Worked out example using Dijkstra's Algorithm https://www.cs.utexas.edu/users/mitra/csSpring2023/cs313/notes/Dijkstra.pdf * Let us look at some fun puzzles that you can use graphs to solve: https://www.cs.utexas.edu/users/mitra/csSpring2023/cs313/notes/Graph_Puzzles.pdf * Here is one solution: https://www.cs.utexas.edu/users/mitra/csSpring2023/cs313/notes/Graph_Images.pdf * Some additional terms: https://www.cs.utexas.edu/users/mitra/csSpring2023/cs313/notes/Graph_Terms.pdf Dynamic Programming is a computational problem solving paradigm that relies on two factors - overlapping sub-problems and optimal substructure. In dynamic programming you break a problem into sub-problems and use the optimal solution of the sub-problems to solve the main problem. In dynamic programming you store the results of the sub-problems so that you do not repeat the same computations (otherwise known as memoization). A great example of overlapping sub-problems is the computation of terms in the Fibonacci series. The general term in the Fibonacci series can be written as fib (n) = fib (n - 1) + fib (n - 2) fib (n - 1) = fib (n - 2) + fib (n - 3) fib (n - 2) = fib (n - 3) + fin (n - 4) A problem must have optimal substructure property if it can be solved by dynamic programming. Consider the shortest path algorithm that we did last class. If a vertex q lies on the shortest path from p to r then the shortest path from p to r is the sum of the shortest path from p to q and the shortest path from q to r. Dynamic Programming is a computational problem solving paradigm that relies on two factors - overlapping sub-problems and optimal substructure. In dynamic programming you break a problem into sub-problems and use the optimal solution of the sub-problems to solve the main problem. In dynamic programming you store the results of the sub-problems so that you do not repeat the same computations (otherwise known as memoization). A great example of overlapping sub-problems is the computation of terms in the Fibonacci series. The general term in the Fibonacci series can be written as fib (n) = fib (n - 1) + fib (n - 2) fib (n - 1) = fib (n - 2) + fib (n - 3) fib (n - 2) = fib (n - 3) + fin (n - 4) A problem must have optimal substructure property if it can be solved by dynamic programming. Consider the shortest path algorithm that we did last class. If a vertex q lies on the shortest path from p to r then the shortest path from p to r is the sum of the shortest path from p to q and the shortest path from q to r. Here is a worksheet for the classic rod cutting problem: https://www.cs.utexas.edu/users/mitra/csSpring2023/cs313/notes/Rod_Cutting_Problem.pdf https://www.cs.utexas.edu/users/mitra/csSpring2023/cs313/notes/Rod_Cutting_Problem.docx Code for the Rod Cutting Problem def cut_rod (p, n): r = [] s = [] for i in range (n + 1): r.append (0) s.append (0) for j in range (1, n + 1): max_price = -1 for k in range (1, j + 1): new_price = 0 if (k < len(p)): new_price = p[k] + r[j - k] else: new_price = r[j - k] if (new_price > max_price): max_price = new_price # remember the best value of the cut s[j] = k r[j] = max_price print (r) print (s) return r, s def main(): # define the price per length of rod p = [0, 1, 5, 8, 9, 10, 17, 17, 20] # prompt the user to enter the size of the rod to be cut n = int (input ("Enter size of rod: ")) # get the optimal price for cutting a rod of length n r, s = cut_rod (p, n) # print the optimal price print ("Optimal price = ", r[n]) # print the cuts of the rod while (n > 0): print (s[n]) n = n - s[n] main() Code for Number of Cuts for a Given Rod Size: def main(): dim = int (input ("Enter dimension of grid: ")) cuts = [] for i in range (dim + 1): row = [] for j in range (dim + 1): row.append (0) cuts.append (row) for i in range (dim + 1): for j in range (dim + 1): if (i == 0) and (j == 0): cuts[i][j] = 1 else: if (i > j): cuts[i][j] = cuts[i - 1][j] else: cuts[i][j] = cuts[i - 1][j] + cuts[i][j - i] # now print the grid for i in range (dim + 1): for j in range (dim + 1): print (cuts[i][j], end = " ") print () print () main() * Longest Increasing Subsequence: Given a sequence of numbers find the length of the longest increasing subsequence. The subsequence contains elements that are not necessarily contiguous. Here is a sequence of numbers: 3, 1, 4, 5, 9, 2, 6, 8 What is the length of the longest increasing subsequence? The length of the longest increasing subsequence is 5. There are two increasing subsequences of length 5. 3, 4, 5, 6, 8 1, 4, 5, 6, 8 * Here are the worksheets for the Longest Increasing Subsequence: https://www.cs.utexas.edu/users/mitra/csSpring2023/cs313/notes/Longest_Increasing_Subsequence.pdf https://www.cs.utexas.edu/users/mitra/csSpring2023/cs313/notes/Longest_Increasing_Subsequence.docx * Coin Change problem: You have a large number of coins of the following denominations [1, 7, 13, 23]. What is the minimum number of coins that you can use to hand out change for 37? Number of coins: C[37] = min {(C[37 - 1], C[37 - 7], C[37 -13], C[37 -23]} + 1 = min {C[36], C[30], C[24], C[14]} + 1 * Here are the worksheets for the Coin Change Problem: https://www.cs.utexas.edu/users/mitra/csSpring2023/cs313/notes/Coin_Change_Problem.pdf https://www.cs.utexas.edu/users/mitra/csSpring2023/cs313/notes/Coin_Change_Problem.docx * Maximum Weighted Independent Set: You have a set of bottle with different volumes filled with your favorite beverage. You may drink as much as you want with the constraint that you cannot drink from two adjacent bottles. How can you maximize your intake? The volumes are {6, 2, 9, 5, 1, 4, 7, 8, 3} Let us say you have n bottles. Then you chose the last bottle or you do not chose the last bottle. If you do not chose the last bottle then you want to find the best solution from among the first n - 1 bottles. If V[i] is the volume of the ith bottle and S[i] is the largest sum so far, then the solution is of the form: S[i] = max {(V[i] + S[i - 2]), S[i - 1]} * Here are the worksheets for the Maximum Weighted Independent Set: https://www.cs.utexas.edu/users/mitra/csSpring2023/cs313/notes/Maximum_Weighted_Independent_Set.pdf https://www.cs.utexas.edu/users/mitra/csSpring2023/cs313/notes/Maximum_Weighted_Independent_Set.docx * Maximum Nesting Rectangles You did the nesting boxes problems as a recursive problem. You found all combinations of boxes and then found the largest subset of boxes that fit inside one another. This algorithm was of order 2^n and did not scale well for large values of n. We will do a simpler variation of that problem - nesting rectangles using dynamic programming. Assume that you have a large set of rectangles that you can rotate. Find the largest set of rectangles that nest within each other. The sides are integral values. The sides have been sorted - x is the smaller side and y the larger side. The (x, y) pairs representing the sides have been also sorted. Download the worksheet and find the largest set of nesting rectangles. https://www.cs.utexas.edu/users/mitra/csSpring2023/cs313/notes/Nesting_Rectangles.pdf https://www.cs.utexas.edu/users/mitra/csSpring2023/cs313/notes/Nesting_Rectangles.xlsx After you have finished the worksheet write the code that will solve the problem. * Climbing Stairs: Imagine that you have a long flight of stairs to climb. You can take either 1 step, or 2 steps, or 3 steps or any combinations of the three ways to get to the top. Complete the worksheet to get the answer. https://www.cs.utexas.edu/users/mitra/csSpring2023/cs313/notes/Climb_Stairs.pdf https://www.cs.utexas.edu/users/mitra/csSpring2023/cs313/notes/Climb_Stairs.docx * Longest Common Subsequence: Given two strings p and q return the length of the longest common subsequence. A common subsequence is a sequence of characters that appears in the same relative order in both strings but are not necessarily contiguous. https://www.cs.utexas.edu/users/mitra/csSpring2023/cs313/notes/Longest_Common_Subsequence.pdf https://www.cs.utexas.edu/users/mitra/csSpring2023/cs313/notes/Longest_Common_Subsequence.docx * Weighted Job Scheduling: A job is characterized by {Start Time, Finish Time, Profit}. Given a set of jobs chose a subset of jobs that are non-overlapping with maximum profit. https://www.cs.utexas.edu/users/mitra/csSpring2023/cs313/notes/Weighted_Job_Scheduling.pdf https://www.cs.utexas.edu/users/mitra/csSpring2023/cs313/notes/Weighted_Job_Scheduling.docx