5
$\begingroup$

I have an output of many large graph files, specified by pair of vertices, in a format like

[3,0],[3,0],[3,2],[5,1],[5,1],[5,4],[6,0],[6,1],[7,2],[7,7],[8,6],[9,4],[10,2],[11,4]

which is like the standard-graph form of Maple.

I would like to use the graph-functions of Mathematica. As far as I understood, Mathematica by default uses a syntax like

Graph[{1 -> 2, 2 -> 3, 3 -> 1}]

Do you know of a quick way to use/convert/import my graph-format in Mathematica, or do I first need to convert my graph-files with some script to the correct Mathematica-input?

Thanks a lot for any help.

$\endgroup$
  • 4
    $\begingroup$ Don't try to convert Maple code to Mathematica code. It makes no sense when you can simply export to a standard graph format from Maple (ExportGraph) and import it in Mathematica (Import). $\endgroup$ – Szabolcs Jul 30 at 19:58
  • 1
    $\begingroup$ I absolutely agree with Szabolcs' comment. $\endgroup$ – Henrik Schumacher Jul 31 at 0:30
7
$\begingroup$

I am assuming that you can import the data as a string

data = "[3,0],[3,0],[3,2],[5,1],[5,1],[5,4],[6,0],[6,1],[7,2],[7,7],
        [8,6],[9,4],[10,2],[11,4]"

Given that try these steps

Extract the numbers

digits = ToExpression[StringCases[data, DigitCharacter..]]
(* {3, 0, 3, 0, 3, 2, 5, 1, 5, 1, 5, 4, 6, 0, 6, 1, 7, 2, 7, 7,
    8, 6, 9, 4, 10, 2, 11, 4} *)

Use Partition to create pairs

pairs = Partition[digits, 2]
(* {{3, 0}, {3, 0}, {3, 2}, {5, 1}, {5, 1}, {5, 4}, {6, 0},
    {6, 1}, {7, 2}, {7, 7}, {8, 6}, {9, 4}, {10, 2}, {11, 4}} *)

Use a rule to convert {3,0} to 3->0 and generate a list

list = pairs /. {x_, y_} -> (x -> y)
(* {3 -> 0, 3 -> 0, 3 -> 2, 5 -> 1, 5 -> 1, 5 -> 4, 6 -> 0, 
    6 -> 1, 7 -> 2, 7 -> 7, 8 -> 6, 9 -> 4, 10 -> 2, 11 -> 4} *)

Then graph it

Graph[list]

Mathematica graphics

Update

In order to create {{3 -> 0, 1},{3 -> 0, 2},{3-> 2, 3}..} use MapIndexed with pairs as the input

MapIndexed[{#1[[1]] -> #1[[2]], #2[[1]]} &, pairs]
(* {{3 -> 0, 1}, {3 -> 0, 2}, {3 -> 2, 3}, {5 -> 1, 4}, 
    {5 -> 1, 5}, {5 -> 4, 6}, {6 -> 0, 7}, {6 -> 1, 8}, {7 -> 2, 9}, 
    {7 -> 7, 10}, {8 -> 6, 11}, {9 -> 4, 12}, {10 -> 2, 13},
    {11 -> 4, 14}}
*)
$\endgroup$
  • $\begingroup$ Thanks a lot for your answer. It is exactly what I need and also taught me some new Mathematica things. It is not directly related to the initial question, but do you also know of a way how I could get the list in the form {{3 -> 0, 1},{3 -> 0, 2},{3-> 2, 3}..} and so on? For some manipulations I need to give the different elements of the graph these "indices", which I add with a "," after the arrow-part. $\endgroup$ – Guest23232 Jul 30 at 16:12
  • $\begingroup$ Thanks for your answer, but I need to add these indices ( the "," and then the number of the pair that goes from 1 to total number of pairs) in Mathematica, it is not in my graph data yet. My first thought was something like {x_, y_} ->{ {x -> y},1} or so, but even if it worked, I would have a 1 everywhere, and not an index that goes from 1 to the total number of pairs, which I need to number my "edges" ( 1 edge being one pair of vertices in my list). $\endgroup$ – Guest23232 Jul 31 at 7:32
  • 1
    $\begingroup$ @Guest23232 See the update, sorry I had misread your original comment. $\endgroup$ – Jack LaVigne Jul 31 at 12:32
  • $\begingroup$ Thank you a lot again, thats exactly what I needed. It is not directly related to the initial question again, but same within the same problem, so if may ask one last thing: Is there an easy way to find the largest number in the list expression with the "->" arrows ( before I add this additional index). I would like to avoid creating Graph[..] objects for that or many new replacements. So for the example above I would like to get 11. I cant just count the number of elements because I delete edges from time to time, so the highest number can higher than neccessary. $\endgroup$ – Guest23232 Jul 31 at 16:11
  • $\begingroup$ To get the highest number try Max[pair[[All, 1]]] $\endgroup$ – Jack LaVigne Aug 1 at 0:38
5
$\begingroup$

Given data (as defined by @Jack LaVigne),

data = "[3,0],[3,0],[3,2],[5,1],[5,1],[5,4],[6,0],[6,1],[7,2],[7,7],
        [8,6],[9,4],[10,2],[11,4]"

I might just have written

Graph[ToExpression["{" ~~ StringReplace[data, "[" -> "DirectedEdge["] ~~ "}"]]

to get the graph.

$\endgroup$
  • $\begingroup$ On reading @kglr's answer I realised mine was deficient, in transforming [0,1] to Rule[0,1], so I changed my answer. $\endgroup$ – High Performance Mark Jul 30 at 16:00
5
$\begingroup$

Stitching several functions together to get a function that takes a string containing edges in Maple format and all the options of Graph:

ClearAll[mapleEdgesToGraph]
mapleEdgesToGraph = Graph[ToExpression @* ToString @* List @* 
  StringReplace["[" -> "DirectedEdge["] @ #, ##2]&;

Example: Using data from Jack's answer:

mapleEdgesToGraph[data]

enter image description here

mapleEdgesToGraph[data, 
  GraphStyle -> "IndexLabeled", 
  EdgeStyle -> Blue, 
  GraphLayout -> "CircularEmbedding", 
  ImageSize -> Large]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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