Minimum Spanning Trees - Data Structures and Algorithm - Lecture Slides, Slides of Data Structures and Algorithms

Some concept of Data Structures and Algorithm are Permutation, Representation, Implemented, Algorithm Design, Dynamic Programming, Graph Data Structures, String Processing, General Trees. Main points of this lecture are: Minimum Spanning Trees, Arrange, Rambunctious, Children, Straight, Statements, Capable, Throwing, Minimum Number, Efficient Algorithm

Typology: Slides

2012/2013

Uploaded on 04/27/2013

shareeka_555
shareeka_555 🇮🇳

4

(6)

74 documents

1 / 37

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Minimum Spanning Trees
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25

Partial preview of the text

Download Minimum Spanning Trees - Data Structures and Algorithm - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

Minimum Spanning Trees

Problem of the Day

Your job is to arrange n rambunctious children in a straight line, facing front. You are given a list of m statements of the form “i hates j”. If i hates j, then you do not want put i somewhere behind j, because then i is capable of throwing something at j.

  1. Give an algorithm that orders the line, (or says that it is not possible) in O(m + n) time.

Weighted Graph Algorithms

Beyond DFS/BFS exists an alternate universe of algorithms for edge-weighted graphs. Our adjancency list representation quietly supported these graphs:

typedef struct { int y; int weight; struct edgenode *next; } edgenode;

Minimum Spanning Trees

A tree is a connected graph with no cycles. A spanning tree is a subgraph of G which has the same set of vertices of G and is a tree. A minimum spanning tree of a weighted graph G is the spanning tree of G whose edges sum to minimum weight. There can be more than one minimum spanning tree in a graph → consider a graph with identical weight edges.

Why Minimum Spanning Trees?

The minimum spanning tree problem has a long history – the first algorithm dates back at least to 1926!. Minimum spanning tree is always taught in algorithm courses since (1) it arises in many applications, (2) it is an important example where greedy algorithms always give the optimal answer, and (3) Clever data structures are necessary to make it work. In greedy algorithms, we make the decision of what next to do by selecting the best local option from all available choices

  • without regard to the global structure.

Applications of Minimum Spanning Trees

Minimum spanning trees are useful in constructing networks, by describing the way to connect a set of sites using the smallest total amount of wire. Minimum spanning trees provide a reasonable way for clustering points in space into natural groups. What are natural clusters in the friendship graph?

Minimum Spanning Trees and TSP

When the cities are points in the Euclidean plane, the minimum spanning tree provides a good heuristic for traveling salesman problems. The optimum traveling salesman tour is at most twice the length of the minimum spanning tree.

The Option Traveling System tour is at most twicethe length of the minimum spanning tree. Note: There can be more than one minimum spanningtree considered as a group with identical weight edges.

Prim’s Algorithm

If G is connected, every vertex will appear in the minimum spanning tree. If not, we can talk about a minimum spanning forest. Prim’s algorithm starts from one vertex and grows the rest of the tree an edge at a time. As a greedy algorithm, which edge should we pick? The cheapest edge with which can grow the tree by one vertex without creating a cycle.

Prim’s Algorithm in Action

6

G Prim(G,A) Kruskal(G)

3

A

2 3 4

1 5 A

4 2 6

5 1 A

2

5 (^43) 7 12

7

4 7

2 9 5

Why is Prim Correct?

We use a proof by contradiction: Suppose Prim’s algorithm does not always give the minimum cost spanning tree on some graph. If so, there is a graph on which it fails. And if so, there must be a first edge (x, y) Prim adds such that the partial tree V ′^ cannot be extended into a minimum spanning tree.

Prim’s Algorithm is correct!

Thus we cannot go wrong with the greedy strategy the way we could with the traveling salesman problem.

How Fast is Prim’s Algorithm?

That depends on what data structures are used. In the simplest implementation, we can simply mark each vertex as tree and non-tree and search always from scratch:

Select an arbitrary vertex to start. While (there are non-tree vertices) select minimum weight edge between tree and fringe add the selected edge and vertex to the tree

This can be done in O(nm) time, by doing a DFS or BFS to loop through all edges, with a constant time test per edge, and a total of n iterations.

v = start; while (intree[v] == FALSE) { intree[v] = TRUE; p = g− >edges[v]; while (p! = NULL) { w = p− >y; weight = p− >weight; if ((distance[w] > weight) && (intree[w] == FALSE)) { distance[w] = weight; parent[w] = v; } p = p− >next; } v = 1; dist = MAXINT; for (i=1; i<= g− >nvertices; i++) if ((intree[i] == FALSE) && (dist > distance[i])) { dist = distance[i]; v = i; } } }

Prim’s Analysis

Finding the minimum weight fringe-edge takes O(n) time – just bump through fringe list. After adding a vertex to the tree, running through its adjacency list to update the cost of adding fringe vertices (there may be a cheaper way through the new vertex) can be done in O(n) time. Total time is O(n^2 ).