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 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 cost updated
Dijkstra's Algorithm Finds shortest path to all other vertices Can terminate early once goal is known Assumption: No negative edges
A* A-Star : Dijkstra's algorithm, but include estimate of future cost for every vertex Estimate must never overestimate cost https://qiao.github.io/PathFinding.js/visual/
MST Minimum Spanning Tree Spanning tree with minimal possible total weight
Prim's MST Conceptual: Sound familiar? 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: One big difference… 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 Assumption: May be multiple equal cost Undirected