Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6721657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:21:20+00:00 2026-05-26T09:21:20+00:00

I am having trouble figuring out how I can print out the path of

  • 0

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! 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:21:20+00:00Added an answer on May 26, 2026 at 9:21 am

    How about this?

    You know that vertices that are part of cycles must have both out-going edges and in-going edges. So begin like you would with topological sorting and “remove” (or simply mark) vertices with 0 in-going edges and remove their out-going edges. Go to it’s neighbors and remove each one which now has 0 in-going edges, and continue. If you still have vertices left in the graph you know that there is a cycle somewhere in the remaining vertices, but not where, or how many cycles. So this is where you do a sort-of-reversed topological sort where you begin removing vertices with 0 out-going edges. This is because you know that a vertex with no out-going edges cannot be part of a cycle. Continue as you would with topological sort, i.e. remove the vertex’s edges, and repeat with the neighbors. Those vertices that are left in your graph are part of one or more cycles. To find out the amount of cycles and the order the vertices appear in each cycle, you can perform a depth-first search amongst the remaining graph.

    If I’m not mistaken I believe the complexity should be O(|V| + |E|)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been having a lot of trouble figuring out how I can access
I'm having trouble figuring out why I can't get keywords to parse properly through
Perhaps I'm just having trouble figuring out the callbackyness, but I can't figure out
I'm having some trouble figuring out why I can only get the children of
I'm having trouble figuring out if/how I can an internal webserver setup for testing
I am having trouble figuring out what the code segment below is doing. It
I'm having trouble figuring out how I can take a string of an object
im having trouble figuring out how to bind mouseout() to my entire nav bar
I'm having trouble figuring out how to get the testing framework set up and
I'm having trouble figuring out how to access a cookie from a compiled object.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.