I am trying to use the functionality of Expectation and Probability for random graphs, in particular for percolation models.

For example, I would like to be able to compute the expected graph distance between two fixed vertices in a complete graph with independent exponential edge weights. The following naive code does not work.

n = 5;
CompleteGraph[n,EdgeWeight -> Table[ew[m], {m,1,(n (n-1))/2}]], 1, 2], 
Table[ew[m] \[Distributed] ExponentialDistribution[1], {m,1,(n(n-1))/2}]



indicating that the random weights are not actually assigned to the edges.

A similar example would be the computation of the probability that two fixed vertices in a complete graph are in the same connected component after edges are deleted with probability $1-p$ and retained with probability $p$. Again the following naive code does not work.

n = 5; p = 1/3;
CompleteGraph[n, EdgeWeight -> Table[ew[m], {m, 1, (n(n-1))/2}]], 1, 2] == 0, 
Table[ew[m] \[Distributed] EmpiricalDistribution[{1-p, p} -> {1, 0}], {m,1,(n(n-1))/2}]

Ideally, I would like to be able to compute this probability (a polynomial in $p$) for symbolic $p$. Of course I would also be grateful for other approaches to the problem, but using Expectation and Probability would feel very intuitive to me.


The problem can be circumvented by computing the probability of a generic event which is only specified after Probability has been applied:

Clear[f, g]
n = 5;
f[ewarray_] := 
 GraphDistance[CompleteGraph[n, EdgeWeight -> ewarray], 1, 2]
g[Table[ew[m], {m, 1, (n(n-1))/2}]] == 0, Table[ew[m] \[Distributed] BernoulliDistribution[1-p], {m, 1, (n(n-1))/2}]
] /. g -> f // Simplify

The results are the same as @ubpdqn's MC calculations. I have experimented with Hold and Release to avoid the use of the extra function f but do not know enough those functions to make it work.


1 Answer 1


I may have misunderstood the aims, If so, I apologize.

For the first question:

f[n_] := GraphDistance[CompleteGraph[5, EdgeWeight -> #], 1, 2] & /@ 
   RandomVariate[ExponentialDistribution[1], {n, 10}];

: This generates a sample of size n of graph distances between vertex 1 and 2.

You can visualize:


enter image description here

Estimating expectation using Mean,e.g,:



For the second aim:

In the following let p be probability that edge/link/connection exists at an instant in the complete graph (5). The probability nodes 1 and 2 will be connected can be estimated as follows:

func[p_, n_] := (N[
      Length@#]) &@((GraphDistance[
        Graph[Range[5], Pick[EdgeList[CompleteGraph[5]], #, 1]], 1, 
        2] & /@ RandomVariate[
       BernoulliDistribution[p], {n, 10}]) /. {Infinity -> 0})

Visualizing relationship between p and probability 1 and 2 connected:

lp = ListPlot[Table[{j, func[j, 1000]}, {j, 0, 1, 0.01}], 
  Frame -> True, 
  FrameLabel -> {"p", 
    "fraction of graphs with path\n bewteen 1 and 2"}, 
  BaseStyle -> 16]

enter image description here

Visualizing some example graphs:

vis[p_, n_, m_] := 
   HighlightGraph[CompleteGraph[5, VertexLabels -> "Name"], 
      Style[Pick[EdgeList[CompleteGraph[5]], #, 1], Red, Thick], 
      ImageSize -> 100] & /@ 
    RandomVariate[BernoulliDistribution[p], {n, 10}], m], 
  Frame -> All]

For example:

vis[0.5, 25, 5]

enter image description here

Again, I apologize if I have misunderstood. Experts will have better answers. Perhaps, this will kickstart. Note this could be generalized to other nodes and the sample and visualization could be combined to allow calculations and visualizations. I do not have time to tidy up at present.

  • $\begingroup$ Thanks, this is a beautiful Monte-Carlo approach to the problem. I am mainly interested in symbolic calculations, though. $\endgroup$
    – Eckhard
    Aug 11, 2014 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.