CS170 Midterm 2 Lecture Notes.docx - October 2 2019 Lecture Dynamic Programming o Longest Path in a DAG Input A DAG(Directed Acyclic Graph Graph G with

# CS170 Midterm 2 Lecture Notes.docx - October 2 2019 Lecture...

• Notes
• 5
• 100% (1) 1 out of 1 people found this document helpful

This preview shows page 1 - 3 out of 5 pages.

October 2, 2019 Lecture: Dynamic Programming o Longest Path in a DAG Input: A DAG (Directed Acyclic Graph), Graph G with edges {1, …, n} o Assume: 1, 2, …, n is a topological sort of the graph 1 2 3 4 5 6- n Goal: Find the length of the longest path Step 1: Define: “Subproblems” Let L[i] be the length of the longest path ending at vertex i o L[1], L[2], …, L[n], n subproblems o Longest path in DAG = max(L[1], L[2], …, L[n]) Step 2: Write a recurrence relation among subproblems Look at all the edges that end at vertex i, take vertex 7 for instance o If there is an edge from vertex 3 to vertex 7, the length of the longest path will be equal to L[3] + 1 o If there is an edge from vertex 5 to vertex 7, the length of the longest path will be equal to L[5] + 1, etc. o The longest path will be a maximum of these recurrence solutions Step 3: Use the Recurrence Relation to solve subproblems Assume there is an edge from vertex j to vertex i, then the longest path to vertex i is equal to the max{L[j] + 1} for all j < i. Pseudocode o Initialize all L[i] = 0 for all i from 1, …, n o For I = 1 to n L[i] = maximum, over j I for j < i, {L[j] + 1} o Return maximum {L[1], L[2], L[3],…, L[n]} You need previous pointers so that you can recover the path for which max is attained. For example prev[i] = j. The Runtime of this Algorithm is Linear in terms of its edges, O(|E| + |V|) o Longest Increasing Subsequence (LIS) Input: Array of numbers: A[1], A[2], … , A[n] Goal: Find the LIS Convert all the numbers A[1], …, A[n] into vertices Draw an edge from k j if A[k] < A[j] The longest path in the DAG will be equal to the longest increasing subsequence We can use the Longest Path in a DAG algorithm to solve this problem. Runtime:
The number of edges in this DAG can be approximately O(n 2 ). The worst case scenario is Omega(n 2 ) since there cannot be more edges. October 4, 2019: Lecture Dynamic Programming II o Knapsack There are bunch of items, lets say A, B, C, …, F. Each item has an associated weight. Each item also has an associated value. What is the most valuable set of items you can put into your knapsack so that it does not exceed the weight limit but maximizes its value.

#### You've reached the end of your free preview.

Want to read all 5 pages?

• Fall '09

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern