예제 #1
0
        private ISet <Tuple <InstNode, InstNode> > FindCycles2(InstNode root)
        {
            var visitedNodes = new HashSet <InstNode>();
            var cycles       = new HashSet <Tuple <InstNode, InstNode> >();

            GraphTraversal.PerformTraversal <InstNode>(root, (node, fringeAdder) =>
            {
                if (visitedNodes.Contains(node))
                {
                    return;
                }
                visitedNodes.Add(node);
                if (node.Left != null)
                {
                    if (visitedNodes.Contains(node.Left))
                    {
                        cycles.Add(new Tuple <InstNode, InstNode>(node, node.Left));
                    }
                }
                if (node.Right != null)
                {
                    if (visitedNodes.Contains(node.Right))
                    {
                        cycles.Add(new Tuple <InstNode, InstNode>(node, node.Right));
                    }
                }
                if (node.Left != null)
                {
                    if (!visitedNodes.Contains(node.Left))
                    {
                        fringeAdder(node.Left);
                    }
                }
                if (node.Right != null)
                {
                    if (!visitedNodes.Contains(node.Right))
                    {
                        fringeAdder(node.Right);
                    }
                }
            });
            return(cycles);
        }
예제 #2
0
        private void GenerateGraphLinks(Node <IList <IInstruction> > root, Dictionary <InstNode, GraphVizNode> graphVizNodes, StringBuilder builder)
        {
            var visitedNodes = new HashSet <InstNode>();

            GraphTraversal.PerformTraversal <InstNode>(root, (node, fringeAdder) =>
            {
                if (visitedNodes.Contains(node))
                {
                    return;
                }
                visitedNodes.Add(node);
                if (node.Left != null)
                {
                    builder.AppendLine(String.Format("node{0}->node{1}", graphVizNodes[node].Id, graphVizNodes[node.Left].Id));
                    fringeAdder(node.Left);
                }
                if (node.Right != null)
                {
                    builder.AppendLine(String.Format("node{0}->node{1}", graphVizNodes[node].Id, graphVizNodes[node.Right].Id));
                    fringeAdder(node.Right);
                }
            });
        }