suman
(Suman Kundu)
April 4, 2014, 10:32am
1
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.
dlkeith
(David Keith)
April 4, 2014, 4:14pm
3
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"}
dreiss
(David Reiss)
April 4, 2014, 6:12pm
4
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.