graphs-spanning trees · minimum spanning tree - prim’s algorithm given: weighted graph. find a...

53
Graphs-Spanning Trees November 17, 2016 CMPE 250 Graphs-Spanning Trees November 17, 2016 1 / 53

Upload: others

Post on 13-Jul-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Graphs-Spanning Trees

November 17, 2016

CMPE 250 Graphs-Spanning Trees November 17, 2016 1 / 53

Page 2: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Topics

Spanning Trees in Unweighted Graphs

Minimum Spanning Trees in Weighted Graphs -

Prim’s Algorithm

Minimum Spanning Trees in Weighted Graphs - Kruskal’s Algorithm

CMPE 250 Graphs-Spanning Trees November 17, 2016 2 / 53

Page 3: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Definitions

Spanning tree: a tree that contains all vertices in the graph.Number of nodes: |V|Number of edges: |V|-1

CMPE 250 Graphs-Spanning Trees November 17, 2016 3 / 53

Page 4: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Spanning trees for unweighted graphs - data structures

A table (an array) Tsize = number of vertices,Tv = parent of vertex v

Adjacency lists

A queue of vertices to be processed

CMPE 250 Graphs-Spanning Trees November 17, 2016 4 / 53

Page 5: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Algorithm - initialization

1 Choose a vertex S and store it in a queue, seta counter = 0 (counts the processed nodes), andTs = 0 (to indicate the root),Ti = −1 ,i 6= s (to indicate vertex not processed)

CMPE 250 Graphs-Spanning Trees November 17, 2016 5 / 53

Page 6: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Algorithm - basic loop

2 While queue not empty and counter < |V | − 1 :Read a vertex V from the queueFor all adjacent vertices U :If Tu = −1 (not processed)Tu ← Vcounter ← counter + 1store U in the queue

CMPE 250 Graphs-Spanning Trees November 17, 2016 6 / 53

Page 7: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Algorithm – results and complexity

Result:Root: S, such that Ts = 0Edges in the tree: (Tv , V)

Complexity: O(|E |+ |V |) - we process all edges and all nodes

CMPE 250 Graphs-Spanning Trees November 17, 2016 7 / 53

Page 8: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Example

Table

0 1 is the root

1 edge (1,2)

2 edge (2,3)

1 edge (1,4)

2 edge (2,5)

Edge format: (Tv,V)

CMPE 250 Graphs-Spanning Trees November 17, 2016 8 / 53

Page 9: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Definitions

Minimum Spanning tree: a treethat contains all vertices in the graph,is connected,is acyclic, andhas minimum total edge weight.

CMPE 250 Graphs-Spanning Trees November 17, 2016 9 / 53

Page 10: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Applications of Minimum Spanning Trees

Communication networks

VLSI design

Transportation systems

CMPE 250 Graphs-Spanning Trees November 17, 2016 10 / 53

Page 11: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Minimum Spanning Tree - Prim’s algorithm

Given: Weighted graph.

Find a spanning tree with the minimal sum of the weights.Similar to shortest paths in weighted graphs.Difference: we record the weight of the current edge, not the length ofthe path .

CMPE 250 Graphs-Spanning Trees November 17, 2016 11 / 53

Page 12: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Data Structures

Three arrays:cost[v] = the weight of the shortest edge connecting v to a known vertexpath[v] = the last vertex to cause a change in cost[v]known[v] = True, if vertex v is fixed in the tree, False otherwise

CMPE 250 Graphs-Spanning Trees November 17, 2016 12 / 53

Page 13: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Data Structures (cont.)

Adjacency listsA priority queue of vertices to be processed.

Priority of each vertex is determined by the weight of edge that linksthe vertex to its parent.The priority may change if we change the parent, provided the vertex isnot yet fixed in the tree.

CMPE 250 Graphs-Spanning Trees November 17, 2016 13 / 53

Page 14: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Prim’s Algorithm

For all vcost[v ] =∞; known[v ] = false; path[v ] = −

cost[s] = 0Insert s onto a priority queue that percolates vertices

with lowest cost[] to the top.While priority queue not empty

DeleteMin v from priority queueIf not known[v ]

known[v ] = trueFor all unknown successors w of v

If weight[v ,w ] < cost[w ]cost[w ] = weight[v ,w ]path[w ] = vinsert w onto priority queue

CMPE 250 Graphs-Spanning Trees November 17, 2016 14 / 53

Page 15: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Results and Complexity

At the end of the algorithm, the tree would be represented with itsedges

{(v , path[v ])|v = 1, 2, . . . , |V |}

Complexity: O(|E |log(|V |))

CMPE 250 Graphs-Spanning Trees November 17, 2016 15 / 53

Page 16: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Example

CMPE 250 Graphs-Spanning Trees November 17, 2016 16 / 53

Page 17: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Example

CMPE 250 Graphs-Spanning Trees November 17, 2016 17 / 53

Page 18: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Example

CMPE 250 Graphs-Spanning Trees November 17, 2016 18 / 53

Page 19: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Example

CMPE 250 Graphs-Spanning Trees November 17, 2016 19 / 53

Page 20: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Example

CMPE 250 Graphs-Spanning Trees November 17, 2016 20 / 53

Page 21: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Example

