lecture 35: spanning trees

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

Upload: casey

Post on 24-Feb-2016

44 views

Category:

Documents


0 download

DESCRIPTION

CSC 213 – Large Scale Programming. Lecture 35: Spanning Trees. Minimum Spanning Tree (MST). Spanning subgraph Subgraph w/ all vertices. ORD. 10. 1. PIT. DEN. 6. 7. 9. 3. DCA. STL. 4. 5. 8. 2. DFW. ATL. Minimum Spanning Tree (MST). Spanning subgraph - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 35: Spanning Trees

LECTURE 35:SPANNING TREES

CSC 213 – Large Scale Programming

Page 2: Lecture 35: Spanning Trees

Minimum Spanning Tree (MST)

Spanning subgraph Subgraph w/ all

vertices

ORDPIT

ATL

STL

DEN

DFW

DCA

101

9

8

6

3

25

7

4

Page 3: Lecture 35: Spanning Trees

Minimum Spanning Tree (MST)

Spanning subgraph Subgraph w/ all

vertices

ORDPIT

ATL

STL

DEN

DFW

DCA

101

9

8

6

3

25

7

4

Page 4: Lecture 35: Spanning Trees

Minimum Spanning Tree (MST)

Spanning subgraph Subgraph w/ all

vertices Spanning tree

Combinesspanning subgraph + tree

ORDPIT

ATL

STL

DEN

DFW

DCA

101

9

8

6

3

25

7

4

Page 5: Lecture 35: Spanning Trees

Minimum Spanning Tree (MST)

Spanning subgraph Subgraph w/ all

vertices Spanning tree

Combinesspanning subgraph + tree

MST Spanning tree which

minimizes sum of edge weights

ORDPIT

ATL

STL

DEN

DFW

DCA

101

9

8

6

3

25

7

4

Page 6: Lecture 35: Spanning Trees

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: Lecture 35: Spanning Trees

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 e7

4

28

5

7

3

9

8

Page 8: Lecture 35: Spanning Trees

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: Lecture 35: Spanning Trees

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: Lecture 35: Spanning Trees

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: Lecture 35: Spanning Trees

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: Lecture 35: Spanning Trees

Kruskal’s AlgorithmAlgorithm 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: Lecture 35: Spanning Trees

Kruskal Example

8672704

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI184

MIA

DFWLAX

SFO

187

Page 14: Lecture 35: Spanning Trees

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI184

MIA

DFWLAX

SFO

Page 15: Lecture 35: Spanning Trees

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI184

MIA

DFWLAX

SFO

Page 16: Lecture 35: Spanning Trees

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI184

MIA

DFWLAX

SFO

Page 17: Lecture 35: Spanning Trees

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI184

MIA

DFWLAX

SFO

Page 18: Lecture 35: Spanning Trees

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI184

MIA

DFWLAX

SFO

Page 19: Lecture 35: Spanning Trees

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI184

MIA

DFWLAX

SFO

Page 20: Lecture 35: Spanning Trees

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI184

MIA

DFWLAX

SFO

Page 21: Lecture 35: Spanning Trees

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI184

MIA

DFWLAX

SFO

Page 22: Lecture 35: Spanning Trees

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI184

MIA

DFWLAX

SFO

Page 23: Lecture 35: Spanning Trees

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI184

MIA

DFWLAX

SFO

Page 24: Lecture 35: Spanning Trees

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI184

MIA

DFWLAX

SFO

Page 25: Lecture 35: Spanning Trees

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI184

MIA

DFWLAX

SFO

Page 26: Lecture 35: Spanning Trees

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI184

MIA

DFWLAX

SFO

Page 27: Lecture 35: Spanning Trees

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI184

MIA

DFWLAX

SFO

Page 28: Lecture 35: Spanning Trees

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI184

MIA

DFWLAX

SFO

Page 29: Lecture 35: Spanning Trees

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI184

MIA

DFWLAX

SFO

Page 30: Lecture 35: Spanning Trees

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI184

MIA

DFWLAX

SFO

Page 31: Lecture 35: Spanning Trees

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI184

MIA

DFWLAX

SFO

Page 32: Lecture 35: Spanning Trees

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI184

MIA

DFWLAX

SFO

Page 33: Lecture 35: Spanning Trees

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI184

MIA

DFWLAX

SFO

Page 34: Lecture 35: Spanning Trees

Kruskal Example

8672704

187

1258

849

144740

1391

9461090

1121

2342

1846621

8021464

1235

337

BOS

PVD

JFK

ORD

BWI184

MIA

DFWLAX

SFO

Page 35: Lecture 35: Spanning Trees

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