hw09solutions - CSE 331 Spring 2008 Section 1 Homework 9...

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

View Full Document Right Arrow Icon
CSE 331 Spring 2008, Section 1 Homework 9 Solutions 1. Consider the following graph: This is a directed, acyclic graph containing a single negative-weighted edge. If we run Dijkstra's algorithm on this graph, we will get the following series of tables. Step Known Distance Parent A B C A B C A B C init F F F 0 / / / A T F F 0 1 2 / A A B T T F 0 1 2 / A A C T T T 0 1 2 / A A As we can see, Dijkstra's algorithm has us go from A to B directly, following an edge with a weight of 1. Once it has found this path and removed B from the priority queue, Dijkstra's algorithm assumes that it is the shortest possible path to B and ignores the negative-weighted edge from B to C. As a result, Dijkstra's algorithm misses the true minimum-weight path from A to B, which passes from A to C to B for a total weight of (2 – 47) = -45. 2. Our goal is to find the number of minimum-weight paths from vertex v to each vertex u . We will denote this number as count[ u ]. There are three cases we need to consider when modifying Dijkstra's algorithm: initializing the count table, updating count when we find a new minimum path, and updating count when we find a duplicate minimum path. Working with the pseudocode from the text (figure 9.31), we make the following changes: In the for each loop at the beginning of the dijkstra function, we add a line to set count[ v ] to 0. Within the innermost if block, when we update w .path to point back to v , we will also set count[ w ] to count[ v ]. In this case, we have found that if we visit
Background image of page 1

Info iconThis 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 ]}

Page1 / 5

hw09solutions - CSE 331 Spring 2008 Section 1 Homework 9...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online