public void IsDAGTest() { using (StreamReader sr = new StreamReader(@"E:\Study\ALG2017\ALGRKC\dataSelf\tinyDG.txt")) { Digraph dg = new Digraph(sr); TopologicalOrder tlOrder = new TopologicalOrder(dg); Assert.AreEqual(tlOrder.IsDAG(), true); } }
public void TopologicalOrderTest() { using (StreamReader sr = new StreamReader(@"E:\Study\ALG2017\ALGRKC\dataSelf\tinyDG.txt")) { Digraph dg = new Digraph(sr); TopologicalOrder tlOrder = new TopologicalOrder(dg); IEnumerable <int> order = tlOrder.Order(); foreach (int i in order) { Console.Write(i + " , "); } Console.WriteLine(); } }
public AcyclicSP(EdgeWeightedDiagraph ewg, int source) { edgeTo = new DirectedEdge[ewg.V()]; distTo = new double[ewg.V()]; for (int i = 0; i < ewg.V(); i++) { distTo[i] = double.PositiveInfinity; } distTo[source] = 0; TopologicalOrder tlOrder = new TopologicalOrder(ewg); foreach (int v in tlOrder.Order()) { Relax(ewg, v); } }
void ShowDAG() { // Do topological ordering TopologicalOrder.Clear(); foreach (GameObject line in GameObject.FindGameObjectsWithTag("NodeLine")) { Destroy(line); } HashSet <Node> currSet = new HashSet <Node>(); Dictionary <Node, int> degree = new Dictionary <Node, int>(); Queue <Node> queue = new Queue <Node>(); degree[Root] = 0; queue.Enqueue(Root); while (queue.Count > 0) { Node curr = queue.Dequeue(); if (currSet.Contains(curr)) { continue; } currSet.Add(curr); if (curr.LeftChild) { queue.Enqueue(curr.LeftChild); if (!degree.ContainsKey(curr.LeftChild)) { degree[curr.LeftChild] = 0; } degree[curr.LeftChild]++; } if (curr.RightChild) { queue.Enqueue(curr.RightChild); if (!degree.ContainsKey(curr.RightChild)) { degree[curr.RightChild] = 0; } degree[curr.RightChild]++; } } currSet.Clear(); currSet.Add(Root); while (currSet.Count > 0) { TopologicalOrder.Add(currSet); HashSet <Node> nextSet = new HashSet <Node>(); foreach (Node n in currSet) { if (n.LeftChild) { degree[n.LeftChild]--; if (degree[n.LeftChild] == 0) { nextSet.Add(n.LeftChild); } } if (n.RightChild) { degree[n.RightChild]--; if (degree[n.RightChild] == 0) { nextSet.Add(n.RightChild); } } } currSet = nextSet; } // foreach (HashSet<Node> set in TopologicalOrder) // { // StringBuilder sb = new StringBuilder(); // foreach (Node node in set) // { // sb.Append(node.ToString()).Append(" "); // } // Debug.Log(sb.ToString()); // } // Debug.Log($"Topological Depth: {TopologicalOrder.Count}"); // float range = Mathf.Pow(2, TopologicalOrder.Count-1) * spacing; // PlaceNode(Root, -range, range, 0); // DrawLines(Root, 0); Dictionary <Node, int> heights = new Dictionary <Node, int>(); CalculateMaxHeight(Root, heights, 0); PlaceNode2(Root, heights, 0, 0); DrawLines(Root, 0); }