Esempio n. 1
0
        /// <summary>Constructs a maximum flow algorithm.</summary>
        /// <param name="g">Graph to compute maximum flow on.</param>
        /// <param name="capacities">edge capacities</param>
        /// <param name="reversedEdges">reversed edge map</param>
        /// <exception cref="ArgumentNullException"><paramref name="g"/> or
        /// <paramref name="capacities"/> or <paramref name="reversedEdges"/> is a null
        /// reference.
        /// </exception>
        public MaximumFlowAlgorithm(
            IVertexListGraph g,
            EdgeDoubleDictionary capacities,
            EdgeEdgeDictionary reversedEdges
            )
        {
            if (g == null)
            {
                throw new ArgumentNullException("g");
            }
            if (capacities == null)
            {
                throw new ArgumentNullException("capacities");
            }
            if (reversedEdges == null)
            {
                throw new ArgumentNullException("reversedEdges");
            }

            this.visitedGraph  = g;
            this.capacities    = capacities;
            this.reversedEdges = reversedEdges;

            this.predecessors       = new VertexEdgeDictionary();
            this.residualCapacities = new EdgeDoubleDictionary();
            this.colors             = new VertexColorDictionary();
        }
Esempio n. 2
0
        /// <inheritdoc />
        public int RemoveVertexIf(VertexPredicate <TVertex> predicate)
        {
            if (predicate is null)
            {
                throw new ArgumentNullException(nameof(predicate));
            }

            var verticesToRemove = new VertexList <TVertex>();

            verticesToRemove.AddRange(Vertices.Where(vertex => predicate(vertex)));

            // Remove out edges
            var verticesEdgesRemoved = new VertexEdgeDictionary <TVertex, TEdge>(verticesToRemove.Count);

            foreach (TVertex vertex in verticesToRemove)
            {
                verticesEdgesRemoved[vertex] = _vertexEdges[vertex];
                _vertexEdges.Remove(vertex);
            }
            EdgeCount -= verticesEdgesRemoved.Sum(pair => pair.Value.Count);
            Debug.Assert(EdgeCount >= 0);

            // Remove in edges (Run over edges and remove each edge touching vertices to remove)
            RemoveInEdges(v => verticesToRemove.Contains(v, EqualityComparer <TVertex> .Default));

            NotifyEdgesRemoved(verticesEdgesRemoved.Values.SelectMany(edges => edges));
            NotifyVerticesRemoved(verticesToRemove);

            return(verticesToRemove.Count);
        }
Esempio n. 3
0
        public void Clone()
        {
            var dictionary = new VertexEdgeDictionary <int, EquatableEdge <int> >();

            VertexEdgeDictionary <int, EquatableEdge <int> > clonedDictionary = dictionary.Clone();

            CollectionAssert.IsEmpty(clonedDictionary);

            clonedDictionary = (VertexEdgeDictionary <int, EquatableEdge <int> >)((IVertexEdgeDictionary <int, EquatableEdge <int> >)dictionary).Clone();
            CollectionAssert.IsEmpty(clonedDictionary);

            clonedDictionary = (VertexEdgeDictionary <int, EquatableEdge <int> >)((ICloneable)dictionary).Clone();
            CollectionAssert.IsEmpty(clonedDictionary);

            dictionary.Add(1, new EdgeList <int, EquatableEdge <int> > {
                new EquatableEdge <int>(1, 2)
            });
            dictionary.Add(2, new EdgeList <int, EquatableEdge <int> > {
                new EquatableEdge <int>(2, 3)
            });
            dictionary.Add(3, new EdgeList <int, EquatableEdge <int> >());
            clonedDictionary = dictionary.Clone();
            CollectionAssert.AreEqual(dictionary, clonedDictionary);

            clonedDictionary = (VertexEdgeDictionary <int, EquatableEdge <int> >)((IVertexEdgeDictionary <int, EquatableEdge <int> >)dictionary).Clone();
            CollectionAssert.AreEqual(dictionary, clonedDictionary);

            clonedDictionary = (VertexEdgeDictionary <int, EquatableEdge <int> >)((ICloneable)dictionary).Clone();
            CollectionAssert.AreEqual(dictionary, clonedDictionary);
        }
Esempio n. 4
0
 public OptimalStrategy(VertexEdgeDictionary successors)
 {
     if (successors == null)
     {
         throw new ArgumentNullException("successors");
     }
     this.successors = successors;
 }
Esempio n. 5
0
 /// <summary>
 /// Constructor, uses the given predecessor map.
 /// </summary>
 /// <param name="predecessors">Predecessor map</param>
 /// <exception cref="ArgumentNullException">predecessors is null</exception>
 public PredecessorRecorderVisitor(VertexEdgeDictionary predecessors)
 {
     if (predecessors == null)
     {
         throw new ArgumentNullException("predecessors");
     }
     m_Predecessors = predecessors;
 }
 /// <summary>
 /// Constructor, uses the given predecessor map.
 /// </summary>
 /// <param name="predecessors">Predecessor map</param>
 /// <exception cref="ArgumentNullException">
 /// predecessors is null
 /// </exception>
 public PredecessorRecorderVisitor(VertexEdgeDictionary predecessors)
 {
     if (predecessors == null)
     {
         throw new ArgumentNullException("predecessors");
     }
     this.predecessors    = predecessors;
     this.endPathVertices = new VertexCollection();
 }
Esempio n. 7
0
            public VertexEdgeDictionary Clone()
            {
                VertexEdgeDictionary clone = new VertexEdgeDictionary(this.Count);

                foreach (KeyValuePair <TVertex, EdgeList> kv in this)
                {
                    clone.Add(kv.Key, kv.Value.Clone());
                }
                return(clone);
            }
Esempio n. 8
0
 public AdjacencyGraph(bool allowParallelEdges, int capacity)
 {
     this.allowParallelEdges = allowParallelEdges;
     if (capacity > 0)
     {
         this.vertexEdges = new VertexEdgeDictionary(capacity);
     }
     else
     {
         this.vertexEdges = new VertexEdgeDictionary();
     }
 }
Esempio n. 9
0
 private BidirectionalGraph(
     VertexEdgeDictionary vertexInEdges,
     VertexEdgeDictionary vertexOutEdges,
     int edgeCount,
     int edgeCapacity,
     bool allowParallelEdges
     )
 {
     this.vertexInEdges      = vertexInEdges;
     this.vertexOutEdges     = vertexOutEdges;
     this.edgeCount          = edgeCount;
     this.edgeCapacity       = edgeCapacity;
     this.allowParallelEdges = allowParallelEdges;
 }
Esempio n. 10
0
 public BidirectionalGraph(bool allowParallelEdges, int vertexCapacity)
 {
     this.allowParallelEdges = allowParallelEdges;
     if (vertexCapacity > 0)
     {
         this.vertexInEdges  = new VertexEdgeDictionary(vertexCapacity);
         this.vertexOutEdges = new VertexEdgeDictionary(vertexCapacity);
     }
     else
     {
         this.vertexInEdges  = new VertexEdgeDictionary();
         this.vertexOutEdges = new VertexEdgeDictionary();
     }
 }
        public EdmundsKarpMaximumFlow(
            Graph g,
            EdgeDoubleDictionary edgeCapacities
            ) : base(g)
        {
            if (edgeCapacities == null)
            {
                throw new ArgumentNullException("Edge capacities");
            }

            m_EdgeCapacities         = edgeCapacities;
            m_ResidualEdgeCapacities = null;
            m_Predecessors           = new VertexEdgeDictionary();
        }
