1
$\begingroup$

I would like to use Manipulate on the below function so I can vary the vertex and edge counts on the generated graph. Any help on how to do so would be greatly appreciated, and thank you in advance.

randomDAG[vertexCount_, edgeCount_] /; 
  edgeCount < vertexCount (vertexCount - 1)/2 := 
 Module[{elems, adjacencyMatrix}, 
  elems = 
   RandomSample@
    PadRight[ConstantArray[1, edgeCount], 
     vertexCount (vertexCount - 1)/2];
  adjacencyMatrix = 
   Take[FoldList[RotateLeft, elems, Range[0, vertexCount - 2]], All, 
     vertexCount]~LowerTriangularize~-1;
  AdjacencyGraph[adjacencyMatrix]]
$\endgroup$
0

1 Answer 1

2
$\begingroup$
Manipulate[
 Module[{elems, adjacencyMatrix}, 
  elems = 
   RandomSample@
    PadRight[ConstantArray[1, edgeCount], 
     vertexCount (vertexCount - 1)/2];
  adjacencyMatrix = 
   Take[FoldList[RotateLeft, elems, Range[0, vertexCount - 2]], All, 
     vertexCount]~LowerTriangularize~-1;
  Column[{
    If[adjM, TableForm[adjacencyMatrix,
      TableHeadings -> Automatic], Nothing],
    AdjacencyGraph[adjacencyMatrix,
     VertexLabels -> If[labels, "Name", None],
     ImageSize -> Medium]}]],
 {{vertexCount, 10, "Vertex Count"}, 3, 20, 1,
  Appearance -> "Labeled"},
 {{edgeCount, 15, "Edge Count"}, 
  1, (vertexCount - 2)*(vertexCount + 1)/2, 1,
  Appearance -> "Labeled"},
 Row[{
   Control[{{labels, True, "Vertex Labels"}, {True, False}}],
   Spacer[25],
   Control[{{adjM, False, "Adjacency Matrix"}, {True, False}}]}]]

enter image description here

$\endgroup$

Your Answer

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

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