Math236 Discrete Maths with Applications P. Ittmann UKZN, Pietermaritzburg Semester 1, 2012 Ittmann (UKZN PMB) Math236 2012 1 / 24
Edge weights Sometimes it is desirable to have graphs in which edges have different lengths For example, in a map with cities as vertices and highways as edges Consider the following problem A traveller wishes to drive from Soweto in Gauteng to Graskop in the Eastern Transvaal Given that the traveller has a map of South Africa that shows the distance between particular pairs of cities or towns How does the traveller determine the shortest possible? Ittmann (UKZN PMB) Math236 2012 2 / 24
Edge weights (cont.) Definition A weighted graph is a graph in which every edge e has a positive, real number w ( e ) assigned to it We call w ( e ) the weight of e or the length of e Example Consider the following graph v 1 v 2 1 v 3 1 G: 5 7 3 v 4 v 5 In this case, w ( v 1 v 4 ) = 5, w ( v 3 v 5 ) = 7 Ittmann (UKZN PMB) Math236 2012 3 / 24
Edge weights (cont.) Definition Let G be a graph with vertex set { v 1 , v 2 , . . . , v n } The weight matrix of G is the n × n matrix W = ( w i j ) where w ( v i v j ) if v i and v j are adjacent w i j = ∞ if v i and v j are not adjacent 0 if i = j Ittmann (UKZN PMB) Math236 2012 4 / 24
Edge weights (cont.) Applying this definition of W to the example above, we get 0 ∞ 1 5 ∞ ∞ 0 ∞ ∞ ∞ 1 W = 1 ∞ 0 3 7 5 ∞ 3 0 ∞ ∞ 1 7 ∞ 0 Let G be a weighted graph and let P be a path in G The length of P is the sum of the weights of the edges in P We denote this by l ( p ) Ittmann (UKZN PMB) Math236 2012 5 / 24
Edge weights (cont.) Example Revisiting this example: v 1 v 2 1 v 3 1 G: 5 7 3 v 4 v 5 Let P = v 1 v 4 v 3 v 5 v 2 Then, l ( P ) = 5 + 3 + 7 + 1 = 16 Ittmann (UKZN PMB) Math236 2012 6 / 24
Distance and shortest paths Definition Let G be a connected weighted graph and let a , b ∈ V ( G ) The distance between a and b is the length of the shortest a − b path in G We use the notation d G ( a , b ) Ittmann (UKZN PMB) Math236 2012 7 / 24
Distance and shortest paths (cont.) Example From the example: v 1 v 2 1 v 3 1 G: 5 7 3 v 5 v 4 So, d G ( v 1 , v 3 ) = 1 d G ( v 1 , v 4 ) = 4 d G ( v 3 , v 4 ) = 1 Ittmann (UKZN PMB) Math236 2012 8 / 24
Distance and shortest paths (cont.) Theorem Let P = v 1 v 2 . . . v k be a shortest path from v 1 to v k Then for each i ∈ { 2 , 3 , . . . , k − 1 } The section v 1 v 2 . . . v i is a shortest v 1 − v i path in G, and 1 v 1 is closer to v i than v k (that is, d G ( v 1 , v i ) < d G ( v 1 , v k ) 2 Ittmann (UKZN PMB) Math236 2012 9 / 24
Distance and shortest paths (cont.) Proof. We prove (1.) first Suppose to the contrary that there exists a shorted v 1 − v i path P ′ P v k v 1 v i We can replace the section v 1 v 2 . . . v i of P by P ′ This yields a shorter path from v 1 to v k This contradicts the choice of P as the shortest v 1 − v k path Ittmann (UKZN PMB) Math236 2012 10 / 24
Distance and shortest paths (cont.) Proof. We now prove (2.) We show that d ( v 1 , v i ) < d ( v 1 , v k ) By the proof of 1. above, d ( v 1 , v i ) = l ( v 1 v 2 . . . v i ) < l ( v 1 v 2 . . . v k ) = d ( v 1 , v k ) Ittmann (UKZN PMB) Math236 2012 11 / 24
Dijkstra’s Algorithm Consider the following problem Suppose we are given a connected graph G and a vertex u in G How do we find for each vertex v ∈ V ( G ) − { u } , The distance between u and v A shortest u − v path in G One way to solve this problem is to use Djikstra’s Algorithm Ittmann (UKZN PMB) Math236 2012 12 / 24
Dijkstra’s Algorithm (cont.) Djikstra’s algorithm works on the following idea In the first step , we find a vertex closest to u , call it u 1 A shortest u − u 1 path can only consist of a single edge u 1 u To find u 1 and compute d ( u , u 1 ) we do the following Look up the weight w ( uv ) for all v ∈ V ( G ) − { u } Choose u 1 such that w ( uu 1 ) is minimum Set d ( u , u 1 ) = w ( uu 1 ) Ittmann (UKZN PMB) Math236 2012 13 / 24
Dijkstra’s Algorithm (cont.) Example Consider the following graph v 4 3 t 1 x u 2 9 1 r 2 y Here w ( uv ) = 3, w ( ux ) = 1 and w ( uy ) = 9 The minimum weight is w ( ux ) Hence u 1 = x is the vertex closest to u We conclude that d ( u , u 1 ) = d ( u , x ) = 1 Ittmann (UKZN PMB) Math236 2012 14 / 24
Dijkstra’s Algorithm (cont.) In the second step , we find a vertex second closest to u and call it u 2 A shortest u − u 2 path is of the form uu 2 or uu 1 u 2 u 1 u 1 u u 2 u u 2 Ittmann (UKZN PMB) Math236 2012 15 / 24
Dijkstra’s Algorithm (cont.) To find u 2 and compute d ( u , u 2 ) we do the following for all v ∈ V ( G ) − { u , u 1 } : Look up w ( uv ) and d ( u , u 1 ) + w ( u 1 v ) Choose u 2 such that min { w ( uu 2 ) , d ( u , u 1 ) + w ( u 1 u 2 ) } is minimum Set d ( u , u 2 ) = min { w ( uu 2 ) , d ( u , u 1 ) + w ( u 1 u 2 ) } Ittmann (UKZN PMB) Math236 2012 16 / 24
Dijkstra’s Algorithm (cont.) Example Revisiting the following graph v 4 3 t 1 x u 2 9 1 r 2 y Here w ( uv ) = 3, w ( uy ) = 9 Also, d ( u , u 1 ) + w ( u 1 y ) = 2 The minimum is d ( u , u 1 ) + w ( u 1 y ) = 2 Hence u 2 = y is the vertex second closest to u and d ( u , u 2 ) = d ( u , u 1 ) + w ( u 1 y ) = 2 Ittmann (UKZN PMB) Math236 2012 17 / 24
Dijkstra’s Algorithm (cont.) In the third step , we find a vertex third closest to u and call it u 3 A shortest u − u 3 path is of the form uu 3 or uu 1 u 3 or u · · · u 2 u 3 u 1 u 1 u 2 u 3 u 2 u 3 u u u u 3 Ittmann (UKZN PMB) Math236 2012 18 / 24
Dijkstra’s Algorithm (cont.) To find u 3 and compute d ( u , u 3 ) we do the following for all v ∈ V ( G ) − { u , u 1 , u 2 } : Look up w ( uv ) and d ( u , u 1 ) + w ( u 1 v ) and d ( u , u 2 ) + w ( u 2 v ) Choose u 3 such that min { w ( uu 3 ) , d ( u , u 1 ) + w ( u 1 u 3 ) , d ( u , u 2 ) + w ( u 2 u 3 ) } is minimum Set d ( u , u 2 ) = min { w ( uu 3 ) , d ( u , u 1 ) + w ( u 1 u 3 ) , d ( u , u 2 ) + w ( u 2 u 3 ) } Ittmann (UKZN PMB) Math236 2012 19 / 24
Dijkstra’s Algorithm (cont.) Example v 4 3 t 1 x u 2 9 1 r 2 y Here w ( uv ) = 3 Also, d ( u , u 2 ) + w ( u 2 r ) = 4 The minimum is w ( uv ) = 3 Hence u 3 = v is the vertex third closest to u and d ( u , u 3 ) = 3 Ittmann (UKZN PMB) Math236 2012 20 / 24
Dijkstra’s Algorithm (cont.) In the k th step , we find a vertex k th closest to u and call it u k A shortest u − u k path is of the form uu k or uu 1 u k or u · · · u 2 u k u · · · u 3 u k · · · u · · · u k − 1 u k u k-1 u k u 1 u 2 u k u k u k u u u u Ittmann (UKZN PMB) Math236 2012 21 / 24
Dijkstra’s Algorithm (cont.) To find u k and compute d ( u , u k ) we do the following for all v ∈ V ( G ) − { u , u 1 , u 2 , u 3 , . . . , u k − 1 } : Look up w ( uv ) and d ( u , u 1 ) + w ( u 1 v ) and d ( u , u 2 ) + w ( u 2 v ) and ... d ( u , u k − 1 ) + w ( u k − 1 v ) Choose u k such that min { w ( uu 3 ) , d ( u , u 1 ) + w ( u 1 u 3 ) , . . . , d ( u , u k − 1 ) + w ( u k − 1 u k ) } is minimum Set d ( u , u 2 ) = min { w ( uu 3 ) , d ( u , u 1 ) + w ( u 1 u 3 ) , . . . , d ( u , u k − 1 ) + w ( u k − 1 u k ) } Ittmann (UKZN PMB) Math236 2012 22 / 24
Dijkstra’s Algorithm (cont.) Example v 4 3 t 1 x u 2 9 1 r 2 y To find u 4 , note the following d ( u , u 2 ) + w ( u 2 r ) = 4 and d ( u , u 3 ) + w ( v , t ) = 7 The minimum is d ( u , u 2 ) + w ( u 2 r ) = 4 Hence u 4 = r is the vertex fourth closest to u and d ( u , u 4 ) = 4 Ittmann (UKZN PMB) Math236 2012 23 / 24
Dijkstra’s Algorithm (cont.) Example v 4 3 t 1 x u 2 9 1 r 2 y To find u 5 , note the following d ( u , u 3 ) + w ( u 3 t ) = 7 and d ( u , u 4 ) + w ( u 4 , t ) = 6 The minimum is d ( u , u 4 ) + w ( u 4 , t ) = 6 Hence u 5 = t is the vertex fifth closest to u and d ( u , u 5 ) = 6 Ittmann (UKZN PMB) Math236 2012 24 / 24
Recommend
More recommend