public static CompressedSparseRowGraph <TVertex> FromGraph <TEdge>(
            [NotNull] IVertexAndEdgeListGraph <TVertex, TEdge> visitedGraph)
            where TEdge : IEdge <TVertex>
        {
            if (visitedGraph is null)
            {
                throw new ArgumentNullException(nameof(visitedGraph));
            }

            var outEdgeStartRanges = new Dictionary <TVertex, Range>(visitedGraph.VertexCount);
            var outEdges           = new TVertex[visitedGraph.EdgeCount];

            int start = 0;
            int index = 0;

            foreach (TVertex vertex in visitedGraph.Vertices)
            {
                int end   = start + visitedGraph.OutDegree(vertex);
                var range = new Range(start, end);
                outEdgeStartRanges.Add(vertex, range);

                foreach (TEdge edge in visitedGraph.OutEdges(vertex))
                {
                    outEdges[index++] = edge.Target;
                }

                start = end;
                Debug.Assert(index == end);
            }

            Debug.Assert(index == outEdges.Length);

            return(new CompressedSparseRowGraph <TVertex>(outEdgeStartRanges, outEdges));
        }
        public static CompressedSparseRowGraph <TVertex> FromGraph <TEdge>(
            IVertexAndEdgeListGraph <TVertex, TEdge> visitedGraph
            )
            where TEdge : IEdge <TVertex>
        {
            Contract.Requires(visitedGraph != null);
            Contract.Ensures(Contract.Result <CompressedSparseRowGraph <TVertex> >() != null);

            var outEdgeStartRanges = new Dictionary <TVertex, Range>(visitedGraph.VertexCount);
            var outEdges           = new TVertex[visitedGraph.EdgeCount];

            int start = 0;
            int end   = 0;
            int index = 0;

            foreach (var vertex in visitedGraph.Vertices)
            {
                end = index + visitedGraph.OutDegree(vertex);
                var range = new Range(start, end);
                outEdgeStartRanges.Add(vertex, range);
                foreach (var edge in visitedGraph.OutEdges(vertex))
                {
                    outEdges[index++] = edge.Target;
                }
                Contract.Assert(index == end);
            }
            Contract.Assert(index == outEdges.Length);

            return(new CompressedSparseRowGraph <TVertex>(
                       outEdgeStartRanges,
                       outEdges));
        }
示例#3
0
 public static IEnumerable <TVertex> GetOutNeighbors <TVertex, TEdge>(
     [NotNull] this IVertexAndEdgeListGraph <TVertex, TEdge> graph,
     [NotNull] TVertex vertex)
     where TEdge : IEdge <TVertex>
 {
     return(graph.OutEdges(vertex).Select(e => e.Target).Distinct());
 }
示例#4
0
        public static IEnumerable <TEdge> GetEdgesBetween <TVertex, TEdge>(
            [NotNull] this IVertexAndEdgeListGraph <TVertex, TEdge> graph,
            [NotNull, ItemNotNull] TVertex[] set1,
            [NotNull, ItemNotNull] TVertex[] set2)
            where TEdge : IEdge <TVertex>
        {
            if (graph is null)
            {
                throw new ArgumentNullException(nameof(graph));
            }
            if (set1 is null)
            {
                throw new ArgumentNullException(nameof(set1));
            }
            if (set2 is null)
            {
                throw new ArgumentNullException(nameof(set2));
            }

            foreach (TVertex vertex in set1)
            {
                foreach (TEdge edge in graph.OutEdges(vertex))
                {
                    if (set2.Contains(edge.Target))
                    {
                        yield return(edge);
                    }
                }
            }
        }
示例#5
0
        private static void SameOutEdges <TVertex, TEdge>([NotNull] IVertexAndEdgeListGraph <TVertex, TEdge> graph)
            where TEdge : IEdge <TVertex>
        {
            var adjacencyGraph = graph.ToArrayAdjacencyGraph();

            foreach (TVertex vertex in graph.Vertices)
            {
                CollectionAssert.AreEqual(graph.OutEdges(vertex), adjacencyGraph.OutEdges(vertex));
            }
        }
        public void SameOutEdges <TVertex, TEdge>([PexAssumeNotNull] IVertexAndEdgeListGraph <TVertex, TEdge> g)
            where TEdge : IEdge <TVertex>
        {
            var ag = GraphExtensions.ToArrayAdjacencyGraph(g);

            foreach (var v in g.Vertices)
            {
                PexAssert.AreElementsEqual(g.OutEdges(v), ag.OutEdges(v), (l, r) => l.Equals(r));
            }
        }
