コード例 #1
0
        /// <summary>
        /// Test removing a neighbor from a list of multiple neighbors
        /// </summary>
        static void TestRemoveNeighborMultipleNeighbors()
        {
            GraphNode <int> node       = new GraphNode <int>(4);
            GraphNode <int> removeNode = new GraphNode <int>(5);

            node.AddNeighbor(removeNode);
            node.AddNeighbor(new GraphNode <int>(6));
            bool success = node.RemoveNeighbor(removeNode);

            Console.Write("TestRemoveNeighborMultipleNeighbors: ");
            string nodeString = node.ToString();

            if (nodeString.Equals("[Node Value: 4 Neighbors: 6 ]") &&
                node.Neighbors.Count == 1 &&
                success)
            {
                Console.WriteLine("Passed");
            }
            else
            {
                Console.WriteLine("FAILED!!! Expected: [Node Value: 4  " +
                                  "Neighbors: 6 ], 1, and true Actual: " + nodeString +
                                  ", " + node.Neighbors.Count + " and " +
                                  success);
            }
        }
コード例 #2
0
        /// <summary>
        /// Removes an edge between the nodes with the given values
        /// from the graph. If one or both of the values don't exist
        /// in the graph the method returns false
        /// </summary>
        /// <param name="value1">first value to disconnect</param>
        /// <param name="value2">second value to disconnect</param>
        /// <returns>true if the edge is removed, false otherwise</returns>
        public bool RemoveEdge(T value1, T value2)
        {
            GraphNode <T> node1 = Find(value1);
            GraphNode <T> node2 = Find(value2);

            if (node1 == null ||
                node2 == null)
            {
                return(false);
            }
            else if (!node1.Neighbors.Contains(node2))
            {
                // edge doesn't exist
                return(false);
            }
            else
            {
                // directed graph, so remove node2 as neighbor of node 1
                node1.RemoveNeighbor(node2);
                return(true);
            }
        }