csc 213 – large scale programming. minimum spanning tree (mst) spanning subgraph subgraph w/ all...

35
LECTURE 35: SPANNING TREES CSC 213 – Large Scale Programming

Upload: jasmin-sharp

Post on 18-Jan-2016

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSC 213 – Large Scale Programming. Minimum Spanning Tree (MST)  Spanning subgraph  Subgraph w/ all vertices ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3

LECTURE 35:SPANNING TREES

CSC 213 – Large Scale Programming

Page 2: CSC 213 – Large Scale Programming. Minimum Spanning Tree (MST)  Spanning subgraph  Subgraph w/ all vertices ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3

Minimum Spanning Tree (MST)

Spanning subgraph Subgraph w/ all

vertices

ORD

PIT

ATL

STL

DEN

DFW

DCA

101

9

8

6

3

25

7

4

Page 3: CSC 213 – Large Scale Programming. Minimum Spanning Tree (MST)  Spanning subgraph  Subgraph w/ all vertices ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3

Minimum Spanning Tree (MST)

Spanning subgraph Subgraph w/ all

vertices

ORD

PIT

ATL

STL

DEN

DFW

DCA

101

9

8

6

3

25

7

4

Page 4: CSC 213 – Large Scale Programming. Minimum Spanning Tree (MST)  Spanning subgraph  Subgraph w/ all vertices ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3

Minimum Spanning Tree (MST)

Spanning subgraph Subgraph w/ all

vertices

Spanning tree Combines

spanning subgraph + tree

ORD

PIT

ATL

STL

DEN

DFW

DCA

101

9

8

6

3

25

7

4

Page 5: CSC 213 – Large Scale Programming. Minimum Spanning Tree (MST)  Spanning subgraph  Subgraph w/ all vertices ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3

Minimum Spanning Tree (MST)

Spanning subgraph Subgraph w/ all

vertices

Spanning tree Combines

spanning subgraph + tree

MST Spanning tree which

minimizes sum of edge weights

ORD

PIT

ATL

STL

DEN

DFW

DCA

101

9

8

6

3

25

7

4

Page 6: CSC 213 – Large Scale Programming. Minimum Spanning Tree (MST)  Spanning subgraph  Subgraph w/ all vertices ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3

No Cycles in MST

Edge in MST cheaper than one making cycle Assume there exists an edge e not in MST Cycle, C, occurs after adding e to min.

spanning tree Assume that f in C heavier than e Shrink MST using e and dropping f from C

84

2 36

7

7

9

8e

f 84

2 36

7

7

9

8e

f

Page 7: CSC 213 – Large Scale Programming. Minimum Spanning Tree (MST)  Spanning subgraph  Subgraph w/ all vertices ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3

Partition Property

Given partitioning of vertices in a graph Required that each vertex in exactly 1

partition Use smallest edge to connect each of

the To complete following MST, can use either f

or e

74

2

8

5

7

3

9

8

Page 8: CSC 213 – Large Scale Programming. Minimum Spanning Tree (MST)  Spanning subgraph  Subgraph w/ all vertices ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3

Kruskal’s Algorithm

Similar to Prim-Jarnik, including finding MST Also like Prim-Jarnik, adds edges greedily But Kruskal processes Edges using PriorityQueue

Check if Edge makes cycle before adding to MST No cycles in an MST, so add Edge if no cycle

occurs Detecting cycles hard, however

Page 9: CSC 213 – Large Scale Programming. Minimum Spanning Tree (MST)  Spanning subgraph  Subgraph w/ all vertices ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3

Kruskal’s Algorithm

Similar to Prim-Jarnik, including finding MST Also like Prim-Jarnik, adds edges greedily But Kruskal processes Edges using PriorityQueue

Check if Edge makes cycle before adding to MST No cycles in an MST, so add Edge if no cycle

occurs Detecting cycles hard, however

Page 10: CSC 213 – Large Scale Programming. Minimum Spanning Tree (MST)  Spanning subgraph  Subgraph w/ all vertices ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3

Kruskal’s Algorithm

Similar to Prim-Jarnik, including finding MST Also like Prim-Jarnik, adds edges greedily But Kruskal processes Edges using PriorityQueue

Check if Edge makes cycle before adding to MST No cycles in an MST, so add Edge if no cycle

occurs Detecting cycles hard, however

Page 11: CSC 213 – Large Scale Programming. Minimum Spanning Tree (MST)  Spanning subgraph  Subgraph w/ all vertices ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3

Structure for Kruskal’s Algorithm Kruskal’s needs to maintain collection of

trees Accept Edge connecting 2 trees & reject it

otherwise Data structure named Partition relied

upon Store disjoint Set instances within a Partition

For Kruskal’s, each Set holds tree from graph Methods supporting Sets defined by Partition find(u): find and returns Set containing u union(p,r): replace p & r with their union makeSet(u): creates Set for u & adds to Partition

Page 12: CSC 213 – Large Scale Programming. Minimum Spanning Tree (MST)  Spanning subgraph  Subgraph w/ all vertices ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3

Kruskal’s Algorithm

Algorithm KruskalMST(Graph G)Q new PriorityQueue()T new Graph()P new Partition()for (Vertex v : G.vertices())

