Ejemplo n.º 1
0
        public void GetShortestPath_GivenTwoPaths_ReturnsOnlyShorterOne()
        {
            Graph graph = new Graph();

            UserNode user1 = new UserNode("A", "A");
            UserNode user2 = new UserNode("A", "B");
            UserNode user3 = new UserNode("A", "C");

            user1.AddFriend(user2);
            user2.AddFriend(user3);
            user1.AddFriend(user3);

            graph.Add(user1);
            graph.Add(user2);
            graph.Add(user3);

            var paths = graph.GetShortestPaths(user1, user3);

            int resultPathsSize   = paths.Count;
            int expectedPathsSize = 1;

            Assert.AreEqual(expectedPathsSize, resultPathsSize);

            var resultPath   = paths[0];
            var expectedPath = new LinkedList <UserNode>(new UserNode[] { user1, user3 });

            CollectionAssert.AreEqual(expectedPath, resultPath);
        }
Ejemplo n.º 2
0
        public void GetDistance_GivenNoCommonFriendsAtWithDistanceOf3WithMultipleRoutes_Returns3()
        {
            Graph graph = new Graph();

            UserNode user1  = new UserNode("A", "A");
            UserNode user2  = new UserNode("A", "B");
            UserNode user2b = new UserNode("A", "C");
            UserNode user3  = new UserNode("A", "D");
            UserNode user3b = new UserNode("A", "E");
            UserNode user3c = new UserNode("A", "F");
            UserNode user4  = new UserNode("A", "G");

            user1.AddFriend(user2);
            user1.AddFriend(user2b);
            user2.AddFriend(user3b);
            user3.AddFriend(user2);
            user3.AddFriend(user2b);
            user3.AddFriend(user3b);
            user3.AddFriend(user3c);
            user4.AddFriend(user3);

            graph.Add(user1);
            graph.Add(user2);
            graph.Add(user2b);
            graph.Add(user3);
            graph.Add(user3b);
            graph.Add(user3c);
            graph.Add(user4);

            int distance = graph.GetDistance(user1, user4);
            int expected = 3;

            Assert.AreEqual(expected, distance);
        }
        private static void AllPathsCyclicGraph()
        {
            UserNode NodeA = new UserNode("Node", "A");
            UserNode NodeB = new UserNode("Node", "B");
            UserNode NodeC = new UserNode("Node", "C");
            UserNode NodeD = new UserNode("Node", "D");

            NodeA.AddFriend(NodeB);
            NodeA.AddFriend(NodeC);
            NodeB.AddFriend(NodeD);
            NodeB.AddFriend(NodeC);
            NodeC.AddFriend(NodeD);

            List <UserNode> graph = new List <UserNode>()
            {
                NodeA, NodeB, NodeC, NodeD
            };

            foreach (var node in graph)
            {
                foreach (var targetNode in graph)
                {
                    Console.WriteLine($"\nPath(s) from node {node} to {targetNode} is:");
                    var currentPaths = BFS.BFS.GetAllPaths(graph, node, targetNode);

                    foreach (var path in currentPaths)
                    {
                        Console.Write("Path: ");
                        foreach (var pathMemeber in path)
                        {
                            Console.Write($"{pathMemeber}, ");
                        }
                        Console.WriteLine();
                    }
                }
                Console.WriteLine('\n');
            }
            //var paths = BFS.BFS.GetAllPaths(graph, NodeA, NodeB);
            //Console.WriteLine("DDB");
            //Console.WriteLine(paths);
            //foreach (var path in paths)
            //{
            //    foreach(var pathMember in path)
            //    {
            //        Console.Write($"{pathMember},");
            //    }
            //    Console.WriteLine();
            //}
        }
