Beispiel #1
0
 /// <summary>
 /// Returns with the adjacent vertices of the <code>vertex</code>.
 /// </summary>
 /// <param name="g">The graph.</param>
 /// <param name="vertex">The vertex which neighbours' we want to get.</param>
 /// <returns>List of the adjacent vertices of the <code>vertex</code>.</returns>
 public static IEnumerable <TVertex> GetNeighbours <TVertex, TEdge>(this IBidirectionalGraph <TVertex, TEdge> g, TVertex vertex)
     where TEdge : IEdge <TVertex>
 {
     return(((from e in g.InEdges(vertex) select e.Source)
             .Concat(
                 (from e in g.OutEdges(vertex) select e.Target))).Distinct());
 }
Beispiel #2
0
        private static void CollectAdjacentVerticesRecursive <TVertex, TEdge>(IBidirectionalGraph <TVertex, TEdge> graph,
                                                                              TVertex vertex, EdgeDirection direction, ICollection <TVertex> collectedVertices,
                                                                              EdgePredicate <TVertex, TEdge> edgePredicate = null, bool recursive = false)
            where TEdge : IEdge <TVertex>
        {
            var adjacentEdges = direction == EdgeDirection.In
                ? graph.InEdges(vertex)
                : graph.OutEdges(vertex);

            var adjacentVertices = adjacentEdges
                                   .Where(edge => edgePredicate == null || edgePredicate(edge))
                                   .Select(edge => edge.GetOtherEnd(vertex))
                                   .Distinct();

            foreach (var adjacentVertex in adjacentVertices)
            {
                // Loop detection
                if (collectedVertices.Contains(adjacentVertex))
                {
                    continue;
                }

                collectedVertices.Add(adjacentVertex);

                if (recursive)
                {
                    CollectAdjacentVerticesRecursive(graph, adjacentVertex, direction, collectedVertices, edgePredicate, recursive: true);
                }
            }
        }
        protected static IMutableBidirectionalGraph <T, IEdge <T> > GetSubGraphTo(IBidirectionalGraph <T, IEdge <T> > graph, T target)
        {
            var result = new BidirectionalGraph <T, IEdge <T> >();

            var visited = new HashSet <T>(new IdentityComparer <T>());
            var toVisit = new Queue <T>();

            toVisit.Enqueue(target);

            while (toVisit.Count > 0)
            {
                var node = toVisit.Dequeue();

                var inEdges = graph.InEdges(node);
                visited.Add(node);
                result.AddVertex(node);

                foreach (var inEdge in inEdges)
                {
                    if (!visited.Contains(inEdge.Source))
                    {
                        toVisit.Enqueue(inEdge.Source);
                    }
                    result.AddVerticesAndEdge(inEdge);
                }
            }

            return(result);
        }
Beispiel #4
0
 public IEnumerable <IAction> GetPrevActions(IAction action)
 {
     if (action == null)
     {
         throw new ArgumentNullException("action");
     }
     return(_graph.InEdges(action).Select(x => x.Source));
 }
Beispiel #5
0
 public static IEnumerable <TEdge> GetEdges <TVertex, TEdge>(
     this IBidirectionalGraph <TVertex, TEdge> graph, TVertex vertex, EdgeDirection edgeDirection)
     where TEdge : IEdge <TVertex>
 {
     return(edgeDirection == EdgeDirection.In
         ? graph.InEdges(vertex)
         : graph.OutEdges(vertex));
 }
Beispiel #6
0
 private IEnumerable <IVertex3D> getConnectedVertices(IVertex3D vertex3D)
 {
     return(graph.OutEdges((TVertex)vertex3D).Select(e => e.Target).Cast <IVertex3D>()
            .Union(
                graph.InEdges((TVertex)vertex3D).Select(e => e.Source).Cast <IVertex3D>()
                )
            );
 }
