Esempio n. 1
0
        public void DagWithEdgeLabels()
        {
            DAG dag = new DAG(0, 4);

            DAG.Node root = dag.GetRoot();

            DAG.Node child = dag.MakeNodeInLayer(1, 1);
            dag.AddRelation(root, child);
            child.AddEdgeColor(0, 2);
            root.AddEdgeColor(1, 2);

            child = dag.MakeNodeInLayer(2, 1);
            dag.AddRelation(root, child);
            child.AddEdgeColor(0, 1);
            root.AddEdgeColor(2, 1);

            child = dag.MakeNodeInLayer(3, 1);
            dag.AddRelation(root, child);
            child.AddEdgeColor(0, 1);
            root.AddEdgeColor(3, 1);

            dag.InitializeWithStringLabels(new string[] { "C", "C", "C", "H" });
            dag.UpdateVertexInvariants();
            Console.Out.WriteLine(dag.CopyInvariants());
        }
Esempio n. 2
0
        //[TestMethod()]
        public void TestColoringForUnlabelledThreeCycle()
        {
            DAG dag = new DAG(0, 3);

            DAG.Node root = dag.GetRoot();

            DAG.Node childA = dag.MakeNodeInLayer(1, 1);
            dag.AddRelation(childA, root);

            DAG.Node childB = dag.MakeNodeInLayer(2, 1);
            dag.AddRelation(childB, root);

            DAG.Node childC = dag.MakeNodeInLayer(2, 2);
            dag.AddRelation(childC, childA);

            DAG.Node childD = dag.MakeNodeInLayer(1, 2);
            dag.AddRelation(childD, childB);

            Console.Out.WriteLine(dag);
            dag.InitializeWithStringLabels(new string[] { "C", "C" });

            dag.UpdateVertexInvariants();
            Console.Out.WriteLine(dag.CopyInvariants());

            dag.SetColor(1, 1);
            dag.UpdateVertexInvariants();
            Console.Out.WriteLine(dag.CopyInvariants());
        }
Esempio n. 3
0
        //[TestMethod()]
        public void TestSimpleUnlabelledDAG()
        {
            // Sets up a simple test case with a graph that looks like this:
            //             0 - Node (vertexIndex - label)
            //            / \
            //    1 - Node  2 - Node

            DAG simpleDAG = new DAG(0, 3);


            // First do all the initializations related to the nodes of the graph.
            // Create the nodes.
            DAG.Node parentNode = simpleDAG.GetRoot();
            DAG.Node childNode;

            // Add the first child.
            childNode = simpleDAG.MakeNodeInLayer(1, 1);
            simpleDAG.AddRelation(childNode, parentNode);

            // Add the second child.
            childNode = simpleDAG.MakeNodeInLayer(2, 1);
            simpleDAG.AddRelation(childNode, parentNode);

            // Initialize the all invariants.
            simpleDAG.InitializeWithStringLabels(new string[] { "Node", "Node", "Node" });

            // Canonize DAG by a simple Hopcroft-Tarjan sweep.
            int[] nodeInvariants   = { 0, 0, 0 };
            int[] vertexInvariants = { 1, 2, 2 };
            TestInvariants(
                nodeInvariants, vertexInvariants, simpleDAG.CopyInvariants());

            simpleDAG.UpdateNodeInvariants(DAG.Direction.Down);
            int[] nodeInvariantsAfterDown   = { 1, 0, 0 };
            int[] vertexInvariantsAfterDown = { 1, 2, 2 };
            TestInvariants(nodeInvariantsAfterDown,
                           vertexInvariantsAfterDown,
                           simpleDAG.CopyInvariants());

            simpleDAG.ComputeVertexInvariants();
            int[] nodeInvariantsAfterComputeVertexInv   = { 1, 0, 0 };
            int[] vertexInvariantsAfterComputeVertexInv = { 2, 1, 1 };
            TestInvariants(nodeInvariantsAfterComputeVertexInv,
                           vertexInvariantsAfterComputeVertexInv,
                           simpleDAG.CopyInvariants());

            simpleDAG.UpdateNodeInvariants(DAG.Direction.Up);
            int[] nodeInvariantsAfterUp   = { 1, 1, 1 };
            int[] vertexInvariantsAfterUp = { 2, 1, 1 };
            TestInvariants(nodeInvariantsAfterUp,
                           vertexInvariantsAfterUp,
                           simpleDAG.CopyInvariants());

            string simpleDAGString = simpleDAG.ToString();
            string expected        = "[0 Node ([], [1,2])]\n[1 Node ([0], []), " +
                                     "2 Node ([0], [])]\n";

            Assert.AreEqual(expected, simpleDAGString);
        }
