Example #1
0
        /// <inheritdoc />
        int IReadonlyDirectedGraph.CountOutgoingHeavyEdges(NodeId node)
        {
            int count = 0;

            foreach (var edge in m_graph.GetOutgoingEdges(node))
            {
                if (!edge.IsLight && m_nodeFilter.WasVisited(edge.OtherNode))
                {
                    count++;
                }
            }

            return(count);
        }
Example #2
0
        /// <summary>
        /// Propagates node dirtiness to transitive dependents.
        /// </summary>
        private void ProcessDirtyNodeQueue(Queue <NodeId> nodesToDirty, Action <NodeId> action = null)
        {
            Contract.Requires(nodesToDirty != null);

            do
            {
                NodeId node = nodesToDirty.Dequeue();

                if (MaterializedNodes.Contains(node))
                {
                    MaterializedNodes.Remove(node);
                }

                action?.Invoke(node);

                foreach (var edge in Graph.GetOutgoingEdges(node))
                {
                    NodeId dependent = edge.OtherNode;

                    if (!DirtyNodes.Contains(dependent))
                    {
                        nodesToDirty.Enqueue(dependent);
                        DirtyNodes.Add(dependent);
                    }
                }
            }while (nodesToDirty.Count > 0);
        }
Example #3
0
        private void TestGraphSerializationPerformCommonValidations(DirectedGraph graph, NodeId[] nodes, IReadonlyDirectedGraph sourceGraph)
        {
            XAssert.AreEqual(graph.NodeCount, nodes.Length);
            XAssert.AreEqual(graph.EdgeCount, 12);

            XAssert.IsTrue(graph.IsSinkNode(nodes[0]));
            XAssert.IsTrue(graph.IsSinkNode(nodes[1]));
            XAssert.IsTrue(graph.IsSinkNode(nodes[2]));
            XAssert.IsTrue(graph.IsSinkNode(nodes[3]));
            XAssert.IsTrue(graph.IsSinkNode(nodes[4]));
            XAssert.IsTrue(graph.IsSourceNode(nodes[7]));
            XAssert.IsTrue(graph.IsSourceNode(nodes[10]));
            XAssert.IsTrue(graph.IsSourceNode(nodes[11]));

            XAssert.IsFalse(graph.IsSourceNode(nodes[6]));
            XAssert.IsFalse(graph.IsSinkNode(nodes[6]));

            foreach (var node in sourceGraph.Nodes)
            {
                XAssert.IsTrue(EdgeSetEqual(new HashSet <Edge>(sourceGraph.GetOutgoingEdges(node)), new HashSet <Edge>(graph.GetOutgoingEdges(node))));
                XAssert.IsTrue(EdgeSetEqual(new HashSet <Edge>(sourceGraph.GetIncomingEdges(node)), new HashSet <Edge>(graph.GetIncomingEdges(node))));
            }

            VerifyNodeHeights(graph, nodes);

            var succOfNode9 = new HashSet <Edge>(graph.GetOutgoingEdges(nodes[9]));

            XAssert.IsTrue(
                EdgeSetEqual(
                    succOfNode9,
                    new HashSet <Edge>
            {
                new Edge(nodes[6]),
                new Edge(nodes[3]),
                new Edge(nodes[4])
            }));

            var predOfNode8 = new HashSet <Edge>(graph.GetIncomingEdges(nodes[8]));

            XAssert.IsTrue(
                EdgeSetEqual(
                    predOfNode8,
                    new HashSet <Edge>
            {
                new Edge(nodes[10]),
                new Edge(nodes[11])
            }));
        }
Example #4
0
        /// <summary>
        /// Simple reachability query reference implementation.
        /// </summary>
        private static bool IsReachableTrivial(IReadonlyDirectedGraph graph, NodeId from, NodeId to)
        {
            if (from == to)
            {
                return(true);
            }

            foreach (Edge edge in graph.GetOutgoingEdges(from))
            {
                if (IsReachableTrivial(graph, edge.OtherNode, to))
                {
                    return(true);
                }
            }

            return(false);
        }
Example #5
0
        private void VisitTransitiveClosure(
            Queue <NodeId> queue,
            VisitationTracker visitedNodes,
            VisitNode visitNodeDependencies,
            VisitNode visitNodeDependents,
            bool dependencies,
            bool dependents)
        {
            Contract.Assert(visitNodeDependencies == null || dependencies);
            Contract.Assert(visitNodeDependents == null || dependents);

            while (queue.Count != 0)
            {
                var node = queue.Dequeue();

                if (dependencies)
                {
                    if (visitNodeDependencies == null || visitNodeDependencies(node))
                    {
                        foreach (Edge inEdge in m_dataflowGraph.GetIncomingEdges(node))
                        {
                            if (visitedNodes.MarkVisited(inEdge.OtherNode))
                            {
                                queue.Enqueue(inEdge.OtherNode);
                            }
                        }
                    }
                }

                if (dependents)
                {
                    if (visitNodeDependents == null || visitNodeDependents(node))
                    {
                        foreach (Edge outEdge in m_dataflowGraph.GetOutgoingEdges(node))
                        {
                            if (visitedNodes.MarkVisited(outEdge.OtherNode))
                            {
                                queue.Enqueue(outEdge.OtherNode);
                            }
                        }
                    }
                }
            }
        }