Docsity

Spanning Trees and Minimal Cost Spanning Trees, Study notes of Data Structures and Algorithms

Partial preview of the text

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

Data Structures and Algorithms^ Spanning Trees^ Chris BrooksDepartment of Computer ScienceUniversity of San Francisco

Department of Computer Science — University of San Francisco – p.1/

21-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

Department of Computer Science — University of San Francisco – p.2/

21-2:^ Spanning Tree Examples

Spanning Tree^0

6^ Department of Computer Science — University of San Francisco – p.4/

21-3:^ Spanning Tree Examples

Graph^0

6^ Department of Computer Science — University of San Francisco – p.5/

21-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

Department of Computer Science — University of San Francisco – p.7/

21-6:^ MST Example

2^6

4 1

3 10 2 7 5 8

(^4 )

Department of Computer Science — University of San Francisco – p.8/

21-8:^ Minimal Cost Spanning Trees

Can there be more than one minimal cost spanning treefor a particular graph?

Department of Computer Science — University of San Francisco – p.10/

21-9:^ Minimal Cost Spanning Trees

Can there be more than one minimal cost spanning treefor a particular graph? YES!^ What happens when all edges have unit cost?

Department of Computer Science — University of San Francisco – p.11/

21-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

Department of Computer Science — University of San Francisco – p.13/

21-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

Department of Computer Science — University of San Francisco – p.14/

21-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

Department of Computer Science — University of San Francisco – p.16/

21-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 cycle withpreviously selected edges^ How can we do this?

Department of Computer Science — University of San Francisco – p.17/

21-17:^ Kruskal’s Algorithm

We need to:^ Put each vertex in its own tree^ Given any two vertices

vand^ v, determine if they are^1 in the same tree Given any two vertices

vand^ v, merge the tree^1 containing^ vand the tree containing^1

v^2 ... sound familiar?

Department of Computer Science — University of San Francisco – p.19/

21-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

Department of Computer Science — University of San Francisco – p.20/