protected internal virtual void CutRemainingUnchosenEdges <E, V>(
            NodeStateContextMediator <E, V> nodeStateContextMediator) where E : EdgeNodeStateContext, new() where V : VertexNodeStateContext, new()
        {
            var incidentUnchosenEdgeNodes = this.GetIncidentUnchosenEdgeNodeStateContexts(nodeStateContextMediator).ToList();

            incidentUnchosenEdgeNodes.ForEach(t => t.Cut());
        }
        /// <summary>
        /// This method assumes that this vertex has already been chosen.
        /// </summary>
        /// <param name="nodeStateContextMediator"></param>
        /// <returns></returns>
        protected internal virtual List <E> GetEdgesThatCutWouldOrphanIfAnyOrGetDefaultUnchosenEdgesIfNone <E, V>(
            NodeStateContextMediator <E, V> nodeStateContextMediator) where E : EdgeNodeStateContext, new() where V : VertexNodeStateContext, new()
        {
            List <E> unchosenIncidentEdgeNodes;
            var      numberOfEdgesThatOrphanTargetVertex =
                this.GetNumberOfIncidentEdgesThatOrphanTargetVertex(nodeStateContextMediator);

            if (numberOfEdgesThatOrphanTargetVertex == 0)
            {
                unchosenIncidentEdgeNodes = nodeStateContextMediator.GetIncidentEdgeNodesWithStateUnchosen(this.Position).ToList();
            }
            else // i.e. if at least one unchosen edge orphans the target vertex, then add them all
            {    // because either 1 edge creates an orphan and must be chosen or more exist and must be evaluated as a best-try attempt (even though hamiltonicity is not possible for all vertices in this case).
                unchosenIncidentEdgeNodes = this.GetUnchosenEdgesThatCauseOrphans(nodeStateContextMediator).ToList();

                if (numberOfEdgesThatOrphanTargetVertex > 1)
                {
                    return(unchosenIncidentEdgeNodes); // I could return these if I want to continue traversing the graph even though it's not hamiltonian for all vertices in this case.
                    //return null; // I could return null if I want to stop traversing the graph.
                    // perhaps we should return null here and check for null at the call site.
                    //throw new TooManyEdgesException("We need to deal with this case for the vertex with position: " + this.Position);
                }
            }
            return(unchosenIncidentEdgeNodes);
        }
        protected internal virtual IEnumerable <E> GetUnchosenEdgesThatCauseOrphans <E, V>(
            NodeStateContextMediator <E, V> nodeStateContextMediator) where E : EdgeNodeStateContext, new() where V : VertexNodeStateContext, new()
        {
            var causingOrphansDict            = this.WouldCutToIncidentUnchosenEdgesOrphanTargetVertexDictionary(nodeStateContextMediator);
            var unchosenEdgesThatCauseOrphans = causingOrphansDict.Where(t => t.Value).Select(t => nodeStateContextMediator.GetEdgeNodeStateContextByPosition(t.Key));

            return(unchosenEdgesThatCauseOrphans);
        }
        protected internal virtual int GetNumberOfIncidentEdgesThatOrphanTargetVertex <E, V>(
            NodeStateContextMediator <E, V> nodeStateContextMediator) where E : EdgeNodeStateContext, new() where V : VertexNodeStateContext, new()
        {
            var incidentEdgesThatOrphanVertex =
                this.WouldCutToIncidentUnchosenEdgesOrphanTargetVertexDictionary(nodeStateContextMediator);
            var countOfValuesThatAreTrue = incidentEdgesThatOrphanVertex.Where(t => t.Value).Count();

            return(countOfValuesThatAreTrue);
        }
        protected internal virtual Dictionary <int, bool> WouldCutToIncidentUnchosenEdgesOrphanTargetVertexDictionary <E, V>(
            NodeStateContextMediator <E, V> nodeStateContextMediator) where E : EdgeNodeStateContext, new() where V : VertexNodeStateContext, new()
        {
            var unchosenEdges       = this.GetIncidentEdgeNodeStateContextsWithMatchingState(nodeStateContextMediator, UnchosenEdgeNodeState.Instance);
            var doesCutOrphanVertex = unchosenEdges.ToDictionary(t => t.Position,
                                                                 t => t.DoesCutOrphanTargetVertex(nodeStateContextMediator, this.Position));

            return(doesCutOrphanVertex);
        }
