17. Shortest Path

# 17. Shortest Path - Maggie Johnson CS103B Handout#17...

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

Maggie Johnson CS103B Handout #17 Shortest Path Algorithms Key Topics * Introduction * Dijkstra's Algorithm * Floyd's Algorithm * And One More. .. As shown last time in the Traveling Salesperson Problem, many problems can be modeled using graphs with weights assigned to their edges. A standard example is an airline system that shows the flights between two cities. If the problem involves mileage, the edges are labeled with distances; if the problem involves fares, the edges are labeled with dollar amounts, etc. These are called weighted graphs . Several types of problems can be modeled using weighted graphs. A common question is: what is the length of the shortest path between two vertices? The length of a path in a weighted graph is defined as the sum of the weights of the edges in this path. For example, what is the length of the shortest path from LAX to BOS? Shortest Path Algorithms The first algorithm for solving shortest path length problems was discovered by a Dutch computer scientist named Dijkstra in 1959. The basic premise of this algorithm is to find the length of the shortest path between the starting vertex and a first vertex; then the length of the shortest path between the starting vertex and a second vertex; continuing until the length of the shortest path between the starting vertex and the ending vertex is found. The goal is to always be looking for the vertex closest to the starting vertex. This is similar to critical path evaluation in PERT charts. For example, you could find the shortest path between a and z just by looking at the following graph. To illustrate the algorithm, however, we will find the shortest path systematically.

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

View Full Document
The only paths starting with a are a,b and a,d with lengths 4 and 2, respectively; d is the closest vertex to a. To find the next closest vertex to a, we look at all the paths going through a or d. One path a,d,e has a length of 5; another a,b (which goes through a) has a length of 4. So, d is the closest vertex to a, and b is the next closest, and then e. To find the third closest vertex to a, we have to examine the paths that go through a, b, d, or e. There is a path of length 7 to c; another path of length 7 to e, and a path of length 6 to z. Consequently, z is the next closest vertex to a along the path a, d, e, z. We have found the length of the shortest path. Now, we need to formalize this strategy into an algorithm. Assume the following: we have a weighted, connected simple graph, with all the weights positive (you will see that this is a necessity because the algorithm assumes a building up of path lengths as we proceed). This graph G has vertex set {v 0 , v 1 , v 2 ...} where v 0 = a, the starting vertex for the path, and v n = z, the terminating vertex for the path. Weights are stored in a 2-dim array where w(v i , v j ) = an integer if there is an edge between v i and v j , and ∞ otherwise. An array L[1. ..n] (an entry for each vertex) will keep track of the path lengths as we process. This array is initialized to ∞ for all entries except the
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 10/01/2011 for the course CS 103B taught by Professor Sahami,m during the Winter '08 term at Stanford.

### Page1 / 8

17. Shortest Path - Maggie Johnson CS103B Handout#17...

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

View Full Document
Ask a homework question - tutors are online