5
$\begingroup$

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?

$\endgroup$
6
$\begingroup$

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}]

enter image description here

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.

$\endgroup$
  • $\begingroup$ It seems to work. Thanks very much Jason. $\endgroup$ – Francesco Ciardiello May 13 '18 at 2:33
4
$\begingroup$

If G is the graph,

SparseArray[UnitStep[IncidenceMatrix[G]]]["AdjacencyLists"][[A]]

lists the indices of the edges for each vertex in A.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.