Example #6
0
        /// <summary>
        /// Node: This method only clones the containerFacade and its contexts!
        /// It does not clone the AdjacencyProvider or IncidenceProvider,
        /// so those providers use the existing object references!
        /// </summary>
        /// <param name="nodeStateContextMediator"></param>
        /// <param name="facadeFlyweightFactory"></param>
        /// <returns></returns>
        public NodeStateContextMediator <E, V> Copy(
            NodeStateContextContainerFacadeFlyweightFactory <E, V> facadeFlyweightFactory)
        {
            var facadeCopy   = this.NodeStateContextContainerFacade.Copy(facadeFlyweightFactory);
            var mediatorCopy = new NodeStateContextMediator <E, V>
            {
                NodeStateContextContainerFacade = facadeCopy,
                AdjacencyProvider = this.AdjacencyProvider,
                IncidenceProvider = this.IncidenceProvider
            };

            return(mediatorCopy);
        }
Example #7
0
            //protected internal override bool AreVerticesBothAdjacentAndUncut(NodeStateContextMediator<E, V> mediator, int vertexPosition1, int vertexPosition2)
            //{
            //    return this.AreVerticesBothAdjacentAndUncut(mediator, vertexPosition1, vertexPosition2, ErrorOnMissing.No);
            //}


            public bool AreVerticesBothAdjacentAndUncut <E, V>(NodeStateContextMediator <E, V> mediator, int vertexPosition1, int vertexPosition2, ErrorOnMissing errorOnMissing)
                where E : EdgeNodeStateContext, new()
                where V : VertexNodeStateContext, new()
            {
                if (AreVerticesBothAdjacent(vertexPosition1, vertexPosition2, errorOnMissing))
                {
                    var edgeBetweenThem = mediator.GetEdgeIncidentToBothVertices(vertexPosition1, vertexPosition2);
                    if (edgeBetweenThem != null && !edgeBetweenThem.State.Equals(CutEdgeNodeState.Instance))
                    {
                        return(true);
                    }
                }
                return(false);
            }
        //protected internal virtual List<V> GetAdjacentVertexNodeStateContexts<E, V>(
        //    NodeStateContextMediator<E, V> nodeStateContextMediator)
        //    where E : EdgeNodeStateContext, new()
        //    where V : VertexNodeStateContext, new()
        //{
        //    return nodeStateContextMediator.GetAdjacentVertices(this.Position)
        //}
        /// <summary>
        /// This method must be called before the cut actually occurs.
        /// It checks if the number of incident edgeNodeStateContexts with state != CutEdgeNodeState
        /// is greater than 2. If so, it returns false because there are enough free edges for the
        /// cut to not cause an orphan. But if the value is 2 or below, then it returns true because
        /// the cut would cause this vertex to be orphaned.
        /// </summary>
        /// <param name="nodeStateContextMediator"></param>
        /// <returns></returns>
        protected internal virtual bool WouldCutOrphanVertex <E, V>(NodeStateContextMediator <E, V> nodeStateContextMediator) where E : EdgeNodeStateContext, new() where V : VertexNodeStateContext, new()
        {
            var incidentEdgeNodeContextsWhereStateIsNotCut =
                this.GetIncidentEdgeNodeStateContextsWhereStateIsNotCut(nodeStateContextMediator);

            if (incidentEdgeNodeContextsWhereStateIsNotCut.Count > 2)
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }
Example #9
0
        /// <summary>
        /// This method checks both vertices (that are incident to this edge) to determine if a cut to the edge would orphan either one of them.
        /// If either vertex would be orphaned, this method returns true. For a vertex to be determined to be orphaned, it must have
        /// 2 or fewer incident edges total where edgeState != cut. (Thus, it must have 2 or fewer edges that are either chosen or unchosen
        /// before a cut is attempted.) This is because a cut would reduce the set of {chosen or unchosen edges} to 1 or fewer.
        /// </summary>
        /// <param name="nodeStateContextMediator"></param>
        /// <returns></returns>
        protected internal virtual bool DoesCutOrphanVertex <E, V>(NodeStateContextMediator <E, V> nodeStateContextMediator) where E : EdgeNodeStateContext, new() where V : VertexNodeStateContext, new()
        {
            var incidentVertices          = this.GetIncidentVerticesAsTuple(nodeStateContextMediator);
            var wouldCutOrphanFirstVertex =
                incidentVertices.Item1.WouldCutOrphanVertex(nodeStateContextMediator);

            if (wouldCutOrphanFirstVertex)
            {
                return(true);
            }
            var wouldCutOrphanSecondVertex = incidentVertices.Item2.WouldCutOrphanVertex(nodeStateContextMediator);

            if (wouldCutOrphanSecondVertex)
            {
                return(true);
            }
            return(false);
        }
