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

Post on 13-Jul-2020

8 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Graphs-Spanning Trees

November 17, 2016

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

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

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

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

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

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

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

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

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

Applications of Minimum Spanning Trees

Communication networks

VLSI design

Transportation systems

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

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

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

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

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

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

Example

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

Example

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

Example

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

Example

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

Example

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

Example

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

Example

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

Example

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

Example

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

Example

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

Example

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

Example

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

Example

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

Example

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

Example

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

Example

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

Example

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

Example

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

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

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

Example

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

Example

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

Example

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

Example

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

Example

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

Example

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

Example

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

Example

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

Example

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

Example

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

Example

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

Example

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

Example

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

Example

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

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

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

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

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

top related