Let g be a directed graph. Let be A a set of vertices. How can I list all the edges which start from a vertex in A?
2 Answers
$\begingroup$
$\endgroup$
1
The function EdgeList
can take a pattern as a second argument,
g = Graph[{1, 2, 3, 4, 5}, {1 \[DirectedEdge] 3, 1 \[DirectedEdge] 4,
1 \[DirectedEdge] 5, 2 \[DirectedEdge] 3, 2 \[DirectedEdge] 4,
2 \[DirectedEdge] 5, 3 \[DirectedEdge] 4, 3 \[DirectedEdge] 5}]
EdgeList[g, DirectedEdge[ 3, _]]
(* {3 \[DirectedEdge] 4, 3 \[DirectedEdge] 5} *)
Note that this doesn't include the edges 1 \[DirectedEdge] 3
or 2 \[DirectedEdge] 3
, because they don't match the pattern.
And if A
is a list of vertices, you just need to use DirectedEdge[ Alternatives @@ A, _]
as your pattern.
-
$\begingroup$ It seems to work. Thanks very much Jason. $\endgroup$ Commented May 13, 2018 at 2:33
$\begingroup$
$\endgroup$
If G
is the graph,
SparseArray[UnitStep[IncidenceMatrix[G]]]["AdjacencyLists"][[A]]
lists the indices of the edges for each vertex in A
.