0% found this document useful (0 votes)
14 views

6S-Shortest Path

Uploaded by

badgirlnoha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

6S-Shortest Path

Uploaded by

badgirlnoha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

SHORTEST PATH

7.6 SHORTEST PATHS


7.6.1 Find the shortest paths from a source vertex to all other vertices in G
( Single-Source Shortest-Paths Problem )
1. Questions Raised
A weighted graph G=(V,E) as Transportation network,
• V= vertices: cities
• E= edges: the transport links between cities
• W(U,V)= weight:the length of the line or the time or cost of
transportation along the line
Problem:THE PATH that MINIMIZES the sum of the weights of EACH edge
from a vertex to another vertex——The SHORTEST PATH

2. Dijkstra Algorithm
Generate shortest paths in increasing path length order
7.6 SHORTEST PATHS
7.6.1 Find the shortest paths from a source vertex to all other vertices in G
( Single-Source Shortest-Paths Problem )

3. Assistant Array dist


• dist[i], i=1,...,n stores the current length of the shortest path from
source V0 to vertex i,
• The initial value of dist[i] is the weight of edge (v0, i); If edge (v0, i)
doesn't exist, it's set as infinity 
4.implementation
Let a set to store the vertices of the generated shortest path
initially, there is just the source vertex v0 in U
15
2 4
5
7
1 9
5 13
4 8
9
3 3

20 6

i 1 2 3 4 5 6 7 8
dist[ 5 4 ∞ 9 20 ∞ ∞
i]
4.implementation
Let a set to store the vertices of the generated shortest path
initially, there is just the source vertex v0 in U
15 1st Path : One of
2 4
5 the paths from 1 to
7
1 9 2,5,3,6
5 13
4 8
9
3 3

20 6

i 1 2 3 4 5 6 7 8
dist[ 5 4 ∞ 9 20 ∞ ∞
i]
4.implementation
Let a set to store the vertices of the generated shortest path
initially, there is just the source vertex v0 in U
15 1st Path : One of
2 4
5 the paths from 1 to
7
1 9 2,5,3,6
5 13 Add i that dist[i]is
4 8 minimum to U
9
3 i=3
3
20 6

i 1 2 3 4 5 6 7 8
dist[ 5 4 ∞ 9 20 ∞ ∞
i]
4.implementation
Let a set to store the vertices of the generated shortest path
initially, there is just the source vertex v0 in U
15 1st Path : One of
2 4
5 the paths from 1 to
7
1 9 2,5,3,6
5 13
4
Add i that dist[i]is
9 8
minimum to U i=3
3 3
7 13 After adding 3 to U,
20 6 update some dist[i]
2nd Path : One of
the paths from 1 to
i 1 2 3 4 5 6 7 8 2,5,8,6.
dist[ 5 4 ∞ 9 20 ∞ ∞
i]
4.implementation
Let a set to store the vertices of the generated shortest path
initially, there is just the source vertex v0 in U
15 1st Path : One of
2 4
5 the paths from 1 to
7
1 9 2,5,3,6
5 13
4
Add i that dist[i]is
9 8
minimum to U i=3
3 3
After adding 3 to U,
20 6 update some dist[i]
2nd Path : One of
the paths from 1 to
i 1 2 3 4 5 6 7 8 2,5,8,6.
dist[ 5 4 ∞ 9 7 ∞ 13
i]
Theorem : If the next shortest path goes to vertex x, then all
the intermediate points/vertices from the source point to x
must be in U

Proof (contradiction): If not, then at least one intermediate


point u does not belong to U

vo
U x
u
Theorem : If the next shortest path goes to vertex x, then all
the intermediate points/vertices from the source point to x
must be in U

Proof (contradiction): If not, then at least one intermediate


point u does not belong to U

vo
U x
u

Then,there must be:


dist[u]+ w(u,x) > dist[x] , According to Algo., the shorter
path has generated, that is u belongs to U
4.implementation
Let a set to store the vertices of the generated shortest path
initially, there is just the source vertex v0 in U
15
2 4
5
7
1 9
5 13
4 8
9 Then,there must be:
3 dist[u]+ w(u,x) > dist[x] ,
3
According to Algo., the
20 6 shorter path has
generated, that is u
belongs to U
i 1 2 3 4 5 6 7 8
dist[ 5 4 ∞ 9 7 ∞ 13
i]
If the next shortest path goes to vertex x, then all the intermediate
points from the source point to x must be in U
vo
Proof (contradiction): If not, then at least one U x
intermediate point u does not belong to U
u
6.
G:Dijkstra Algorithm
adjacency matrix,
Initialize set U and assistant array dist[] ,   13 8  30  32
 
