Example #1
0
        private NodeList UpdateNodeStates(NodeList nodes, NodeList backLoopNodes, NodeList candidates)
        {
            for (INodeCursor nc = nodes.Nodes(); nc.Ok; nc.Next())
            {
                Node      node      = nc.Node;
                NodeState nodeState = GetNodeState(node);
                switch (nodeState)
                {
                case NodeState.BackLooping:
                    backLoopNodes.AddFirst(node);
                    break;

                case NodeState.BackLoopingCandidate:
                    candidates.AddFirst(node);
                    break;
                }
                nodeStates[node.Index] = nodeState;
            }
            return(candidates);
        }
        public static void Main()
        {
            Graph graph = new Graph();

            const int nodes = 30000;
            const int loops = 10;

            const int outerLoops = 20;

            for (int i = 0; i < nodes; i++)
            {
                graph.CreateNode();
            }

            for (int loop = 0; loop < outerLoops; loop++)
            {
                Console.Write(".");

                t1.Start();
                INodeMap map = graph.CreateNodeMap();
                for (int i = 0; i < loops; i++)
                {
                    for (INodeCursor nc = graph.GetNodeCursor(); nc.Ok; nc.Next())
                    {
                        Node v = nc.Node;
                        map.SetInt(v, i);
                        i = map.GetInt(v);
                    }
                }
                graph.DisposeNodeMap(map);
                t1.Stop();


                t2.Start();
                map = Maps.CreateIndexNodeMap(new int[graph.N]);
                for (int i = 0; i < loops; i++)
                {
                    for (INodeCursor nc = graph.GetNodeCursor(); nc.Ok; nc.Next())
                    {
                        Node v = nc.Node;
                        map.SetInt(v, i);
                        map.GetInt(v);
                    }
                }
                t2.Stop();


                t3.Start();
                map = Maps.CreateHashedNodeMap();
                for (int i = 0; i < loops; i++)
                {
                    for (INodeCursor nc = graph.GetNodeCursor(); nc.Ok; nc.Next())
                    {
                        Node v = nc.Node;
                        map.SetInt(v, i);
                        i = map.GetInt(v);
                    }
                }
                t3.Stop();

                t4.Start();
                int[] array = new int[graph.N];
                for (int i = 0; i < loops; i++)
                {
                    for (INodeCursor nc = graph.GetNodeCursor(); nc.Ok; nc.Next())
                    {
                        int vid = nc.Node.Index;
                        array[vid] = i;
                        i          = array[vid];
                    }
                }
                t4.Stop();


                t5.Start();
                IDictionary <Node, int> dictionary = new Dictionary <Node, int>(2 * graph.N + 1); //use map with good initial size
                for (int i = 0; i < loops; i++)
                {
                    for (INodeCursor nc = graph.GetNodeCursor(); nc.Ok; nc.Next())
                    {
                        Node v = nc.Node;
                        dictionary[v] = i;
                        i             = dictionary[v];
                    }
                }
                t5.Stop();

                t6.Start();
                IDictionary <Node, object> objectDictionary = new Dictionary <Node, object>(2 * graph.N + 1);
                //use map with good initial size
                for (int i = 0; i < loops; i++)
                {
                    for (INodeCursor nc = graph.GetNodeCursor(); nc.Ok; nc.Next())
                    {
                        Node v = nc.Node;
                        objectDictionary[v] = i;
                        i = (int)objectDictionary[v];
                    }
                }
                t6.Stop();
            }

            Console.WriteLine("");
            Console.WriteLine("TIME:  standard NodeMap  : " + t1);
            Console.WriteLine("TIME:  index    NodeMap  : " + t2);
            Console.WriteLine("TIME:  hashed   NodeMap  : " + t3);
            Console.WriteLine("TIME:  plain array       : " + t4);
            Console.WriteLine("TIME:  Dictionary        : " + t5);
            Console.WriteLine("TIME:  object Dictionary : " + t6);

            Console.WriteLine("\nPress key to end demo.");
            Console.ReadKey();
        }