Beispiel #7
0
 public static IEnumerable <TVertex> GetNeighbors <TVertex, TEdge>(
     [NotNull] this IBidirectionalGraph <TVertex, TEdge> graph,
     [NotNull] TVertex vertex)
     where TEdge : IEdge <TVertex>
 {
     return(graph.InEdges(vertex).Select(e => e.Source)
            .Concat(graph.OutEdges(vertex).Select(e => e.Target))
            .Distinct());
 }
        /// <summary>
        /// Constructs a new ArrayBidirectionalGraph instance from a
        /// IBidirectionalGraph instance
        /// </summary>
        /// <param name="visitedGraph"></param>
        public ArrayBidirectionalGraph(
            IBidirectionalGraph <TVertex, TEdge> visitedGraph
            )
        {
            Contract.Requires(visitedGraph != null);

            this.vertexEdges = new Dictionary <TVertex, InOutEdges>(visitedGraph.VertexCount);
            this.edgeCount   = visitedGraph.EdgeCount;
            foreach (var vertex in visitedGraph.Vertices)
            {
                var outEdges = Enumerable.ToArray(visitedGraph.OutEdges(vertex));
                var inEdges  = Enumerable.ToArray(visitedGraph.InEdges(vertex));
                this.vertexEdges.Add(vertex, new InOutEdges(outEdges, inEdges));
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ArrayBidirectionalGraph{TVertex,TEdge}"/> class.
        /// </summary>
        /// <param name="baseGraph">Wrapped graph.</param>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="baseGraph"/> is <see langword="null"/>.</exception>
        public ArrayBidirectionalGraph([NotNull] IBidirectionalGraph<TVertex, TEdge> baseGraph)
        {
            if (baseGraph is null)
                throw new ArgumentNullException(nameof(baseGraph));

            AllowParallelEdges = baseGraph.AllowParallelEdges;
            _vertexEdges = new Dictionary<TVertex, InOutEdges>(baseGraph.VertexCount);
            EdgeCount = baseGraph.EdgeCount;
            foreach (TVertex vertex in baseGraph.Vertices)
            {
                TEdge[] outEdges = baseGraph.OutEdges(vertex).ToArray();
                TEdge[] inEdges = baseGraph.InEdges(vertex).ToArray();
                _vertexEdges.Add(vertex, new InOutEdges(outEdges, inEdges));
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ArrayBidirectionalGraph{TVertex,TEdge}"/> class.
        /// </summary>
        /// <param name="visitedGraph">Graph to visit.</param>
        public ArrayBidirectionalGraph([NotNull] IBidirectionalGraph <TVertex, TEdge> visitedGraph)
        {
            if (visitedGraph is null)
            {
                throw new ArgumentNullException(nameof(visitedGraph));
            }

            _vertexEdges = new Dictionary <TVertex, InOutEdges>(visitedGraph.VertexCount);
            EdgeCount    = visitedGraph.EdgeCount;
            foreach (var vertex in visitedGraph.Vertices)
            {
                var outEdges = visitedGraph.OutEdges(vertex).ToArray();
                var inEdges  = visitedGraph.InEdges(vertex).ToArray();
                _vertexEdges.Add(vertex, new InOutEdges(outEdges, inEdges));
            }
        }
Beispiel #11
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="g"></param>
        /// <param name="assg"></param>
        public void Transform(IBidirectionalGraph g, IMutableVertexAndEdgeListGraph assg)
        {
            VertexCollection avs = new VertexCollection();

            // adding vertices
            foreach (IEdge e in g.Edges)
            {
                // xi_-(L) = g(xi_-(0), xi_+(L))
                CharacteristicVertex avm = (CharacteristicVertex)assg.AddVertex();
                avm.IncomingEdge = e;
                avm.Vertex       = e.Target;
                avs.Add(avm);

                // xi_+(0) = g(xi_-(0), xi_+(L))
                CharacteristicVertex avp = (CharacteristicVertex)assg.AddVertex();
                avp.IncomingEdge = e;
                avp.Vertex       = e.Source;
                avs.Add(avp);
            }

            // adding out edges
            foreach (CharacteristicVertex av in avs)
            {
                foreach (IEdge e in g.OutEdges(av.Vertex))
                {
                    // find target vertex:
                    CharacteristicVertex avtarget = FindTargetVertex(e);
                    // add xi_-
                    CharacteristicEdge aem = (CharacteristicEdge)assg.AddEdge(av, avtarget);
                    aem.Positive = false;
                    aem.Edge     = e;
                }
                foreach (IEdge e in g.InEdges(av.Vertex))
                {
                    // find target vertex:
                    CharacteristicVertex avtarget = FindTargetVertex(e);
                    // add xi_-
                    CharacteristicEdge aem = (CharacteristicEdge)assg.AddEdge(av, avtarget);
                    aem.Positive = true;
                    aem.Edge     = e;
                }
            }
        }
        public static void BidirectionalContainsEdgeAssertions(
            [NotNull] IBidirectionalGraph <int, IEdge <int> > graph,
            [NotNull] IEdge <int> e12,
            [NotNull] IEdge <int> f12,
            [CanBeNull] IEdge <int> e21,
            [CanBeNull] IEdge <int> f21)
        {
            Assert.AreEqual(0, graph.InDegree(1));
            Assert.AreEqual(1, graph.OutDegree(1));
            Assert.AreEqual(1, graph.InDegree(2));
            Assert.AreEqual(0, graph.OutDegree(2));
            Assert.AreEqual(1, graph.OutEdges(1).Count());
            Assert.AreEqual(1, graph.InEdges(2).Count());

            // e12 must be present in u, because we added it.
            Assert.IsTrue(graph.ContainsEdge(e12));

            // f12 is also in u, because e12 == f12.
            Assert.IsTrue(graph.ContainsEdge(f12));

            // e21 and f21 are not in u, because it's a directed graph.
            if (e21 != null)
            {
                Assert.IsFalse(graph.ContainsEdge(e21));
            }
            if (f21 != null)
            {
                Assert.IsFalse(graph.ContainsEdge(f21));
            }

            // There must be an edge between vertices 1, 2.
            Assert.IsTrue(graph.ContainsEdge(1, 2));

            // No edge between vertices 2, 1, because the graph is directed.
            Assert.IsFalse(graph.ContainsEdge(2, 1));

            // ContainsEdge(1, 3) raises contracts violation in IncidenceGraphContract, because 3 is not in the graph.
            // obviously no edge between vertices 1, 3, as vertex 3 is not even present in the graph.
            // Assert.IsFalse(g.ContainsEdge(1, 3));
        }
Beispiel #13
0
        private CFGTaintInfo AnalyzeNode2(CFGBlock block, IBidirectionalGraph <CFGBlock, TaggedEdge <CFGBlock, EdgeTag> > graph)
        {
            var oldTaint = Taints[block];


            var predecessorsOut = graph.InEdges(block);
            var outTaints       = predecessorsOut.Select(p => new { EdgeType = p.Tag, Source = p.Source })
                                  .Where(s => Taints[s.Source].Out != null && Taints[s.Source].Out.Any())
                                  .Select(s => Taints[s.Source].Out[s.EdgeType.EdgeType])
                                  .Where(o => o != null);
            ImmutableVariableStorage newInTaint;

            if (outTaints.Any())
            {
                newInTaint = oldTaint.In.Merge(outTaints.Aggregate((current, next) => current.Merge(next)));
            }
            else
            {
                newInTaint = oldTaint.In;
            }



            ImmutableDictionary <EdgeType, ImmutableVariableStorage> newOutTaint;

            if (block.AstEntryNode == null)
            {
                newOutTaint = ImmutableDictionary <EdgeType, ImmutableVariableStorage> .Empty.Add(EdgeType.Normal, newInTaint);
            }
            else
            {
                var newOut             = _blockAnalyzer.Analyze(block.AstEntryNode, newInTaint);
                var newOutWithCondSani = _conditionTaintAnalyser.AnalyzeCond(block.AstEntryNode, newOut);

                newOutTaint = newOutWithCondSani.ToImmutableDictionary();
            }

            return(new CFGTaintInfo(newInTaint, newOutTaint));
        }
        public static void ContainsEdgeAssertions(IBidirectionalGraph<int, IEdge<int>> g,
            IEdge<int> e12,
            IEdge<int> f12,
            IEdge<int> e21,
            IEdge<int> f21)
        {
            Assert.AreEqual(0, g.InDegree(1));
            Assert.AreEqual(1, g.OutDegree(1));
            Assert.AreEqual(1, g.InDegree(2));
            Assert.AreEqual(0, g.OutDegree(2));
            Assert.AreEqual(1, g.OutEdges(1).Count());
            Assert.AreEqual(1, g.InEdges(2).Count());

            // e12 must be present in u, because we added it.
            Assert.IsTrue(g.ContainsEdge(e12));

            // f12 is also in u, because e12 == f12.
            Assert.IsTrue(g.ContainsEdge(f12));

            // e21 and f21 are not in u, because it's a directed graph.
            if (e21 != null) Assert.IsFalse(g.ContainsEdge(e21));
            if (f21 != null) Assert.IsFalse(g.ContainsEdge(f21));

            // there must be an edge between vertices 1, 2.
            Assert.IsTrue(g.ContainsEdge(1, 2));

            // no edge between vertices 2, 1, because the graph is directed.
            Assert.IsFalse(g.ContainsEdge(2, 1));

            // ContainsEdge(1, 3) raises contracts violation in IIncidenceGraphContract, because 3 is not in the graph.
            // obviously no edge between vertices 1, 3, as vertex 3 is not even present in the graph.
            // Assert.IsFalse(g.ContainsEdge(1, 3));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="g"></param>
        /// <param name="assg"></param>
        public void Transform(IBidirectionalGraph g, IMutableVertexAndEdgeListGraph assg)
        {
            VertexCollection avs = new VertexCollection();
            // adding vertices
            foreach(IEdge e in g.Edges)
            {
                // xi_-(L) = g(xi_-(0), xi_+(L))
                CharacteristicVertex avm = (CharacteristicVertex)assg.AddVertex();
                avm.IncomingEdge = e;
                avm.Vertex = e.Target;
                avs.Add(avm);

                // xi_+(0) = g(xi_-(0), xi_+(L))
                CharacteristicVertex avp = (CharacteristicVertex)assg.AddVertex();
                avp.IncomingEdge = e;
                avp.Vertex = e.Source;
                avs.Add(avp);
            }

            // adding out edges
            foreach(CharacteristicVertex av in avs)
            {
                foreach(IEdge e in g.OutEdges(av.Vertex))
                {
                    // find target vertex:
                    CharacteristicVertex avtarget = FindTargetVertex(e);
                    // add xi_-
                    CharacteristicEdge aem = (CharacteristicEdge)assg.AddEdge(av,avtarget);
                    aem.Positive = false;
                    aem.Edge = e;
                }
                foreach(IEdge e in g.InEdges(av.Vertex))
                {
                    // find target vertex:
                    CharacteristicVertex avtarget = FindTargetVertex(e);
                    // add xi_-
                    CharacteristicEdge aem = (CharacteristicEdge)assg.AddEdge(av,avtarget);
                    aem.Positive = true;
                    aem.Edge = e;
                }
            }
        }
Beispiel #16
0
 public IEnumerable <TaggedEdge <CFGBlock, EdgeTag> > NextEdges(IBidirectionalGraph <CFGBlock, TaggedEdge <CFGBlock, EdgeTag> > graph, CFGBlock block)
 {
     return(graph.InEdges(block));
 }
Beispiel #17
0
 public static IEnumerable <TVertex> GetInNeighbours <TVertex, TEdge>(
     this IBidirectionalGraph <TVertex, TEdge> graph, TVertex vertex)
     where TEdge : IEdge <TVertex>
 {
     return(graph.InEdges(vertex).Select(e => e.Source).Distinct());
 }
Beispiel #18
0
        private CFGTaintInfo AnalyzeNode2(CFGBlock block, IBidirectionalGraph<CFGBlock, TaggedEdge<CFGBlock, EdgeTag>> graph)
        {
            var oldTaint = Taints[block];

            var predecessorsOut = graph.InEdges(block);
            var outTaints = predecessorsOut.Select(p => new { EdgeType = p.Tag, Source = p.Source })
                                           .Where(s => Taints[s.Source].Out != null && Taints[s.Source].Out.Any())
                                           .Select(s => Taints[s.Source].Out[s.EdgeType.EdgeType])
                                           .Where(o => o != null);
            ImmutableVariableStorage newInTaint;
            if (outTaints.Any())
            {
                newInTaint = oldTaint.In.Merge(outTaints.Aggregate((current, next) => current.Merge(next)));
            }
            else
            {
                newInTaint = oldTaint.In;
            }

            ImmutableDictionary<EdgeType, ImmutableVariableStorage> newOutTaint;

            if (block.AstEntryNode == null)
            {
                newOutTaint = ImmutableDictionary<EdgeType, ImmutableVariableStorage>.Empty.Add(EdgeType.Normal, newInTaint);
            }
            else
            {
                var newOut = _blockAnalyzer.Analyze(block.AstEntryNode, newInTaint);
                var newOutWithCondSani = _conditionTaintAnalyser.AnalyzeCond(block.AstEntryNode, newOut);

                newOutTaint = newOutWithCondSani.ToImmutableDictionary();
            }

            return new CFGTaintInfo(newInTaint, newOutTaint);
        }
Beispiel #19
0
 public static IEnumerable <TEdge> GetAllEdges <TVertex, TEdge>(this IBidirectionalGraph <TVertex, TEdge> graph,
                                                                TVertex vertex)
     where TEdge : IEdge <TVertex>
 {
     return(graph.InEdges(vertex).Union(graph.OutEdges(vertex)));
 }
Beispiel #20
0
 public IEnumerable<TaggedEdge<CFGBlock, EdgeTag>> NextEdges(IBidirectionalGraph<CFGBlock, TaggedEdge<CFGBlock, EdgeTag>> graph, CFGBlock block)
 {
     return graph.InEdges(block);
 }