Esempio n. 1
0
        /// <summary>
        /// Test removing all neighbor from a list of multiple neighbors
        /// </summary>
        static void TestRemoveAllNeighborsMultipleNeighbors()
        {
            GraphNode <int> node = new GraphNode <int>(4);

            node.AddNeighbor(new GraphNode <int>(5));
            node.AddNeighbor(new GraphNode <int>(6));
            bool success = node.RemoveAllNeighbors();

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

            if (nodeString.Equals("[Node Value: 4 Neighbors: ]") &&
                node.Neighbors.Count == 0 &&
                success)
            {
                Console.WriteLine("Passed");
            }
            else
            {
                Console.WriteLine("FAILED!!! Expected: [Node Value: 4  " +
                                  "Neighbors: ], 0, and true Actual: " + nodeString +
                                  ", " + node.Neighbors.Count + " and " +
                                  success);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Test adding a duplicate neighbor to a list of neighbors
        /// </summary>
        static void TestAddNeighborDuplicateNeighbor()
        {
            GraphNode <int> node          = new GraphNode <int>(4);
            GraphNode <int> duplicateNode = new GraphNode <int>(5);

            node.AddNeighbor(duplicateNode);
            bool success = node.AddNeighbor(duplicateNode);

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

            if (nodeString.Equals("[Node Value: 4 Neighbors: 5 ]") &&
                node.Neighbors.Count == 1 &&
                !success)
            {
                Console.WriteLine("Passed");
            }
            else
            {
                Console.WriteLine("FAILED!!! Expected: [Node Value: 4  " +
                                  "Neighbors: 5 ], 1, and false Actual: " + nodeString +
                                  ", " + node.Neighbors.Count + " and " +
                                  success);
            }
        }
Esempio n. 3
0
        public bool AddEdge(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))
            {
                return(false);
            }
            else
            {
                node1.AddNeighbor(node2);
                node2.AddNeighbor(node1);
                return(true);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Adds an edge between the nodes with the given values
        /// in the graph. If one or both of the values don't exist
        /// in the graph the method returns false. If an edge
        /// already exists between the nodes the edge isn't added
        /// and the method retruns false
        /// </summary>
        /// <param name="value1">first value to connect</param>
        /// <param name="value2">second value to connect</param>
        /// <returns>true if the edge is added, false otherwise</returns>
        public bool AddEdge(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 already exists
                return(false);
            }
            else
            {
                // undirected graph, so add as neighbors to each other
                node1.AddNeighbor(node2);
                node2.AddNeighbor(node1);
                return(true);
            }
        }