algorithms for enumerating all spanning trees of undirected and weighted graphs

84
Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs Presented by R97922102 李李李 R97922104 李李李 R97922124 李李李 Sanjiv Kapoor and H.Ramesh

Upload: dylan-brown

Post on 02-Jan-2016

31 views

Category:

Documents


2 download

DESCRIPTION

Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs. Sanjiv Kapoor and H.Ramesh. Presented by R97922102 李孟哲 R97922104 陳翰霖 R97922124 張仕明. Index. Introduction Computation Tree Algorithm 1 Algorithm 2 Algorithm 3. Introduction. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Presented by

R97922102 李孟哲R97922104 陳翰霖R97922124 張仕明

Sanjiv Kapoor and H.Ramesh

Page 2: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Index

• Introduction

• Computation Tree

• Algorithm 1

• Algorithm 2

• Algorithm 3

Page 3: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Introduction

• Spanning tree enumeration in undirected graphs is an important issue in network and circuit analysis

• In this paper, author enumerate spanning trees by the computation tree

• Every node in the computation tree represent a spanning tree

Page 4: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Algorithms Introduction

• We use this way to enumerate all the spanning tree on undirected and weighted graphs.

• Algorithm 1– O(N+V+E) time– O(V2E) space

• Algorithm 2– O(N+V+E) time– O(VE) space

• Algorithm 3 (with sorting)– O(NlogV+VE) time– O(N+VE) space

Page 5: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Spanning Tree

• The original graph G

Page 6: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Spanning Tree

• A Spanning tree of G

Page 7: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Spanning Tree

• add a edge to the tree

Page 8: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Spanning Tree

• get a cycle (fundamental cycle)

Page 9: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Spanning Tree

• remove a edge on the cycle

Page 10: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Spanning Tree

• get a new spanning tree of G

Page 11: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Spanning Tree

• From this cycle, we can obtain several spanning trees.

Page 12: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Computation Tree

Page 13: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Computation tree

• First, we start off with a spanning tree T, and generate all other spanning trees form T by replacing edges in T by edges outside T

• For every node x, Sx is the spanning tree corresponding to this node

• To ensure that each spanning tree is generated exactly once, we use 2 edge set INx and OUTx for every node x

Page 14: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

IN and OUT

• The set INx consist of edges which are always included in node x and it’s descendants

• The set OUTx consist of edges which are always not included in node x and it’s descendants

• The IN and OUT set of the root are both empty

Page 15: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Computation tree

1

2 3 4

51

2 3 4

5

In =[ ] Out=[ ] S =[ ]

,  ,  1,2,4  

Arbitrary choose a spanning tree

Page 16: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Computation tree

1

2 3 4

5

1

2 3 4

5

In =[ ] Out=[ ] S =[ ]

,  ,  1,2,4  

Page 17: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Computation tree

1

2 3 4

5

1

2 3 4

5

In =[ ] Out=[ ] S =[ ]

,  ,  1,2,4  

In =[ ] Out=[ ] S =[ ]

,  ,  ,  

In =[ ] Out=[ ] S =[ ]

,  ,  ,  

In =[ ] Out=[ ] S =[ ]

,  ,  ,  

In =[ ] Out=[ ] S =[ ]

,  ,  ,  

Page 18: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Computation tree

1

2 3 4

5

1

2 3 4

5

In =[ ] Out=[ ] S =[ ]

,  ,  1,2,4  

In =[ ] Out=[ ] S =[ ]

,  ,  2,4,5  

In =[ ] Out=[ ] S =[ ]

,  ,  1,2,5  

In =[ ] Out=[ ] S =[ ]

,  ,  1,2,4  

In =[ ] Out=[ ] S =[ ]

,  ,  1,4,5  

1

2 3 4

5

1

2 3 4

5

1

2 3 4

5

1

2 3 4

5

(-2,+5)

(-1,+5)

(-4,+5)

Page 19: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Computation tree

1

2 3 4

5

1

2 3 4

5

In =[ ] Out=[ ] S =[ ]

,  ,  1,2,4  

In =[ ] Out=[ ] S =[ ]

,  1,  2,4,5  

In =[ ] Out=[ ] S =[ ]

,  4,  1,2,5  

In =[ ] Out=[ ] S =[ ]

,  5,  1,2,4  

In =[ ] Out=[ ] S =[ ]

,  2,  1,4,5  

1

2 3 4

5

