weighted graphs weighted graphs
play

Weighted graphs Weighted graphs Weighted graphs Weighted graphs - PowerPoint PPT Presentation

Weighted graphs Weighted graphs Weighted graphs Weighted graphs Graphs with numbers, called weights , attached to each edge K08 - Often restricted to


  1. Weighted graphs Weighted graphs Weighted graphs Weighted graphs • Graphs with numbers, called weights , attached to each edge K08 Δομές Δεδομένων και Τεχνικές Προγραμματισμού - Often restricted to non-negative Κώστας Χατζηκοκολάκης • Directed or undirected • Examples - Distance between cities - Cost of �ight between airports - Time to send a message between routers / / 1 2 Weighted graphs Weighted graphs Example weighted graph Example weighted graph • Adjacency matrix representation 7 2 3 ⎧ w i , j ⎪ 5 3 if i , j are connected 8 ⎨ 1 10 T [ i , j ] = ∞ if i = j are not connected  ⎪ ⎩ ⎪ 1 4 2 0 if i = j 5 6 6 5 7 • Similarly for adjacency lists / / 3 4

  2. Example weighted graph Example weighted graph Shortest paths Shortest paths • The length of a path is the sum of the weights of its edges • Very common problem - �nd the shortest path from to s d • Examples - Shortest route between cities - Cheapest connecting �ight - Fastest network route Adjacency matrix - … / / 5 6 Shortest path from vertex 1 to vertex 5 Shortest path from vertex 1 to vertex 5 Shortest path problem Shortest path problem Two main variants: 7 2 3 5 3 • Single source s 8 1 10 - Find the shortest path from to each node s 1 4 2 - Dijkstra's algorithm 5 6 6 ◦ Only for non-negative weights (important!) 5 7 • All-pairs - s , d Find the shortest path between all pairs - Floyd-Warshall algorithm ◦ Any weights / / 7 8

  3. Dijkstra's algorithm Dijkstra's algorithm Dijkstra's algorithm Dijkstra's algorithm Main ideas: Main ideas: • • Δ[ u ] Keep a set of visited nodes Adding to might a�ect W w W W = { s } W = {} - - Start with (or ) For some neighbour of u w Δ[ u ] • Keep a matrix • We might now have a shorter path to passing through u w s → … → w → u - - Minimum distance from to passing only through Of the form s u W - Δ[ u ] = T [ s , u ] Δ[ s ] = 0, Δ[ u ] = ∞ - Δ[ u ] > Δ[ w ] + T [ w , u ] Start with (or ) If • w  ∈ W • Δ At each step we enlarge by adding a new vertex In this case we update W - Δ[ u ] = Δ[ w ] + T [ w , u ] Δ[ w ] - w is the one with minumum / / 9 10 Example graph Example graph Expanding the vertex set w in stages Expanding the vertex set w in stages 7 Stage W V-W w Δ(w) Δ(1) Δ(2) Δ(3) Δ(4) Δ(5) Δ(6) 2 3 5 3 Start { 1} { 2,3,4,5,6} - - 0 3 ∞ ∞ ∞ 5 8 1 10 1 4 2 7 2 3 5 6 5 3 6 5 8 7 1 10 1 4 2 5 6 6 5 7 / / 11 12

  4. Expanding the vertex set w in stages Expanding the vertex set w in stages Expanding the vertex set w in stages Expanding the vertex set w in stages W=2 is chosen for the second stage. Stage W V-W w Δ(w) Δ(1) Δ(2) Δ(3) Δ(4) Δ(5) Δ(6) Stage W V-W w Δ(w) Δ(1) Δ(2) Δ(3) Δ(4) Δ(5) Δ(6) Start { 1} { 2,3,4,5,6} - - 0 3 ∞ ∞ ∞ 5 Start { 1} { 2,3,4,5,6} - - 0 3 ∞ ∞ ∞ 5 2 { 1,2} { 3,4,5,6} 2 3 0 3 10 ∞ ∞ 5 7 7 2 3 2 3 5 3 5 3 8 8 1 1 10 10 1 4 1 4 2 2 5 5 6 6 6 6 5 5 7 7 / / 13 14 Expanding the vertex set w in stages Expanding the vertex set w in stages Expanding the vertex set w in stages Expanding the vertex set w in stages W=6 is chosen for the third stage. Stage W V-W w Δ(w) Δ(1) Δ(2) Δ(3) Δ(4) Δ(5) Δ(6) Stage W V-W w Δ(w) Δ(1) Δ(2) Δ(3) Δ(4) Δ(5) Δ(6) Start { 1} { 2,3,4,5,6} - - 0 3 ∞ ∞ ∞ 5 Start { 1} { 2,3,4,5,6} - - 0 3 ∞ ∞ ∞ 5 2 { 1,2} { 3,4,5,6} 2 3 0 3 10 ∞ ∞ 5 2 { 1,2} { 3,4,5,6} 2 3 0 3 10 ∞ ∞ 5 3 { 1,2,6} { 3,4,5} 6 5 0 3 10 7 5 ∞ 7 7 2 3 2 3 5 3 5 3 8 8 1 10 1 10 4 1 4 1 2 2 5 6 5 6 6 6 5 5 7 7 / / 15 16

  5. Expanding the vertex set w in stages Expanding the vertex set w in stages Expanding the vertex set w in stages Expanding the vertex set w in stages W=4 is chosen for the fourth stage. Stage W V-W w Δ(w) Δ(1) Δ(2) Δ(3) Δ(4) Δ(5) Δ(6) Stage W V-W w Δ(w) Δ(1) Δ(2) Δ(3) Δ(4) Δ(5) Δ(6) Start { 1} { 2,3,4,5,6} - - 0 3 ∞ ∞ ∞ 5 Start { 1} { 2,3,4,5,6} - - 0 3 ∞ ∞ ∞ 5 2 { 1,2} { 3,4,5,6} 2 3 0 3 10 ∞ ∞ 5 2 { 1,2} { 3,4,5,6} 2 3 0 3 10 ∞ ∞ 5 3 { 1,2,6} { 3,4,5} 6 5 0 3 10 7 ∞ 5 3 { 1,2,6} { 3,4,5} 6 5 0 3 10 7 ∞ 5 4 { 1,2,6,4} { 3,5} 4 7 0 3 10 7 13 5 7 7 2 3 2 3 5 3 5 3 8 8 1 10 1 10 1 4 1 4 2 2 5 6 5 6 6 6 5 5 7 7 / / 17 18 Expanding the vertex set w in stages Expanding the vertex set w in stages Expanding the vertex set w in stages Expanding the vertex set w in stages W=3 is chosen for the fifth stage. Stage W V-W w Δ(w) Δ(1) Δ(2) Δ(3) Δ(4) Δ(5) Δ(6) Stage W V-W w Δ(w) Δ(1) Δ(2) Δ(3) Δ(4) Δ(5) Δ(6) Start { 1} { 2,3,4,5,6} - - 0 3 ∞ ∞ ∞ 5 Start { 1} { 2,3,4,5,6} - - 0 3 ∞ ∞ ∞ 5 2 { 1,2} { 3,4,5,6} 2 3 0 3 10 5 ∞ ∞ 2 { 1,2} { 3,4,5,6} 2 3 0 3 10 ∞ ∞ 5 3 { 1,2,6} { 3,4,5} 6 5 0 3 10 7 ∞ 5 3 { 1,2,6} { 3,4,5} 6 5 0 3 10 7 ∞ 5 4 { 1,2,6,4} { 3,5} 4 7 0 3 10 7 13 5 4 { 1,2,6,4} { 3,5} 4 7 0 3 10 7 13 5 5 { 1,2,6,4,3} { 5} 3 10 0 3 10 7 11 5 7 2 3 7 2 3 5 3 5 8 3 8 1 10 4 1 1 10 4 1 2 2 5 6 5 6 6 5 6 7 5 7 / / 19 20

  6. Expanding the vertex set w in stages Expanding the vertex set w in stages Expanding the vertex set w in stages Expanding the vertex set w in stages W=5 is chosen for the sixth stage. Stage W V-W w Δ(w) Δ(1) Δ(2) Δ(3) Δ(4) Δ(5) Δ(6) Stage W V-W w Δ(w) Δ(1) Δ(2) Δ(3) Δ(4) Δ(5) Δ(6) Start { 1} { 2,3,4,5,6} - - 0 3 ∞ ∞ ∞ 5 Start { 1} { 2,3,4,5,6} - - 0 3 ∞ ∞ ∞ 5 2 { 1,2} { 3,4,5,6} 2 3 0 3 10 ∞ ∞ 5 2 { 1,2} { 3,4,5,6} 2 3 0 3 10 ∞ ∞ 5 3 { 1,2,6} { 3,4,5} 6 5 0 3 10 7 ∞ 5 3 { 1,2,6} { 3,4,5} 6 5 0 3 10 7 ∞ 5 4 { 1,2,6,4} { 3,5} 4 7 0 3 10 7 13 5 4 { 1,2,6,4} { 3,5} 4 7 0 3 10 7 13 5 5 { 1,2,6,4,3} { 5} 3 10 0 3 10 7 11 5 5 { 1,2,6,4,3} { 5} 3 10 0 3 10 7 11 5 6 { 1,2,6,4,3,5} { } 5 11 0 3 10 7 11 5 7 7 2 3 2 3 5 3 5 3 8 8 1 10 1 4 10 1 1 4 2 2 5 6 5 6 6 6 5 7 5 7 / / 21 22 Dijkstra's algorithm in pseudocode Dijkstra's algorithm in pseudocode Dijkstra's algorithm in pseudocode Dijkstra's algorithm in pseudocode // Δεδομένα // Κυρίως αλγόριθμος src : αρχικός κόμβος while true dest : τελικός κόμβος w = vertex with minimum dist[w], among those with W[w] = 0 // Πληροφορίες που κρατάμε για κάθε κόμβο v W[w] = 1 W[u] : 1 αν ο u είναι στο σύνολο W, 0 διαφορετικά if w == dest dist[u] : ο πίνακας Δ stop prev[u] : ο προηγούμενος του v στο βέλτιστο μονοπάτι // optimal cost = dist[dest] // optimal path = dest <- prev[dest] <- ... <- src (inverse) // Αρχικοποίηση: W={} (εναλλακτικά μπορούμε και W={src}) for each vertex u in Graph for each neighbor u of w dist[u] = INT_MAX // infinity if W[u] == 1 prev[u] = NULL continue W[u] = 0 alt = dist[w] + weight(w,u) // κόστος του src -> ... -> w if alt < dist[u] // καλύτερο από πριν, update dist[src] = 0 dist[u] = alt prev[u] = w / / 23 24

Recommend


More recommend