Docsity

Spanning Trees and Minimal Cost Spanning Trees in Data Structures and Algorithms - Prof. D, Study notes of Data Structures and Algorithms

Partial preview of the text

Download Spanning Trees and Minimal Cost Spanning Trees in Data Structures and Algorithms - Prof. D and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Data Structures and Algorithms^ CS245-2008S-18Spanning Trees

David GallesDepartment of Computer ScienceUniversity of San Francisco

18-0:^ Spanning Trees^ Given a connected, undirected graph

G

A^ subgraph^ of^ G

contains a subset of the vertices and edges in

G

A^ Spanning Tree

T^ of^ G^ is: subgraph of G contains all vertices in^ G connected acyclic

18-2:^ Spanning Tree Examples^ Spanning Tree^0

18-3:^ Spanning Tree Examples^ Graph^0

18-5:^ Minimal Cost Spanning Tree^ Minimal Cost Spanning Tree^ Given a weighted, undirected graph

G

Spanning tree of

G^ which minimizes the sum of

all weights on edges of spanning tree

18-6:^ MST Example

2^6

4 1

3 10 2 7 5 8

(^4 )

18-8:^ Minimal Cost Spanning Trees^ Can there be more than one minimal costspanning tree for a particular graph?

18-9:^ Minimal Cost Spanning Trees^ Can there be more than one minimal costspanning tree for a particular graph?^ YES!^ What happens when all edges have unit cost?

18-11:^ Calculating MST^ Two algorithms to calculate MST:^ Kruskal’s Algorithm^ Build a “forest” of spanning trees^ Combine into one tree^ Prims Algorithm^ Grow a single tree out from a start vertex

18-12:^ Kruskal’s Algorithm^ Start with an empty graph (no edges)^ Sort the edges by cost^ For each edge

e^ (in increasing order of cost) Add e to G if it would not cause a cycle

18-14:^ Kruskal’s Algorithm^ Proof (by contradiction)^ Assume that

no^ optimal MST

T^ contains the

minimum cost edge

e

Add^ e^ to^ T^ , which causes a cycle Remove an edge other than

e^ to break the cycle

′^ cost T ≤^ T^ , a contradiction

18-15:^ Kruskal’s Algorithm^ Coding Kruskal’s Algorithm:^ Place all edges into a list^ Sort list of edges by cost^ For each edge in the list^ Select the edge if it does not form a cyclewith previously selected edges^ How can we do this?

18-17:^ Kruskal’s Algorithm^ We need to:^ Put each vertex in its own tree^ Given any two vertices

vand^ v, determine if^1

they are in the same tree Given any two vertices

vand^ v, merge the^1

tree containing^ v

and the tree containing 1

v^2

... sound familiar?

18-18:^ Kruskal’s Algorithm^ Disjoint sets!^ Create a list of all edges^ Sort list of edges^ For each edge

e^ = (v, v)^ in the list^12 if FIND(v) != FIND(v) 12 Add e to spanning tree UNION(v, v)^12