Function Repository Resource:

RandomWeightedGraph

Source Notebook

Create a random weighted graph

Contributed by: Peter Burbery

ResourceFunction["RandomWeightedGraph"][{v,e},randomfunction]

generates a random graph with v vertices and e edges with edge weights given by randomfunction.

ResourceFunction["RandomWeightedGraph"][graphdistribution,randomfunction]

generates a random graph following graphdistribution.

ResourceFunction["RandomWeightedGraph"][spec,randomfunction,k]

generates k random graphs.

ResourceFunction["RandomWeightedGraph"][spec,randomfunction,dims]

generates an array with dimensions dims.

Details

randomfunction should be based on RandomReal or RandomInteger, for example RandomReal[{1,7}]&.
The function accepts the same options as Graph.

Examples

Basic Examples (2) 

Make a random graph:

In[1]:=
ResourceFunction["RandomWeightedGraph"][{20, 54}, RandomReal]
Out[1]=

Make a random graph with edge weights between 1 and 7:

In[2]:=
ResourceFunction["RandomWeightedGraph"][{20, 54}, RandomReal[{1, 7}] &]
Out[2]=

Scope (5) 

Make a random graph with integer weights:

In[3]:=
ResourceFunction["RandomWeightedGraph"][{20, 54}, RandomInteger[{1, 7}] &]
Out[3]=

Make a list of random graphs:

In[4]:=
ResourceFunction["RandomWeightedGraph"][{20, 54}, RandomInteger[{1, 7}] &, 7]
Out[4]=

Make an array of random graphs:

In[5]:=
ResourceFunction["RandomWeightedGraph"][{20, 54}, RandomInteger[{1, 7}] &, {2, 3, 5}, ImageSize -> Tiny]
Out[5]=

Make a random graph with a graph distribution:

In[6]:=
ResourceFunction["RandomWeightedGraph"][
 BarabasiAlbertGraphDistribution[54, 3], RandomInteger[{1, 7}] &]
Out[6]=

Create a random weighted graph with integer weights with edge labels:

In[7]:=
ResourceFunction["RandomWeightedGraph"][{9, 18}, RandomInteger[{1, 7}] &, EdgeLabels -> "EdgeWeight"]
Out[7]=

Properties and Relations (3) 

Create a random weighted graph:

In[8]:=
gr = ResourceFunction["RandomWeightedGraph"][{9, 18}, RandomInteger[{1, 7}] &, EdgeLabels -> "EdgeWeight"]
Out[8]=

Find a solution to the undirected Chinese Postman problem for this graph:

In[9]:=
tour = FindPostmanTour[gr]
Out[9]=

Make a table highlighting the steps by constructing directed graphs:

In[10]:=
firsttour = First[tour] /. {x_ \[UndirectedEdge] y_ -> x \[DirectedEdge] y};
Table[Graph@Take[firsttour, k], {k, Length[firsttour]}]
Out[11]=

Publisher

Peter Burbery

Version History

  • 1.0.0 – 01 August 2022

Related Resources

License Information