{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

hw6_sol - CSE 413 Homework 6 Solutions 1 As a dynamic...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 413 Homework 6 Solutions 1) As a dynamic programming solution, the first step is to determine how the problem depends upon a number of small problems. Consider the problem of finding the path to v using at most i edges. This can be denoted as path(v,i). Assume that the problem of finding a shortest path to any vertex using at most i-1 edges has already been solved. How can this information be used to determine path(v,i) ? Consider the vertices adjacent to v. Any path from s to v must pass through one of these vertices in order to reach v, using one edge. In other words, the path to v must be the minimum of the costs of the paths to each adjacent vertex plus the cost of the link between that vertex and v. Because of the less than or equal to constraint, path(v,i) may simply be path(v,i-1) if no such path is shorter. So, path(v,i) = Min(path(v,i-1), {For all w such that w is adjacent to v, Path(w,i-1)+ cost(w->v)}). Practically, the pseudocode for this (see next page) will compute the matrix for dynamic programming in a the reverse fashion. In other words for some i<=k, first initialize the cost for each vertex as the same as the i-1th column. This makes sure to consider the <= constraint in the problem. Then consider any vertex that can be reached using i-1 edges. Compute the cost of a path from each such vertex to its neighbors, using the path computed in the i-1th iteration. If the new path cost to any neighbor is less than the current cost to each of those vertices, replace the current path with the new path. It is important to initialize the cost of the path to each vertex as , except for the source vertex, which will be 0. This allows for a check to see if a vertex has been reached by the algorithm yet (by checking if the cost to that vertex is less than ). Note that this algorithm could be simplified by eliminating extra work. If the path to a vertex was not altered in the last iteration, it is not necessary to check that vertex’s neighbors again because they have already been checked and the cost to travel to any vertex will never go up with the less than or equal constraint. However, this is not done now to make the pseudocode more readable. It would be simple to check the count of edges used to get the last path to do this, as this variable is already required to trace the path back easily.
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern