Download presentation
Presentation is loading. Please wait.
Published byPeter Walton Modified over 9 years ago
1
KNURE, Software department, Ph. 7021-446, e-mail: belous@kture.kharkov.ua N.V. Bilous Faculty of computer sciences Software department, KNURE The distance on a graphs. Discrete mathematics.
2
N.V.Belous6.Shortest Path Problems 2 Basic definitions The length of the shortest (u, v) path is called a distance between vertices u and v. In some graphs to edges are attributed numerical characteristics (fares, the costs, weight, distance) Graphs that have a number assigned to each edge are called weighted graphs.
3
N.V.Belous6.Shortest Path Problems 3 The length of a path The length of a path (for unweighted graph) is the quantity of edges in this path. Example. S 1 (a,b)=(a,v 3,v 4,v 5,b)=4; S 2 (a,b)=(a,v 1,v 2,b)=3; S 3 (a,b)=(a,v 2,b)=2; S 4 (a,b)=(a,v 1,v 2,v 5,b)=4; S 5 (a,b)=(a,v 3,v 4,v 5,v 2,b)=5; S 6 (a,b)=(a,v 2,v 5,b)=3. The shortest length S 3 (a,b)=2. Then d(a,b)=2.
4
N.V.Belous6.Shortest Path Problems 4 The length of a path The length of a path (for a weighted graph) is the sum of the weights for the edges that belong to this path. S 1 (a,b)=(a,v 3,v 4,v 5,b)=10; S 2 (a,b)=(a,v 1,v 2,b)=17; S 3 (a,b)=(a,v 2,b)=19; S 4 (a,b)=(a,v 1,v 2,v 5,b)=15; S 5 (a,b)=(a,v 3,v 4,v 5,v 2,b)=30; S 6 (a,b)=(a,v 2,v 5,b)=17. The shortest length S 1 (a,b)=10. Then d(a,b)=10. Example.
5
N.V.Belous6.Shortest Path Problems 5 Axioms of the metrics Axioms of the metrics: 1)d(u,v) 0,d(u,v)=0 iff u=v, 2)d(u,v)=d(v,u), 3) d(u,v)+d(v,w) d(u,w)
6
N.V.Belous6.Shortest Path Problems 6 Dijkstra's Algorithm. Dijkstra's algorithm proceeds by finding the length of the shortest path from a to a first vertex, the length of the shortest path from a to a second vertex, and so on, until the length of the shortest path from a to z is found.
7
N.V.Belous6.Shortest Path Problems 7 Dijkstra's Algorithm. 1) Labeling a with 0 and the other vertices with ∞: L 0 (a) = 0 L 0 (v i ) = ∞. These labels are the lengths of the shortest paths from a to the vertices, where the paths contain only the vertex a.
8
N.V.Belous6.Shortest Path Problems 8 Dijkstra's Algorithm. 2) Let S k is a set of vertices after k iterations of the labeling procedure. S 0 = . The set S k is formed from S k-1 by adding a vertex u not in S k-1 with the smallest label. Once u is added to S k, we update the labels of all vertices not in S k, so that L k (v), the label of the vertex v at the k-th stage, is the length of the shortest path from a to v that contains vertices only in S k
9
N.V.Belous6.Shortest Path Problems 9 Dijkstra's Algorithm. 3) Let v be a vertex not in S k. To update the label of v, note that L k (v) is the length of the shortest path from a to v containing only vertices in S k. The shortest path from a to v containing only elements of S k is either the shortest path from a to v that contain only elements of S k- i or it is the shortest path from a to u at the (k -1 )st stage with the edge (u, v) added. In other words L k (a,v) = min{L k-1 (a, v), L k-1 (a, u)+ w(u, v)}
10
N.V.Belous6.Shortest Path Problems 10 Dijkstra's Algorithm. 4) When z is added to the distinguished set, its label is the length of the shortest path from a to z.
11
N.V.Belous6.Shortest Path Problems 11 Dijkstra's Algorithm. procedure Dijkstra (G: weighted connected simple graph, with all weights positive) {G has vertices a = v 0, v 1, …, v n = z and weights w (v i, v j ), where w(v i, v j ) = ∞ if {v i, v j } is not an edge in G} for i: = 1 to n L (v i ) : = ∞ L (a) : = 0 S : = {the labels are now initialized so that the label of a is zero and all other labels are ∞, and S is the empty set}. while z S begin u : =a vertex not in S with L(u) minimal S:=S {u} for all vertices v not in S if L(u) + w(u, v) < L(v) then L(v): =L(u) + w(u, v) {this adds a vertex to S with minimal label and updates the labels of vertices not in S} end {L(z) = length of shortest path from a to z}
12
N.V.Belous6.Shortest Path Problems 12 a)b) Example of Dijkstra's Algorithm. d ∞ b ∞ e ∞c ∞ 0 a z ∞ 8 32 4 6 1 2 5 10 0 a b (4,a) c (2,a) 8 32 4 6 1 2 5 10 d (10,c)b (3,c) e (12,c) c (2,a) 0 a8 32 4 6 1 2 5 10 d (8,b)b (3,c) e (12,c)c (2,a) 0 az ∞8 32 4 6 1 2 5 10 c) d) d (∞,a) e (∞,a) z (∞,a) z (∞,c)
13
N.V.Belous6.Shortest Path Problems 13 z (14,d) d (8,b) b (3,c) e (10,d) c (2,a) 0 a 8 32 4 6 1 2 5 10 d (8,b) b (3,c) e (10,d)c (2,a) 0 a z (13,e) 8 32 4 6 1 2 5 10 d (8,b) b (3,c) e (10,d) c (2,a) 0 az (13,e)8 32 4 6 1 2 5 10 d(a,b) = 3(a,c,b) d(a,c) = 2(a,c) d(a,d) = 8(a,c,b,d) d(a,e) = 10(a,c,b,d,e) d(a,z) = 13(a,c,b,d,e,z) Example of Dijkstra's Algorithm. e) f) g)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.