190 likes | 336 Views
This article explores the concepts of weighted graphs and their shortest path algorithms, focusing on Dijkstra's Algorithm and Prim's Minimum Spanning Tree (MST). It explains how edges in weighted graphs have associated costs and how Dijkstra's method efficiently computes the shortest paths from a starting vertex to all other vertices, ensuring no negative edges are present. Additionally, it outlines the similarities with Prim's Algorithm in finding an MST, emphasizing the importance of the cost-minimizing strategies used in both algorithms. Practical implementations and complexities are also discussed.
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