Minimum Spanning Trees: Algorithms and Techniques, Exams of Computer Science

An overview of minimum spanning trees (msts), their applications, and algorithms for finding msts. Topics include spanning trees, depth-first and breadth-first traversals, prim's algorithm, kruskal's algorithm, and union-find. Msts are essential in minimizing the length of telephone lines, airplane routes, and other applications.

Typology: Exams

Pre 2010

Uploaded on 07/30/2009

koofers-user-51v-1
koofers-user-51v-1 🇺🇸

10 documents

1 / 29

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CMSC 132:
Object-Oriented Programming II
Minimal Spanning Tree
Algorithms
Department of Computer Science
University of Maryland, College Park
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d

Partial preview of the text

Download Minimum Spanning Trees: Algorithms and Techniques and more Exams Computer Science in PDF only on Docsity!

CMSC 132:

Object-Oriented Programming II

Minimal Spanning Tree

Algorithms

Department of Computer ScienceUniversity of Maryland, College Park

Overview

Spanning trees Minimum spanning tree (MST)^ Prim’s algorithm^ Kruskal’s algorithm

Union-Find

Spanning Tree Construction

Recursive algorithmKnown = { start }explore ( start );void explore (Node X) {for each successor Y of X

if (Y is not in Known)Parent[Y] = XAdd Y to Knownexplore(Y) }

Spanning Tree Construction

Iterative algorithmKnown = { start }Discovered = { start }while ( Discovered

take node X out of Discoveredfor each successor Y of Xif (Y is not in Known)

Parent[Y] = XAdd Y to DiscoveredAdd Y to Known }

Depth-First Spanning Tree Example

Breadth-First Spanning Tree Example

Minimum Spanning Tree (MST) Spanning tree with minimum total edge weight

Minimum Spanning Tree (MST) Possible to have multiple MSTs Different spanning trees with same weight Example applications Minimize length of telephone lines for neighborhood Minimize distance of airplane routes serving cities

Algorithms for Finding MST

1.^ Bor

ů vka’s algorithm Add vertices to MST in parallel

2.^ Prim’s algorithm

Add vertices to MST^ One at a time^ Closest vertex first

3.^ Kruskal’s algorithm

Add edges to MST^ One at a time^ Lightest edge first

Shortest Path – Dijkstra’s AlgorithmS =^ ∅ P[ ] = none for all nodesC[start] = 0, C[ ] =

∞^ for all other nodes

while ( not all nodes in S

find node K not in S with smallest C[K]add K to Sfor each node J not in S adjacent to Kif ( C[K] + cost of (K,J) < C[J] )

C[J] = C[K] + cost of (K,J)P[J] = K Optimal solution computed with greedy algorithm

MST – Kruskal’s Algorithm

sort edges by weight (from least to most)tree =

for each edge (X,Y) in orderif it does not create a cycle

add (X,Y) to treestop when tree has N–1 edges Keeps track of

lightest edge remaining whether adding edge to MST creates cycle Optimal solution computed with greedy algorithm

MST – Kruskal’s Algorithm Example

MST – Kruskal’s Algorithm

Traversal approach^ Traverse tree starting at X^ If we can reach Y, adding (X,Y) would create cycle Example^ Question

Add (X,Y) to MST? Answer No, since can alreadyreach Y from X bytraversing MST

x

y z w

?

MST – Kruskal’s Algorithm

Connected subgraph approach^ Maintain set of nodes for each connected subgraph^ Initialize one connected subgraph for each node^ If X, Y in same set, adding (X,Y) would create cycle^ Otherwise1.

Add edge (X,Y) to spanning tree2. Merge sets containing X, Y

To test set membership^ Use Union-Find algorithm