示例#1
0
        public int[] GetLayers()
        {
            //sort the vertices in topological order
            int[] topoOrder = PolyIntEdge.GetOrder(graph);
            int[] layering  = new int[graph.NodeCount];

            //going backward from leaves
            int k = graph.NodeCount;

            while (k-- > 0)
            {
                int v = topoOrder[k];
                foreach (PolyIntEdge e in graph.InEdges(v))
                {
                    int u = e.Source;
                    int l = layering[v] + e.Separation;
                    if (layering[u] < l)
                    {
                        layering[u] = l;
                    }
                }
            }
            return(layering);
        }
        void AssignCoordinatesByLongestPath()
        {
            this.x = this.xCoords[this.EnumRightUp] = new double[this.nOfVertices];

            /*
             * We create a graph of blocks or rather of block roots. There is an edge
             * from u-block to v-block  if some of elements of u-block is to the left of v
             * on the same layer. Then we topologically sort the graph and assign coordinates
             * taking into account separation between the blocks.
             */
            //create the graph first
            List <PolyIntEdge> edges = new List <PolyIntEdge>();

            for (int v = 0; v < nOfVertices; v++)
            {
                if (v == root[v]) //v is a root
                {
                    int w = v;    //w will be running over the block
                    do
                    {
                        int rightNeighbor;
                        if (TryToGetRightNeighbor(w, out rightNeighbor))
                        {
                            edges.Add(new PolyIntEdge(v, root[rightNeighbor]));
                        }
                        w = align[w];
                    }while (w != v);
                }
            }

            BasicGraphOnEdges <PolyIntEdge> blockGraph = new BasicGraphOnEdges <PolyIntEdge>(edges, nOfVertices);

            //sort the graph in the topological order
            int[] topoSort = PolyIntEdge.GetOrder(blockGraph);
            //start placing the blocks according to the order

            foreach (int v in topoSort)
            {
                if (v == root[v])//not every element of topoSort is a root!
                {
                    double vx          = 0;
                    bool   vIsLeftMost = true;
                    int    w           = v;//w is running over the block
                    do
                    {
                        int wLeftNeighbor;
                        if (TryToGetLeftNeighbor(w, out wLeftNeighbor))
                        {
                            if (vIsLeftMost)
                            {
                                vx          = x[root[wLeftNeighbor]] + DeltaBetweenVertices(wLeftNeighbor, w);
                                vIsLeftMost = false;
                            }
                            else
                            {
                                vx = RightMost(vx, x[root[wLeftNeighbor]] + DeltaBetweenVertices(wLeftNeighbor, w));
                            }
                        }
                        w = align[w];
                    }while (w != v);

                    x[v] = vx;
                }
            }

            //push the roots of the graph maximally to the right
            foreach (int v in topoSort)
            {
                if (v == root[v])
                {
                    if (blockGraph.InEdges(v).Count == 0)
                    {
                        int    w         = v;//w runs over the block
                        double xLeftMost = RightMost(-infinity, infinity);
                        double xl        = xLeftMost;
                        do
                        {
                            int wRightNeigbor;
                            if (TryToGetRightNeighbor(w, out wRightNeigbor))
                            {
                                xLeftMost = LeftMost(xLeftMost,
                                                     x[root[wRightNeigbor]] - DeltaBetweenVertices(w, wRightNeigbor));
                            }

                            w = align[w];
                        } while (w != v);

                        //leave the value zero if there are no right neighbours
                        if (xl != xLeftMost)
                        {
                            x[v] = xLeftMost;
                        }
                    }
                }
            }

            for (int v = 0; v < this.nOfVertices; v++)
            {
                if (v != root[v])
                {
                    x[v] = x[root[v]];
                }
            }
        }