Example #1
0
        /* 4.7 Build Order: You are given a list of projects and a list of dependencies (which is a list of pairs of
         *                  projects, where the second project is dependent on the first project). All of a project's dependencies
         *                  must be built before the project is. Find a build order that will allow the projects to be built. If there
         *                  is no valid build order, return an error.
         *                  EXAMPLE
         *                  Input:
         *                  projects: a, b, c, d, e, f
         *                  dependencies: (a, d), (f, b), (b, d), (f, a), (d, c)
         *                  Output: f, e, a, b, d, c
         *
         * 1- Build a graph with dependencies list for each project
         * 2 - For each project in the graph we will perfom DFS.
         *          - We mark the current node as State.Partial and go deep;
         *          - When we hit the leaf, we mark the leaf as completed (avoid do again in next steps) and add it to the stack created to build as order as go
         *          add all the previus call to that
         *  PSEUDO LOGIC:
         *          if (project.getState() == Project.State.PARTIAL)
         *          {
         *              return false; // Cycle
         *          }
         *
         *          if (project.getState() == Project.State.BLANK)
         *          {
         *              project.setState(Project.State.PARTIAL);
         *              foreach (var child in Project.children)
         *              {
         *                  do DFS(child)
         *              }
         *              project.setState(Project.State.COMPLETE);
         *              stack.Push(project);
         */
        public static CCIChallenges.BinaryTree.Graph BuildGraph(String[] projects, String[][] dependencies)
        {
            var graph = new CCIChallenges.BinaryTree.Graph();

            foreach (var dependency in dependencies)
            {
                string first  = dependency[0];
                string second = dependency[1];
                graph.addEdge(first, second);
            }

            return(graph);
        }
Example #2
0
 public static Stack <Project> findBuildOrder(String[] projects, String[][] dependencies)
 {
     CCIChallenges.BinaryTree.Graph graph = BuildGraph(projects, dependencies);
     return(OrderProjects(graph.getNodes()));
 }