# Get the End Points of an Edge

Posted 9 years ago
6497 Views
|
3 Replies
|
1 Total Likes
|
 Hello,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.
3 Replies
Sort By:
Posted 9 years ago
 List @@ # & /@ EdgeList[gFriendShip, "Rose" \[UndirectedEdge] _]returns{{"Anna", "Rose"}, {"Ben", "Rose"}, {"Rose", "Nora"}}Another perhaps more transparent pattern matching approch might be,EdgeList[gFriendShip,   "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 9 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 9 years ago
 Cross-posted: http://mathematica.stackexchange.com/questions/45355