Message Boards Message Boards

0
|
4369 Views
|
5 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Turn a list of pairs of numbers into nodes of a graph?

I have function that creates a list of pairs of numbers such as {{2, 6}, {2 , 4}, {4, 5},{ 5, 6}} and would like to turn that into a graph like this {2 -> 6, 2 -> 4, 4 -> 5, 5 -> 6} so that I can use functions like TreePlot and GraphPlot3D. My problem is that I am not sure how to convert from the list of pairs of numbers to the list of nodes. I thought of having a function generate a string list "{{2, 6}, {2 , 4}, {4, 5},{ 5, 6}" and then replace the commas with the "->" symbol and then somehow "Evaluate" the string. I know it seems convoluted, but that is what I can think of for now unless there is some easy way to go from the list of pairs of numbers to the list of nodes of a graph.

POSTED BY: Henrick Jeanty
5 Replies

Thank you Richard! Your solution called edges2 solves my problem in a way that appeals to me! Thank you very much.

POSTED BY: Henrick Jeanty

POSTED BY: Richard Frost

Thank you for the pointer Hans. This led me to read about Rule, but I don't quite understand how your statement of "Rule @@@ listOfListsOfPairs" changes the "," located between numbers into a "->" I did look at Rule (->,->) I thought that I could do something like this Rule(,->) But my problem is that the symbol I want to replace (namely the ",") is a separator that separates the lhs from the rhs in the Rule syntax. Furthermore. the replacement symbol (here the "->") is also a special character).

So what I am really looking for is an expression that I can just copy in my code or try out interactively that would take a listOfListsOfPairs and returns the same list except that the "," separating the numbers of a pair is replaced by the "->" symbol,

POSTED BY: Henrick Jeanty
Posted 3 years ago

Hi Henrik,

@@@ is equivalent to for Apply[f, expr, {1}]. Apply changes the Head of an expression.

l = {1, 2}
FullForm@l
(* List[1,2] *)

Replace List with Plus, then Plus is evaluated.

Apply[Plus, l]
(* 3 *)

What Hans showed is exactly what you want

would like to turn that into a graph like this {2 -> 6, 2 -> 4, 4 -> 5, 5 -> 6} so that I can use functions like TreePlot and GraphPlot3D

The example listOfListsOfPairs does not represent a tree, so Graph is a better function to use than TreePlot.

Rule @@@ listOfListsOfPairs // Graph[#, VertexLabels -> Automatic] &

Rule @@@ listOfListsOfPairs // GraphPlot3D[#, VertexLabels -> Automatic] &

To use DirectedEdge or UndirectedEdge rather than Rule

DirectedEdge @@@ listOfListsOfPairs
UndirectedEdge @@@ listOfListsOfPairs
POSTED BY: Rohit Namjoshi
Posted 3 years ago
listOfListsOfPairs = {{2, 6}, {2, 4}, {4, 5}, {5, 6}};

Rule @@@ listOfListsOfPairs
(* {2->6, 2->4, 4->5, 5->6} *)
POSTED BY: Hans Milton
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract