public void BalanceTwice() { g = GraphFactory.UnBalancedFlow(); algo = new GraphBalancerAlgorithm(g, Traversal.FirstVertex(g), Traversal.FirstVertex(g)); algo.Balance(); algo.Balance(); }
public void BalanceUnBalancedFlowGraph() { g = GraphFactory.UnBalancedFlow(); IVertex source = null; IVertex sink = null; foreach (IVertex v in g.Vertices) { if (g.InDegree(v) == 0) { source = v; continue; } if (g.OutDegree(v) == 0) { sink = v; continue; } } Assert.IsNotNull(source); Assert.IsNotNull(sink); int vertexCount = g.VerticesCount; int edgeCount = g.EdgesCount; algo = new GraphBalancerAlgorithm(g, source, sink); algo.DeficientVertexAdded += new VertexEventHandler(algo_DeficientVertexAdded); algo.SurplusVertexAdded += new VertexEventHandler(algo_SurplusVertexAdded); algo.Balance(); VerifyBalanced(vertexCount, edgeCount); }
public void UnBalance_Throws() { var graph = new BidirectionalGraph <int, Edge <int> >(); graph.AddVertexRange(new[] { 1, 2 }); VertexFactory <int> vertexFactory = () => 1; EdgeFactory <int, Edge <int> > edgeFactory = (source, target) => new Edge <int>(source, target); var algorithm = new GraphBalancerAlgorithm <int, Edge <int> >(graph, 1, 2, vertexFactory, edgeFactory); Assert.Throws <InvalidOperationException>(() => algorithm.UnBalance()); }
public void GetBalancingIndex_Throws() { var source = new TestVertex("1"); var sink = new TestVertex("2"); var graph = new BidirectionalGraph <TestVertex, Edge <TestVertex> >(); graph.AddVertexRange(new[] { source, sink }); VertexFactory <TestVertex> vertexFactory = () => new TestVertex(); EdgeFactory <TestVertex, Edge <TestVertex> > edgeFactory = (s, t) => new Edge <TestVertex>(s, t); var algorithm = new GraphBalancerAlgorithm <TestVertex, Edge <TestVertex> >( graph, source, sink, vertexFactory, edgeFactory); // ReSharper disable once ReturnValueOfPureMethodIsNotUsed // ReSharper disable once AssignNullToNotNullAttribute Assert.Throws <ArgumentNullException>(() => algorithm.GetBalancingIndex(null)); }
public void Constructor() { var graph = new BidirectionalGraph <int, Edge <int> >(); graph.AddVertexRange(new[] { 1, 2 }); graph.AddVerticesAndEdge(new Edge <int>(1, 3)); VertexFactory <int> vertexFactory = () => 1; EdgeFactory <int, Edge <int> > edgeFactory = (source, target) => new Edge <int>(source, target); var capacities = new Dictionary <Edge <int>, double>(); var algorithm = new GraphBalancerAlgorithm <int, Edge <int> >(graph, 1, 2, vertexFactory, edgeFactory); Assert.AreSame(graph, algorithm.VisitedGraph); Assert.AreSame(vertexFactory, algorithm.VertexFactory); Assert.AreSame(edgeFactory, algorithm.EdgeFactory); Assert.IsFalse(algorithm.Balanced); Assert.AreEqual(1, algorithm.Source); Assert.AreEqual(2, algorithm.Sink); Assert.IsNotNull(algorithm.Capacities); Assert.AreEqual(graph.EdgeCount, algorithm.Capacities.Count); 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); algorithm = new GraphBalancerAlgorithm <int, Edge <int> >(graph, 1, 2, vertexFactory, edgeFactory, capacities); Assert.AreSame(graph, algorithm.VisitedGraph); Assert.AreSame(vertexFactory, algorithm.VertexFactory); Assert.AreSame(edgeFactory, algorithm.EdgeFactory); Assert.IsFalse(algorithm.Balanced); Assert.AreEqual(1, algorithm.Source); Assert.AreEqual(2, algorithm.Sink); Assert.AreSame(capacities, algorithm.Capacities); 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 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 Balance() { const int source = 1; const int sink = 3; var edge12 = new EquatableEdge <int>(1, 2); var edge13 = new EquatableEdge <int>(1, 3); var edge23 = new EquatableEdge <int>(2, 3); var edge32 = new EquatableEdge <int>(3, 2); var edge34 = new EquatableEdge <int>(3, 4); var edge35 = new EquatableEdge <int>(3, 5); var edge42 = new EquatableEdge <int>(4, 2); var edge55 = new EquatableEdge <int>(5, 5); var edge67 = new EquatableEdge <int>(6, 7); var edge78 = new EquatableEdge <int>(7, 8); var graph = new BidirectionalGraph <int, EquatableEdge <int> >(); graph.AddVerticesAndEdgeRange(new[] { edge12, edge13, edge23, edge32, edge34, edge35, edge42, edge55, edge67, edge78 }); int vertexID = 9; VertexFactory <int> vertexFactory = () => vertexID++; EdgeFactory <int, EquatableEdge <int> > edgeFactory = (s, t) => new EquatableEdge <int>(s, t); var algorithm = new GraphBalancerAlgorithm <int, EquatableEdge <int> >(graph, source, sink, vertexFactory, edgeFactory); algorithm.BalancingSourceAdded += vertex => Assert.AreEqual(source, vertex); algorithm.BalancingSinkAdded += vertex => Assert.AreEqual(sink, vertex); var surplusSet = new HashSet <int> { 2, 5, 8 }; algorithm.SurplusVertexAdded += vertex => Assert.IsTrue(surplusSet.Remove(vertex)); var deficitSet = new HashSet <int> { 6 }; algorithm.DeficientVertexAdded += vertex => Assert.IsTrue(deficitSet.Remove(vertex)); algorithm.Balance(); Assert.IsTrue(algorithm.Balanced); Assert.AreEqual(source, algorithm.Source); Assert.AreEqual(sink, algorithm.Sink); CollectionAssert.IsEmpty(surplusSet); CollectionAssert.IsEmpty(deficitSet); CollectionAssert.AreEquivalent(new[] { 2, 5, 8 }, algorithm.SurplusVertices); CollectionAssert.AreEquivalent( new[] { new EquatableEdge <int>(algorithm.BalancingSource, 2), new EquatableEdge <int>(algorithm.BalancingSource, 5), new EquatableEdge <int>(algorithm.BalancingSource, 8) }, algorithm.SurplusEdges); CollectionAssert.AreEquivalent(new[] { 6 }, algorithm.DeficientVertices); CollectionAssert.AreEquivalent( new[] { new EquatableEdge <int>(6, algorithm.BalancingSink) }, algorithm.DeficientEdges); Assert.AreEqual(9, algorithm.BalancingSource); Assert.AreEqual(new EquatableEdge <int>(algorithm.BalancingSource, source), algorithm.BalancingSourceEdge); Assert.AreEqual(10, algorithm.BalancingSink); Assert.AreEqual(new EquatableEdge <int>(sink, algorithm.BalancingSink), algorithm.BalancingSinkEdge); }
public void UnBalanceBeforeBalancing() { g = GraphFactory.UnBalancedFlow(); algo = new GraphBalancerAlgorithm(g, Traversal.FirstVertex(g), Traversal.FirstVertex(g)); algo.UnBalance(); }