CMPE 250 Graphs-Spanning Trees November 17, 2016 21 / 53

Page 22: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Example

CMPE 250 Graphs-Spanning Trees November 17, 2016 22 / 53

Page 23: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Example

CMPE 250 Graphs-Spanning Trees November 17, 2016 23 / 53

Page 24: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Example

CMPE 250 Graphs-Spanning Trees November 17, 2016 24 / 53

Page 25: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Example

CMPE 250 Graphs-Spanning Trees November 17, 2016 25 / 53

Page 26: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Example

CMPE 250 Graphs-Spanning Trees November 17, 2016 26 / 53

Page 27: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Example

CMPE 250 Graphs-Spanning Trees November 17, 2016 27 / 53

Page 28: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Example

CMPE 250 Graphs-Spanning Trees November 17, 2016 28 / 53

Page 29: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Example

CMPE 250 Graphs-Spanning Trees November 17, 2016 29 / 53

Page 30: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Example

CMPE 250 Graphs-Spanning Trees November 17, 2016 30 / 53

Page 31: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Example

CMPE 250 Graphs-Spanning Trees November 17, 2016 31 / 53

Page 32: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Example

CMPE 250 Graphs-Spanning Trees November 17, 2016 32 / 53

Page 33: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Example

CMPE 250 Graphs-Spanning Trees November 17, 2016 33 / 53

Page 34: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Kruskal’s Algorithm

The algorithm works with :set of edges,tree forests,each vertex belongs to only one tree in the forest.

CMPE 250 Graphs-Spanning Trees November 17, 2016 34 / 53

Page 35: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

The Algorithm

1 Build |V| trees of one vertex only - each vertex is a tree of its own.Store edges in priority queue

2 Choose an edge (u,v) with minimum weightif u and v belong to one and the same tree,

do nothingif u and v belong to different trees,

link the trees by the edge (u,v)3 Perform step 2 until all trees are combined into one tree only

CMPE 250 Graphs-Spanning Trees November 17, 2016 35 / 53

Page 36: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Example

CMPE 250 Graphs-Spanning Trees November 17, 2016 36 / 53

Page 37: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Example

CMPE 250 Graphs-Spanning Trees November 17, 2016 37 / 53

Page 38: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Example

CMPE 250 Graphs-Spanning Trees November 17, 2016 38 / 53

Page 39: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Example

CMPE 250 Graphs-Spanning Trees November 17, 2016 39 / 53

Page 40: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Example

CMPE 250 Graphs-Spanning Trees November 17, 2016 40 / 53

Page 41: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Example

CMPE 250 Graphs-Spanning Trees November 17, 2016 41 / 53

Page 42: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Example

CMPE 250 Graphs-Spanning Trees November 17, 2016 42 / 53

Page 43: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Example

CMPE 250 Graphs-Spanning Trees November 17, 2016 43 / 53

Page 44: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Example

CMPE 250 Graphs-Spanning Trees November 17, 2016 44 / 53

Page 45: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Example

CMPE 250 Graphs-Spanning Trees November 17, 2016 45 / 53

Page 46: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Example

CMPE 250 Graphs-Spanning Trees November 17, 2016 46 / 53

Page 47: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Example

CMPE 250 Graphs-Spanning Trees November 17, 2016 47 / 53

Page 48: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Example

CMPE 250 Graphs-Spanning Trees November 17, 2016 48 / 53

Page 49: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Example

CMPE 250 Graphs-Spanning Trees November 17, 2016 49 / 53

Page 50: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Operations on Disjoint Sets

Comparison: Are the vertices in the same tree?

Union: Combine two disjoint sets to form one set - the new treeImplementation

Union/find operations: the unions are represented as trees - nlogncomplexity.The set of trees is implemented by an array.

CMPE 250 Graphs-Spanning Trees November 17, 2016 50 / 53

Page 51: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Complexity of Kruskal’s Algorithm

The complexity is determined by the heap operations and theunion/find operations

Union/find operations on disjoint sets represented as trees: treesearch operations, with complexity O(log|V |)DeleteMin in Binary heaps with N elements is O(logN),

When performed for N elements, it is O(NlogN).

CMPE 250 Graphs-Spanning Trees November 17, 2016 51 / 53

Page 52: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Complexity of Kruskal’s Algorithm (Cont.)

Kruskal’s algorithm works with a binary heap that contains all edges:O(|E |log(|E |))However, a complete graph has

|E | = |V | × (|V | − 1)/2, i.e. |E | = O(|V |2)

Thus for the binary heap we haveO(|E |log(|E |)) = O(|E |log(|V |2) = O(2|E |log(|V |)= O(|E |log(|V |))Since for each edge we perform DeleteMin operation and union/findoperation, the overall complexity is:

O(|E |(log(|V |) + log(|V |)) = O(|E |log(|V |))

CMPE 250 Graphs-Spanning Trees November 17, 2016 52 / 53

Page 53: Graphs-Spanning Trees · Minimum Spanning Tree - Prim’s algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in weighted

Discussion

Sparse trees - Kruskal’s algorithm :Guided by edges

Dense trees - Prim’s algorithm :The process is limited by the number of the processed vertices

CMPE 250 Graphs-Spanning Trees November 17, 2016 53 / 53