public IEnumerator <int> GetEnumerator()
        {
#else
        IEnumerator <int> IEnumerable <int> .GetEnumerator()
        {
#endif
            IEnumerable e = graph.InEdges(vert);
            if (e == null)
            {
                return(new EmptyEnumerator());
            }
            else
            {
                return(new PredEnumerator(e.GetEnumerator()));
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            IEnumerable e = graph.InEdges(vert);

            if (e == null)
            {
                return(new EmptyEnumerator());
            }
            else
            {
                return(new PredEnumerator(e.GetEnumerator()));
            }
        }
 /// <summary>
 /// enumerates over edges of a node
 /// </summary>
 /// <param name="node"></param>
 /// <returns></returns>
 public IEnumerable <LayerEdge> InEdges(int node)
 {
     if (node < BaseGraph.NodeCount)//original node
     {
         foreach (IntEdge e in BaseGraph.InEdges(node))
         {
             if (e.Source != e.Target && e.LayerEdges != null)
             {
                 yield return(LastEdge(e));
             }
         }
     }
     else if (node >= firstVirtualNode)
     {
         yield return(InEdgeOfVirtualNode(node));
     }
 }
        void CreateMappingOfNeibBlocks()
        {
            BasicGraph <IntPair> graph = BasicGraphFromLeftRightIntNeibs();

            for (int root = 0; root < graph.NodeCount; root++)
            {
                if (graph.InEdges(root).Count == 0 && !nodeToBlockRoot.ContainsKey(root))
                {
                    var block   = new List <int>();
                    int current = root;
                    for (IList <IntPair> outEdges = graph.OutEdges(current); outEdges.Count > 0;
                         outEdges = graph.OutEdges(current))
                    {
                        current = outEdges[0].Second;
                        block.Add(current);
                        nodeToBlockRoot[current] = root;
                    }
                    if (block.Count > 0)
                    {
                        BlockRootToBlock[root] = block;
                    }
                }
            }
        }
        public int[] GetLayers()
        {
            //sort the vertices in topological order
            int[] topoOrder = IntEdge.GetOrder(graph);
            int[] layering  = new int[graph.NodeCount];

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

            while (k-- > 0)
            {
                int v = topoOrder[k];
                foreach (IntEdge e in graph.InEdges(v))
                {
                    int u = e.Source;
                    int l = layering[v] + e.Separation;
                    if (layering[u] < l)
                    {
                        layering[u] = l;
                    }
                }
            }
            return(layering);
        }
Beispiel #5
0
        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 <IntEdge> edges = new List <IntEdge>();

            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 IntEdge(v, root[rightNeighbor]));
                        }
                        w = align[w];
                    }while (w != v);
                }
            }

            BasicGraph <IntEdge> blockGraph = new BasicGraph <IntEdge>(edges, nOfVertices);

            //sort the graph in the topological order
            int[] topoSort = IntEdge.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]];
                }
            }
        }
        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<IntEdge> edges = new List<IntEdge>();
            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 IntEdge(v, root[rightNeighbor]));
                        w = align[w];
                    }
                    while (w != v);
                }
            }

            BasicGraph<IntEdge> blockGraph = new BasicGraph<IntEdge>(edges, nOfVertices);
            //sort the graph in the topological order
            int[] topoSort = IntEdge.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]];
            }

        }