hw09solutions

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

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

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

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

View Full Document
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
Ask a homework question - tutors are online