Skip to main content

POJ3557 Map Generator

· 3 min read
Xinyu Ma
Research Scientist @ Meta

Discuss POJ3557 Map Generator and its variants.

Problem Statement

Given NN nodes (labeled). For each pair of node, the probability that there is an edge is pp. Ask the probability to get a connected graph.

Solution - DP

Let GnG_n denote the probability that the graph of nn nodes is not connected. Fix a node 11. Consider the component 11 is in. Assume the size is kk. Then, this component should have no edges with any other node out of this component. Which means:

Gn=k=1n1(n1k1)(1Gk)(1p)k(nk)G_n = \sum_{k=1}^{n-1} {n-1 \choose k-1}(1 - G_k)(1-p)^{k(n-k)}

Use DP to calculate in O(n2)O(n^2). The answer is 1Gn1-G_n.

Variant

Ask the probability to get a graph with KK components.

Solution - DP

Let Pn,cP_{n,c} denote the probability that a graph of size nn have cc components. Now we try to split the graph into two disconnected parts: one has kk nodes and ll components, the other has nkn-k and clc-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 cc components into 22 non-empty sets, i.e. 2S(c,2)=2c22S(c, 2) = 2^c - 2. Here, S(c,2)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=12c2k=1n1l=1min(c1,k)(nk)Pk,lPnk,cl(1p)k(nk)P_{n,c} = \frac{1}{2^c -2}\sum_{k=1}^{n-1}\sum_{l=1}^{\min(c-1,k)} {n \choose k}P_{k,l}P_{n-k,c-l}(1-p)^{k(n-k)}

This can be solved by DP in O(n4)O(n^4).

Discussion

Clearly, 1Gn=Pn,11 - G_n = P_{n,1}.

Now let's get rid of the probability PP and consider the total number of graphs. Let pn,cp_{n,c} denote the number of labeled graphs of size nn with cc components. Then,

a1++ak=n1k!(na1,a2,,ak)pa1,1pa2,1pak,1=2n(n1)/2\sum_{a_1+\cdots+a_k = n} \frac{1}{k!}{n \choose a_1, a_2, \ldots, a_k} p_{a_1,1}p_{a_2,1}\cdots p_{a_k,1} = 2^{n(n-1)/2}

Let hn=pn,1h_n = p_{n,1} and HH be the exponential generating function (e.g.f.) of hnh_n. We have:

2n(n1)/2n!=a1++ak=n1k!ha1a1!hakak!=[tn]k=01k!i=1k(h11!+h22!+)=[tn]k=0(H1)kk!=[tn]eH1\begin{darray}{rcl} \frac{2^{n(n-1)/2}}{n!} &=& \sum_{a_1+\cdots+a_k=n} \frac{1}{k!} \frac{h_{a_1}}{a_1!}\cdots \frac{h_{a_k}}{a_k!} \\ &=& [t^n ]\sum_{k=0}^{\infty}\frac{1}{k!}\prod_{i=1}^{k}\left( \frac{h_1}{1!} + \frac{h_2}{2!} + \cdots \right) \\ &=& [t^n ]\sum_{k=0}^{\infty}\frac{(H-1)^k}{k!} \\ &=& [t^n ]e^{H-1} \end{darray}

Thus, the e.g.f. is:

H=1+ln(n=12n(n1)/2n!tn)H = 1 + \ln\left( \sum_{n=1}^{\infty}\frac{2^{n(n-1)/2}}{n!}t^n \right)

which is not elementary. See also OEIS A001187.

Now consider the original DP. Let p=1/2p= 1/2. Then, hn=2n(n1)/2(1Gn)h_n = 2^{n(n-1)/2}(1-G_n) and

2n(n1)/2hn=k=1n1(n1k1)hk2(nk)(nk1)/22n(n+1)/2=k=0n(nk)hk+12(nk)(nk+1)/2\begin{darray}{rcl} 2^{n(n-1)/2} - h_n &=& \sum_{k=1}^{n-1} {n-1 \choose k-1}h_k 2^{(n-k)(n-k-1)/2} \\ 2^{n(n+1)/2} &=& \sum_{k=0}^{n} {n \choose k} h_{k+1} 2^{(n-k)(n-k+1)/2} \end{darray}

This looks similar to a binomial inversion, but we cannot separate nn from the right hand side, so it does not work.