Source vertex: vo ; |V|= n 。      9 7
g[i,j]: weight of edge(i, j) ;    5   
 
if (i, j) does not exist, g[i,j] is set as
g      6  
infinity ∞;  
     2 
1. dist[i]= g[v0,i] i=1,2,….,n  
      17 
2. U=[v0];  
      
3. Repeat following steps (n-1) times
① Select v that: dist[v]=min{ dist[u]|
uU}
② Add v into U: U=U{v}
③ Update dist[w] that wU:
dist[w]=min{dist[w],dist[v]+g[v,w]|
1 wU} 32
8 13 2
3
30 9 7
5 7
4
6 17
6 2
5
Gn*n
6. : adjacency
Dijkstra matrix,
Algorithm
  13 8  30  32
Initialize set U and assistant array dist[] ,  
Source vertex: vo ; |V|= n 。      9 7
g[i,j]: weight of edge(i, j) ;    5   
 
if (i, j) does not exist, g[i,j] is set as g      6  
infinity ∞;  
     2 
1. dist[i]= g[v0,i] i=1,2,….,n  
      17 
2. U=[v0];  
      
3. Repeat following steps (n-1) times
① Select v that: dist[v]=min{ dist[u]| i 1 2 3 4 5 6 7
uU} dist ∞ 1 8 ∞ 3 ∞ 32
② Add v into U: U=U{v} 3 0
③ Update dist[w] that wU: i 1 2 3 4 5 6 7
dist[w]=min{dist[w],dist[v]+g[v,w]| dist ∞ 1 8 13 3 ∞ 32
1 wU} 32 3 0
8 13 i 1 2 3 4 5 6 7
2
3 dist ∞ 1 8 13 3 22 20
30 9 7 3 0
5 7 i 1 2 3 4 5 6 7
4 dist ∞ 1 8 13 1 22 20
6 17
6 2 3 9
i 1 2 3 4 5 6 7 i 1 2 3 4 5 6 7
5
dist ∞ 1 8 13 1 21 20 dist ∞ 1 8 13 1 21 20
7. Dijkstra Algorithm in C
since the index (subscript) of C/C++ starts from 0, we make
some changes and using NUM denoting the number of
vertices of G
#define NUM
void shortpath_dij(g[][NUM],v0) //v0: source , w:1~NUM
{
for(i=0;i<NUM;i++){
set[i]=0;
dist[i]=g[v0-1][i];}
set[v0-1]=1;
for(i=1;i<NUM;i++)
{ min=MAXINT;
for(w=0;w<NUM;w++)
if(set[w]= =0 && dist[w]<min)
{ v=w; min=dist[w]; } Time
set[v]=1; Complexity:
for(w=0;w<NUM;w++) O(n2)
if(set[w]= =0 && dist[v]+g[v][w]<dist[w])
dist[w]=dist[v]+g[v][w];
}//for i
} //shortpath_dij
#include<iostream.h> int search(char e);// 查找顶点在顺序表中的下表
#define max 20 int indegree(char e);// 计算 e 的入度
class Arcbox{ int outdegree(char e);// 计算 e 的出度
public: };
int tailvex,headvex; int OLgraph::search(char e){
Arcbox *hlink,*tlink;}; for(int i=0;i<vexnum;i++)
class VexNode{ if(xlist[i].data==e) return i;
public: return -1;}
char data; int OLgraph::indegree(char e){
Arcbox int count=0;
*firstin,*firstout;};
int i=search(e);
class OLgraph{
Arcbox *p=xlist[i].firstout;
public:
while(p){ count++;
VexNode xlist[max];
p=p->tlink;}
int vexnum,arcnum;
return count;
void CreateDG();
}
// 创建图 ( 十字链表 )
void OLgraph::CreateDG(){ p=new Arcbox;
char v,w; p->tailvex=i;p->headvex=j;
int i,j,k; p->hlink=xlist[j].firstin;
Arcbox *p; p->tlink=xlist[i].firstout;
cin>>vexnum>>arcnum; xlist[j].firstin=p;
for( i=0;i<vexnum;i++){ xlist[i].firstout=p;}
cin>>xlist[i].data; }
xlist[i].firstin=NULL; int OLgraph::outdegree(char e){
xlist[i].firstout=NULL;} int count=0;
for( k=0;k<arcnum;++k){ int i=search(e);
lab:cin>>v>>w; Arcbox *p=xlist[i].firstin;
i=search(v); while(p){
j=search(w); count++;
if(i==-1||j==-1){ p=p->hlink;}
cout<<" 查无此点 "; return count;
goto lab;} }
void main(void){
char ch;
OLgraph G;
G.CreateDG();
cin>>ch;
cout<<G.indegree(ch)<<endl;
cout<<G.outdegree(ch)<<endl;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy