Convert graph format for Mathematica graph functions

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.

• 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). – Szabolcs Jul 30 at 19:58
• I absolutely agree with Szabolcs' comment. – Henrik Schumacher Jul 31 at 0:30

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] Update

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

MapIndexed[{#1[] -> #1[], #2[]} &, 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}}
*)
• 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. – Guest23232 Jul 30 at 16:12
• 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). – Guest23232 Jul 31 at 7:32
• @Guest23232 See the update, sorry I had misread your original comment. – Jack LaVigne Jul 31 at 12:32
• 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. – Guest23232 Jul 31 at 16:11
• To get the highest number try Max[pair[[All, 1]]] – Jack LaVigne Aug 1 at 0:38

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.

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

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] mapleEdgesToGraph[data,
GraphStyle -> "IndexLabeled",
EdgeStyle -> Blue,
GraphLayout -> "CircularEmbedding",
ImageSize -> Large] 