Ejemplo n.º 1
0
        static bool CheckFriendConnectionBFS(string name1, string name2, SocialNetwork sn)
        {
            Queue<Panda> q = new Queue<Panda>();
            HashSet<Panda> visited = new HashSet<Panda>();

            q.Enqueue(sn[name1]);
            visited.Add(sn[name1]);

            Console.WriteLine("Starting from " + name1);
            while(q.Count > 0)
            {
                Panda current = q.Dequeue();
                Console.WriteLine("Currently at " + current.Name);
                foreach(Panda friend in current.Friends)
                {
                    if(friend.Name == name2)
                    {
                        return true;
                    }

                    if (!visited.Contains(friend))
                    {
                        Console.WriteLine("Added friend " + friend.Name);
                        q.Enqueue(friend);
                        visited.Add(friend);
                    }
                }
            }

            return false;
        }
Ejemplo n.º 2
0
        static bool CheckFriendConnectionBFS(string name1, string name2, SocialNetwork sn)
        {
            Queue <Panda>   q       = new Queue <Panda>();
            HashSet <Panda> visited = new HashSet <Panda>();

            q.Enqueue(sn[name1]);
            visited.Add(sn[name1]);

            Console.WriteLine("Starting from " + name1);
            while (q.Count > 0)
            {
                Panda current = q.Dequeue();
                Console.WriteLine("Currently at " + current.Name);
                foreach (Panda friend in current.Friends)
                {
                    if (friend.Name == name2)
                    {
                        return(true);
                    }

                    if (!visited.Contains(friend))
                    {
                        Console.WriteLine("Added friend " + friend.Name);
                        q.Enqueue(friend);
                        visited.Add(friend);
                    }
                }
            }

            return(false);
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            SocialNetwork sn = new SocialNetwork();

            sn.Users.Add(new Panda("Panda 1"));
            sn.Users.Add(new Panda("Panda 2"));
            sn.Users.Add(new Panda("Panda 3"));
            sn.Users.Add(new Panda("Panda 4"));
            sn.Users.Add(new Panda("Panda 5"));
            sn.Users.Add(new Panda("Panda 6"));

            sn["Panda 1"].Friends.Add(sn["Panda 2"]);
            sn["Panda 2"].Friends.Add(sn["Panda 1"]);

            sn["Panda 3"].Friends.Add(sn["Panda 2"]);
            sn["Panda 2"].Friends.Add(sn["Panda 3"]);

            sn["Panda 4"].Friends.Add(sn["Panda 2"]);
            sn["Panda 2"].Friends.Add(sn["Panda 4"]);

            sn["Panda 5"].Friends.Add(sn["Panda 6"]);
            sn["Panda 6"].Friends.Add(sn["Panda 5"]);

            sn["Panda 1"].Friends.Add(sn["Panda 5"]);
            sn["Panda 5"].Friends.Add(sn["Panda 1"]);

            bool res = CheckFriendConnectionBFS("Panda 1", "Panda 6", sn);

            Console.WriteLine(res);
            Console.WriteLine("Finished BFS");
            Console.WriteLine();

            res = CheckFriendConnectionDFS("Panda 1", "Panda 6", sn);
            Console.WriteLine(res);
            Console.WriteLine("Finished DFS");
            Console.WriteLine();


            res = CheckFriendConnectionDFSRecursive(sn["Panda 1"], "Panda 6", new HashSet <Panda>());
            Console.WriteLine(res);
            Console.WriteLine("Finished Recursive DFS");
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            SocialNetwork sn = new SocialNetwork();
            sn.Users.Add(new Panda("Panda 1"));
            sn.Users.Add(new Panda("Panda 2"));
            sn.Users.Add(new Panda("Panda 3"));
            sn.Users.Add(new Panda("Panda 4"));
            sn.Users.Add(new Panda("Panda 5"));
            sn.Users.Add(new Panda("Panda 6"));
            
            sn["Panda 1"].Friends.Add(sn["Panda 2"]);
            sn["Panda 2"].Friends.Add(sn["Panda 1"]);

            sn["Panda 3"].Friends.Add(sn["Panda 2"]);
            sn["Panda 2"].Friends.Add(sn["Panda 3"]);

            sn["Panda 4"].Friends.Add(sn["Panda 2"]);
            sn["Panda 2"].Friends.Add(sn["Panda 4"]);

            sn["Panda 5"].Friends.Add(sn["Panda 6"]);
            sn["Panda 6"].Friends.Add(sn["Panda 5"]);

            sn["Panda 1"].Friends.Add(sn["Panda 5"]);
            sn["Panda 5"].Friends.Add(sn["Panda 1"]);

            bool res = CheckFriendConnectionBFS("Panda 1", "Panda 6", sn);
            Console.WriteLine(res);
            Console.WriteLine("Finished BFS");
            Console.WriteLine();

            res = CheckFriendConnectionDFS("Panda 1", "Panda 6", sn);
            Console.WriteLine(res);
            Console.WriteLine("Finished DFS");
            Console.WriteLine();


            res = CheckFriendConnectionDFSRecursive(sn["Panda 1"], "Panda 6", new HashSet<Panda>());
            Console.WriteLine(res);
            Console.WriteLine("Finished Recursive DFS");

        }