1

2 3 4

5

1

2 3 4

5

1

2 3 4

5

(-2,+5)

(-1,+5)

(-4,+5)

Page 20: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Computation tree

1

2 3 4

5

1

2 3 4

5

In =[ ] Out=[ ] S =[ ]

,  ,  1,2,4  

In =[ ] Out=[ ] S =[ ]

5,  1,  2,4,5  

In =[ ] Out=[ ] S =[ ]

5,  4,  1,2,5  

In =[ ] Out=[ ] S =[ ]

,  5,  1,2,4  

In =[ ] Out=[ ] S =[ ]

5,  2,  1,4,5  

1

2 3 4

5

1

2 3 4

5

1

2 3 4

5

1

2 3 4

5

(-2,+5)

(-1,+5)

(-4,+5)

Page 21: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Computation tree

1

2 3 4

5

1

2 3 4

5

In =[ ] Out=[ ] S =[ ]

,  ,  1,2,4  

In =[ ] Out=[ ] S =[ ]

5,2,  1,  2,4,5  

In =[ ] Out=[ ] S =[ ]

5,2,1,  4,  1,2,5  

In =[ ] Out=[ ] S =[ ]

,  5,  1,2,4  

In =[ ] Out=[ ] S =[ ]

5,  2,  1,4,5  

1

2 3 4

5

1

2 3 4

5

1

2 3 4

5

1

2 3 4

5

(-2,+5)

(-1,+5)

(-4,+5)

Page 22: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Computation tree

1

2 3 4

5

1

2 3 4

5

In =[ ] Out=[ ] S =[ ]

,  5,  1,2,4  

Page 23: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Computation tree

1

2 3 4

5

1

2 3 4

5

In =[ ] Out=[ ] S =[ ]

,  5,  1,2,4  

In =[ ] Out=[ ] S =[ ]

,  5,  1,3,4  

In =[ ] Out=[ ] S =[ ]

,  5,3  1,2,4  

In =[ ] Out=[ ] S =[ ]

,  5,  2,3,4  

1

2 3 4

5

1

2 3 4

5

1

2 3 4

5

(-1,+3)

(-2,+3)

Page 24: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Computation tree

1

2 3 4

5

1

2 3 4

5

In =[ ] Out=[ ] S =[ ]

,  5,  1,2,4  

In =[ ] Out=[ ] S =[ ]

1,3,  5,2  1,3,4  

In =[ ] Out=[ ] S =[ ]

,  5,3  1,2,4  

In =[ ] Out=[ ] S =[ ]

3,  5,1  2,3,4  

1

2 3 4

5

1

2 3 4

5

1

2 3 4

5

(-1,+3)

(-2,+3)

Page 25: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Computation tree

1

2 3 4

5

Page 26: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Computation tree C’(G)

Page 27: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Appear not only once

• The last son and its parent are the same

Page 28: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Another Computation tree C’(G)

1

2 3 4

5

G

C(G) C’(G)

Page 29: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Another Computation tree C’(G)

Page 30: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Computation tree C(G)’s property

• The son has zero or one edge differ from its parent.

• The number of sons equals to the length of the fundamental cycle

Page 31: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Computation tree C’(G)’s property

• The son has one edge differ from its parent.• The number of sons equals to the sum of the

length of all fundamental cycles

Page 32: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Lemma 1

• The computation tree has at its internal nodes and leaves all the spanning trees of G

• [Proof] • Following from induction and inclusion/exclusion

principle• Let A be a node of computation tree C(G),

B1,...,Bk+1 be the son of A

Page 33: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

• All spanning trees in B1,…, Bk contain edge f

• All spanning trees in Bk+1 don’t contain edge f

• The spanning trees as Bj ‘s descendants contain edges e1,…,ej-1, but the edge ej

• e1,…,ek and f form a cycle

A

B1 B2 Bk Bk+1…

… … … …computation tree

f

e1

e2

ek

Page 34: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Algorithm 1

Page 35: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

The Algorithm 1

Generate a random spanning tree and initialize some data structures.

Prepare and modify the data structures.

Recursively call the algorithm.

Page 36: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Possible problem in algorithm

• How to maintain the IN , OUT, S ?• How to find the fundamental cycles?

Page 37: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Possible problem in algorithm

• How to maintain the IN , OUT, S ?– S is easy, just add a edge and delete another one.– We use a data structure AG to maintain IN, OUT.– We would choose edges from AG

