Docsity

Minimum Spanning Trees and Shortest Paths Algorithms: Kruskal's, Prim's, and Dijkstra's, Study notes of Data Structures and Algorithms

Partial preview of the text

Download Minimum Spanning Trees and Shortest Paths Algorithms: Kruskal's, Prim's, and Dijkstra's and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Data and File Structures

Minimum Spanning Trees

What is a Minimum Spanning Tree.

  • Let G = (V, E) be a simple, connected, undirected graph that is not edge-weighted.
  • (^) A spanning tree of G is a free tree (i.e., a tree with no root) with | V | - 1 edges that connects all the vertices of the graph.
  • Thus a minimum spanning tree for G is a graph, T = (V’, E’) with the following properties:  (^) V’ = V  (^) T is connected  (^) T is acyclic.
  • (^) A spanning tree is called a tree because every acyclic undirected graph can be viewed as a general, unordered tree. Because the edges are undirected, any vertex may be chosen to serve as the root of the tree.

Constructing Minimum Spanning Trees

  • (^) Any traversal of a connected, undirected graph visits all the vertices in that graph. The set of edges which are traversed during a traversal forms a spanning tree.
  • (^) For example, Fig:(b) shows the spanning tree obtained from a breadth-first traversal starting at vertex b.
  • (^) Similarly, Fig:(c) shows the spanning tree obtained from a depth-first traversal starting at vertex c. Graph G)a ( (b) Breadth-first spanning tree of G rooted at b (c) Depth-first spanning tree of G rooted at c

GENERIC-MST( G , w )

A = Ø

while A does not form a spanning tree

find an edge ( u , v ) that is safe for A

A = A U {( u , v )}

return A

MST-KRUSKAL( G , w ) A = Ø for each vertex v Є V [ G ] MAKE-SET( v ) sort the edges of E into nondecreasing order by weight w for each edge ( u , v ) Є E , taken in nondecreasing order by weight if (FIND-SET( u ) != FIND-SET( v )) A = A U {( u , v )} UNION( u , v ) return A

KRUSKAL’S ALGORITHM

MST-PRIM( G , w , r )

for each u Є V [ G ]

key [ u ] = ∞

π[ u ] = NIL

key [ r ] = 0

Q = V [ G ]

while Q != Ø

u = EXTRACT-MIN( Q )

for each v Є Adj [ u ]

if v Є Q and w ( u , v ) < key [ v ]

π[ v ] = u

key [ v ] = w ( u , v )

PRIM’S ALGORITHM

Dijkstra’s Shortest Path Algorithm

  • (^) What is the Shortest Path Problem?
  • (^) Is the shortest path problem well defined?
  • (^) The Dijkstra's Algorithm for Shortest Path Problem.
  • (^) Implementation Dijkstra's Algorithm

What is the shortest path problem?

  • (^) In an edge-weighted graph, the weight of an edge measures the cost of traveling that edge.
  • (^) For example, in a graph representing a network of airports, the weights could represent: distance, cost or time.
  • (^) Such a graph could be used to answer any of the following:
    • (^) What is the fastest way to get from A to B?
    • (^) Which route from A to B is the least expensive?
    • (^) What is the shortest possible distance from A to B?
  • (^) Each of these questions is an instance of the same problem: The shortest path problem!

Is the shortest path problem well defined? - Cont'd

  • (^) Things get difficult for a graph with negative weights.
  • (^) For example, the path D, A, C, E, F costs 4 even though the edge (D, A) costs 5 -- the longer the less costly.
  • (^) The problem gets even worse if the graph has a negative cost cycle. e.g. {D, A, C, D}
  • (^) A solution can be found even for negative-weight graphs but not for graphs involving negative cost cycles. {D, A, C, D, A, C, E, F} = 2 {D, A, C, D, A, C, D, A, C, E, F} = 0

The Dijkstra's Algorithm

  • (^) Dijkstra's algorithm solves the single-source shortest

path problem for a non-negative weights graph.

  • (^) It finds the shortest path from an initial vertex, say s, to

all the other vertices.

Floyed Warshall’s Algorithm