Docsity

Graph Algorithms, Slides of Data Structures and Algorithms

Partial preview of the text

Download Graph Algorithms and more Slides Data Structures and Algorithms in PDF only on Docsity!

Road Map

  • Shortest path problem
    • Find the shortest path for each vertex for a specified

starting vertex

  • Unweighted graph: breadth-first traversal
  • Non-negative-weighted graph: Dijkstra’s algorithm
  • Negative-weighted graph: Bellman-Ford algorithm
  • Minimum spanning tree ß
  • Definition
  • Prim’s algortihm

Warmup: Dijkstra’s Algorithm

Minimum Spanning Trees

• MST: a spanning tree with the minimum total edge

weight

• Example

• Applications?

Graph G

MST of G

Spanning Tree Motivation

Prim’s Algorithm

  • Idea: identify which edges should be included so that
    • All nodes are connected
    • No cycle is formed; total weight is minimum
  • Prim
    • Assume undirected graphs
    • Start with any vertex S and greedily grow a tree T from S
    • For each step, pick the minimum edge that connects the

current T and (G-T) (node v in G-T)

  • Update T / G-T
  • Update the set of edges connecting these two sets (v and its nbrs)
  • Repeat until all vertices are connected in the tree

Prim Example

  • Initialize
  • Pick node with minimal cost to add to T
    • Starting vertex 0 is the trivial winner

Node 0 1 2 3 4 5 6

Edge cost 0 ∞ ∞ ∞ ∞ ∞ ∞

Prev - 1 - - - - - -

T = {}

G-T = {0,1,2,3,4,5,6}

Prim Example

  • Only consider edges between T and (G-T)
  • Update cost if lowered
  • Next pick: 1 (assume lower ID preferred)

Node 0 1 2 3 4 5 6

Edge cost 0 1 1 3 ∞ ∞ ∞

Prev - 1 0 0 0 - - -

T = {0}

G-T = {1,2,3,4,5,6}

Prim Example

  • Only consider edges between T and (G-T)
  • Edges between {0, 1} and {2,3,4,5,6}
    • Updating 1’s nbrs with a shorter edge: 4 and 6

Node 0 1 2 3 4 5 6

Edge cost 0 1 1 3 2 ∞ 7

Prev - 1 0 0 0 1 - 1

T = {0,1}

G-T = {2,3,4,5,6}

Prim Example

  • Only consider edges between T and (G-T)
  • Edges between {0,1,2} and {3,4,5,6}
    • Pick node 3, update?

Node 0 1 2 3 4 5 6

Edge cost 0 1 1 3 1 2 ∞ 7

Prev - 1 0 0 0 2 1 - 1

T = {0,1,2}

G-T = {3,4,5,6}

Prim Example

  • Only consider edges between T and (G-T)
  • Edges between {0, 1,2,3} and {4,5,6}
    • Pick node 4, update?

Node 0 1 2 3 4 5 6

Edge cost 0 1 1 1 2 1 ∞ 7

Prev - 1 0 0 2 1 3 - 1

T = {0,1,2,3}

G-T = {4,5,6}

Prim Example

  • Only consider edges between T and (G-T)
  • Edges between {0,1,2,3,4, 5} and {6}
    • Pick node 6

Node 0 1 2 3 4 5 6

Edge cost 0 1 1 1 1 1 7 1

Prev - 1 0 0 2 3 4 1 5

T = {0,1,2,3,4,5}

G-T = {6}

Prim Example

  • Final result
    • All nodes processed
    • No cycle
    • Minimum total weight (6)

Node 0 1 2 3 4 5 6

Edge cost 0 1 1 1 1 1 1

Prev - 1 0 0 2 3 4 5

T = {0,1,2,3,4,5, 6}

G-T = {}

Compare…

  • Dijkstra’s Shortest Path Algorithm:
    • All nodes start at ∞ (except source)
    • Take the node with minimum cost (path)
    • Update connecting nodes (current cost + edge cost)
    • Finds the SHORTEST PATH
  • Prim’s Minimum Spanning Tree Algorithm :
    • All nodes start at ∞ (except source)
    • Take the node with minimum cost (edge)
    • Update connecting nodes (edge cost only )
    • Finds the MINIMUM SPANNING TREE

Other MST Algorithms

  • Kruskal
    • Sort edges based on their weights in ascending order
    • For each step, pick the lowest one to add to T unless that

will create a cycle

  • Both Kruskal and Prim are greedy algorithms
  • Many other MST algorithms
  • http://en.wikipedia.org/wiki/Minimum_spanning_tree