/** * Returns a list of vertices that are the direct predecessors of a specified vertex. If the * graph is a multigraph, vertices may appear more than once in the returned list. * * <p> * The method uses {@link Graph#incomingEdgesOf(Object)} to traverse the graph. * * @param g the graph to look for predecessors in * @param vertex the vertex to get the predecessors of * @param <V> the graph vertex type * @param <E> the graph edge type * * @return a list of the vertices that are the direct predecessors of the specified vertex. */ public static List <V> predecessorListOf <V, E>(Graph <V, E> g, V vertex) { List <V> predecessors = new List <V>(); HashSet <E> edges = g.incomingEdgesOf(vertex); foreach (E e in edges) { predecessors.Add(getOppositeVertex(g, e, vertex)); } return(predecessors); }
/** * Check if a vertex has any direct predecessors. * * @param graph the graph to look for predecessors * @param vertex the vertex to look for predecessors * @param <V> the graph vertex type * @param <E> the graph edge type * * @return true if the vertex has any predecessors, false otherwise */ public static bool vertexHasPredecessors <V, E>(Graph <V, E> graph, V vertex) { return(graph.incomingEdgesOf(vertex).Count != 0); }