Example #3
0
        public static void Main()
        {
            //instantiates an empty graph
            yWorks.Algorithms.Graph graph = new yWorks.Algorithms.Graph();

            //create a temporary node array for fast lookup
            Node[] tmpNodes = new Node[5];

            //create some nodes in the graph and store references in the array
            for (int i = 0; i < 5; i++)
            {
                tmpNodes[i] = graph.CreateNode();
            }

            //create some edges in the graph
            for (int i = 0; i < 5; i++)
            {
                for (int j = i + 1; j < 5; j++)
                {
                    //create an edge from node at index i to node at index j
                    graph.CreateEdge(tmpNodes[i], tmpNodes[j]);
                }
            }


            //output the nodes of the graph
            Console.WriteLine("The nodes of the graph");
            for (INodeCursor nc = graph.GetNodeCursor(); nc.Ok; nc.Next())
            {
                Node node = nc.Node;
                Console.WriteLine(node);
                Console.WriteLine("in edges #" + node.InDegree);
                for (IEdgeCursor ec = node.GetInEdgeCursor(); ec.Ok; ec.Next())
                {
                    Console.WriteLine(ec.Edge);
                }
                Console.WriteLine("out edges #" + node.OutDegree);
                for (IEdgeCursor ec = node.GetOutEdgeCursor(); ec.Ok; ec.Next())
                {
                    Console.WriteLine(ec.Edge);
                }
            }


            //output the edges of the graph
            Console.WriteLine("\nThe edges of the graph");
            for (IEdgeCursor ec = graph.GetEdgeCursor(); ec.Ok; ec.Next())
            {
                Console.WriteLine(ec.Edge);
            }

            //reverse edges that have consecutive neighbors in graph
            //reversing means switching source and target node
            for (IEdgeCursor ec = graph.GetEdgeCursor(); ec.Ok; ec.Next())
            {
                if (Math.Abs(ec.Edge.Source.Index - ec.Edge.Target.Index) == 1)
                {
                    graph.ReverseEdge(ec.Edge);
                }
            }

            Console.WriteLine("\nthe edges of the graph after some edge reversal");
            for (IEdgeCursor ec = graph.GetEdgeCursor(); ec.Ok; ec.Next())
            {
                Console.WriteLine(ec.Edge);
            }

            ///////////////////////////////////////////////////////////////////////////
            // Node- and EdgeMap handling   ///////////////////////////////////////////
            ///////////////////////////////////////////////////////////////////////////

            //create a nodemap for the graph
            INodeMap nodeMap = graph.CreateNodeMap();

            foreach (var node in graph.Nodes)
            {
                //associate descriptive String to the node via a nodemap
                nodeMap.Set(node, "this is node " + node.Index);
            }

            //create an edgemap for the graph
            IEdgeMap edgeMap = graph.CreateEdgeMap();

            foreach (var edge in graph.Edges)
            {
                //associate descriptive String to the edge via an edgemap
                edgeMap.Set(edge, "this is edge [" +
                            nodeMap.Get(edge.Source) + "," +
                            nodeMap.Get(edge.Target) + "]");
            }

            //output the nodemap values of the nodes
            Console.WriteLine("\nThe node map values of the graph");
            foreach (var node in graph.Nodes)
            {
                Console.WriteLine(nodeMap.Get(node));
            }

            //output the edges of the graph
            Console.WriteLine("\nThe edge map values of the graph");
            foreach (var edge in graph.Edges)
            {
                Console.WriteLine(edgeMap.Get(edge));
            }

            //cleanup unneeded node and edge maps again (free resources)
            graph.DisposeNodeMap(nodeMap);
            graph.DisposeEdgeMap(edgeMap);

            ///////////////////////////////////////////////////////////////////////////
            // removing elements from the graph  //////////////////////////////////////
            ///////////////////////////////////////////////////////////////////////////

            for (INodeCursor nc = graph.GetNodeCursor(); nc.Ok; nc.Next())
            {
                //remove node that has a edge degree > 2
                if (nc.Node.Degree > 2)
                {
                    //removed the node and all of its adjacent edges from the graph
                    graph.RemoveNode(nc.Node);
                }
            }
            Console.WriteLine("\ngraph after some node removal");
            Console.WriteLine(graph);

            Console.WriteLine("\nPress key to end demo.");
            Console.ReadKey();
        }
Example #4
0
        /// <inheritdoc/>
        public override void AssignLayers(LayoutGraph graph, ILayers layers, ILayoutDataProvider ldp)
        {
            // get core layer assignment
            base.AssignLayers(graph, layers, ldp);

            // Hide all edges that are no sequence flows
            var graphHider = new LayoutGraphHider(graph);

            foreach (var edge in graph.GetEdgeArray())
            {
                if (!BpmnLayout.IsSequenceFlow(edge, graph))
                {
                    graphHider.Hide(edge);
                }
            }

            // determine current layer of all nodes
            currentLayers = new int[graph.NodeCount];
            for (int i = 0; i < layers.Size(); i++)
            {
                for (INodeCursor nc = layers.GetLayer(i).List.Nodes(); nc.Ok; nc.Next())
                {
                    currentLayers[nc.Node.Index] = i;
                }
            }

            // mark nodes on a back-loop and candidates that may be on a back loop if other back-loop nodes are reassigned
            nodeStates = new NodeState[graph.NodeCount];
            NodeList candidates    = new NodeList();
            NodeList backLoopNodes = new NodeList();

            for (int i = layers.Size() - 1; i >= 0; i--)
            {
                // check from last to first layer to detect candidates as well
                NodeList nodes = layers.GetLayer(i).List;
                UpdateNodeStates(nodes, backLoopNodes, candidates);
            }

            // swap layer for back-loop nodes
            while (backLoopNodes.Count > 0)
            {
                for (INodeCursor nc = backLoopNodes.Nodes(); nc.Ok; nc.Next())
                {
                    Node node         = nc.Node;
                    int  currentLayer = currentLayers[node.Index];
                    // the target layer is the next layer after the highest fixed target node layer
                    int targetLayer = 0;
                    for (Edge edge = node.FirstOutEdge; edge != null; edge = edge.NextOutEdge)
                    {
                        int targetNodeIndex = edge.Target.Index;
                        if (nodeStates[targetNodeIndex] == NodeState.Fixed)
                        {
                            targetLayer = Math.Max(targetLayer, currentLayers[targetNodeIndex] + 1);
                        }
                    }
                    if (targetLayer == 0)
                    {
                        // no fixed target found, so all targets must be candidates
                        // -> we skip the node as we don't know where the candidates will be placed at the end
                        continue;
                    }
                    if (targetLayer < currentLayer)
                    {
                        layers.GetLayer(currentLayer).Remove(node);
                        layers.GetLayer(targetLayer).Add(node);
                        currentLayers[node.Index] = targetLayer;
                        nodeStates[node.Index]    = NodeState.Fixed;
                    }
                }
                backLoopNodes.Clear();

                // update states of the candidates
                candidates = UpdateNodeStates(candidates, backLoopNodes, new NodeList());
            }

            // remove empty layers
            for (int i = layers.Size() - 1; i >= 0; i--)
            {
                if (layers.GetLayer(i).List.Count == 0)
                {
                    layers.Remove(i);
                }
            }

            // cleanup
            graphHider.UnhideAll();
            nodeStates    = null;
            currentLayers = null;
        }