Skip to main content
version with interactive editing of graphs - added example output
Source Link
JaM
  • 192
  • 7

I suppose thatwrote some code for drawing a graph interactively using interactively meansDynamicModule and changing some parametersEventHandler. For this, youYou can simply useadd vertices with right click and edges with left-click. The adjacency list is printed at the bottom. Admittedly, the graph is stored as a list of points (in the graphics object coordinates), not a ManipulateGraph. Also, the style of the edges is fixed.

Manipulate[Column[DynamicModule[{CompleteGraph[p]newEdge = {}, AdjacencyMatrix[CompleteGraph[p]]edges = {}, vertices = {}, pos = {}},
 Dynamic[
  EventHandler[
   Column[{
     Framed@
      Graphics[{Black, Line[edges], PointSize[0.05], Red, 
        Point /@ vertices}, PlotRange -> {{0, 10}, {0, 10}}, 
       ImageSize -> 300],
     edges
     },
    Spacings -> 10
    ],
   {p
    {"MouseClicked", 1} :> (
      If[Length[newEdge] > 0, 
 50      Print["Please select a second vertex!"], 
 1      pos = Round[MousePosition["Graphics"]];
       If [MemberQ[vertices, pos],
        Print["Vertex already exists!"],
        AppendTo[vertices, pos]
        ]
       ]
      ),
    {"MouseClicked", 2} :> (
      pos = Round[MousePosition["Graphics"]];
      If[MemberQ[vertices, pos],
       AppendTo[newEdge, pos];
       If[Length[newEdge] == 2,
        AppendTo[edges, newEdge];
        newEdge = {}
        ]
       ]
      )
    }
   ]
  ]
 ]

Example output is attached below.

Example of the result

I suppose that interactively means changing some parameters. For this, you can simply use Manipulate,

Manipulate[Column[{CompleteGraph[p], AdjacencyMatrix[CompleteGraph[p]]}], {p, 1, 50, 1}]

I wrote some code for drawing a graph interactively using DynamicModule and EventHandler. You can add vertices with right click and edges with left-click. The adjacency list is printed at the bottom. Admittedly, the graph is stored as a list of points (in the graphics object coordinates), not a Graph. Also, the style of the edges is fixed.

DynamicModule[{newEdge = {}, edges = {}, vertices = {}, pos = {}},
 Dynamic[
  EventHandler[
   Column[{
     Framed@
      Graphics[{Black, Line[edges], PointSize[0.05], Red, 
        Point /@ vertices}, PlotRange -> {{0, 10}, {0, 10}}, 
       ImageSize -> 300],
     edges
     },
    Spacings -> 10
    ],
   {
    {"MouseClicked", 1} :> (
      If[Length[newEdge] > 0, 
       Print["Please select a second vertex!"], 
       pos = Round[MousePosition["Graphics"]];
       If [MemberQ[vertices, pos],
        Print["Vertex already exists!"],
        AppendTo[vertices, pos]
        ]
       ]
      ),
    {"MouseClicked", 2} :> (
      pos = Round[MousePosition["Graphics"]];
      If[MemberQ[vertices, pos],
       AppendTo[newEdge, pos];
       If[Length[newEdge] == 2,
        AppendTo[edges, newEdge];
        newEdge = {}
        ]
       ]
      )
    }
   ]
  ]
 ]

Example output is attached below.

Example of the result

Source Link
JaM
  • 192
  • 7

I suppose that interactively means changing some parameters. For this, you can simply use Manipulate,

Manipulate[Column[{CompleteGraph[p], AdjacencyMatrix[CompleteGraph[p]]}], {p, 1, 50, 1}]