– Initial: AG is the graph itself.

Page 38: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Possible problem in algorithm

– Modify AG – Adding a edge into IN– Contract the edge

12

3

12

3

Page 39: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Possible problem in algorithm

– Modify AG – Add a edge into OUT– Delete the edge

12

3

12

3

Page 40: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Modify the fundamental cycle

• After we exchange the edges, the fundamental cycles of this tree would change

• We need to modify the fundamental cycles

f

e1 e2

e3

f

e1

e3

e2

f

e1

e3

e2

Page 41: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Modify the fundamental cycle

• Author uses the data structure C to maintain the fundamental cycles

• C maintains the fundamental cycles corresponding to the current tree

• There are 3 operations to construct computation tree:1. After deleting edge ei in AG, merge the fundamental

cycles contain ei

2. After contracting edge e in AG, contract edge e in C

3. Delete the nontree edge for the last son

Page 42: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Merge the fundamental cycles

• Use 4 pointer to modify the cycles

• This operation takes time proportional to the size of resulting cycle

Page 43: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Contract edge in C

• This operation takes time proportional to fundamental cycles containing the contracted edge

Page 44: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Delete the nontree edge in C

• If the nontree edge is a part of a multiedge, then do nothing

• else delete the fundamental cycle containing the nontree edge

• This operation takes time proportional to the size of its fundamental cycle

Page 45: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

AG and C

• When some edge e is added to set IN:– Contract e in AG and C

• When some edge e is added to set OUT:– Delete e in AG and C – Merge the fundamental cycles containing e

Page 46: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Lemma 1.2

• The algorithm done at each node A of C(G) is O(|s(A)|+|g(A)|)

• Where s(A) is the set of sons of A in C(G), g(A) is the set of sons in C’(G) of nodes in s(A)

Page 47: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Proof

• When replace tree edge ei by f , We need to

– Contract f in AG: constant time

– merge fundamental cycles in C containing ei:it takes time proportional to the sum size of resulting cycle ( O(|g(A)|) )

– Contract ei in C: it takes time proportional to the number of cycles contain ei ( O(|g(A)|) )

– Contract ei in AG:

it takes time proportional to the number of multiedges incident to the endpoints of ei ( O(|g(A)|) )

Page 48: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Proof

• Before operating the node is the last son, then – Delete f in AG:

constant time – Delete f in C:

time proportional to the size of its fundamental cycle( O(s(A)) )

• Every node takes O(|s(A)|+|g(A)|) time

Page 49: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Theorem 1.3

• All spanning trees can be correctly generated in O( N + V + E ) time by Algorithm 1

• [Proof]– First, construct a spanning tree and setup it’s

data structure• require O( V + E ) time

– Every node in computation tree C(G) takes O(|s(A)|+|g(A)|) time

– Summing overall nodes of C(G) is O(N)

Page 50: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Theorem 1.4

• The space requirement of the spanning tree enumeration Algorithm 1 is O( V2E )

• [Proof]– At each node of C(G), take O( VE ) space– The height of C’(G) is at most V

… …

Page 51: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Algorithm 2

Page 52: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Algorithm 2

• We play two tricks to reduce the space complexity from O(V2E) to O(VE) – DFS tree– Biconnected components

Page 53: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

DFS tree

• The first spanning tree is generated by depth first search.– Nontree edges are back edges

• It is easy to find the fundamental cycle of the depth first search tree

Page 54: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Why it’s easier at DFS tree

• To find a fundamental cycle, we just need to trace the back edge

• We can reduce the space requirement since we don’t have to maintain the fundamental cycle set

Page 55: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Maintaining the d.f.s invariant

• For a node A of C(G), SA is a d.f.s tree of GA

• Assume the vertices of SA are numbered by a post-order traversal

1

23

45

6

7

8root

Page 56: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Maintaining the d.f.s invariant

• Select the nontree edge whose upper endpoint has the smallest number (upper is the endpoint closer to the root)

• This edge is the replacement edge

1

23

45

6

7

8root

Page 57: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Maintaining the d.f.s invariant

• Replace the tree in order of the post-order number

• The new graph’s spanning tree is still a d.f.s tree

1

23

45

6

7

8 1

23

45

6

7

81

23

5

6

7

8

Page 58: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Biconnected components

Page 59: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Biconnected components

Page 60: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Biconnected components

Page 61: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Biconnected components