Esempio n. 4
0
        private void AddNode(int layer, DAG.Node parentNode, int vertexIndex,
                             List <DAG.Arc> layerArcs, List <DAG.Arc> usedArcs,
                             List <DAG.Node> nextLayer)
        {
            // look up the mapping or create a new mapping for the vertex index
            int mappedVertexIndex;

            if (vertexMapping.ContainsKey(vertexIndex))
            {
                mappedVertexIndex = vertexMapping[vertexIndex];
            }
            else
            {
                vertexMapping[vertexIndex] = vertexCount;
                mappedVertexIndex          = vertexCount;
                vertexCount++;
            }

            // find an existing node if there is one
            var arc = new DAG.Arc(parentNode.vertexIndex, mappedVertexIndex);

            if (usedArcs.Contains(arc))
            {
                return;
            }
            DAG.Node existingNode = null;
            foreach (var otherNode in nextLayer)
            {
                if (otherNode.vertexIndex == mappedVertexIndex)
                {
                    existingNode = otherNode;
                    break;
                }
            }

            // if there isn't, make a new node and add it to the layer
            if (existingNode == null)
            {
                existingNode = dag.MakeNode(mappedVertexIndex, layer);
                nextLayer.Add(existingNode);
            }

            // add the edge label to the node's edge label list
            int originalParentIndex =
                GetOriginalVertexIndex(parentNode.vertexIndex);
            string edgeLabel = GetEdgeLabel(originalParentIndex, vertexIndex);
            int    edgeColor = ConvertEdgeLabelToColor(edgeLabel);

            existingNode.AddEdgeColor(parentNode.vertexIndex, edgeColor);
            parentNode.AddEdgeColor(mappedVertexIndex, edgeColor);

            dag.AddRelation(existingNode, parentNode);
            layerArcs.Add(arc);
        }
Esempio n. 5
0
        //[TestMethod()]
        public void TestSimpleLabelledDAG()
        { // {
          // Sets up a simple test case with a graph that looks like this:
          //             0 - Node0 (vertexIndex - label)
          //            / \
          //    1 - Node2  2 - Node1

            DAG simpleDAG = new DAG(0, 3);


            // First do all the initializations related to the nodes of the graph.
            // Create the nodes.
            DAG.Node parentNode = simpleDAG.GetRoot();
            // Add the first child.
            DAG.Node childNode = simpleDAG.MakeNodeInLayer(1, 1);
            simpleDAG.AddRelation(childNode, parentNode);
            // Add the second child.
            childNode = simpleDAG.MakeNodeInLayer(2, 1);
            simpleDAG.AddRelation(childNode, parentNode);

            // Initialize the all invariants.
            simpleDAG.InitializeWithStringLabels(new string[] { "Node0", "Node2", "Node1" });

            //Console.Out.WriteLine(simpleDAG.ToString());

            // Canonize DAG by a simple Hopcroft-Tarjan sweep.
            int[] nodeInvariants   = { 0, 0, 0 };
            int[] vertexInvariants = { 1, 3, 2 };
            TestInvariants(
                nodeInvariants, vertexInvariants, simpleDAG.CopyInvariants());

            simpleDAG.UpdateNodeInvariants(DAG.Direction.Down);
            int[] nodeInvariantsAfterDown   = { 1, 0, 0 };
            int[] vertexInvariantsAfterDown = { 1, 3, 2 };
            TestInvariants(nodeInvariantsAfterDown,
                           vertexInvariantsAfterDown,
                           simpleDAG.CopyInvariants());

            simpleDAG.ComputeVertexInvariants();
            int[] nodeInvariantsAfterComputeVertexInv   = { 1, 0, 0 };
            int[] vertexInvariantsAfterComputeVertexInv = { 2, 1, 1 };
            TestInvariants(nodeInvariantsAfterComputeVertexInv,
                           vertexInvariantsAfterComputeVertexInv,
                           simpleDAG.CopyInvariants());

            simpleDAG.UpdateNodeInvariants(DAG.Direction.Up);
            int[] nodeInvariantsAfterUp   = { 1, 1, 1 };
            int[] vertexInvariantsAfterUp = { 2, 1, 1 };
            TestInvariants(nodeInvariantsAfterUp,
                           vertexInvariantsAfterUp,
                           simpleDAG.CopyInvariants());
        }
