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$

2 Answers 2

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$
1
  • $\begingroup$ It seems to work. Thanks very much Jason. $\endgroup$ May 13, 2018 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 and acknowledge you have read our privacy policy.

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