Example #10
0
        /// <summary>
        /// The elements in the resulting tuple are not ordered.
        /// </summary>
        /// <param name="mediator"></param>
        /// <returns></returns>
        protected internal virtual Tuple <V, V> GetIncidentVerticesAsTuple <E, V>(
            NodeStateContextMediator <E, V> mediator) where E : EdgeNodeStateContext, new() where V : VertexNodeStateContext, new()
        {
            //var vertices = this.GetIncidentVertices(mediator).ToList();
            //if (vertices.Count < 2)
            //{
            //    throw new NotEnoughEdgesException();
            //}
            //if (vertices.Count > 3)
            //{
            //    throw new TooManyEdgesException();
            //}
            //var firstVertex = vertices[0];
            //var secondVertex = vertices[1];
            //var tuple = new Tuple<V, V>(firstVertex, secondVertex);
            var tuple = mediator.GetIncidentVerticesAsTuple(this.Position);

            return(tuple);
        }
Example #11
0
        /// <summary>
        /// This method assumes that only one of the incident vertices is unchosen!
        /// (It assumes that the other incident vertex is chosen!)
        /// </summary>
        /// <param name="nodeStateContextMediator"></param>
        /// <returns></returns>
        protected internal virtual V GetIncidentUnchosenVertexNodeStateContext <E, V>(
            NodeStateContextMediator <E, V> nodeStateContextMediator) where E : EdgeNodeStateContext, new() where V : VertexNodeStateContext, new()
        {
            var unchosenVertices = this.GetIncidentUnchosenVertexNodeStateContexts(nodeStateContextMediator).ToList();

            if (unchosenVertices.Count > 1)
            {
                throw new TooManyVerticesException("problem in GetIncidentUnchosenVertexNodeStateContext(..)!");
            }
            if (unchosenVertices.Count < 1)
            {
                return(null);
                //throw new NotEnoughVerticesException("problem in GetIncidentUnchosenVertexNodeStateContext(..)!");
            }
            var firstUnchosenVertex = unchosenVertices.FirstOrDefault();

            if (firstUnchosenVertex == null)
            {
                throw new NullReferenceException(
                          "firstUnchosenVertex is null in GetIncidentUnchosenVertexNodeStateContext(..)!");
            }
            return(firstUnchosenVertex);
        }
Example #12
0
        protected internal virtual bool DoesCutOrphanTargetVertex <E, V>(NodeStateContextMediator <E, V> nodeStateContextMediator,
                                                                         int sourceVertexPosition) where E : EdgeNodeStateContext, new() where V : VertexNodeStateContext, new()
        {
            var incidentVertices = this.GetIncidentVertices(nodeStateContextMediator).ToList();

            if (incidentVertices.Count > 2)
            {
                throw new NotEnoughEdgesException("Unexpected number of incident vertices!");
            }
            var targetVertex = incidentVertices.FirstOrDefault(t => t.Position != sourceVertexPosition);

            if (targetVertex == null)
            {
                throw new NullReferenceException("Problem in DoesCutOrphanTargetVertex(..)");
            }

            var wouldCutOrphanSecondVertex = targetVertex.WouldCutOrphanVertex(nodeStateContextMediator);

            if (wouldCutOrphanSecondVertex)
            {
                return(true);
            }
            return(false);
        }
 protected internal override int GetNumberOfIncidentEdgesThatOrphanTargetVertex <E, V>(NodeStateContextMediator <E, V> nodeStateContextMediator)
 {
     return(base.GetNumberOfIncidentEdgesThatOrphanTargetVertex(nodeStateContextMediator));
 }
 protected internal virtual List <E> GetIncidentEdgeNodeStateContextsWithNotMatchingState <E, V>(
     NodeStateContextMediator <E, V> nodeStateContextMediator, NodeState state) where E : EdgeNodeStateContext, new() where V : VertexNodeStateContext, new()
 {
     return(nodeStateContextMediator.GetIncidentEdgeElements(this.Position).Where(t => !t.State.Equals(state)).ToList());
 }
 protected internal virtual List <E> GetIncidentEdgeNodeStateContextsWhereStateIsNotCut <E, V>(
     NodeStateContextMediator <E, V> nodeStateContextMediator) where E : EdgeNodeStateContext, new() where V : VertexNodeStateContext, new()
 {
     return(GetIncidentEdgeNodeStateContextsWithNotMatchingState(nodeStateContextMediator,
                                                                 CutEdgeNodeState.Instance));
 }
 protected internal override IEnumerable <E> GetIncidentUnchosenEdgeNodeStateContexts <E, V>(NodeStateContextMediator <E, V> nodeStateContextMediator)
 {
     return(base.GetIncidentUnchosenEdgeNodeStateContexts(nodeStateContextMediator));
 }
 protected internal virtual List <E> GetIncidentEdgeNodeStateContexts <E, V>(
     NodeStateContextMediator <E, V> nodeStateContextMediator) where E : EdgeNodeStateContext, new() where V : VertexNodeStateContext, new()
 {
     return(nodeStateContextMediator.GetIncidentEdgeElements(this.Position));
 }
 protected internal virtual IEnumerable <E> GetIncidentUnchosenEdgeNodeStateContexts <E, V>(
     NodeStateContextMediator <E, V> nodeStateContextMediator) where E : EdgeNodeStateContext, new() where V : VertexNodeStateContext, new()
 {
     return(this.GetIncidentEdgeNodeStateContextsWithMatchingState(nodeStateContextMediator,
                                                                   UnchosenEdgeNodeState.Instance));
 }
 protected internal override IEnumerable <E> GetUnchosenEdgesThatCauseOrphans <E, V>(NodeStateContextMediator <E, V> nodeStateContextMediator)
 {
     return(base.GetUnchosenEdgesThatCauseOrphans(nodeStateContextMediator));
 }
 protected internal virtual bool IsUncutEdgeCountAboveTwo <E, V>(
     NodeStateContextMediator <E, V> nodeStateContextMediator) where E : EdgeNodeStateContext, new() where V : VertexNodeStateContext, new()
 {
     return(this.GetUncutEdgeCount(nodeStateContextMediator) > 2);
 }
        protected internal virtual int NumberOfEdgesThatCanBeCut <E, V>(NodeStateContextMediator <E, V> nodeStateContextMediator) where E : EdgeNodeStateContext, new() where V : VertexNodeStateContext, new()
        {
            var uncutEdgeCount = this.GetUncutEdgeCount(nodeStateContextMediator);

            return(uncutEdgeCount - 2);
        }