• The red edges (bridges) must be chosen in each spanning tree of G.

• Let nST(G) be the number of spanning trees in G.

• Then nST(G) = nST(G1)*nST(G2)*…..nST(Gn)

G1

G2

G3

G4

G5

Page 62: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Biconnected components

• No change in time and space complexity in analysis, but make the algorithm efficiency in most cases.

Page 63: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Bridge

• When exchange edges, there may be some tree edges which is not contained by any fundamental cycles– We call those edges bridges– A bridge must be in this spanning tree

Page 64: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

How to detect bridge

• Bridge only occurs on a fundamental cycle when we exchange 2 edges

• Since x4 has a branch in current graph GA ,e4 is a bridge when we delete e3

e1

e2

e4

e5

e3

x4

Page 65: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Lemma 2.1

• The work done at each node A of C(G) is O(|s(A)|+|g(A)|)

• Proof : – Removing bridges spends time in O(|s(A)|)– Exchanging and contracting edges spend

O(g|A|)

Page 66: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Theorem 2.2

• All spanning tree can be correctly generated in O( N + V + E ) time by Algorithm 2

• [Proof]– First, we construct a spanning and setup it’s

data structure • require O( V + E ) time

– Every node in computation tree C(G) takes O(|s(A)|+|g(A)|) time

– Summing overall nodes of C(G) takes O(N) time

Page 67: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Theorem 2.3

• The space requirement in Algorithm 2 is O( VE )• [Proof]

– At each node of C(G), takes O( E ) space– The height of C’(G) is at most V

… …

Page 68: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Algorithm 3

Page 69: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Algorithm 3

• If we want to show all the spanning trees in increasing order.– Generate all the trees O(N+V+E)– Sorting all the trees O(NlogN)

• Algorithm 3 generates sorted order in O(NlogV+VE)time

Page 70: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Algorithm 3

• We create M queues, M = (V-1)(E-V+1)=O(VE) and indexed by “exchange”

Page 71: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Algorithm 3

• We create a priority queue (heap) whose size is equal to M=O(VE), and height is O(logVE)

• This heap maintains the first element of each queue.

….

Page 72: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Algorithm 3

We sort all the nontree edges by increasing order.

Page 73: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Algorithm 3

Minimum spanning tree

Output this one

Page 74: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Algorithm 3

Add the smallest nontree edge and delete a tree edge.

We can generate the sons

Page 75: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Algorithm 3

… Insert these nodes into queue corresponding to “exchange”

Page 76: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Algorithm 3

…Get the minimum cost tree from the heap.

Output this tree.

Do the algorithm again

….

Page 77: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Algorithm 3

…Insert these nodes into queues corresponding to “exchange”

Page 78: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Algorithm 3

We choose the minimum cost tree from the heap again,

These blue vertices are possible to be chosen.

Page 79: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Analysis of algorithm 3

• [Lemma 3.1] Any child’s cost is larger than parent’s.

Tree edges = {e1,e2….en}

Nontree edges = {f1,f2…..fm}

This node’s cost must be larger than( or equal to) parent’s.

Page 80: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Analysis of algorithm 3

• [Lemma 3.1] Any child’s cost is larger than parent’s.

Tree edges = {e1,e2….en}U{f1}

Nontree edges = {f2…..fm}

This node’s cost must be larger than( or equal to) parent’s.

Page 81: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Analysis of algorithm 3

• [Lemma 3.2] The spanning trees enter the queue for each of the exchanges in sorted order.

• Blue one is smaller than red one.

Page 82: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Analysis of algorithm 3

• Those two nodes are in queue, which implies their parents have been output.

• Blue one is in front, because its parent outputs earlier than dark red one.

• Because they have same “exchange”, so blue one < red one. …

Page 83: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Analysis of algorithm 3

• Time complexity– Sorting edges: O(ElogE) time– Generation: O(N+V+E) time– O(VE) queues, N trees– Create a heap: O(VE) time.– One operation in heap: O(logVE) time– N operation in heap: O(NlogVE) time

– O(N logVE) + O(ElogE) + O(N+V+E) + O(VE)

=O(N logV3 + ElogE + N + V + E + VE)

=O( N logV + VE)

Page 84: Algorithms for Enumerating All Spanning Trees of Undirected and Weighted Graphs

Analysis of algorithm 3

• Space complexity– O(N) trees– O(VE) queues

– O(N+VE) space