1 / 16

CHEMINS DANS LES GRAPHES

CHEMINS DANS LES GRAPHES. Le PCC entre un sommet et tous les autres sommets dans un graphe sans poids Le PCC entre un sommet et tous les autres sommets dans un graphe à des poids positifs Le PCC entre un sommet et tous les autres sommets dans un graphe pondéré.

inga
Download Presentation

CHEMINS DANS LES GRAPHES

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CHEMINS DANS LES GRAPHES • Le PCC entre un sommet et tous les autres sommets dans un graphe sans poids • Le PCC entre un sommet et tous les autres sommets dans un graphe à des poids positifs • Le PCC entre un sommet et tous les autres sommets dans un graphe pondéré. • Le PCC entre un sommet et tous les autres sommets dans un graphe orienté et sans cycle

  2. GenericShortestPath(G, s) Dist= ∞ for u in G Dists = 0 P =  Ps = s Q =  Q.append(s) whileQ: z = Q.get_and_delete() forxin Adjz: If Distx > Distz+w(z, x): /♯ the edge(z,x) is « tense» Distx = Distz+w(z, x) Px= z Q.append(x) Return < P, D >

  3. Le plus court chemin entre 2 sommets • Données: Un grapheG avec des arêtes (arcs) sans poidset sommets de G. • But: Le plus court chemin entre s et les autres sommets du graphe Complexité: O(V+E)

  4. ShortestPath(G, s) Dist= ∞ for u in G Dists = 0 Ps = s Q =  Q.append(s) whileQ: z = Q.pop() forxin Adjz: If Px== 0: Distx= Distz +1 Px= z Q.append(x) returnpath(P)

  5. Le plus court chemin: Dijkstra • Données: Un grapheG avec des arêtes (arcs) à des poidspositifset un sommets. • But: Le plus court chemin entre s et les autres sommets du graphe. Complexité: O(V2+E) // O((V+E)log V) // O(E+Vlog V)

  6. The algorithm : DIJKSTRA(G,s) Dist= ∞for u in G Dists=0 Ps=s Q = V whileQ: z = Q.delete_min() forxin Adjz: Relax(z, x) ... Return <P, Dist>

  7. Dijkstra1 (G.w, s) Dist= ∞for u in G Dists = 0 Ps = s Q = Makeheap(V) (usingdist-values as keys) whileQ: z = Q.deletemin_key() /♯extractmin operation forxin Adjz: If Distx > Distz+w(z, x) Q.decrease_key(x) /♯Distx = Distz+w(z, x),Px = z

  8. Dijkstra2 (altenative) (G,s) M = 0 for u in G Dist= ∞ for u in G Dists = 0 Ps = s Q.add(s) /♯altenatively, Q={s} and insert nodeswhenreached whileQ: z = Q.delete_min() forxin Adjz: If Distx > Distz+w(z, x) Q.decrease_key(x) /♯Distx = Distz+w(z, x),Px = z If Mx==0: Mx=1 Q.add(x)

  9. COMPLEXITE • Opérations: Extract-Min Decrease-Key # ops: V E • Temps/Op. • Array: O(V) O(1) • Heap: O(logV) O(logV) • Fibonacci Heap: O(logV) O(1) (amortie) • Complexitétotale en Temps: • Array: O(V2) • Heap: O((V+E)logV) • FibonacciHeap: O(E+VlogV)

  10. Le plus court chemin: Bellman-Ford Données: Un graphevaluéG et un sommets sans cycle négatif. But: Le plus court chemin entre s et les autres sommets du graphe. Complexité: O(VE)

  11. Bellman-Ford (G.w,s) M = 0 for u in G Dist= ∞ for u in G Ms=1 Dists=0 Q.enqueue(s) Ps=s whileQ: z = Q.dequeue() Mz=0 forxin Adjz: If Distx > Distz+w(z,x) Distx = Distz+w(z,x) Px= z if Mx==0: Mx=1 Q.enqueue(x)

  12. Le chemin le plus court: DAGs • Données: Un grapheorienté et sans cycle G, et un sommetsde G. • But: Le plus court chemin entre s et les autres sommets du graphe. Complexité: O(V+E)

  13. SHORTEST_PATHS_DAGs(G.w, s) Dist= ∞for u in G Dists =0 Ps = s Q = Topological_sort(G) whileQ: z = Q.pop() forxin Adjz: If Distx > Distz+w(z, x) Distx = Distz+w(z, x) Px = z

  14. Détection des Cycles négatifs • Données: Un grapheG et un sommets. • But: G posséde t-il un cycle négatif? Complexité: O(VE)

  15. NEGATIVE_CYCLE(G.w, s) M = 0 for u in G, Dist= ∞ for u in G, Dists=0, a =1, Q.enqueue(s), Q.enqueue(♯) whileQ: z = Q.dequeue() if z ==♯: if a < n : Q.enqueue(♯) a+=1 elif Q: PRINT ‘Gcontains a negativecycle’ break forxin Adjz: If Distx > Distz+w(z,x): Distx = Distz+w(z,x) if Mx ≠ a: Mx = a Q.enqueue(x) G does not containsa negative cycle

  16. Shortestpaths application: Currency conversion Currencyconversion. Givencurrencies and exchange rates, whatis best way to convert one ounce of gold to US dollars? 1 oz. Gold → 327.25 US Dollar. 1 oz. gold→ 208.10 →UKPound→ 327.00 US Dollar[ 208.10 x 1.5714 ] 1 oz. gold → 455.2 Francs→ 304.39 Euros→ 327.28UKPound[ 455.2 x 0.6677 x 1.0752 ] CurrencyUKPound Euro JapYenSwiss US Dollar Gold (oz.) UKPound1.0000 0.6853 0.005290 0.4569 0.6368 208.100 Euro1.4599 1.0000 0.007721 0.6677 0.9303 304.028 JapYen189.050 129.520 1.0000 85.4694 120.400 39346.7 Swiss2.1904 1.4978 0.011574 1.0000 1.3941 455.200 US Dollar 1.57141.0752 0.008309 0.7182 1.0000 327.250 Gold(oz.) 0.004816 0.00329 0.0000255 0.002201 0.003065 1.0000

More Related