示例#7
0
 public static void AnalyzeDependencies <TVertex, TEdge>(IVertexAndEdgeListGraph <TVertex, TEdge> graph, TVertex root, HashSet <TVertex> dependencies) where TEdge : IEdge <TVertex>
 {
     if (dependencies.Contains(root))
     {
         return;
     }
     dependencies.Add(root);
     foreach (var item in graph.OutEdges(root).Select(x => x.Target))
     {
         AnalyzeDependencies(graph, item, dependencies);
     }
 }
        public static ISet <TVertex> FindReachable <TVertex, TEdge>(this IVertexAndEdgeListGraph <TVertex, TEdge> graph,
                                                                    IEnumerable <TVertex> source,
                                                                    IEqualityComparer <TVertex> comparer = null)
            where TEdge : IEdge <TVertex>
        {
            var availableSources = source.Intersect(graph.Vertices, comparer);
            var processed        = availableSources
                                   .Traverse(x => graph.OutEdges(x).Select(y => y.Target))
                                   .ToHashSet(comparer);

            return(processed);
        }
示例#9
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="g"></param>
 public void DisplayGraph(IVertexAndEdgeListGraph g)
 {
     foreach (IVertex v in g.Vertices)
     {
         Console.Write("{0}: ", v.ID);
         foreach (IEdge e in g.OutEdges(v))
         {
             Console.Write("{0} ", e.ID);
         }
         Console.WriteLine();
     }
 }
        /// <summary>
        /// If the graph g is directed, then returns every edges which source is one of the vertices in the <code>set1</code>
        /// and the target is one of the vertices in <code>set2</code>.
        /// </summary>
        /// <typeparam name="TVertex">Type of the vertex.</typeparam>
        /// <typeparam name="TEdge">Type of the edge.</typeparam>
        /// <param name="g">The graph.</param>
        /// <param name="set1"></param>
        /// <param name="set2"></param>
        /// <returns>Return the list of the selected edges.</returns>
        public static IEnumerable <TEdge> GetEdgesBetween <TVertex, TEdge>(this IVertexAndEdgeListGraph <TVertex, TEdge> g, List <TVertex> set1, List <TVertex> set2)
            where TEdge : IEdge <TVertex>
        {
            var edgesBetween = new List <TEdge>();

            //vegig kell menni az osszes vertex minden elen, es megnezni, hogy a target hol van
            foreach (var v in set1)
            {
                edgesBetween.AddRange(g.OutEdges(v).Where(edge => set2.Contains(edge.Target)));
            }

            return(edgesBetween);
        }
示例#11
0
 public ArrayAdjacencyGraph(
     IVertexAndEdgeListGraph <TVertex, TEdge> visitedGraph
     )
 {
     Contract.Requires(visitedGraph != null);
     this.vertexOutEdges = new Dictionary <TVertex, TEdge[]>(visitedGraph.VertexCount);
     this.edgeCount      = visitedGraph.EdgeCount;
     foreach (var vertex in visitedGraph.Vertices)
     {
         var outEdges = new List <TEdge>(visitedGraph.OutEdges(vertex));
         this.vertexOutEdges.Add(vertex, outEdges.ToArray());
     }
 }
        private static void AssertSameProperties <TVertex, TEdge>(IVertexAndEdgeListGraph <TVertex, TEdge> graph)
            where TEdge : IEdge <TVertex>
        {
            ArrayAdjacencyGraph <TVertex, TEdge> adjacencyGraph = graph.ToArrayAdjacencyGraph();

            Assert.AreEqual(graph.VertexCount, adjacencyGraph.VertexCount);
            CollectionAssert.AreEqual(graph.Vertices, adjacencyGraph.Vertices);

            Assert.AreEqual(graph.EdgeCount, adjacencyGraph.EdgeCount);
            CollectionAssert.AreEqual(graph.Edges, adjacencyGraph.Edges);

            foreach (TVertex vertex in graph.Vertices)
            {
                CollectionAssert.AreEqual(graph.OutEdges(vertex), adjacencyGraph.OutEdges(vertex));
            }
        }
