Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 19 Minimal Spanning Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.

Similar presentations


Presentation on theme: "Lecture 19 Minimal Spanning Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine."— Presentation transcript:

1 Lecture 19 Minimal Spanning Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine

2 CSCI 1900 Lecture 19 - 2 Lecture Introduction Reading –Rosen Sections 11.4, 11.5 Spanning tree Weighted graphs Minimal spanning tree –Two algorithms to generate Prim's algorithm Kruskal's algorithm

3 CSCI 1900 Lecture 19 - 3 Tree Recall the definition of a tree –A connected acyclic graph Any tree with n vertices has n - 1 edges

4 CSCI 1900 Lecture 19 - 4 Spanning Tree T is a spanning tree of graph G, if T is a tree with –The same vertices as G, and –A subset of the edges of G

5 CSCI 1900 Lecture 19 - 5 Notation A weighted graph is a graph, whose edges have an associated real-number weight Example: The weight of the edge {A, B} is also called the distance between A and B C 30 10 15 7 A B D

6 CSCI 1900 Lecture 19 - 6 Notation Two vertices are neighbors if they are connected by an edge –Two vertices are adjacent if they are neighbors The nearest neighbor of a vertex is the neighbor with the smallest weight

7 CSCI 1900 Lecture 19 - 7 Notation (cont) Neighbors of A: –B, C Neighbors of C: –B, A, D Nearest Neighbor of A: –C Nearest Neighbor of C: –D C 30 10 15 7 A B D

8 CSCI 1900 Lecture 19 - 8 Minimal Spanning Tree A minimal spanning tree is a spanning tree for which the sum of the weights of all the edges is as small as possible

9 CSCI 1900 Lecture 19 - 9 Spanning Trees – Example C 30 10 7 A B D C 15 7 A B D C C 30 15 7 A B D C 30 10 15 7 A B D ∑ w i = 32 ∑ w i = 47 ∑ w i = 52

10 CSCI 1900 Lecture 19 - 10 Prim’s Algorithm Let R be a symmetric, connected relation with n vertices 1.Chose a vertex v 1 of R Let V = {v 1 } and E ={ } 2.Choose a nearest neighbor v i of V that is adjacent to v j, v j  V, and for which the edge (v i, v j ) does not form a cycle with existing elements of E 3.Repeat step 2 until | E | = n-1

11 CSCI 1900 Lecture 19 - 11 Prim’s Algorithm (cont) Prim’s algorithm is a member of the class of algorithms call greedy algorithms Greedy algorithms take decisions based upon what is optimal locally Greedy algorithms do not always generate an overall optimal solution However, Prim’s algorithm can be shown to be optimal Running time = Θ( n 2 )

12 CSCI 1900 Lecture 19 - 12 Prim’s Algorithm 13 6 1 5 7 2 11 6 3 10 5 8 12 7 1 2 3 a b c d e f g h 1 2 6 3 1 2 3 a b c d e f g h Start with vertex f

13 CSCI 1900 Lecture 19 - 13 Kruskal’s Algorithm Let R be a symmetric, connected relation with n vertices, with S = { e 1, e 2, …, e k }: the set of weighted edges 1.Choose an edge e 1 in S of the smallest weight Let E = {e 1 } Replace S with S – {e 1 } 2.Chose an edge e i in S of the smallest weight that does not make a cycle with members of E Replace E with E U {e i } and S with S – {e 1 } 3.Repeat Step 2 until | E | = n-1

14 CSCI 1900 Lecture 19 - 14 Kruskal’s Algorithm (cont) Kruskal’s algorithm is also a greedy algorithm Running time = Θ(n lg( n ))

15 CSCI 1900 Lecture 19 - 15 Kruskal’s Algorithm 13 6 1 5 7 2 11 6 3 10 5 8 12 7 1 2 3 a b c d e f g h 1 2 6 3 1 2 3 a b c d e f g h

16 CSCI 1900 Lecture 19 - 16 Key Concepts Summary Spanning tree Weighted graphs Minimal spanning tree –Two algorithms to generate Prim's algorithm Kruskal's algorithm Reading for next time –Kolman Section 10.3


Download ppt "Lecture 19 Minimal Spanning Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine."

Similar presentations


Ads by Google