Download - Weighted Graphs & Shortest Paths
![Page 1: Weighted Graphs & Shortest Paths](https://reader030.vdocuments.net/reader030/viewer/2022033102/56814c31550346895db936ab/html5/thumbnails/1.jpg)
Weighted Graphs& Shortest Paths
![Page 2: Weighted Graphs & Shortest Paths](https://reader030.vdocuments.net/reader030/viewer/2022033102/56814c31550346895db936ab/html5/thumbnails/2.jpg)
Weighted Graphs
• Edges have an associated weight or cost
![Page 3: Weighted Graphs & Shortest Paths](https://reader030.vdocuments.net/reader030/viewer/2022033102/56814c31550346895db936ab/html5/thumbnails/3.jpg)
BFS?
• BFS only gives shortest path in terms of edge count, not edge weight
![Page 4: Weighted Graphs & Shortest Paths](https://reader030.vdocuments.net/reader030/viewer/2022033102/56814c31550346895db936ab/html5/thumbnails/4.jpg)
Dijkstra's Shortest Path Algorithm
• Conceptual:– V = all vertices– T = included vertices– Pick starting vertex, include in T– Pick element not in T with minimal cost to reach from T
• Move to T• Update costs of remaining vertices
![Page 5: Weighted Graphs & Shortest Paths](https://reader030.vdocuments.net/reader030/viewer/2022033102/56814c31550346895db936ab/html5/thumbnails/5.jpg)
Dijkstra's
• Find paths from u to all others:
![Page 6: Weighted Graphs & Shortest Paths](https://reader030.vdocuments.net/reader030/viewer/2022033102/56814c31550346895db936ab/html5/thumbnails/6.jpg)
Dijkstra's
• Find paths from u to all others:
![Page 7: Weighted Graphs & Shortest Paths](https://reader030.vdocuments.net/reader030/viewer/2022033102/56814c31550346895db936ab/html5/thumbnails/7.jpg)
Dijkstra's
• Find paths from u to all others:
![Page 8: Weighted Graphs & Shortest Paths](https://reader030.vdocuments.net/reader030/viewer/2022033102/56814c31550346895db936ab/html5/thumbnails/8.jpg)
Dijkstra's
• Find paths from u to all others:
![Page 9: Weighted Graphs & Shortest Paths](https://reader030.vdocuments.net/reader030/viewer/2022033102/56814c31550346895db936ab/html5/thumbnails/9.jpg)
Dijkstra's
• Find paths from u to all others:
![Page 10: Weighted Graphs & Shortest Paths](https://reader030.vdocuments.net/reader030/viewer/2022033102/56814c31550346895db936ab/html5/thumbnails/10.jpg)
Dijkstra's
• Find paths from u to all others:
![Page 11: Weighted Graphs & Shortest Paths](https://reader030.vdocuments.net/reader030/viewer/2022033102/56814c31550346895db936ab/html5/thumbnails/11.jpg)
Dijkstra's
• Find paths from u to all others:
![Page 12: Weighted Graphs & Shortest Paths](https://reader030.vdocuments.net/reader030/viewer/2022033102/56814c31550346895db936ab/html5/thumbnails/12.jpg)
Details
• Implementation– Known once visited– Cost starts at • Update all neighbors at
each visit
– Path marked when costupdated
![Page 13: Weighted Graphs & Shortest Paths](https://reader030.vdocuments.net/reader030/viewer/2022033102/56814c31550346895db936ab/html5/thumbnails/13.jpg)
Dijkstra's Algorithm
• Finds shortest path to all other vertices– Can terminate early once goal is known
• Assumption:– No negative edges
• Big O – for the curious– O(V2) with table• V times do linear search for next vertex to visit
– O((E + V)logV) with priority queue (binary heap)• O(E + VlogV) possible with fibonacci heap
![Page 14: Weighted Graphs & Shortest Paths](https://reader030.vdocuments.net/reader030/viewer/2022033102/56814c31550346895db936ab/html5/thumbnails/14.jpg)
MST
• Minimum Spanning Tree– Spanning tree with minimal possible total weight
![Page 15: Weighted Graphs & Shortest Paths](https://reader030.vdocuments.net/reader030/viewer/2022033102/56814c31550346895db936ab/html5/thumbnails/15.jpg)
Prim's MST
• Conceptual:– V = all vertices– T = included vertices– Pick starting vertex, include in T– Pick element not in T with minimal cost to reach from T
• Move to T• Update costs of remaining vertices
Sound familiar?
![Page 16: Weighted Graphs & Shortest Paths](https://reader030.vdocuments.net/reader030/viewer/2022033102/56814c31550346895db936ab/html5/thumbnails/16.jpg)
Prim's MST ~ Dijkstra's
• Conceptual:– V = all vertices– T = included vertices– Pick starting vertex, include in T– Pick element not in T with minimal cost to reach from T
• Move to T• Update costs of remaining vertices : cost = just current edge
One big difference…
![Page 17: Weighted Graphs & Shortest Paths](https://reader030.vdocuments.net/reader030/viewer/2022033102/56814c31550346895db936ab/html5/thumbnails/17.jpg)
Prim's Implementation
• Same as Dijkstra's but…– Cost of vertex = min( all known edges to it )
![Page 18: Weighted Graphs & Shortest Paths](https://reader030.vdocuments.net/reader030/viewer/2022033102/56814c31550346895db936ab/html5/thumbnails/18.jpg)
Prim's Algorithm
• Finds a MST– May be multiple equal cost
• Assumption:– Undirected
• Big O – for the curious– O(V2) with table• V times do linear search for next vertex to visit
– O((E + V)logV) with priority queue (binary heap)• O(E + VlogV) possible with fibonacci heap
![Page 19: Weighted Graphs & Shortest Paths](https://reader030.vdocuments.net/reader030/viewer/2022033102/56814c31550346895db936ab/html5/thumbnails/19.jpg)
A*
• A-Star : Dijkstra's algorithm, butinclude estimate of future cost for everyvertex
• Estimate must never underestimate cost
http://computerscience.chemeketa.edu/cs160Reader/NineAlgorithms/Search2.html