I am having trouble figuring out how I can print out the path of which circular dependency (cycle) exists. If the cycle exists that is. I am doing a topological sort on a graph (representing a project) that contains different vertices (tasks). Is there a easy way to do this or is another algorithm like depth-first search better for this?
Here is what i have so far:
public boolean canProjectBeCompleted() {
if(this.getProjectTasks() == null) {
return false;
}
LinkedList<Task> queue = new LinkedList<Task>();
Task task;
int counter = 0;
Iterator<Task> taskIterator = this.getProjectTasks().values().iterator();
while(taskIterator.hasNext()) {
Task t = taskIterator.next();
if(t.getInEdgesPredecessors().size() == 0) {
queue.add(t);
}
}
while(!queue.isEmpty()) {
task = queue.removeFirst();
counter++;
for(int i = 0; i < task.getOutEdgesSuccesors().size(); i++) {
Task neighbour = task.getOutEdgesSuccesors().get(i);
neighbour.setCopyNumberOfIncomingEdges(neighbour.getCopyNumberOfIncomingEdges()-1);
if(neighbour.getCopyNumberOfIncomingEdges() == 0) {
queue.add(neighbour);
}
}
}
if(counter < amountOfTasks) {
return false; //Cycle found, The topological sort cannot complete
}
return true; //No cycle found
}
Thanks in advance! :)