Art_of_Programming_Contest_Part9

Art_of_Programming_Contest_Part9 - CHAPTER 12 GRAPHS 163

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

View Full Document Right Arrow Icon
CHAPTER 12 GRAPHS 163 initialize_single_source(Graph G,Node s) for each vertex v in Vertices(G) G.d[v]:=infinity G.pi[v]:=nil G.d[s]:=0; This sets up the graph so that each node has no predecessor ( pi[v] = nil ) and the estimates of the cost (distance) of each node from the source ( d[v] ) are infinite, except for the source node itself ( d[s] = 0 ). Note that we have also introduced a further way to store a graph (or part of a graph - as this structure can only store a spanning tree), the predecessor sub-graph - the list of predecessors of each node, pi[j], 1 <= j <= |V| . The edges in the predecessor sub-graph are (pi[v],v) . The relaxation procedure checks whether the current best estimate of the shortest distance to v ( d[v] ) can be improved by going through u ( i.e. by making u the predecessor of v ): relax(Node u,Node v,double w[][]) if d[v] > d[u] + w[u,v] then d[v]:=d[u] + w[u,v] pi[v]:=u Dijkstra Algorithm Dijkstra's algorithm (invented by Edsger W. Dijkstra) solves the problem of finding the shortest path from a point in a graph (the source ) to a destination. It turns out that one can find the shortest paths from a given source to all points in a graph in the same time, hence this problem is called the Single-source shortest paths problem. There will also be no cycles as a cycle would define more than one path from the selected vertex to at least one other vertex. For a graph, G=(V,E) where V is a set of vertices and E is a set of edges. Dijkstra's algorithm keeps two sets of vertices: S ( the set of vertices whose shortest paths from the source have already been determined) and V-S ( the remaining vertices). The other data structures needed are: d (array of best estimates of shortest path to each vertex) & pi (an array of predecessors for each vertex) The basic mode of operation is: 1. Initialise d and pi, 2. Set S to empty,
Background image of page 1

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

View Full DocumentRight Arrow Icon
CHAPTER 12 GRAPHS 164 3. While there are still vertices in V-S, 4. Sort the vertices in V-S according to the current best estimate of their distance from source, 5. Add u, the closest vertex in V-S, to S, 6. Relax all the vertices still in V-S connected to u DIJKSTRA(Graph G,Node s) initialize_single_source(G,s) S:={ 0 } /* Make S empty */ Q:=Vertices(G) /* Put the vertices in a PQ */ while not Empty(Q) u:=ExtractMin(Q); AddNode(S,u); /* Add u to S */ for each vertex v which is Adjacent with u relax(u,v,w) Bellman-Ford Algorithm A more generalized single-source shortest paths algorithm which can find the shortest path in a graph with negative weighted edges. If there is no negative cycle in the graph, this algorithm will updates each d[v] with the shortest path from s to v, fill up the predecessor list "pi", and return TRUE. However, if there is a negative cycle in the given graph, this algorithm will return FALSE. BELLMAN_FORD(Graph G,double w[][],Node s)
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 20

Art_of_Programming_Contest_Part9 - CHAPTER 12 GRAPHS 163

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

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