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 861123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:55:41+00:00 2026-05-15T08:55:41+00:00

Where can I find a Java implementation of the Critical Path Method Algorithm? I

  • 0

Where can I find a Java implementation of the Critical Path Method Algorithm? I am sure there’s some implementation in the cloud. I have already searched on google obviously, but haven’t found any implementation that works well. That’s why I am asking.

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-15T08:55:41+00:00Added an answer on May 15, 2026 at 8:55 am

    Here is an implementation of the algorithm based on the explanation provided on this page
    There is a wrapper class to hold the task, cost, and critical path cost. It starts by calculating the critical cost as the maximum critical cost of all dependencies plus its own cost. Then once the critical costs are available it uses a comparator to sort the tasks based on the critical cost with dependency as a tie breaker (choosing randomly if there is no dependency). Note that an exception will be thrown if there is a cycle and it will fail if any of the costs are negative.

    Here is the implementation:

    public class CriticalPath {
    
      public static void main(String[] args) {
        //The example dependency graph from
        //http://www.ctl.ua.edu/math103/scheduling/scheduling_algorithms.htm
        HashSet<Task> allTasks = new HashSet<Task>();
        Task end = new Task("End", 0);
        Task F = new Task("F", 2, end);
        Task A = new Task("A", 3, end);
        Task X = new Task("X", 4, F, A);
        Task Q = new Task("Q", 2, A, X);
        Task start = new Task("Start", 0, Q);
        allTasks.add(end);
        allTasks.add(F);
        allTasks.add(A);
        allTasks.add(X);
        allTasks.add(Q);
        allTasks.add(start);
        System.out.println("Critical Path: "+Arrays.toString(criticalPath(allTasks)));
      }
    
      //A wrapper class to hold the tasks during the calculation
      public static class Task{
        //the actual cost of the task
        public int cost;
        //the cost of the task along the critical path
        public int criticalCost;
        //a name for the task for printing
        public String name;
        //the tasks on which this task is dependant
        public HashSet<Task> dependencies = new HashSet<Task>();
        public Task(String name, int cost, Task... dependencies) {
          this.name = name;
          this.cost = cost;
          for(Task t : dependencies){
            this.dependencies.add(t);
          }
        }
        @Override
        public String toString() {
          return name+": "+criticalCost;
        }
        public boolean isDependent(Task t){
          //is t a direct dependency?
          if(dependencies.contains(t)){
            return true;
          }
          //is t an indirect dependency
          for(Task dep : dependencies){
            if(dep.isDependent(t)){
              return true;
            }
          }
          return false;
        }
      }
    
      public static Task[] criticalPath(Set<Task> tasks){
        //tasks whose critical cost has been calculated
        HashSet<Task> completed = new HashSet<Task>();
        //tasks whose ciritcal cost needs to be calculated
        HashSet<Task> remaining = new HashSet<Task>(tasks);
    
        //Backflow algorithm
        //while there are tasks whose critical cost isn't calculated.
        while(!remaining.isEmpty()){
          boolean progress = false;
    
          //find a new task to calculate
          for(Iterator<Task> it = remaining.iterator();it.hasNext();){
            Task task = it.next();
            if(completed.containsAll(task.dependencies)){
              //all dependencies calculated, critical cost is max dependency
              //critical cost, plus our cost
              int critical = 0;
              for(Task t : task.dependencies){
                if(t.criticalCost > critical){
                  critical = t.criticalCost;
                }
              }
              task.criticalCost = critical+task.cost;
              //set task as calculated an remove
              completed.add(task);
              it.remove();
              //note we are making progress
              progress = true;
            }
          }
          //If we haven't made any progress then a cycle must exist in
          //the graph and we wont be able to calculate the critical path
          if(!progress) throw new RuntimeException("Cyclic dependency, algorithm stopped!");
        }
    
        //get the tasks
        Task[] ret = completed.toArray(new Task[0]);
        //create a priority list
        Arrays.sort(ret, new Comparator<Task>() {
    
          @Override
          public int compare(Task o1, Task o2) {
            //sort by cost
            int i= o2.criticalCost-o1.criticalCost;
            if(i != 0)return i;
    
            //using dependency as a tie breaker
            //note if a is dependent on b then
            //critical cost a must be >= critical cost of b
            if(o1.isDependent(o2))return -1;
            if(o2.isDependent(o1))return 1;
            return 0;
          }
        });
    
        return ret;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 433k
  • Answers 433k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer TextWrangler is a text editor, not a word processor. You… May 15, 2026 at 2:49 pm
  • Editorial Team
    Editorial Team added an answer As explained by the programmers that did the listView in… May 15, 2026 at 2:48 pm
  • Editorial Team
    Editorial Team added an answer try to use SELECT MIN(Table_1.acctnum) as acctnum , MIN(Table_1.sub) as… May 15, 2026 at 2:48 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.