Exemplo n.º 1
0
 public override void Compute(CircleNodeScene scene)
 {
     this.mAlg = new DFLongestPath <CircleNode, ArrowEdge>(
         scene.Graph, this.Directed, this.Reversed);
     this.mAlg.Compute();
     Digraph <CircleNode, ArrowEdge> .GEdge[] edges
         = scene.Graph.InternalEdges;
     for (int i = 0; i < edges.Length; i++)
     {
         edges[i].Data.LineColor = Color.Black;
     }
     ArrowEdge[] pEdges = this.mAlg.PathEdges;
     for (int j = 0; j < pEdges.Length; j++)
     {
         pEdges[j].LineColor = Color.Red;
     }
 }
Exemplo n.º 2
0
        protected static int ArrangeByLongestPath(
            Digraph <Node, Edge> graph, Digraph <Node, Edge> .GNode[] nodes)
        {
            if (graph == null)
            {
                throw new ArgumentNullException("graph");
            }
            if (nodes == null)
            {
                throw new ArgumentNullException("vNodes");
            }
            // Take the easy way out if possible
            if (graph.NodeCount == 0 || graph.EdgeCount == 0)
            {
                return(0);
            }
            int j, k, count, vCount = 0;
            int nCount = graph.NodeCount;
            int eCount = graph.EdgeCount;

            Digraph <Node, Edge> .GNode gNode;
            Digraph <Node, Edge> .GEdge gEdge;
            // Check the capacity of vNodes
            for (j = 0; j < nCount; j++)
            {
                if (!graph.InternalNodeAt(j).Hidden)
                {
                    vCount++;
                }
            }
            if (vCount > nodes.Length)
            {
                throw new ArgumentException(
                          "Array too small to hold all visible nodes", "vNodes");
            }
            // Compute the longest path in the graph
            DFLongestPath <Node, Edge> alg
                = new DFLongestPath <Node, Edge>(graph, false, false);

            alg.Compute();
            int[] nis = alg.PathNodeIndexes;
            // Initially color all nodes White.
            for (j = 0; j < nCount; j++)
            {
                graph.InternalNodeAt(j).Color = GraphColor.White;
            }
            // Add all the nodes in the longest path to the embedding circle
            // and color them Black.
            vCount = nis.Length;
            for (j = 0; j < vCount; j++)
            {
                gNode       = graph.InternalNodeAt(nis[j]);
                gNode.Color = GraphColor.Black;
                nodes[j]    = gNode;
            }
            bool[] flags = new bool[nCount];
            // Here Gray is used to determine which nodes are neighbors
            // of the current node to be added to the embedding circle.
            for (k = 0; k < nCount; k++)
            {
                gNode = graph.InternalNodeAt(k);
                if (gNode.Color != GraphColor.Black && !gNode.Hidden)
                {
                    // Clear neighbor flags from previous iteration,
                    // so that all nodes are now either Black or White.
                    for (j = 0; j < nCount; j++)
                    {
                        flags[j] = false;
                    }
                    // Set which nodes are neighbors to the current node.
                    count = 0;
                    for (j = 0; j < eCount; j++)
                    {
                        gEdge = graph.InternalEdgeAt(j);
                        if (gEdge.SrcNode.Index == k &&
                            gEdge.DstNode.Color == GraphColor.Black)
                        {
                            flags[gEdge.DstNode.Index] = true;
                            count++;
                        }
                        else if (gEdge.DstNode.Index == k &&
                                 gEdge.SrcNode.Color == GraphColor.Black)
                        {
                            flags[gEdge.SrcNode.Index] = true;
                            count++;
                        }
                    }
                    // Look for two consecutive neighbors in the list
                    // and insert the current node between them.
                    if (count >= 2)
                    {
                        for (j = 0; j < vCount; j++)
                        {
                            if (flags[nodes[j].Index] &&
                                flags[nodes[(j + 1) % vCount].Index])
                            {
                                j++;
                                Array.Copy(nodes, j,
                                           nodes, j + 1, vCount - j);
                                nodes[j] = gNode;
                                vCount++;
                                gNode.Color = GraphColor.Black;
                                break;
                            }
                        }
                    }
                    // Look for any neighbor in the list
                    // and insert the current node after it.
                    if (gNode.Color != GraphColor.Black && count > 0)
                    {
                        for (j = 0; j < vCount; j++)
                        {
                            if (flags[nodes[j].Index])
                            {
                                j++;
                                Array.Copy(nodes, j,
                                           nodes, j + 1, vCount - j);
                                nodes[j] = gNode;
                                vCount++;
                                gNode.Color = GraphColor.Black;
                                break;
                            }
                        }
                    }
                    // Place all orphaned nodes at the end of the list
                    if (gNode.Color != GraphColor.Black)
                    {
                        nodes[vCount++] = gNode;
                        gNode.Color     = GraphColor.Black;
                    }
                }
            }
            for (k = vCount; k < nodes.Length; k++)
            {
                nodes[k] = null;
            }
            return(vCount);
        }