0
|
6601 Views
|
3 Replies
|
1 Total Likes
View groups...
Share
GROUPS:

# Get the End Points of an Edge

Posted 10 years ago
 Hello,I want to get the end points of an Edge of a Graph. For example see the following source,  In:=gFriendShip = ExampleData[{"NetworkGraph", "Friendship"}];In:= EdgeList[gFriendShip, "Rose" \[UndirectedEdge] _]Out:={"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 10 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 10 years ago
 The function Part can get into the parts of anything, not just lists: In:= graph =    Graph[{"Anna" \[UndirectedEdge] "Rose",      "Ben" \[UndirectedEdge] "Rose", "Rose" \[UndirectedEdge] "Nora"}];  In:= edges = EdgeList[graph]  Out= {"Anna" \[UndirectedEdge] "Rose",   "Ben" \[UndirectedEdge] "Rose", "Rose" \[UndirectedEdge] "Nora"} In:= edges[]Out= "Anna" \[UndirectedEdge] "Rose"In:= edges[[1, 1]]Out= "Anna"In:= edges[[1, 2]]Out= "Rose"In:= edges[[All, {1, 2}]]Out= {"Anna" \[UndirectedEdge] "Rose", "Ben" \[UndirectedEdge] "Rose", "Rose" \[UndirectedEdge] "Nora"}
Posted 10 years ago
 Cross-posted: http://mathematica.stackexchange.com/questions/45355