Ejemplo n.º 1
0
        /// <summary>
        /// Adds a neighbor to the node.
        /// </summary>
        /// <param name="neighborToAdd"></param>
        /// <param name="connectionWeight"></param>
        public void AddNeighbor(WeightedUndirectedGraphNode <T> neighborToAdd, int connectionWeight)
        {
            //First check if the connection doesn't already exist.
            if (!m_Neighbors.ContainsValue(neighborToAdd.Data))
            {
                //Create a new connection object. This is used for the weighted graph.
                WeightedGraphConnection <T> newConnection = new WeightedGraphConnection <T>(neighborToAdd, connectionWeight);

                //Add the new connection to the list of connections.
                m_Neighbors.Add(newConnection, neighborToAdd.Data);

                //Tell the neighbor to add this connection to it's list of connections.
                //This is done because the graph is undirected.
                neighborToAdd.AddNeighbor(this, connectionWeight);
            }
            else
            {
                var myKey = m_Neighbors.FirstOrDefault(x => x.Value.Equals(neighborToAdd.Data)).Key;
                if (connectionWeight != myKey.ConnectionWeight)
                {
                    WeightedGraphConnection <T> newConnection = new WeightedGraphConnection <T>(neighborToAdd, connectionWeight);

                    m_Neighbors.Add(newConnection, neighborToAdd.Data);
                    neighborToAdd.OneWayAdd(this, connectionWeight);
                }
            }
        }
Ejemplo n.º 2
0
        public void OneWayAdd(WeightedUndirectedGraphNode <T> neighborToAdd, int connectionWeight)
        {
            var myKey = m_Neighbors.FirstOrDefault(x => x.Value.Equals(neighborToAdd.Data)).Key;

            if (connectionWeight != myKey.ConnectionWeight)
            {
                //Create a new connection object. This is used for the weighted graph.
                WeightedGraphConnection <T> newConnection = new WeightedGraphConnection <T>(neighborToAdd, connectionWeight);

                //Add the new connection to the list of connections.
                m_Neighbors.Add(newConnection, neighborToAdd.Data);
            }
        }