Discuss POJ3557 Map Generator and its variants.
Problem Statement
Given N nodes (labeled). For each pair of node, the probability that there is an edge is p.
Ask the probability to get a connected graph.
Solution - DP
Let Gn denote the probability that the graph of n nodes is not connected.
Fix a node 1. Consider the component 1 is in.
Assume the size is k.
Then, this component should have no edges with any other node out of this component.
Which means:
Gn=k=1∑n−1(k−1n−1)(1−Gk)(1−p)k(n−k)
Use DP to calculate in O(n2). The answer is 1−Gn.
Variant
Ask the probability to get a graph with K components.
Solution - DP
Let Pn,c denote the probability that a graph of size n have c components.
Now we try to split the graph into two disconnected parts:
one has k nodes and l components,
the other has n−k and c−l.
Note that we will double count some cases.
The number of times a graph is counted is equal to twice of the number of ways we put c components into 2 non-empty sets,
i.e. 2S(c,2)=2c−2.
Here, S(c,2) denote the second kind of Stirling number, and we double it because we count on each side.
(Another way to consider this is the number of all subsets, except for the empty and full).
Thus,
Pn,c=2c−21k=1∑n−1l=1∑min(c−1,k)(kn)Pk,lPn−k,c−l(1−p)k(n−k)
This can be solved by DP in O(n4).
Discussion
Clearly, 1−Gn=Pn,1.
Now let's get rid of the probability P and consider the total number of graphs.
Let pn,c denote the number of labeled graphs of size n with c components.
Then,
a1+⋯+ak=n∑k!1(a1,a2,…,akn)pa1,1pa2,1⋯pak,1=2n(n−1)/2
Let hn=pn,1 and H be the exponential generating function (e.g.f.) of hn.
We have:
n!2n(n−1)/2====a1+⋯+ak=n∑k!1a1!ha1⋯ak!hak[tn]k=0∑∞k!1i=1∏k(1!h1+2!h2+⋯)[tn]k=0∑∞k!(H−1)k[tn]eH−1
Thus, the e.g.f. is:
H=1+ln(n=1∑∞n!2n(n−1)/2tn)
which is not elementary. See also OEIS A001187.
Now consider the original DP.
Let p=1/2.
Then, hn=2n(n−1)/2(1−Gn) and
2n(n−1)/2−hn2n(n+1)/2==k=1∑n−1(k−1n−1)hk2(n−k)(n−k−1)/2k=0∑n(kn)hk+12(n−k)(n−k+1)/2
This looks similar to a binomial inversion, but we cannot separate n from the right hand side, so it does not work.