11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

390 Chap. 11 GraphsA103B2205D11C15EFigure 11.16 Example graph for shortest-p<strong>at</strong>h definitions.orithm (in the worst case) for finding the shortest p<strong>at</strong>h to a single vertex than to findshortest p<strong>at</strong>hs to all vertices. The algorithm described here will only compute thedistance to every such vertex, r<strong>at</strong>her than recording the actual p<strong>at</strong>h. Recording thep<strong>at</strong>h requires modific<strong>at</strong>ions to the algorithm th<strong>at</strong> are left as an exercise.<strong>Computer</strong> networks provide an applic<strong>at</strong>ion for the single-source shortest-p<strong>at</strong>hsproblem. The goal is to find the cheapest way for one computer to broadcast amessage to all other computers on the network. The network can be modeled by agraph with edge weights indic<strong>at</strong>ing time or cost to send a message to a neighboringcomputer.For unweighted graphs (or whenever all edges have the same cost), the singlesourceshortest p<strong>at</strong>hs can be found using a simple breadth-first search. Whenweights are added, BFS will not give the correct answer.One approach to solving this problem when the edges have differing weightsmight be to process the vertices in a fixed order. Label the vertices v 0 to v n−1 , withS = v 0 . When processing Vertex v 1 , we take the edge connecting v 0 <strong>and</strong> v 1 . Whenprocessing v 2 , we consider the shortest distance from v 0 to v 2 <strong>and</strong> compare th<strong>at</strong> tothe shortest distance from v 0 to v 1 to v 2 . When processing Vertex v i , we considerthe shortest p<strong>at</strong>h for Vertices v 0 through v i−1 th<strong>at</strong> have already been processed.Unfortun<strong>at</strong>ely, the true shortest p<strong>at</strong>h to v i might go through Vertex v j for j > i.Such a p<strong>at</strong>h will not be considered by this algorithm. However, the problem wouldnot occur if we process the vertices in order of distance from S. Assume th<strong>at</strong> wehave processed in order of distance from S to the first i − 1 vertices th<strong>at</strong> are closestto S; call this set of vertices S. We are now about to process the ith closest vertex;call it X. A shortest p<strong>at</strong>h from S to X must have its next-to-last vertex in S. Thus,d(S, X) = min(d(S, U) + w(U, X)).U∈SIn other words, the shortest p<strong>at</strong>h from S to X is the minimum over all p<strong>at</strong>hs th<strong>at</strong> gofrom S to U, then have an edge from U to X, where U is some vertex in S.This solution is usually referred to as Dijkstra’s algorithm. It works by maintaininga distance estim<strong>at</strong>e D(X) for all vertices X in V. The elements of D are ini-

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!