private static void CalculateChildrenDifficulties(TargetNode root) { if (root.Children.Count() == 0) { root.ChildrenDifficulty = root.NodeDifficulty; } else { foreach (var child in root.Children) { CalculateChildrenDifficulties(child); } root.ChildrenDifficulty = root.Children.Select(x => x.ChildrenDifficulty).Aggregate((a, b) => a + b); } }
// 1.Order target tree public static List <TargetNode> TopologicalSort(TargetNode main) { CalculateChildrenDifficulties(main); int currOrder = 1; List <TargetNode> orderedTargets = new List <TargetNode>(); Stack <TargetNode> memory = new Stack <TargetNode>(); TargetNode currItem = null; while (main.Children.Count() != 0) { if (currItem == null) { currItem = main.GetLeaves().OrderBy(x => x.NodeDifficulty).First(); } while (currItem.Children.Count() == 0) { if (currItem.Parents.Count() == 0) { break; } currItem.Order = currOrder; ++currOrder; orderedTargets.Add(currItem); TargetNode buf = currItem; TargetNode next = null; if (memory.Count() == 0) { next = buf.Parents.OrderBy(x => x.NodeDifficulty).First(); } else { next = memory.Pop(); } currItem = next; for (int i = 0; i < buf.Parents.Count(); ++i) { buf.Parents[i].RemoveChild(buf); } } if (currItem != main) { memory.Push(currItem); while (currItem.Children.Count() != 0) { currItem = currItem.Children.OrderBy(x => x.ChildrenDifficulty).First(); } } else { currItem = null; } } main.Order = currOrder; orderedTargets.Add(main); return(orderedTargets); }
public void RemoveParent(TargetNode t) { Parents.Remove(t); }
public void AddParent(TargetNode t) { Parents.Add(t); }
public void RemoveChild(TargetNode t) { Children.Remove(t); }
public void AddChild(TargetNode t) { Children.Add(t); }