Graphs: Finding shortest paths

  • 985 views
Uploaded on

Graphs: Finding shortest paths …

Graphs: Finding shortest paths

Topics:
Definitions
Floyd-Warshall algorithm
Dijkstra algorithm
Bellman-Ford algorithm

Teaching material for the course of "Tecniche di Programmazione" at Politecnico di Torino in year 2012/2013. More information: http://bit.ly/tecn-progr

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
985
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
68
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Graphs: Finding shortest pathsTecniche di Programmazione – A.A. 2012/2013
  • 2. SummaryA.A. 2012/2013Tecniche di programmazione2 Definitions Floyd-Warshall algorithm Dijkstra algorithm Bellman-Ford algorithm
  • 3. DefinitionGraphs: Finding shortest paths
  • 4. Definition: weight of a pathA.A. 2012/2013Tecniche di programmazione4 Consider a directed, weighted graph G=(V,E), with weightfunction w: ER This is the general case: undirected or un-weighted areautomatically included The weight w(p) of a path p is the sum of the weights ofthe edges composing the path𝑤 𝑝 = 𝑤(𝑢, 𝑣)(𝑢,𝑣)∈𝑝
  • 5. Definition: shortest pathA.A. 2012/2013Tecniche di programmazione5 The shortest path between vertex u and vertex v isdefined as the mininum-weight path between u and v, ifthe path exists. The weight of the shortest path is represented as d(u,v) If v is not reachable from u, then d(u,v)=
  • 6. Finding shortest pathsA.A. 2012/2013Tecniche di programmazione6 Single-source shortest path Given u and v, find the shortest path between u and v Given u, find the shortest path between u and any other vertex All-pairs shortest path Given a graph, find the shortest path between any pair ofvertices
  • 7. What to find?A.A. 2012/2013Tecniche di programmazione7 Depending on the problem, you might want: The value of the shortest path weight Just a real number The actual path having such minimum weight For simple graphs, a sequence of vertices. For multigraphs, a sequenceof edges
  • 8. ExampleA.A. 2012/2013Tecniche di programmazione8u vsx y3564312 7 26What is the shortest path betweens and v ?
  • 9. Representing shortest pathsA.A. 2012/2013Tecniche di programmazione9 To store all shortest paths from a single source u, we mayadd For each vertex v, the weight of the shortest path d(u,v) For each vertex v, the “preceding” vertex p(v) that allows toreach v in the shortest path For multigraphs, we need the preceding edge Example: Source vertex: u For any vertex v: double v.weight ; Vertex v.preceding ;
  • 10. ExampleA.A. 2012/2013Tecniche di programmazione10u vsx y3564312 7 26Vertex PrevioussuxvyVertex Weights 0uxvy
  • 11. LemmaA.A. 2012/2013Tecniche di programmazione11 The “previous” vertex in an intermediate node of aminimum path does not depend on the final destination Example: Let p1 = shortest path between u and v1 Let p2 = shortest path between u and v2 Consider a vertex w  p1  p2 The value of p(w) may be chosen in a single way and stillguarantee that both p1 and p2 are shortest
  • 12. Shortest path graphA.A. 2012/2013Tecniche di programmazione12 Consider a source node u Compute all shortest paths from u Consider the relation Ep = { (v.preceding, v) } Ep  E Vp = { v V : v reachable from u } Gp = G(Vp, Ep) is a subgraph of G(V,E) Gp: the predecessor-subgraph
  • 13. Shortest path treeA.A. 2012/2013Tecniche di programmazione13 Gp is a tree (due to the Lemma) rooted in u In Gp, the (unique) paths starting from u are alwaysshortest paths Gp is not unique, but all possible Gp are equivalent (sameweight for every shortest path)
  • 14. ExampleA.A. 2012/2013Tecniche di programmazione14u vsx y3564312 7 26Vertex PrevioussuxvyVertex Weights 0uxvy
  • 15. Special caseA.A. 2012/2013Tecniche di programmazione15 If G is an un-weighted graph, then the shortest paths maybe computed just with a breadth-first visit
  • 16. A.A. 2012/2013Tecniche di programmazione16Negative-weight cycles Minimum paths cannot be defined if there are negative-weight cycles in the graph In this case, the minimum path does not exist, becauseyou may always decrease the path weight by going oncemore through the loop. Conventionally, in these case we say that the path weightis -.
  • 17. A.A. 2012/2013Tecniche di programmazione17Examplea bs c d ge f352-44876-33-6
  • 18. A.A. 2012/2013Tecniche di programmazione18Examplea bs c d ge f352-44876-33-603 -1---5 11Minimum-weight paths fromsource vertex s
  • 19. A.A. 2012/2013Tecniche di programmazione19Lemma Consider an ordered weighted graph G=(V,E), with weightfunction w: ER. Let p=<v1, v2, …, vk> a shortest path from vertex v1 tovertex vk. For all i,j such that1ijk, let pij=<vi, vi+1, …, vj> be thesub-path of p, from vettex vi to vertex vj. Therefore, pij is a shortest path from vi to vj.
  • 20. A.A. 2012/2013Tecniche di programmazione20Corollary Let p be a shortest path from s to v Consider the vertex u, such that (u,v) is the last edge inthe shortest path We may decompose p (from s to v) into: A sub-path from s to u The final edge (u,v) Therefore d(s,v)=d(s,u)+w(u,v)
  • 21. A.A. 2012/2013Tecniche di programmazione21Lemma If we chose arbitrarily the vertex u, then for all edges(u,v)E we may say that d(s,v)d(s,u)+w(u,v)
  • 22. A.A. 2012/2013Tecniche di programmazione22Relaxation Most shortest-path algorithms are based on therelaxation technique It consists of Keeping track of an updated estimate d[u] of the shortest pathtowards each node u Relaxing (i.e., updating) d[v] (and therefore the predecessorp[v]) whenever we discover that node v is more convenientlyreached by traversing edge (u,v)
  • 23. A.A. 2012/2013Tecniche di programmazione23Initial state Initialize-Single-Source(G(V,E), s)1. for all vertices v V2. do1. d[v]2. p[v]NIL3. d[s]0
  • 24. A.A. 2012/2013Tecniche di programmazione24Relaxation We consider an edge (u,v) with weight w Relax(u, v, w)1. if d[v] > d[u]+w(u,v)2. then1. d[v]  d[u]+w(u,v)2. p[v]  u
  • 25. A.A. 2012/2013Tecniche di programmazione25Example 1u v5 92u v5 72Relax(u,v,w)Before:Shortest path to vweights 9, does notcontain (u,v)After:Shortest path to vweights 7, the pathincludes (u,v)
  • 26. A.A. 2012/2013Tecniche di programmazione26Example 2u v5 62u v5 62Relax(u,v,w)Before:Shortest path to vweights 6, does notcontain (u,v)After:No relaxation possible,shortest path unchanged
  • 27. A.A. 2012/2013Tecniche di programmazione27Lemma Consider an ordered weighted graph G=(V,E), with weightfunction w: ER. Let (u,v) be an edge in G. After relaxation of (u,v) we may write that: d[v]d[u]+w(u,v)
  • 28. LemmaA.A. 2012/2013Tecniche di programmazione28 Consider an ordered weighted graph G=(V,E), with weightfunction w: ER and source vertex sV.Assume that Ghas no negative-weight cycles reachable from s. Therefore After calling Initialize-Single-Source(G,s), the predecessorsubgraph Gp is a rooted tree, with s as the root. Any relaxation we may apply to the graph does not invalidatethis property.
  • 29. A.A. 2012/2013Tecniche di programmazione29Lemma Given the previous definitions. Apply any possible sequence of relaxation operations Therefore, for each vertex v d[v]  d(s,v) Additionally, if d[v] = d(s,v), then the value of d[v] will notchange anymore due to relaxation operations.
  • 30. Shortest path algorithmsA.A. 2012/2013Tecniche di programmazione30 Differ according to one-source or all-sourcesrequirement Adopt repeated relaxation operations Vary in the order of relaxation operations they perform May be applicable (or not) to graph with negative edges(but no negative cycles)
  • 31. Floyd-Warshall algorithmGraphs: Finding shortest paths
  • 32. Floyd-Warshall algorithmA.A. 2012/2013Tecniche di programmazione32 Computes the all-sourceshortest path dist[i][j] is an n-by-n matrixthat contains the length of ashortest path from vi to vj. if dist[u][v] is ∞, there is nopath from u to v pred[s][j] is used toreconstruct an actualshortest path: stores thepredecessor vertex forreaching vj starting fromsource vs
  • 33. Floyd-Warshall: initializationA.A. 2012/2013Tecniche di programmazione33
  • 34. Example, after initializationA.A. 2012/2013Tecniche di programmazione34
  • 35. Floyd-Warshall: relaxationA.A. 2012/2013Tecniche di programmazione35
  • 36. Example, after step t=0A.A. 2012/2013Tecniche di programmazione36
  • 37. Example, after step t=1A.A. 2012/2013Tecniche di programmazione37
  • 38. Example, after step t=2A.A. 2012/2013Tecniche di programmazione38
  • 39. Example, after step t=3A.A. 2012/2013Tecniche di programmazione39
  • 40. ComplexityA.A. 2012/2013Tecniche di programmazione40 The Floyd-Warshall is basically executing 3 nested loops,each iterating over all vertices in the graph Complexity: O(V3)
  • 41. ImplementationA.A. 2012/2013Tecniche di programmazione41
  • 42. ResourcesA.A. 2012/2013Tecniche di programmazione42 Algorithms in a Nutshell, G. Heineman, G. Pollice, S.Selkow, O’Reilly, ISBN 978-0-596-51624-6, Chapter 6http://shop.oreilly.com/product/9780596516246.do http://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm
  • 43. Licenza d’usoA.A. 2012/2013Tecniche di programmazione43 Queste diapositive sono distribuite con licenza Creative Commons“Attribuzione - Non commerciale - Condividi allo stesso modo (CCBY-NC-SA)” Sei libero: di riprodurre, distribuire, comunicare al pubblico, esporre in pubblico,rappresentare, eseguire e recitare questopera di modificare questopera Alle seguenti condizioni: Attribuzione — Devi attribuire la paternità dellopera agli autori originalie in modo tale da non suggerire che essi avallino te o il modo in cui tuusi lopera. Non commerciale — Non puoi usare questopera per fini commerciali. Condividi allo stesso modo — Se alteri o trasformi questopera, o se lausi per crearne unaltra, puoi distribuire lopera risultante solo con unalicenza identica o equivalente a questa. http://creativecommons.org/licenses/by-nc-sa/3.0/