示例#13
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ArrayAdjacencyGraph{TVertex,TEdge}"/> class.
        /// </summary>
        /// <param name="visitedGraph">Graph to visit.</param>
        public ArrayAdjacencyGraph([NotNull] IVertexAndEdgeListGraph <TVertex, TEdge> visitedGraph)
        {
            if (visitedGraph is null)
            {
                throw new ArgumentNullException(nameof(visitedGraph));
            }

            _vertexOutEdges = new Dictionary <TVertex, TEdge[]>(visitedGraph.VertexCount);
            EdgeCount       = visitedGraph.EdgeCount;
            foreach (TVertex vertex in visitedGraph.Vertices)
            {
                _vertexOutEdges.Add(
                    vertex,
                    visitedGraph.OutEdges(vertex).ToArray());
            }
        }
示例#14
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ArrayAdjacencyGraph{TVertex,TEdge}"/> class.
        /// </summary>
        /// <param name="baseGraph">Wrapped graph.</param>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="baseGraph"/> is <see langword="null"/>.</exception>
        public ArrayAdjacencyGraph([NotNull] IVertexAndEdgeListGraph <TVertex, TEdge> baseGraph)
        {
            if (baseGraph is null)
            {
                throw new ArgumentNullException(nameof(baseGraph));
            }

            AllowParallelEdges = baseGraph.AllowParallelEdges;
            _vertexOutEdges    = new Dictionary <TVertex, TEdge[]>(baseGraph.VertexCount);
            EdgeCount          = baseGraph.EdgeCount;
            foreach (TVertex vertex in baseGraph.Vertices)
            {
                _vertexOutEdges.Add(
                    vertex,
                    baseGraph.OutEdges(vertex).ToArray());
            }
        }
示例#15
0
        /// <summary>
        /// If the graph g is directed, then returns every edges which source is one of the vertices in the <code>set1</code>
        /// and the target is one of the vertices in <code>set2</code>.
        /// </summary>
        /// <typeparam name="TVertex">Type of the vertex.</typeparam>
        /// <typeparam name="TEdge">Type of the edge.</typeparam>
        /// <param name="g">The graph.</param>
        /// <param name="set1"></param>
        /// <param name="set2"></param>
        /// <param name="undirected">Assume undirected graph - get both in and out edges</param>
        /// <returns>Return the list of the selected edges.</returns>
        public static IEnumerable <TEdge> GetEdgesBetween <TVertex, TEdge>(this IVertexAndEdgeListGraph <TVertex, TEdge> g, List <TVertex> set1, List <TVertex> set2, bool undirected = false)
            where TEdge : IEdge <TVertex>
            where TVertex : class
        {
            var edgesBetween = new List <TEdge>();

            //vegig kell menni az osszes vertex minden elen, es megnezni, hogy a target hol van
            foreach (var v in set1)
            {
                edgesBetween.AddRange(g.OutEdges(v).Where(edge => set2.Contains(edge.Target)));
                if (undirected)
                {
                    edgesBetween.AddRange(g.Edges.Where(a => a.Target == v).Where(edge => set2.Contains(edge.Source)));
                }
            }

            return(edgesBetween);
        }
示例#16
0
        public static string WriteGraphvizScript <TVertex, TEdge>(IVertexAndEdgeListGraph <TVertex, TEdge> graph, string name) where TEdge : IEdge <TVertex>
        {
            var script = new StringBuilder();

            script.Append(graph.IsDirected ? "digraph" : "graph")
            .Append(" ")
            .Append(name)
            .AppendLine("{");
            foreach (var vertex in graph.Vertices)
            {
                script.Append("\"")
                .Append(vertex)
                .Append("\"")
                .Append(graph.IsDirected ? "->" : "--")
                .Append("{")
                .Append(string.Join(" ", graph.OutEdges(vertex).Select(x => $"\"{x.Target}\"")))
                .Append("}")
                .AppendLine();
            }
            script.AppendLine("}");
            return(script.ToString());
        }
示例#17
0
 public static IEnumerable <TVertex> GetOutNeighbours <TVertex, TEdge>(this IVertexAndEdgeListGraph <TVertex, TEdge> g, TVertex vertex)
     where TEdge : IEdge <TVertex>
 {
     return((from e in g.OutEdges(vertex)
             select e.Target).Distinct());
 }
 /// <inheritdoc />
 public IEnumerable <TEdge> OutEdges(TVertex vertex)
 {
     return(_baseGraph.OutEdges(vertex));
 }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="g"></param>
 public void DisplayGraph(IVertexAndEdgeListGraph g)
 {
     foreach(IVertex v in g.Vertices)
     {
         Console.Write("{0}: ",v.ID);
         foreach(IEdge e in g.OutEdges(v))
             Console.Write("{0} ",e.ID);
         Console.WriteLine();
     }
 }