4
$\begingroup$

I need a Mathematica script, called graph, with three arguments: vert, edges,groups, which produces a graph with vertice labels given in the list vert, and coloured differently, and also with different shapes, according to specified groups. My guess is there must be a simple way to color + shape vertices differently, but I can't find it in the dcumentation. Hence the script I (actually ChatGPT) wrote, which works for coloring only

    (*SAIR Example*)
    graph[comp_List, edges_List, groups_List : {}] := 
 Module[{colorList, vertexColors, 
   defaultColor = Yellow},(*If groups are provided,
  assign colors.Otherwise,set default color*)
  If[Length[groups] > 0,(*Define colors and map groups to colors*)
   colorList = {Green, Red, Blue, Purple, Orange};
   vertexColors = 
    Flatten[MapThread[
      Thread[#1 -> #2] &, {groups, 
       Take[colorList, 
        Length[groups]]}]],(*Default to coloring all vertices Yellow \
if groups not specified*)
   vertexColors = Thread[comp -> defaultColor]];
  (*Use Graph to generate the plot*)
  Graph[comp, edges, VertexStyle -> vertexColors, 
   VertexLabels -> 
    "Name",(*Directly show names on vertices without extra circles*)
   ImageSize -> 500,(*Adjusted image size for smaller output*)
   GraphLayout -> "LayeredDigraphEmbedding"]]

(*Example usage*)
(*Define vertices and edges*)
comp = {"S", "A", "I", "R", "S" + "I", "S" + "A", "A" + "I", 2 "A", 0};
edges = {0 -> "S", "S" + "A" -> 2 "A", "S" + "I" -> "A" + "I", 
   "A" -> "I", "A" -> "R", "S" -> "R", "I" -> "R", "R" -> "S", 
   "S" -> 0, "A" -> 0, "I" -> 0, "R" -> 0};

(*Default case-all vertices Yellow*)
graph[comp, edges]

(*With groups for custom colors*)
groups = {{"S", "A"}, {"I", "R"}, {"S" + "I", "S" + "A", "A" + "I", 
    2 "A", 0}};
graph[comp, edges, groups]

But, when I try to add shapes, all hell breaks lose

$\endgroup$
1
  • 1
    $\begingroup$ Give Claude.ai a chance! It seems to be better at code than ChatGPT (if you saved your prompt, it's a fair comparison). $\endgroup$ Commented Oct 18 at 7:11

1 Answer 1

5
$\begingroup$

How about this?

Clear["Global`*"];
graph[comp_List, edges_List, groups_List : {}] := 
  Module[{colorList, shapeList, vertexColors, vertexShapes, 
    defaultColor = Yellow}, 
   colorList = {Green, Red, Blue, Purple, Orange};
   shapeList = {"Star", "Triangle", "Square", "Pentagon", "Hexagon"}; 
   vertexColors = 
    Join[Flatten[
      MapIndexed[Thread[#1 -> colorList[[#2[[1]]]]] &, 
       groups]], # -> defaultColor & /@ 
      Complement[comp, Flatten[groups]]]; 
   vertexShapes = 
    Flatten[MapIndexed[Thread[#1 -> shapeList[[#2[[1]]]]] &, groups]];
   Graph[edges, VertexShapeFunction -> vertexShapes, 
    VertexStyle -> vertexColors, VertexSize -> 0.3, 
    VertexLabels -> "Name", GraphLayout -> "LayeredDigraphEmbedding", 
    ImageSize -> 500]
   ];

(* I added vertices "X" and "Y" to check default color. *)
comp = {"S","A","I","R","S"+"I","S"+"A","A"+"I",2"A",0,"X", "Y"};
edges = {0->"S","S"+"A"->2"A","S"+"I"->"A"+"I", 
   "A"->"I","A"->"R","S"->"R","I"->"R","R"->"S", 
   "S"->0,"A"->0,"I"->0,"R"->0,"X"->"X","X"->"Y", 
   "Y"->"Y"};
groups = {{"S","A"}, {"I","R"},{"S"+"I","S"+"A","A"+"I",2"A",0}};
graph[comp, edges, groups]

enter image description here

$\endgroup$
2
  • 1
    $\begingroup$ Thanks! I had also asked ChatGPT, who had failed, and then I asked him to comment on your solution:" 1)Handling edges as the First Argument in Graph simplifies. 2)Manual looping and conditionals are avoided, by directly using MapIndexed, and Thread. 3) By directly using VertexShapeFunction and VertexStyle, the solution makes the most of Mathematica’s graph properties, which simplifies customization and ensures compatibility with various graph layouts! So, you gave Chat a lesson (but I confess the first, rather lengthy program, was due to him, not to me:) $\endgroup$
    – florin
    Commented Oct 17 at 11:16
  • 1
    $\begingroup$ @florin I'm glad I was able to help you. ChatGPT's code is not too bad. Since the number of commands in Wolfram language increases so rapidly, AI will be of great help. But I don't want to lose the fun of programming by myself r(^_^) $\endgroup$
    – A. Kato
    Commented Oct 17 at 12:16

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.