0
|
0__1__0
| | |
1__1__0
|
1
Let's say I have a undirected graph. We have these conditions:
You are only allowed to delete nodes labeled as '1'.
Deletion of any node must not make the graph a forest
We are allowed to delete multiple nodes, but the above conditions must be satisfied.
Count the number of different trees(unrooted) that can be made by the above process. Note that there is no such thing as 'root' here. We only count the different structures.
For the above, answer is 4 because:
0
|
0 0
| |
1__1__0 ------> #1
|
1
0
|
0 0
| | -------> #2
1__1__0
0
|
0__1__0
| | ---------> #3
1 0
0
|
0__1__0 ---------> #4
|
0
I would appreciate any kind of help or hints.
(In case the graph is already a tree, we are still allowed to delete nodes to get new trees, subject to the above conditions)
As you have already pointed out, a naive exponential solution would be to take all subsets of the 1-nodes and for each, checking that removing the nodes gains a tree graph. Two thoughts how you could prune some of the subsets:
Let me denote the 1-nodes in your example A, B, C, D:
0
|
0__A__0
| | |
C__B__0
|
D
Removing {A, B}
partitions the graph. Therefore obviously removing {A, B, C}
or {A, B, D}
or{A, B, C, D}
also partitions the graph. You don't need to explicitly check any of those.
(Unless all but one of the partitioned graph components consist solely of the 1-nodes. Then removing all these 1-node components could also gain a valid solution. You may need to check this as a special case.)
For example, by removing A
we get a tree:
0
|
0 0
| |
C__B__0
|
D
We can generate additional trees by removing further nodes. For these we only need to check that by removing them we don't partition the graph. If not, we can be sure the graph remains a tree. Removing D
in this example illustrates the idea.
These optimizations probably won't make the algorithm better than exponential in the worst case, but they could make it practical for reasonably small input.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With