Message Boards Message Boards

0
|
3456 Views
|
5 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Get list of directed edges

Hi All,

I am terribly sorry about asking trivial things, but I am complete beginner when it comes to Mathematica. I also have yet to master a skill of searching through previously posted questions and answers on this forum as I just registered.

I am trying to created a list of directed edges from a list of ordered pairs. So I start with

V = {1,2}
G  = Tuples[{V, V}]
DirectedEdge /@ G

Unfortunately, I have extra curly brackets so my output is

{DirectedEdge[{1, 1}], DirectedEdge[{1, 2}], DirectedEdge[{2, 1}], 
 DirectedEdge[{2, 2}]}

Instead of what I really need

{DirectedEdge[1, 1], DirectedEdge[1, 2], DirectedEdge[2, 1], 
 DirectedEdge[2, 2]}

I am so sorry for bad formatting. I will fix things a bit later as I am in a hurry to fix the problem.

5 Replies
Posted 4 years ago

Hi Predrag,

DirectedEdge expects two arguments not a list with two elements. One way to do that

DirectedEdge @@@ G
POSTED BY: Rohit Namjoshi

Exactly what I was looking for!

Posted 4 years ago

OK.... This caused me to scratch my head a bit. Now that I took my dog for a walk right before the down-pour started, I cleared up my confusion.

DirectedEdge @@@ {1,2} 

does NOT work because {1,2} is a just a list and does not have a level 1. But, since it does have a level 0. Of course,

DirectedEdge @@ {1,2} 

does work because we only have level 0.

But, the original question was about a list of directed edges:

 DirectedEdge @@@ {{1,2} , {3,4}}

works because we are now working at level 1.

THANK YOU both for providing a real-world challenge that cleared this topic up for me.

POSTED BY: Mike Besso
Posted 4 years ago

Hi Mike,

I found this question and the answers / referenced links cleared up a lot of my confusion with levels. Part of the confusion for me was that Level always returns a List, which is not part of the expression on which Level is called.

POSTED BY: Rohit Namjoshi
Posted 4 years ago

Thanks Rohit. That helped.

POSTED BY: Mike Besso
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