List <string> BuildOrder(char[][] dependencies, char[] projects) { List <string> buildOrder = new List <string>(); NamedDirectedNodeManager nodeManager = CreateGraph(dependencies, projects); List <NamedDirectedNode> processedNodes = new List <NamedDirectedNode>(); bool keepProcessing = true; while (keepProcessing) { keepProcessing = false; foreach (NamedDirectedNode currNode in nodeManager.nodes) { if (!nodeManager.DoesNodeHaveIncoming(currNode)) { if (!processedNodes.Contains(currNode)) { buildOrder.Add(currNode.name); processedNodes.Add(currNode); keepProcessing = true; } } } foreach (NamedDirectedNode currNode in processedNodes) { currNode.children.Clear(); } if (!keepProcessing) { if (processedNodes.Count == projects.Length) { return(buildOrder); } else { return(null); } } } return(null); }
NamedDirectedNodeManager CreateGraph(char[][] dependencies, char[] projects) { List <NamedDirectedNode> nodeList = new List <NamedDirectedNode>(); foreach (char projectName in projects) { NamedDirectedNode newNode = new NamedDirectedNode(projectName.ToString()); nodeList.Add(newNode); } NamedDirectedNodeManager nodeManager = new NamedDirectedNodeManager(); nodeManager.nodes = nodeList; foreach (char[] dependency in dependencies) { NamedDirectedNode parent = nodeManager.GetNodeByName(dependency[0].ToString()); NamedDirectedNode child = nodeManager.GetNodeByName(dependency[1].ToString()); parent.children.Add(child); } return(nodeManager); }