/** * Indicates if there is at least one edge leading to or from vertex of given label * * @return <code>true</code> if this vertex is connected with other vertex,<code>false</code> otherwise */ public boolean isConnected( final String label ) { final Vertex vertex = getVertex( label ); final boolean retValue = vertex.isConnected(); return retValue; }
/** * @param label * @return */ public List<String> getChildLabels( final String label ) { final Vertex vertex = getVertex( label ); return vertex.getChildLabels(); }
/** * @param label * @return */ public List<String> getParentLabels( final String label ) { final Vertex vertex = getVertex( label ); return vertex.getParentLabels(); }
public void removeEdge( final String from, final String to ) { final Vertex v1 = addVertex( from ); final Vertex v2 = addVertex( to ); removeEdge( v1, v2 ); }
/** * Return the list of labels of successor in order decided by topological sort * * @param label The label of the vertex whose predecessors are searched * @return The list of labels. Returned list contains also the label passed as parameter to this method. This label * should always be the last item in the list. */ public List<String> getSuccessorLabels( final String label ) { final Vertex vertex = getVertex( label ); final List<String> retValue; // optimization. if ( vertex.isLeaf() ) { retValue = new ArrayList<String>( 1 ); retValue.add( label ); } else { retValue = TopologicalSorter.sort( vertex ); } return retValue; }
private static void dfsVisit( final Vertex vertex, final Map<Vertex, Integer> vertexStateMap, final List<String> list ) { vertexStateMap.put( vertex, VISITING ); for ( Vertex v : vertex.getChildren() ) { if ( isNotVisited( v, vertexStateMap ) ) { dfsVisit( v, vertexStateMap, list ); } } vertexStateMap.put( vertex, VISITED ); list.add( vertex.getLabel() ); }
public void addEdge( final Vertex from, final Vertex to ) throws CycleDetectedException { from.addEdgeTo( to ); to.addEdgeFrom( from ); final List<String> cycle = CycleDetector.introducesCycle( to ); if ( cycle != null ) { // remove edge which introduced cycle removeEdge( from, to ); final String msg = "Edge between '" + from + "' and '" + to + "' introduces to cycle in the graph"; throw new CycleDetectedException( msg, cycle ); } }
public boolean hasEdge( final String label1, final String label2 ) { final Vertex v1 = getVertex( label1 ); final Vertex v2 = getVertex( label2 ); final boolean retValue = v1.getChildren().contains( v2 ); return retValue; }
public List getDependents( String id ) { return dag.getParentLabels( id ); }
public static List<String> hasCycle( final DAG graph ) { final List<Vertex> vertices = graph.getVertices(); final Map<Vertex, Integer> vertexStateMap = new HashMap<Vertex, Integer>(); List<String> retValue = null; for ( Vertex vertex : vertices ) { if ( isNotVisited( vertex, vertexStateMap ) ) { retValue = introducesCycle( vertex, vertexStateMap ); if ( retValue != null ) { break; } } } return retValue; }
private static List<String> dfs( final DAG graph ) { // we need to use addFirst method so we will use LinkedList explicitly final List<String> retValue = new LinkedList<String>(); final Map<Vertex, Integer> vertexStateMap = new HashMap<Vertex, Integer>(); for ( Vertex vertex : graph.getVertices() ) { if ( isNotVisited( vertex, vertexStateMap ) ) { dfsVisit( vertex, vertexStateMap, retValue ); } } return retValue; }
public List<String> getDependencies( String id ) { return dag.getChildLabels( id ); }
public void removeEdge( final Vertex from, final Vertex to ) { from.removeEdgeTo( to ); to.removeEdgeFrom( from ); }
/** * @deprecated instead use {@link #getVertices()} */ @Deprecated public List<Vertex> getVerticies() { return getVertices(); }
/** * @param graph * @return List of String (vertex labels) */ public static List<String> sort( final DAG graph ) { return dfs( graph ); }
public String getMessage() { return super.getMessage() + " " + cycleToString(); } }
public static List<String> introducesCycle( final Vertex vertex ) { final Map<Vertex, Integer> vertexStateMap = new HashMap<Vertex, Integer>(); return introducesCycle( vertex, vertexStateMap ); }
public List<String> getDependents( String id ) { return dag.getParentLabels( id ); }