P.makeSet(v)T.insertVertex(v)

for (Edge e : G.edges())Q.insert(e.getWeight(), e);

while (T.numEdges() < T.numVertices()-1)Edge e = Q.removeMin().value()Assign u, v to G.endpoints(e)if P.find(u) P.find(v) T.insertEdge(e) P.union(P.find(u),P.find(v))

return T

Page 13: CSC 213 – Large Scale Programming. Minimum Spanning Tree (MST)  Spanning subgraph  Subgraph w/ all vertices ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3

Kruskal Example

8672704

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI

184

MIA

DFW

LAX

SFO

187

Page 14: CSC 213 – Large Scale Programming. Minimum Spanning Tree (MST)  Spanning subgraph  Subgraph w/ all vertices ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI

184

MIA

DFW

LAX

SFO

Page 15: CSC 213 – Large Scale Programming. Minimum Spanning Tree (MST)  Spanning subgraph  Subgraph w/ all vertices ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI

184

MIA

DFW

LAX

SFO

Page 16: CSC 213 – Large Scale Programming. Minimum Spanning Tree (MST)  Spanning subgraph  Subgraph w/ all vertices ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI

184

MIA

DFW

LAX

SFO

Page 17: CSC 213 – Large Scale Programming. Minimum Spanning Tree (MST)  Spanning subgraph  Subgraph w/ all vertices ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI

184

MIA

DFW

LAX

SFO

Page 18: CSC 213 – Large Scale Programming. Minimum Spanning Tree (MST)  Spanning subgraph  Subgraph w/ all vertices ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI

184

MIA

DFW

LAX

SFO

Page 19: CSC 213 – Large Scale Programming. Minimum Spanning Tree (MST)  Spanning subgraph  Subgraph w/ all vertices ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI

184

MIA

DFW

LAX

SFO

Page 20: CSC 213 – Large Scale Programming. Minimum Spanning Tree (MST)  Spanning subgraph  Subgraph w/ all vertices ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI

184

MIA

DFW

LAX

SFO

Page 21: CSC 213 – Large Scale Programming. Minimum Spanning Tree (MST)  Spanning subgraph  Subgraph w/ all vertices ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI

184

MIA

DFW

LAX

SFO

Page 22: CSC 213 – Large Scale Programming. Minimum Spanning Tree (MST)  Spanning subgraph  Subgraph w/ all vertices ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI

184

MIA

DFW

LAX

SFO

Page 23: CSC 213 – Large Scale Programming. Minimum Spanning Tree (MST)  Spanning subgraph  Subgraph w/ all vertices ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI

184

MIA

DFW

LAX

SFO

Page 24: CSC 213 – Large Scale Programming. Minimum Spanning Tree (MST)  Spanning subgraph  Subgraph w/ all vertices ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI

184

MIA

DFW

LAX

SFO

Page 25: CSC 213 – Large Scale Programming. Minimum Spanning Tree (MST)  Spanning subgraph  Subgraph w/ all vertices ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI

184

MIA

DFW

LAX

SFO

Page 26: CSC 213 – Large Scale Programming. Minimum Spanning Tree (MST)  Spanning subgraph  Subgraph w/ all vertices ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI

184

MIA

DFW

LAX

SFO

Page 27: CSC 213 – Large Scale Programming. Minimum Spanning Tree (MST)  Spanning subgraph  Subgraph w/ all vertices ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI

184

MIA

DFW

LAX

SFO

Page 28: CSC 213 – Large Scale Programming. Minimum Spanning Tree (MST)  Spanning subgraph  Subgraph w/ all vertices ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI

184

MIA

DFW

LAX

SFO

Page 29: CSC 213 – Large Scale Programming. Minimum Spanning Tree (MST)  Spanning subgraph  Subgraph w/ all vertices ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI

184

MIA

DFW

LAX

SFO

Page 30: CSC 213 – Large Scale Programming. Minimum Spanning Tree (MST)  Spanning subgraph  Subgraph w/ all vertices ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI

184

MIA

DFW

LAX

SFO

Page 31: CSC 213 – Large Scale Programming. Minimum Spanning Tree (MST)  Spanning subgraph  Subgraph w/ all vertices ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI

184

MIA

DFW

LAX

SFO

Page 32: CSC 213 – Large Scale Programming. Minimum Spanning Tree (MST)  Spanning subgraph  Subgraph w/ all vertices ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI

184

MIA

DFW

LAX

SFO

Page 33: CSC 213 – Large Scale Programming. Minimum Spanning Tree (MST)  Spanning subgraph  Subgraph w/ all vertices ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI

184

MIA

DFW

LAX

SFO

Page 34: CSC 213 – Large Scale Programming. Minimum Spanning Tree (MST)  Spanning subgraph  Subgraph w/ all vertices ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI

184

MIA

DFW

LAX

SFO

Page 35: CSC 213 – Large Scale Programming. Minimum Spanning Tree (MST)  Spanning subgraph  Subgraph w/ all vertices ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3

For Next Lecture

Weekly assignment available on Angel Due at special time: before next Monday’s

quiz Programming assignment #3 designs

due today

Graph Quiz will be on Monday Started before test, so could include

implementations Bring notes, templates, & anything else you

want Weekly assignment question review highly

encouraged