当前位置:
文档之家› cs3460_ch9_shortest_path 美国高校《数据结构》(Data Structure)ppt课件,共9章,英文版
cs3460_ch9_shortest_path 美国高校《数据结构》(Data Structure)ppt课件,共9章,英文版
/* 4*/ /* 5*/ /* 6*/ /* 7*/ /* 8*/ /* 9*/ /*10*/ /*11*/ /*12*/ /*13*/
void Print_Path( Vertex V, Table T ) {
if( T[ V ].Path != Not_A_Vertex ) {
Print_Path( T[ V ].Path, T ); cout << " to "; } cout << V; }
if( T[ W ].Dist == Infinity ) {
T[ W ].Dist = T[ V ].Dist; T[ W ].Path = V; Q.Enqueue( W ); } }
Steps in the Processing
Analysis of Version 2
• Processing continues until all vertices are marked as known distances
Declarations for Dijkstra’s Algorithm
/* 1*/ // Vertices are numbered starting at 1.
/* 2*/ const Not_A_Vertex = 0; /* 3*/ typedef unsigned int Vertex;
Unweighted shortest path - 2
• Follow edges from vertices of distance 1 to unvisited vertices and mark with distance 2
• Follow links from vertices with 2 to unvisited vertices and mark with 3
/* 2*/ Weighted_Negative( Table T )
/* 3*/ {
/* 4*/
/*13*/
Dist_Type Dist;
/*14*/
Vertex Path;
/*15*/ };
/*16*/ typedef Table_Entry Table [ Num_Vertex + 1 ];
Table Initialization & Printing Shortest Path
/* 1*/ /* 2*/ /* 3*/ /* 4*/ /* 5*/ /* 6*/ /* 7*/ /* 8*/ /* 9*/ /*10*/ /*11*/ /*12*/
/* 4*/ struct Adj_Vertex
/* 5*/ {
/* 6*/
Vertex W;
/* 7*/
Dist_Type V_W;
/* 8*/ };
/* 9*/ struct Table_Entry
/*10*/ {
/*11*/
List<Adj_Vertex> Adj_List;
/*12*/
ห้องสมุดไป่ตู้
int Known;
/*19*/
}
/*20*/
}
/*21*/ }
Group Work
• Find the shortest path from A to all other vertices
Group Work
• Find the shortest path from B to all other vertices
enqueued, but all of the current distance nodes will be processed before the more distant nodes are processed
Pseudocode for Version 2
/* 1*/ /* 2*/ /* 3*/ /* 4*/ /* 5*/
Graphs with Negative Edges
• Dijkstra’s algorithm needs to be modified
– Modify by removing notion of a “known” vertex – We add a bit to know if a vertex is in the queue – When v is dequeued, find all vertices w where
Unweighted shortest path - 1
• Label the starting vertex with a distance zero
• Follow links to find all vertices that can be reached using one edge
• Label their distance as 1
void
// T is initialized ( Fig 9.30 ).
Unweighted( Table T )
{
Vertex V, W;
Queue<Vertex> Q( Num_Vertex );
/* 6*/
/* 7*/ /* 8*/ /* 9*/ /*10*/ /*11*/ /*12*/ /*13*/ /*14*/ /*15*/ /*16*/ /*17*/ /*18*/ /*19*/ }
• The analysis is similar to topological sort and has complexity is O( |E| + |V| )
• This assumes an adjacency list structure is used
Dijkstra’s Algorithm
/*13*/
if( T[ V ].Dist + C ( V, W ) < T[ W ].Dist )
/*14*/
{
/*15*/
// Update w.
/*16*/
Decrease( T[ W ].Dist To
/*17*/
T[ V ].Dist + C ( V, W );
/*18*/
T[ W ].Path = V;
void
Init_Table( Vertex Start, Graph G, Table T )
{
Read_Graph( G, T );
// Read graph somehow.
for( int i = 1; i <= Num_Vertex; i++ )
{
T[ i ].Known = FALSE;
Dijkstra’s Algorithm
/* 1*/ /* 2*/ /* 3*/ /* 4*/
/* 5*/ /* 6*/ /* 7*/ /* 8*/ /* 9*/
void Dijkstra( Table T ) {
Vertex V, W;
complexity is O( |E| log |V| )
for( ; ; ) {
V = Smallest Unkown Distance Vertex; if( V == Not_A_Vertex )
break;
/*10*/
T[ V ].Known = TRUE;
/*11*/
for Each W Adjacent To V
/*12*/
if( !T[ V ].Known )
/* 5*/ /* 6*/ /* 7*/ /* 8*/ /* 9*/ /*10*/ /*11*/ /*12*/ /*13*/ /*14*/ /*15*/ /*16*/ /*17*/ }
for( int Curr_Dist = 0; Curr_Dist < Num_Vertex; Curr_Dist++ ) for Each Vertex V if( !T[ V ].Known && T[ V ].Dist == Curr_Dist ) { T[ V ].Known = TRUE; for Each W Adjacent To V if( T[ W ].Dist == Infinity ) { T[ W ].Dist = Curr_Dist + 1; T[ W ].Path = V; } }
• All vertices have been visited
Pseudocode Algorithm Version 1
/* 1*/ /* 2*/ /* 3*/ /* 4*/
void Unweighted( Table T ) {
Vertex V, W;
// T is initialized ( Fig 9.30 ).
– Worst case each vertex can be dequeued |V| times – Overall complexity is O( |E| |V| )
Pseudocode for Negative Edges
/* 1*/ void
// Assume T is initialized as in Fig 9.18.
• The values stored in p will allow use to recreate the path from the start to the vertex; this will be explained in more detail when the algorithm for weighted graphs is studied
Shortest Path Algorithms - 1
• The problem statement is simple
• An example with positive weights • The shortest path from v1 to v6 is 1+4+1= 6 via v4 and v7 • Notice this is not the least number of edges due to the weights