Esempio n. 12
0
 public AdjacencyGraph(
     bool allowParallelEdges,
     int capacity,
     int edgeCapacity,
     Func <int, VertexEdgeDictionary <TVertex, TEdge> > vertexEdgesDictionaryFactory)
 {
     if (vertexEdgesDictionaryFactory == null)
     {
         throw new ArgumentNullException("vertexComparer");
     }
     this.allowParallelEdges = allowParallelEdges;
     this.vertexEdges        = vertexEdgesDictionaryFactory(capacity);
     this.edgeCapacity       = edgeCapacity;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="BidirectionalGraph{TVertex,TEdge}"/> class.
 /// </summary>
 /// <param name="allowParallelEdges">Indicates if parallel edges are allowed.</param>
 /// <param name="vertexCapacity">Vertex capacity.</param>
 public BidirectionalGraph(bool allowParallelEdges, int vertexCapacity)
 {
     AllowParallelEdges = allowParallelEdges;
     if (vertexCapacity > -1)
     {
         _vertexInEdges  = new VertexEdgeDictionary <TVertex, TEdge>(vertexCapacity);
         _vertexOutEdges = new VertexEdgeDictionary <TVertex, TEdge>(vertexCapacity);
     }
     else
     {
         _vertexInEdges  = new VertexEdgeDictionary <TVertex, TEdge>();
         _vertexOutEdges = new VertexEdgeDictionary <TVertex, TEdge>();
     }
 }
Esempio n. 14
0
        public UndirectedGraph(bool allowParallelEdges, EdgeEqualityComparer <TVertex, TEdge> edgeEqualityComparer, int vertexCapacity, IEqualityComparer <TVertex> vertexComparer)
        {
            Contract.Requires(edgeEqualityComparer != null);
            Contract.Requires(vertexComparer != null);

            this.allowParallelEdges   = allowParallelEdges;
            this.edgeEqualityComparer = edgeEqualityComparer;
            if (vertexCapacity > -1)
            {
                this.adjacentEdges = new VertexEdgeDictionary <TVertex, TEdge>(vertexCapacity, vertexComparer);
            }
            else
            {
                this.adjacentEdges = new VertexEdgeDictionary <TVertex, TEdge>(vertexComparer);
            }
        }
Esempio n. 15
0
        private UndirectedGraph(
            VertexEdgeDictionary <TVertex, TEdge> adjacentEdges,
            EdgeEqualityComparer <TVertex, TEdge> edgeEqualityComparer,
            int edgeCount,
            int edgeCapacity,
            bool allowParallelEdges
            )
        {
            Contract.Requires(adjacentEdges != null);
            Contract.Requires(edgeEqualityComparer != null);
            Contract.Requires(edgeCount >= 0);

            this.adjacentEdges        = adjacentEdges;
            this.edgeEqualityComparer = edgeEqualityComparer;
            this.edgeCount            = edgeCount;
            this.edgeCapacity         = edgeCapacity;
            this.allowParallelEdges   = allowParallelEdges;
        }
Esempio n. 16
0
        private UndirectedGraph(
            [NotNull] VertexEdgeDictionary <TVertex, TEdge> adjacentEdges,
            [NotNull] EdgeEqualityComparer <TVertex> edgeEqualityComparer,
            int edgeCount,
            int edgeCapacity,
            bool allowParallelEdges)
        {
            if (edgeCount < 0)
            {
                throw new ArgumentException("Must be positive", nameof(edgeCount));
            }

            _adjacentEdges       = adjacentEdges ?? throw new ArgumentNullException(nameof(adjacentEdges));
            EdgeEqualityComparer = edgeEqualityComparer ?? throw new ArgumentNullException(nameof(edgeEqualityComparer));
            EdgeCount            = edgeCount;
            EdgeCapacity         = edgeCapacity;
            AllowParallelEdges   = allowParallelEdges;
        }
Esempio n. 17
0
        public AdjacencyGraph(bool allowParallelEdges, int vertexCapacity, int edgeCapacity, IEqualityComparer <TVertex> vertexComparer)
        {
            if (vertexComparer == null)
            {
                throw new ArgumentNullException("vertexComparer");
            }

            this.allowParallelEdges = allowParallelEdges;
            if (vertexCapacity > -1)
            {
                this.vertexEdges = new VertexEdgeDictionary <TVertex, TEdge>(vertexCapacity, vertexComparer);
            }
            else
            {
                this.vertexEdges = new VertexEdgeDictionary <TVertex, TEdge>(vertexComparer);
            }
            this.edgeCapacity = edgeCapacity;
        }
Esempio n. 18
0
        public void Serialization()
        {
            var dictionary = new VertexEdgeDictionary <int, EquatableEdge <int> >();

            VertexEdgeDictionary <int, EquatableEdge <int> > deserializedDictionary = SerializeAndDeserialize(dictionary);

            Assert.AreNotSame(dictionary, deserializedDictionary);
            CollectionAssert.IsEmpty(deserializedDictionary);

            dictionary.Add(1, new EdgeList <int, EquatableEdge <int> > {
                new EquatableEdge <int>(1, 2)
            });
            dictionary.Add(2, new EdgeList <int, EquatableEdge <int> > {
                new EquatableEdge <int>(2, 3)
            });
            deserializedDictionary = SerializeAndDeserialize(dictionary);
            Assert.AreNotSame(dictionary, deserializedDictionary);
            CollectionAssert.AreEqual(dictionary, deserializedDictionary);
        }
Esempio n. 19
0
        public void GenerateWalls(int outi, int outj, int ini, int inj)
        {
            // here we use a uniform probability distribution among the out-edges
            CyclePoppingRandomTreeAlgorithm pop =
                new CyclePoppingRandomTreeAlgorithm(this.graph);

            // we can also weight the out-edges

/*
 *                      EdgeDoubleDictionary weights = new EdgeDoubleDictionary();
 *                      foreach(IEdge e in this.graph.Edges)
 *                              weights[e]=1;
 *                      pop.EdgeChain = new WeightedMarkovEdgeChain(weights);
 */
            IVertex root = this.latice[outi, outj];

            if (root == null)
            {
                throw new ArgumentException("outi,outj vertex not found");
            }

            pop.RandomTreeWithRoot(this.latice[outi, outj]);
            this.successors = pop.Successors;

            // build the path to ini, inj
            IVertex v = this.latice[ini, inj];

            if (v == null)
            {
                throw new ArgumentException("ini,inj vertex not found");
            }
            this.outPath = new EdgeCollection();
            while (v != root)
            {
                IEdge e = this.successors[v];
                if (e == null)
                {
                    throw new Exception();
                }
                this.outPath.Add(e);
                v = e.Target;
            }
        }
        internal void Augment(Vertex src, Vertex sink, VertexEdgeDictionary predecessors)
        {
            // find minimum residual capacity along the augmenting path
            double delta = double.MaxValue;
            Edge   e     = predecessors[sink];
            Vertex u     = null;

            do
            {
                delta = Math.Min(delta, ResidualEdgeCapacities[e]);
                u     = e.Source;
                e     = predecessors[u];
            } while (u != src);

            // push delta units of flow along the augmenting path
            e = predecessors[sink];
            do
            {
                ResidualEdgeCapacities[e] -= delta;
                ResidualEdgeCapacities[ReverseEdges[e]] += delta;
                u = e.Source;
                e = predecessors[u];
            } while (u != src);
        }
Esempio n. 21
0
 /// <summary>
 /// Default constructor
 /// </summary>
 public PredecessorRecorderVisitor()
 {
     m_Predecessors = new VertexEdgeDictionary();
 }
Esempio n. 22
0
 public XUndirectedGraph(bool allowParallelEdges, EdgeEqualityComparer <TVertex, TEdge> edgeEqualityComparer)
 {
     AllowParallelEdges    = allowParallelEdges;
     _edgeEqualityComparer = edgeEqualityComparer;
     _adjacentEdges        = new VertexEdgeDictionary <TVertex, TEdge>();
 }
 /// <summary>
 /// Default constructor
 /// </summary>
 public PredecessorRecorderVisitor()
 {
     this.predecessors    = new VertexEdgeDictionary();
     this.endPathVertices = new VertexCollection();
 }