Esempio n. 6
0
        /// <summary>
        /// Recursively print the signature into the buffer.
        /// </summary>
        /// <param name="buffer">the string buffer to print into</param>
        /// <param name="node">the current node of the signature</param>
        /// <param name="parent">the parent node, or null</param>
        /// <param name="arcs">the list of already visited arcs</param>
        private void Print(StringBuilder buffer, DAG.Node node, DAG.Node parent, List <DAG.Arc> arcs)
        {
            int vertexIndex = GetOriginalVertexIndex(node.vertexIndex);

            // print out any symbol for the edge in the input graph
            if (parent != null)
            {
                int parentVertexIndex = GetOriginalVertexIndex(parent.vertexIndex);
                buffer.Append(GetEdgeLabel(vertexIndex, parentVertexIndex));
            }

            // print out the text that represents the node itself
            buffer.Append(AbstractVertexSignature.StartNodeSymbolChar);
            buffer.Append(GetVertexSymbol(vertexIndex));
            int color = dag.ColorFor(node.vertexIndex);

            if (color != -1)
            {
                buffer.Append(',').Append(color);
            }
            buffer.Append(AbstractVertexSignature.EndNodeSymbolChar);

            // Need to sort the children here, so that they are printed in an order
            // according to their invariants.
            node.children.Sort(dag.nodeComparator);

            // now print the sorted children, surrounded by branch symbols
            bool addedBranchSymbol = false;

            foreach (var child in node.children)
            {
                DAG.Arc arc = new DAG.Arc(node.vertexIndex, child.vertexIndex);
                if (arcs.Contains(arc))
                {
                    continue;
                }
                else
                {
                    if (!addedBranchSymbol)
                    {
                        buffer.Append(AbstractVertexSignature.StartBranchSymbolChar);
                        addedBranchSymbol = true;
                    }
                    arcs.Add(arc);
                    Print(buffer, child, node, arcs);
                }
            }
            if (addedBranchSymbol)
            {
                buffer.Append(AbstractVertexSignature.EndBranchSymbolChar);
            }
        }
Esempio n. 7
0
        //[TestMethod()]
        public void TestColoring()
        {
            // C12CC1C1
            DAG ring = new DAG(0, 4);

            DAG.Node root = ring.GetRoot();

            DAG.Node child1 = ring.MakeNodeInLayer(1, 1);
            ring.AddRelation(child1, root);

            DAG.Node child2 = ring.MakeNodeInLayer(2, 1);
            ring.AddRelation(child2, root);

            DAG.Node child3 = ring.MakeNodeInLayer(3, 1);
            ring.AddRelation(child3, root);

            DAG.Node child4 = ring.MakeNodeInLayer(3, 2);
            ring.AddRelation(child4, child1);
            ring.AddRelation(child4, child2);

            DAG.Node child5 = ring.MakeNodeInLayer(1, 2);
            ring.AddRelation(child5, child3);

            DAG.Node child6 = ring.MakeNodeInLayer(2, 2);
            ring.AddRelation(child6, child3);

            Console.Out.WriteLine(ring);
            string[] labels = new string[] { "C", "C", "C" };
            ring.InitializeWithStringLabels(labels);

            ring.UpdateNodeInvariants(DAG.Direction.Up);
            Console.Out.WriteLine(ring.CopyInvariants());

            ring.ComputeVertexInvariants();
            Console.Out.WriteLine(ring.CopyInvariants());

            ring.UpdateNodeInvariants(DAG.Direction.Down);
            Console.Out.WriteLine(ring.CopyInvariants());

            ring.ComputeVertexInvariants();
            Console.Out.WriteLine(ring.CopyInvariants());

            ring.UpdateVertexInvariants();
            Console.Out.WriteLine(ring.CopyInvariants());

            //        List<Integer> orbit = ring.CreateOrbit();
            //        Console.Out.WriteLine(orbit);
            //
            //        ring.SetColor(orbit[0], 1);
            //        ring.UpdateVertexInvariants();
            //        Console.Out.WriteLine(ring.CopyInvariants());
            //
            //        orbit = ring.CreateOrbit();
            //        Console.Out.WriteLine(orbit);
            //
            //        ring.SetColor(orbit[0], 2);
            //        ring.UpdateVertexInvariants();
            //        Console.Out.WriteLine(ring.CopyInvariants());
            //
            //        orbit = ring.CreateOrbit();
            //        Console.Out.WriteLine(orbit);
            //        ring.SetColor(orbit[0], 3);
            //        Console.Out.WriteLine(ring.CopyInvariants());
        }