190 likes | 329 Views
Weighted Graphs & Shortest Paths. Weighted Graphs. Edges have an associated weight or cost. BFS?. BFS only gives shortest path in terms of edge count , not edge weight. Dijkstra's Shortest Path Algorithm. Conceptual: V = all vertices T = included vertices
E N D
Weighted Graphs • Edges have an associated weight or cost
BFS? • BFS only gives shortest path in terms of edge count, not edge weight
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
Dijkstra's • Find paths from u to all others:
Dijkstra's • Find paths from u to all others:
Dijkstra's • Find paths from u to all others:
Dijkstra's • Find paths from u to all others:
Dijkstra's • Find paths from u to all others:
Dijkstra's • Find paths from u to all others:
Dijkstra's • Find paths from u to all others:
Details • Implementation • Known once visited • Cost starts at • Update all neighbors at each visit • Path marked when costupdated
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
MST • Minimum Spanning Tree • Spanning tree with minimal possible total weight
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?
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…
Prim's Implementation • Same as Dijkstra's but… • Cost of vertex = min( all known edges to it )
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