19
Weighted Graphs & Shortest Paths

Weighted Graphs & Shortest Paths

  • Upload
    devin

  • View
    69

  • Download
    0

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Page 1: Weighted Graphs & Shortest Paths

Weighted Graphs& Shortest Paths

Page 2: Weighted Graphs & Shortest Paths

Weighted Graphs

• Edges have an associated weight or cost

Page 3: Weighted Graphs & Shortest Paths

BFS?

• BFS only gives shortest path in terms of edge count, not edge weight

Page 4: Weighted Graphs & Shortest Paths

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

Dijkstra's

• Find paths from u to all others:

Page 6: Weighted Graphs & Shortest Paths

Dijkstra's

• Find paths from u to all others:

Page 7: Weighted Graphs & Shortest Paths

Dijkstra's

• Find paths from u to all others:

Page 8: Weighted Graphs & Shortest Paths

Dijkstra's

• Find paths from u to all others:

Page 9: Weighted Graphs & Shortest Paths

Dijkstra's

• Find paths from u to all others:

Page 10: Weighted Graphs & Shortest Paths

Dijkstra's

• Find paths from u to all others:

Page 11: Weighted Graphs & Shortest Paths

Dijkstra's

• Find paths from u to all others:

Page 12: Weighted Graphs & Shortest Paths

Details

• Implementation– Known once visited– Cost starts at • Update all neighbors at

each visit

– Path marked when costupdated

Page 13: Weighted Graphs & Shortest Paths

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

MST

• Minimum Spanning Tree– Spanning tree with minimal possible total weight

Page 15: Weighted Graphs & Shortest Paths

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

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

Prim's Implementation

• Same as Dijkstra's but…– Cost of vertex = min( all known edges to it )

Page 18: Weighted Graphs & Shortest Paths

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

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