Social Network Analysis - Get the End Points of an Edge
The FullForm
of "Rose" \[UndirectedEdge] "Nora"
is:
UndirectedEdge["Rose", "Nora"]
and the FullForm of the list {"Rose", "Nora"}
is:
List["Rose", "Nora"]
What you want therefore is to replace the Head
of the UndirectedEdge
expression with List
. You can do this with Apply
(shorthand: @@
)
List @@ UndirectedEdge["Rose", "Nora"]
(* {"Rose", "Nora"} *)
So you could define your function as:
EndPoints = List @@ # &
Then you get:
EndPoints["Rose" <-> "Nora"]
(* {"Rose", "Nora"} *)
If you want to get all the endpoint pairs from a list of edges, you could Map
(shorthand: /@
) the EndPoints
function over the list:
EndPoints /@ EdgeList[gFriendShip, "Rose" <-> _]
(* {{"Anna", "Rose"}, {"Ben", "Rose"}, {"Rose", "Nora"}} *)
or alternatively you can use Apply
at level 1 (shorthand: @@@
)
List @@@ EdgeList[gFriendShip, "Rose" <-> _]
(* {{"Anna", "Rose"}, {"Ben", "Rose"}, {"Rose", "Nora"}} *)
With version 10.3 modifications VertexList
can be used with a list of edges as inputs:
- VertexList[g, patt] gives a list of vertices that match the pattern patt.
- VertexList[{v -> w, …}, …] uses rules v -> w to specify the graph g.
Example:
edges =EdgeList[gFriendShip, "Rose" <-> _]
{"Anna" <-> "Rose", "Ben" <-> "Rose", "Rose" <-> "Nora"}
VertexList[{#}]&/@edges
{{"Anna","Rose"},{"Ben","Rose"},{"Rose","Nora"}}
Note: Based on Szabolcs's comment, in version 11.2, we need to use UndirectedEdge["Rose", _]
or "Rose" \[UndirectedEdge] _
in place of "Rose" <-> _
above.