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 ); } }