Exemplo 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());
        }
Exemplo n.º 2
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);
        }