Ejemplo n.º 4
0
        public void GetShortestPath_GivenTwoEqualPaths_ReturnsBoth()
        {
            Graph graph = new Graph();

            UserNode user1  = new UserNode("A", "A");
            UserNode user2  = new UserNode("A", "B");
            UserNode user2b = new UserNode("A", "B");
            UserNode user3  = new UserNode("A", "C");

            user1.AddFriend(user2);
            user1.AddFriend(user2b);
            user2b.AddFriend(user3);
            user2.AddFriend(user3);

            graph.Add(user1);
            graph.Add(user2);
            graph.Add(user2b);
            graph.Add(user3);

            var paths = graph.GetShortestPaths(user1, user3);

            var expectedPath1 = new LinkedList <UserNode>(new UserNode[] { user1, user2, user3 });
            var expectedPath2 = new LinkedList <UserNode>(new UserNode[] { user1, user2b, user3 });

            bool path1Found = false;
            bool path2Found = false;

            foreach (var list in paths)
            {
                if (list.SequenceEqual(expectedPath1))
                {
                    path1Found = true;
                }

                if (list.SequenceEqual(expectedPath2))
                {
                    path2Found = true;
                }
            }

            Assert.IsTrue(path1Found && path2Found);
        }
Ejemplo n.º 5
0
        public void GetFriendsOfFriends_GivenADepthOf2_ReturnsFriendsOfFriends()
        {
            Graph graph = new Graph();

            UserNode user1  = new UserNode("A", "A");
            UserNode user2  = new UserNode("A", "B");
            UserNode user2b = new UserNode("A", "C");
            UserNode user3  = new UserNode("A", "D");

            user1.AddFriend(user2);
            user1.AddFriend(user2b);
            user2.AddFriend(user3);

            graph.Add(user1);
            graph.Add(user2);
            graph.Add(user2b);
            graph.Add(user3);

            List <UserNode> friends = graph.GetFriendsOfFriends(user1, 2);

            Assert.IsTrue(friends.Contains(user2) && friends.Contains(user2b) && friends.Contains(user3));
        }
 private void GenerateTree(UserNode user, List <UserNode> allUsers, int depth)
 {
     if (depth == 0)
     {
         return;
     }
     for (int i = 0; i < depth; i++)
     {
         UserNode newUser = GenerateNewUser();
         user.AddFriend(newUser);
         allUsers.Add(newUser);
         GenerateTree(newUser, allUsers, depth - 1);
     }
 }
Ejemplo n.º 7
0
        public void GetFriendsOfFriends_GivenADepthOf1_ContainsFriendsOnlyOnce()
        {
            Graph graph = new Graph();

            UserNode user1  = new UserNode("A", "A");
            UserNode user2  = new UserNode("A", "B");
            UserNode user2b = new UserNode("A", "C");

            user1.AddFriend(user2);
            user1.AddFriend(user2b);
            user2.AddFriend(user2b);

            graph.Add(user1);
            graph.Add(user2);
            graph.Add(user2b);

            List <UserNode> friends = graph.GetFriendsOfFriends(user1, 1);

            int result   = friends.Count;
            int expected = 2;

            Assert.AreEqual(expected, result);
        }
Ejemplo n.º 8
0
        public void GetDistance_GivenMultipleCommonFriends_Returns2()
        {
            Graph graph = new Graph();

            UserNode user1  = new UserNode("A", "A");
            UserNode user2  = new UserNode("A", "B");
            UserNode user2b = new UserNode("A", "C");
            UserNode user3  = new UserNode("A", "D");

            user1.AddFriend(user2);
            user3.AddFriend(user2);
            user1.AddFriend(user2b);
            user3.AddFriend(user2b);

            graph.Add(user1);
            graph.Add(user2);
            graph.Add(user2b);
            graph.Add(user3);

            int distance = graph.GetDistance(user1, user3);
            int expected = 2;

            Assert.AreEqual(expected, distance);
        }
Ejemplo n.º 9
0
        public void GetDistance_GivenDirectFriends_Returns1()
        {
            Graph graph = new Graph();

            UserNode user1 = new UserNode("A", "A");
            UserNode user2 = new UserNode("A", "C");

            user1.AddFriend(user2);

            graph.Add(user1);
            graph.Add(user2);

            int distance = graph.GetDistance(user1, user2);
            int expected = 1;

            Assert.AreEqual(expected, distance);
        }