Example #22
0
 protected internal override bool AreVerticesBothAdjacentAndUncut <E, V>(NodeStateContextMediator <E, V> mediator, int vertexPosition1, int vertexPosition2)
 {
     return(this.AreVerticesBothAdjacentAndUncut(mediator, vertexPosition1, vertexPosition2, ErrorOnMissing.No));
 }
 protected internal override int NumberOfEdgesThatCanBeCut <E, V>(NodeStateContextMediator <E, V> nodeStateContextMediator)
 {
     return(base.NumberOfEdgesThatCanBeCut(nodeStateContextMediator));
 }
 protected internal override int GetUncutEdgeCount <E, V>(NodeStateContextMediator <E, V> nodeStateContextMediator)
 {
     return(base.GetUncutEdgeCount(nodeStateContextMediator));
 }
 protected internal override void CutRemainingUnchosenEdges <E, V>(NodeStateContextMediator <E, V> nodeStateContextMediator)
 {
     base.CutRemainingUnchosenEdges(nodeStateContextMediator);
 }
 protected internal virtual IEnumerable <E> GetIncidentUncutEdges <E, V>(
     NodeStateContextMediator <E, V> nodeStateContextMediator) where E : EdgeNodeStateContext, new() where V : VertexNodeStateContext, new()
 {
     return(nodeStateContextMediator.GetIncidentEdgeNodesWithStateUncut(this.Position));
 }
 protected internal override List <E> GetEdgesThatCutWouldOrphanIfAnyOrGetDefaultUnchosenEdgesIfNone <E, V>(
     NodeStateContextMediator <E, V> nodeStateContextMediator)
 {
     return(base.GetEdgesThatCutWouldOrphanIfAnyOrGetDefaultUnchosenEdgesIfNone(nodeStateContextMediator));
 }
Example #28
0
 abstract protected internal bool AreVerticesBothAdjacentAndUncut <E, V>(NodeStateContextMediator <E, V> mediator, int vertexPosition1, int vertexPosition2)
     where E : EdgeNodeStateContext, new()
     where V : VertexNodeStateContext, new();
 protected internal virtual int GetUncutEdgeCount <E, V>(
     NodeStateContextMediator <E, V> nodeStateContextMediator) where E : EdgeNodeStateContext, new() where V : VertexNodeStateContext, new()
 {
     return(this.GetIncidentUncutEdges(nodeStateContextMediator).ToList().Count);
 }
 protected internal override bool IsUncutEdgeCountAboveTwo <E, V>(NodeStateContextMediator <E, V> nodeStateContextMediator)
 {
     return(base.IsUncutEdgeCountAboveTwo(nodeStateContextMediator));
 }