public void PageRank() { var graph = new BidirectionalGraph <string, Edge <string> >(); graph.AddVerticesAndEdgeRange(new[] { new Edge <string>("Amazon", "Twitter"), new Edge <string>("Amazon", "Microsoft"), new Edge <string>("Microsoft", "Amazon"), new Edge <string>("Microsoft", "Facebook"), new Edge <string>("Microsoft", "Twitter"), new Edge <string>("Microsoft", "Apple"), new Edge <string>("Facebook", "Amazon"), new Edge <string>("Facebook", "Twitter"), new Edge <string>("Twitter", "Microsoft"), new Edge <string>("Apple", "Twitter") }); var algorithm = new PageRankAlgorithm <string, Edge <string> >(graph); algorithm.Compute(); IEnumerable <string> order = algorithm.Ranks.OrderByDescending(pair => pair.Value).Select(pair => pair.Key); CollectionAssert.AreEqual( new[] { "Microsoft", "Twitter", "Amazon", "Facebook", "Apple" }, order); Assert.Positive(algorithm.GetRanksSum()); double rankSum = algorithm.Ranks.Sum(pair => pair.Value); Assert.AreEqual(rankSum, algorithm.GetRanksSum()); Assert.Positive(algorithm.GetRanksSum()); Assert.AreEqual(rankSum / graph.VertexCount, algorithm.GetRanksMean()); }
public void SmallTest() { var g1 = new BidirectionalGraph <int, Edge <int> >(); var g2 = new BidirectionalGraph <int, Edge <int> >(); g1.AddVerticesAndEdgeRange(new[] { new Edge <int>(1, 2), new Edge <int>(1, 3), new Edge <int>(2, 4) }); g2.AddVerticesAndEdgeRange(new[] { new Edge <int>(1, 2), new Edge <int>(1, 3), new Edge <int>(2, 4) }); var dictionary = new Dictionary <Tuple <int, int>, double>(); foreach (var i in g1.Vertices) { foreach (var j in g2.Vertices) { if (i == j) { dictionary[Tuple.Create(i, j)] = 1.0; } else { dictionary[Tuple.Create(i, j)] = 0.0; } } } var algo = new MaxCardinality <int, Edge <int> >(g1, g2, dictionary, 0.5, (u, v) => new Edge <int>(u, v)); var res = algo.compMaxCardinality(); var e = Enumerable.Range(1, 4).Select(x => Tuple.Create(x, x)); var correctResult = SetModule.OfSeq(e); Assert.AreEqual(res, correctResult); }
public void SimpleGraph() { var graph = new BidirectionalGraph <int, Edge <int> >(); graph.AddVerticesAndEdgeRange(new[] { new Edge <int>(1, 2), new Edge <int>(2, 3), new Edge <int>(2, 6), new Edge <int>(2, 8), new Edge <int>(4, 2), new Edge <int>(4, 5), new Edge <int>(5, 6), new Edge <int>(7, 5), new Edge <int>(7, 8) }); var algorithm = new SourceFirstBidirectionalTopologicalSortAlgorithm <int, Edge <int> >(graph); algorithm.Compute(); CollectionAssert.AreEqual( new[] { 1, 7, 4, 2, 5, 8, 3, 6 }, algorithm.SortedVertices); algorithm = new SourceFirstBidirectionalTopologicalSortAlgorithm <int, Edge <int> >(graph, TopologicalSortDirection.Backward); algorithm.Compute(); CollectionAssert.AreEqual( new[] { 3, 6, 8, 5, 2, 7, 1, 4 }, algorithm.SortedVertices); }
public void ForestGraph() { var graph = new BidirectionalGraph <int, Edge <int> >(); graph.AddVerticesAndEdgeRange(new[] { new Edge <int>(0, 1), new Edge <int>(1, 2), new Edge <int>(1, 3), new Edge <int>(2, 3), new Edge <int>(3, 4), new Edge <int>(5, 6) }); var algorithm = new SourceFirstBidirectionalTopologicalSortAlgorithm <int, Edge <int> >(graph); algorithm.Compute(); CollectionAssert.AreEqual( new[] { 0, 5, 1, 6, 2, 3, 4 }, algorithm.SortedVertices); algorithm = new SourceFirstBidirectionalTopologicalSortAlgorithm <int, Edge <int> >(graph, TopologicalSortDirection.Backward); algorithm.Compute(); CollectionAssert.AreEqual( new[] { 4, 6, 3, 5, 2, 1, 0 }, algorithm.SortedVertices); }
public void SmallTest() { var g1 = new BidirectionalGraph<int, Edge<int>>(); var g2 = new BidirectionalGraph<int, Edge<int>>(); g1.AddVerticesAndEdgeRange(new[] {new Edge<int>(1, 2), new Edge<int>(1, 3), new Edge<int>(2, 4)}); g2.AddVerticesAndEdgeRange(new[] {new Edge<int>(1, 2), new Edge<int>(1, 3), new Edge<int>(2, 4)}); var dictionary = new Dictionary<Tuple<int, int>, double>(); foreach (var i in g1.Vertices) { foreach (var j in g2.Vertices) { if (i == j) dictionary[Tuple.Create(i, j)] = 1.0; else dictionary[Tuple.Create(i, j)] = 0.0; } } var algo = new MaxCardinality<int, Edge<int>>(g1, g2, dictionary, 0.5, (u, v) => new Edge<int>(u, v)); var res = algo.compMaxCardinality(); var e = Enumerable.Range(1, 4).Select(x => Tuple.Create(x, x)); var correctResult = SetModule.OfSeq(e); Assert.AreEqual(res, correctResult); }
public void Test() { var graph = new BidirectionalGraph<int, Edge<int>>(); graph.AddVerticesAndEdgeRange(new[] { new Edge<int>(1, 2) , new Edge<int>(2, 3), new Edge<int>(3, 4), new Edge<int>(3, 5) }); var result = graph.ComputeTransitiveClosure((u, v) => new Edge<int>(u, v)); Assert.AreEqual(9, result.EdgeCount); }
public static void edges(this BidirectionalGraph <object, IEdge <object> > bidirectionalGraph, List <IEdge <object> > edges) { try { bidirectionalGraph.AddVerticesAndEdgeRange(edges.ToArray()); } catch (System.Exception ex) { ex.log("in edges"); } }
public void Test() { var graph = new BidirectionalGraph <int, Edge <int> >(); graph.AddVerticesAndEdgeRange(new[] { new Edge <int>(1, 2), new Edge <int>(2, 3), new Edge <int>(3, 4), new Edge <int>(3, 5) }); var result = graph.ComputeTransitiveClosure((u, v) => new Edge <int>(u, v)); Assert.AreEqual(9, result.EdgeCount); }
public static BidirectionalGraph <TVertex, TEdge> CopyToBidirectionalGraph <TVertex, TEdge>( this IVertexAndEdgeListGraph <TVertex, TEdge> graph) where TEdge : IEdge <TVertex> { var newGraph = new BidirectionalGraph <TVertex, TEdge>(); //copy the vertices newGraph.AddVerticesAndEdgeRange(graph.Edges); return(newGraph); }
public void TransitiveReduction_ValueType() { // Test 1 var edge12 = new SEdge <int>(1, 2); var edge13 = new SEdge <int>(1, 3); var edge14 = new SEdge <int>(1, 4); var edge15 = new SEdge <int>(1, 5); var edge24 = new SEdge <int>(2, 4); var edge34 = new SEdge <int>(3, 4); var edge35 = new SEdge <int>(3, 5); var edge45 = new SEdge <int>(4, 5); var graph = new BidirectionalGraph <int, SEdge <int> >(); graph.AddVerticesAndEdgeRange(new[] { edge12, edge13, edge14, edge15, edge24, edge34, edge35, edge45 }); BidirectionalGraph <int, SEdge <int> > result = graph.ComputeTransitiveReduction(); AssertHasVertices(result, new[] { 1, 2, 3, 4, 5 }); AssertHasEdges( result, new[] { edge12, edge13, edge24, edge34, edge45 }); Assert.AreEqual(5, result.EdgeCount); // Test 2 var edge01 = new SEdge <int>(0, 1); var edge02 = new SEdge <int>(0, 2); var edge03 = new SEdge <int>(0, 3); var edge23 = new SEdge <int>(2, 3); var edge25 = new SEdge <int>(2, 5); var edge65 = new SEdge <int>(6, 5); var edge67 = new SEdge <int>(6, 7); var edge74 = new SEdge <int>(7, 4); graph = new BidirectionalGraph <int, SEdge <int> >(); graph.AddVerticesAndEdgeRange(new[] { edge01, edge02, edge03, edge23, edge24, edge25, edge35, edge45, edge65, edge67, edge74 }); result = graph.ComputeTransitiveReduction(); AssertHasVertices(result, new[] { 0, 1, 2, 3, 4, 5, 6, 7 }); AssertHasEdges( result, new[] { edge01, edge02, edge23, edge24, edge35, edge45, edge67, edge74 }); }
public static BidirectionalGraph <TVertex, TEdge> CopyToBidirectionalGraph <TVertex, TEdge>( this IVertexAndEdgeListGraph <TVertex, TEdge> graph) where TEdge : IEdge <TVertex> { Contract.Requires(graph != null); Contract.Ensures(Contract.Result <BidirectionalGraph <TVertex, TEdge> >() != null); var newGraph = new BidirectionalGraph <TVertex, TEdge>(); //copy the vertices newGraph.AddVerticesAndEdgeRange(graph.Edges); return(newGraph); }
public void TransitiveClosure_ReferenceType() { // Test 1 var graph = new BidirectionalGraph <int, EquatableEdge <int> >(); graph.AddVerticesAndEdgeRange(new[] { new EquatableEdge <int>(1, 2), new EquatableEdge <int>(2, 3) }); BidirectionalGraph <int, EquatableEdge <int> > result = graph.ComputeTransitiveClosure((u, v) => new EquatableEdge <int>(u, v)); AssertHasVertices(result, new[] { 1, 2, 3 }); AssertHasEdges( result, new[] { new EquatableEdge <int>(1, 2), new EquatableEdge <int>(1, 3), new EquatableEdge <int>(2, 3) }); // Test 2 graph = new BidirectionalGraph <int, EquatableEdge <int> >(); graph.AddVerticesAndEdgeRange(new[] { new EquatableEdge <int>(1, 2), new EquatableEdge <int>(2, 3), new EquatableEdge <int>(3, 4), new EquatableEdge <int>(3, 5) }); result = graph.ComputeTransitiveClosure((u, v) => new EquatableEdge <int>(u, v)); AssertHasVertices(result, new[] { 1, 2, 3, 4, 5 }); AssertHasEdges( result, new[] { new EquatableEdge <int>(1, 2), new EquatableEdge <int>(1, 3), new EquatableEdge <int>(1, 4), new EquatableEdge <int>(1, 5), new EquatableEdge <int>(2, 3), new EquatableEdge <int>(2, 4), new EquatableEdge <int>(2, 5), new EquatableEdge <int>(3, 4), new EquatableEdge <int>(3, 5) }); }
public void SmallTest() { var graph = new BidirectionalGraph<int, Edge<int>>(); graph.AddVerticesAndEdgeRange(new[] { new Edge<int>(1, 2), new Edge<int>(1, 3), new Edge<int>(1, 4), new Edge<int>(1, 5), new Edge<int>(2, 4), new Edge<int>(3, 4), new Edge<int>(3, 5), new Edge<int>(4, 5) }); var result = graph.ComputeTransitiveReduction(); Assert.AreEqual(5, result.EdgeCount); }
public void SmallTest() { var graph = new BidirectionalGraph <int, Edge <int> >(); graph.AddVerticesAndEdgeRange(new[] { new Edge <int>(1, 2), new Edge <int>(1, 3), new Edge <int>(1, 4), new Edge <int>(1, 5), new Edge <int>(2, 4), new Edge <int>(3, 4), new Edge <int>(3, 5), new Edge <int>(4, 5) }); var result = graph.ComputeTransitiveReduction(); Assert.AreEqual(5, result.EdgeCount); }
public static BidirectionalGraph<object, IEdge<object>> testGraph() { var bidirectionalGraph = new BidirectionalGraph<object, IEdge<object>>(); var vertices = new object[] { "A", "B", "C", "D", "E", "F" }; var edges = new IEdge<object>[] { new Edge<object>(vertices[0], vertices[1]), new Edge<object>(vertices[1], vertices[2]), new Edge<object>(vertices[1], vertices[3]), new Edge<object>(vertices[3], vertices[4]), new Edge<object>(vertices[0], vertices[4]), new Edge<object>(vertices[4], vertices[5]) }; bidirectionalGraph.AddVerticesAndEdgeRange(edges); return bidirectionalGraph; }
public void TrimEdgeExcess() { var graph = new BidirectionalGraph <int, Edge <int> >(true, 12) { EdgeCapacity = 50 }; graph.AddVerticesAndEdgeRange(new[] { new Edge <int>(1, 2), new Edge <int>(1, 3), new Edge <int>(1, 4) }); Assert.DoesNotThrow(() => graph.TrimEdgeExcess()); }
public void NonAcyclic() { var graph = new BidirectionalGraph <int, Edge <int> >(); graph.AddVerticesAndEdgeRange(new[] { new Edge <int>(1, 2), new Edge <int>(2, 3), new Edge <int>(3, 1), new Edge <int>(4, 1) }); var algorithm = new LayeredTopologicalSortAlgorithm <int, Edge <int> >(graph); Assert.Throws <NonAcyclicGraphException>(() => algorithm.Compute()); }
public void SourceFirstBidirectionalTopologicalSort_Throws() { var cyclicGraph = new BidirectionalGraph <int, Edge <int> >(); cyclicGraph.AddVerticesAndEdgeRange(new[] { new Edge <int>(1, 2), new Edge <int>(2, 3), new Edge <int>(1, 4), new Edge <int>(3, 1) }); var algorithm = new SourceFirstBidirectionalTopologicalSortAlgorithm <int, Edge <int> >(cyclicGraph); Assert.Throws <NonAcyclicGraphException>(() => algorithm.Compute()); }
public static BidirectionalDepthFirstSearchAlgorithm <int, Edge <int> > CreateAlgorithmAndMaybeDoComputation( [NotNull] ContractScenario scenario) { var graph = new BidirectionalGraph <int, Edge <int> >(); graph.AddVerticesAndEdgeRange(scenario.EdgesInGraph.Select(e => new Edge <int>(e.Source, e.Target))); graph.AddVertexRange(scenario.SingleVerticesInGraph); var algorithm = new BidirectionalDepthFirstSearchAlgorithm <int, Edge <int> >(graph); if (scenario.DoComputation) { algorithm.Compute(scenario.Root); } return(algorithm); }
public static BidirectionalGraph <object, IEdge <object> > testGraph() { var bidirectionalGraph = new BidirectionalGraph <object, IEdge <object> >(); var vertices = new object[] { "A", "B", "C", "D", "E", "F" }; var edges = new IEdge <object>[] { new Edge <object>(vertices[0], vertices[1]), new Edge <object>(vertices[1], vertices[2]), new Edge <object>(vertices[1], vertices[3]), new Edge <object>(vertices[3], vertices[4]), new Edge <object>(vertices[0], vertices[4]), new Edge <object>(vertices[4], vertices[5]) }; bidirectionalGraph.AddVerticesAndEdgeRange(edges); return(bidirectionalGraph); }
protected override void InternalCompute() { // Clone the visited graph transitiveReduction.AddVerticesAndEdgeRange(this.VisitedGraph.Edges); // Get the topological sorted graph var topoSort = this.VisitedGraph.TopologicalSort(); // Iterate in topo order, track indirect ancestors and remove edges from them to the visited vertex var ancestorsOfVertices = new Dictionary <TVertex, HashSet <TVertex> >(); foreach (var vertexId in this.VisitedGraph.TopologicalSort()) { var thisVertexPredecessors = new List <TVertex>(); var thisVertexAncestors = new HashSet <TVertex>(); ancestorsOfVertices[vertexId] = thisVertexAncestors; // Get indirect ancestors foreach (var inEdge in this.VisitedGraph.InEdges(vertexId)) { var predecessor = inEdge.Source; thisVertexPredecessors.Add(predecessor); // Add all the ancestors of the predeccessors foreach (var ancestorId in ancestorsOfVertices[predecessor]) { thisVertexAncestors.Add(ancestorId); } } // Remove indirect edges foreach (var indirectAncestor in thisVertexAncestors) { Edge foundIndirectEdge; if (transitiveReduction.TryGetEdge(indirectAncestor, vertexId, out foundIndirectEdge)) { transitiveReduction.RemoveEdge(foundIndirectEdge); } } // Add predecessors to ancestors list foreach (var pred in thisVertexPredecessors) { thisVertexAncestors.Add(pred); } } }
public void Construction() { var wrappedGraph = new BidirectionalGraph <int, Edge <int> >(); var graph = new ArrayBidirectionalGraph <int, Edge <int> >(wrappedGraph); AssertGraphProperties(graph); AssertEmptyGraph(graph); wrappedGraph.AddVertexRange(new[] { 2, 3, 1 }); graph = new ArrayBidirectionalGraph <int, Edge <int> >(wrappedGraph); AssertGraphProperties(graph); AssertHasVertices(graph, new[] { 1, 2, 3 }); AssertNoEdge(graph); var edge1 = new Edge <int>(1, 2); var edge2 = new Edge <int>(2, 2); var edge3 = new Edge <int>(3, 4); var edge4 = new Edge <int>(1, 4); wrappedGraph.AddVerticesAndEdgeRange(new[] { edge1, edge2, edge3, edge4 }); graph = new ArrayBidirectionalGraph <int, Edge <int> >(wrappedGraph); AssertGraphProperties(graph); AssertHasVertices(graph, new[] { 1, 2, 3, 4 }); AssertHasEdges(graph, new[] { edge1, edge2, edge3, edge4 }); wrappedGraph = new BidirectionalGraph <int, Edge <int> >(false); wrappedGraph.AddVerticesAndEdgeRange(new[] { edge1, edge1, edge2, edge3, edge4 }); graph = new ArrayBidirectionalGraph <int, Edge <int> >(wrappedGraph); AssertGraphProperties(graph, false); AssertHasVertices(graph, new[] { 1, 2, 3, 4 }); AssertHasEdges(graph, new[] { edge1, edge2, edge3, edge4 }); #region Local function void AssertGraphProperties <TVertex, TEdge>( ArrayBidirectionalGraph <TVertex, TEdge> g, bool allowParallelEdges = true) where TEdge : IEdge <TVertex> { Assert.IsTrue(g.IsDirected); Assert.AreEqual(allowParallelEdges, g.AllowParallelEdges); } #endregion }
public static TSP <T, EquatableEdge <T>, BidirectionalGraph <T, EquatableEdge <T> > > CreateAlgorithmAndMaybeDoComputation <T>( [NotNull] ContractScenario <T> scenario) { var graph = new BidirectionalGraph <T, EquatableEdge <T> >(); graph.AddVerticesAndEdgeRange(scenario.EdgesInGraph.Select(e => new EquatableEdge <T>(e.Source, e.Target))); graph.AddVertexRange(scenario.SingleVerticesInGraph); double Weights(Edge <T> e) => 1.0; var algorithm = new TSP <T, EquatableEdge <T>, BidirectionalGraph <T, EquatableEdge <T> > >(graph, Weights); if (scenario.DoComputation) { algorithm.Compute(scenario.Root); } return(algorithm); }
public void EdgeCondensationSomeVertices() { var edge12 = new Edge <int>(1, 2); var edge13 = new Edge <int>(1, 3); var edge23 = new Edge <int>(2, 3); var edge38 = new Edge <int>(3, 8); var edge42 = new Edge <int>(4, 2); var edge43 = new Edge <int>(4, 3); var edge44 = new Edge <int>(4, 4); var edge45 = new Edge <int>(4, 5); var edge56 = new Edge <int>(5, 6); var edge57 = new Edge <int>(5, 7); var edge76 = new Edge <int>(7, 6); var edge71 = new Edge <int>(7, 1); var edge89 = new Edge <int>(8, 9); var edge82 = new Edge <int>(8, 2); var graph = new BidirectionalGraph <int, Edge <int> >(); graph.AddVerticesAndEdgeRange(new[] { edge12, edge13, edge23, edge38, edge42, edge43, edge44, edge45, edge56, edge57, edge76, edge71, edge89, edge82 }); IMutableBidirectionalGraph <int, MergedEdge <int, Edge <int> > > condensedGraph = graph.CondensateEdges(v => v == 4 || v == 8); Assert.IsNotNull(condensedGraph); Assert.AreEqual(2, condensedGraph.VertexCount); Assert.AreEqual(6, condensedGraph.EdgeCount); CollectionAssert.AreEquivalent(new[] { 4, 8 }, condensedGraph.Vertices); CollectionAssert.AreEquivalent(new[] { edge82, edge23, edge38 }, condensedGraph.Edges.ElementAt(0).Edges); CollectionAssert.AreEquivalent(new[] { edge44 }, condensedGraph.Edges.ElementAt(1).Edges); CollectionAssert.AreEquivalent(new[] { edge43, edge38 }, condensedGraph.Edges.ElementAt(2).Edges); CollectionAssert.AreEquivalent(new[] { edge42, edge23, edge38 }, condensedGraph.Edges.ElementAt(3).Edges); CollectionAssert.AreEquivalent(new[] { edge45, edge57, edge71, edge13, edge38 }, condensedGraph.Edges.ElementAt(4).Edges); CollectionAssert.AreEquivalent(new[] { edge45, edge57, edge71, edge12, edge23, edge38 }, condensedGraph.Edges.ElementAt(5).Edges); }
public static BidirectionalGraph <TVertex, TEdge> CopyToBidirectionalGraph <TVertex, TEdge>( this IVertexAndEdgeListGraph <TVertex, TEdge> graph, bool includeEmpty = true) where TEdge : IEdge <TVertex> { var newGraph = new BidirectionalGraph <TVertex, TEdge>(); //copy the vertices if (!includeEmpty) { newGraph.AddVerticesAndEdgeRange(graph.Edges); } else { newGraph.AddVertexRange(graph.Vertices); newGraph.AddEdgeRange(graph.Edges); } return(newGraph); }
public void Clone() { var graph = new BidirectionalGraph <int, Edge <int> >(); AssertEmptyGraph(graph); var clonedGraph = graph.Clone(); Assert.IsNotNull(clonedGraph); AssertEmptyGraph(clonedGraph); clonedGraph = new BidirectionalGraph <int, Edge <int> >(graph); Assert.IsNotNull(clonedGraph); AssertEmptyGraph(clonedGraph); clonedGraph = (BidirectionalGraph <int, Edge <int> >)((ICloneable)graph).Clone(); Assert.IsNotNull(clonedGraph); AssertEmptyGraph(clonedGraph); var edge1 = new Edge <int>(1, 2); var edge2 = new Edge <int>(1, 3); var edge3 = new Edge <int>(2, 3); graph.AddVerticesAndEdgeRange(new[] { edge1, edge2, edge3 }); AssertHasVertices(graph, new[] { 1, 2, 3 }); AssertHasEdges(graph, new[] { edge1, edge2, edge3 }); clonedGraph = graph.Clone(); Assert.IsNotNull(clonedGraph); AssertHasVertices(clonedGraph, new[] { 1, 2, 3 }); AssertHasEdges(clonedGraph, new[] { edge1, edge2, edge3 }); clonedGraph = new BidirectionalGraph <int, Edge <int> >(graph); Assert.IsNotNull(clonedGraph); AssertHasVertices(clonedGraph, new[] { 1, 2, 3 }); AssertHasEdges(clonedGraph, new[] { edge1, edge2, edge3 }); clonedGraph = (BidirectionalGraph <int, Edge <int> >)((ICloneable)graph).Clone(); Assert.IsNotNull(clonedGraph); AssertHasVertices(clonedGraph, new[] { 1, 2, 3 }); AssertHasEdges(clonedGraph, new[] { edge1, edge2, edge3 }); }
//private Func<TK, TV> GetTypeMapIterator<TK, TV>(IDictionary<TK, TV> dict) //{ // Func<TK, TV> nextLevelFunc = t => dict.GetValue(t) ?? new HashSet<TV>(comparer); // return nextLevelFunc; //} #endregion #region Graph Processing public IMutableBidirectionalGraph <string, TaggedEdge <string, List <Relation> > > BuildDependencyGraph (IEnumerable <TypeReference> entryPoints, Func <TypeReference, bool> typeFilter = null) { var entryPointsList = entryPoints.ToList(); typeFilter = typeFilter ?? (t => !t.Namespace.StartsWith("System")); var typesUsages = Usages(entryPointsList, typeFilter).ToList(); var distinctTypesRelations = typesUsages.Where(x => x.Kind != RelationKind.Interface).Compact().ToList(); var graph = new BidirectionalGraph <string, TaggedEdge <string, List <Relation> > >(false); graph.AddVertexRange(entryPointsList.Select(x => x.FullName)); graph.AddVerticesAndEdgeRange( distinctTypesRelations.Select( x => new TaggedEdge <string, List <Relation> >(x.Source, x.Target, x.Usage))); return(graph); }
public void ConstructGraph() { try { dbUtils = new DbUtils(); var personIdDict = dbUtils.GetAllPersonsIds(); //key - person id, value - list of friends Graph = new BidirectionalGraph <object, IEdge <object> >(false); Graph.AddVerticesAndEdgeRange(personIdDict.Select(x => x.Value.Where(y => x.Key.Id < y.Id).Select(y => new Edge <object>(x.Key, y)).ToList() ).SelectMany(l => l).ToList()); CanExecute = true; Loaded = true; } catch (Exception ex) { ExceptionLogger.Instance.LogFile(ex.ToString()); ErrorMessage = ex.Message; Timer exitTimer = new Timer(ExceptionExit, null, 10000, Timeout.Infinite); } }
public GraphLayout() { if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)) { var g = new BidirectionalGraph <object, IEdge <object> >(); var vertices = new object[] { "S", "A", "M", "P", "L", "E" }; var edges = new IEdge <object>[] { new Edge <object>(vertices[0], vertices[1]), new Edge <object>(vertices[1], vertices[2]), new Edge <object>(vertices[1], vertices[3]), new Edge <object>(vertices[3], vertices[4]), new Edge <object>(vertices[0], vertices[4]), new Edge <object>(vertices[4], vertices[5]) }; g.AddVerticesAndEdgeRange(edges); OverlapRemovalAlgorithmType = "FSA"; LayoutAlgorithmType = "FR"; Graph = g; } }
public void GraphWithSelfEdge_Throws() { var graph = new BidirectionalGraph <int, Edge <int> >(); graph.AddVerticesAndEdgeRange(new[] { new Edge <int>(0, 1), new Edge <int>(1, 2), new Edge <int>(1, 3), new Edge <int>(2, 3), new Edge <int>(2, 2), new Edge <int>(3, 4) }); var algorithm = new SourceFirstBidirectionalTopologicalSortAlgorithm <int, Edge <int> >(graph); Assert.Throws <NonAcyclicGraphException>(() => algorithm.Compute()); algorithm = new SourceFirstBidirectionalTopologicalSortAlgorithm <int, Edge <int> >(graph, TopologicalSortDirection.Backward); Assert.Throws <NonAcyclicGraphException>(() => algorithm.Compute()); }
public void DifferentGraphsTest() { var g1 = new BidirectionalGraph<int, Edge<int>>(); var g2 = new BidirectionalGraph<int, Edge<int>>(); g1.AddVerticesAndEdgeRange(new[] {new Edge<int>(1, 2), new Edge<int>(1, 3), new Edge<int>(2, 4), new Edge<int>(2, 5)}); g2.AddVerticesAndEdgeRange(new[] {new Edge<int>(1, 2), new Edge<int>(2, 3), new Edge<int>(2, 4)}); var dictionary = new Dictionary<Tuple<int, int>, double>(); foreach (var i in g1.Vertices) { foreach (var j in g2.Vertices) { dictionary[Tuple.Create(i, j)] = 0.0; } } dictionary[Tuple.Create(1, 1)] = 1.0; dictionary[Tuple.Create(2, 2)] = 1.0; dictionary[Tuple.Create(3, 2)] = 0.6; dictionary[Tuple.Create(4, 3)] = 1.0; dictionary[Tuple.Create(5, 4)] = 1.0; var algo = new MaxCardinality<int, Edge<int>>(g1, g2, dictionary, 0.5, (u, v) => new Edge<int>(u, v)); var res = algo.compMaxCardinality(); var correctResult = SetModule.Empty<Tuple<int, int>>() .Add(Tuple.Create(1, 1)) .Add(Tuple.Create(2, 2)) .Add(Tuple.Create(3, 2)) .Add(Tuple.Create(4, 3)) .Add(Tuple.Create(5, 4)); Assert.AreEqual(res, correctResult); }
public void UnBalance() { var edge12 = new Edge <int>(1, 2); var edge13 = new Edge <int>(1, 3); var edge23 = new Edge <int>(2, 3); var edge32 = new Edge <int>(3, 2); var edge34 = new Edge <int>(3, 4); var edge56 = new Edge <int>(5, 6); var graph = new BidirectionalGraph <int, Edge <int> >(); graph.AddVerticesAndEdgeRange(new[] { edge12, edge13, edge23, edge32, edge34, edge56 }); int vertexID = 6; VertexFactory <int> vertexFactory = () => vertexID++; EdgeFactory <int, Edge <int> > edgeFactory = (source, target) => new Edge <int>(source, target); var algorithm = new GraphBalancerAlgorithm <int, Edge <int> >(graph, 1, 3, vertexFactory, edgeFactory); algorithm.Balance(); Assert.IsTrue(algorithm.Balanced); algorithm.UnBalance(); Assert.IsFalse(algorithm.Balanced); Assert.AreEqual(1, algorithm.Source); Assert.AreEqual(3, algorithm.Sink); CollectionAssert.IsEmpty(algorithm.SurplusVertices); CollectionAssert.IsEmpty(algorithm.SurplusEdges); CollectionAssert.IsEmpty(algorithm.DeficientVertices); CollectionAssert.IsEmpty(algorithm.DeficientEdges); Assert.AreEqual(default(int), algorithm.BalancingSource); Assert.AreEqual(default(Edge <int>), algorithm.BalancingSourceEdge); Assert.AreEqual(default(int), algorithm.BalancingSink); Assert.AreEqual(default(Edge <int>), algorithm.BalancingSinkEdge); }
public void DifferentGraphsTest() { var g1 = new BidirectionalGraph <int, Edge <int> >(); var g2 = new BidirectionalGraph <int, Edge <int> >(); g1.AddVerticesAndEdgeRange(new[] { new Edge <int>(1, 2), new Edge <int>(1, 3), new Edge <int>(2, 4), new Edge <int>(2, 5) }); g2.AddVerticesAndEdgeRange(new[] { new Edge <int>(1, 2), new Edge <int>(2, 3), new Edge <int>(2, 4) }); var dictionary = new Dictionary <Tuple <int, int>, double>(); foreach (var i in g1.Vertices) { foreach (var j in g2.Vertices) { dictionary[Tuple.Create(i, j)] = 0.0; } } dictionary[Tuple.Create(1, 1)] = 1.0; dictionary[Tuple.Create(2, 2)] = 1.0; dictionary[Tuple.Create(3, 2)] = 0.6; dictionary[Tuple.Create(4, 3)] = 1.0; dictionary[Tuple.Create(5, 4)] = 1.0; var algo = new MaxCardinality <int, Edge <int> >(g1, g2, dictionary, 0.5, (u, v) => new Edge <int>(u, v)); var res = algo.compMaxCardinality(); var correctResult = SetModule.Empty <Tuple <int, int> >() .Add(Tuple.Create(1, 1)) .Add(Tuple.Create(2, 2)) .Add(Tuple.Create(3, 2)) .Add(Tuple.Create(4, 3)) .Add(Tuple.Create(5, 4)); Assert.AreEqual(res, correctResult); }
public void ProcessAllComponents(bool processAll) { var graph = new BidirectionalGraph <int, Edge <int> >(); graph.AddVerticesAndEdgeRange(new[] { new Edge <int>(1, 2), new Edge <int>(1, 3), new Edge <int>(2, 1), new Edge <int>(2, 4), new Edge <int>(2, 5), new Edge <int>(6, 7), new Edge <int>(6, 8), new Edge <int>(8, 6) }); var algorithm = new BidirectionalDepthFirstSearchAlgorithm <int, Edge <int> >(graph) { ProcessAllComponents = processAll }; algorithm.Compute(1); if (processAll) { QuikGraphAssert.TrueForAll(algorithm.VerticesColors, pair => pair.Value == GraphColor.Black); } else { QuikGraphAssert.TrueForAll( new[] { 1, 2, 3, 4, 5 }, vertex => algorithm.VerticesColors[vertex] == GraphColor.Black); QuikGraphAssert.TrueForAll( new[] { 6, 7, 8 }, vertex => algorithm.VerticesColors[vertex] == GraphColor.White); } }
public void TransitiveReduction2() { var graph = new BidirectionalGraph <int, Edge <int> >(); graph.AddVerticesAndEdgeRange(new[] { new Edge <int>(0, 1), new Edge <int>(0, 2), new Edge <int>(0, 3), new Edge <int>(2, 3), new Edge <int>(2, 4), new Edge <int>(2, 5), new Edge <int>(3, 5), new Edge <int>(4, 5), new Edge <int>(6, 5), new Edge <int>(6, 7), new Edge <int>(7, 4) }); BidirectionalGraph <int, Edge <int> > result = graph.ComputeTransitiveReduction(); Assert.AreEqual(8, result.EdgeCount); }