Message Boards Message Boards

Get the End Points of an Edge


I want to get the end points of an Edge of a Graph. For example see the following source,  
In[1]:=gFriendShip = ExampleData[{"NetworkGraph", "Friendship"}];
In[2]:= EdgeList[gFriendShip, "Rose" \[UndirectedEdge] _]
Out[2]:={"Anna" \[UndirectedEdge] "Rose", "Ben" \[UndirectedEdge] "Rose",
"Rose" \[UndirectedEdge] "Nora"}
Now I want to write something like EndPoints["Rose" \ "Nora"] (actually, EndPoints[%2] in above example) and want te get {"Rose", "Nora"} as output.

It might be a basic thing but I am new to mathematica.

Please help. Thank You.
POSTED BY: Suman Kundu
3 Replies
List @@ # & /@ EdgeList[gFriendShip, "Rose" \[UndirectedEdge] _]

{{"Anna", "Rose"}, {"Ben", "Rose"}, {"Rose", "Nora"}}

Another perhaps more transparent pattern matching approch might be,
  "Rose" \[UndirectedEdge] _] /. {x_ \[UndirectedEdge] y_ :> {x, y}}

This is a 3 element list (rather than the 1 element list you suggested) because the EdgeList returns a 3 element list.  If you only wanted cases where only {"Rose", "Nora"} is returned then you might use Select or Cases to get what you wish from the list above.
POSTED BY: David Reiss
Posted 11 years ago
The function Part can get into the parts of anything, not just lists:
 In[15]:= graph =
   Graph[{"Anna" \[UndirectedEdge] "Rose",
     "Ben" \[UndirectedEdge] "Rose", "Rose" \[UndirectedEdge] "Nora"}];
 In[16]:= edges = EdgeList[graph]
 Out[16]= {"Anna" \[UndirectedEdge] "Rose",
  "Ben" \[UndirectedEdge] "Rose", "Rose" \[UndirectedEdge] "Nora"}
In[17]:= edges[[1]]

Out[17]= "Anna" \[UndirectedEdge] "Rose"

In[18]:= edges[[1, 1]]

Out[18]= "Anna"

In[19]:= edges[[1, 2]]

Out[19]= "Rose"

In[20]:= edges[[All, {1, 2}]]

Out[20]= {"Anna" \[UndirectedEdge] "Rose",
"Ben" \[UndirectedEdge] "Rose", "Rose" \[UndirectedEdge] "Nora"}
POSTED BY: David Keith
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract