public int GetDistanceBetweenTwoUsers(UserNode userOne, UserNode userTwo)
        {
            if (userOne.Equals(userTwo))
            {
                return(0);
            }

            List <UserNode> visitedUsers = new List <UserNode>();
            Queue <KeyValuePair <UserNode, int> > usersToVisit = new Queue <KeyValuePair <UserNode, int> >();

            usersToVisit.Enqueue(new KeyValuePair <UserNode, int>(userOne, 0));

            while (usersToVisit.Count != 0)
            {
                KeyValuePair <UserNode, int> currentUser = usersToVisit.Dequeue();

                visitedUsers.Add(currentUser.Key);

                foreach (UserNode friend in currentUser.Key.Friends)
                {
                    if (visitedUsers.Contains(friend) || usersToVisit.Select(u => u.Key).Contains(friend))
                    {
                        continue;
                    }
                    if (friend.Id.Equals(userTwo.Id))
                    {
                        return(currentUser.Value + 1);
                    }
                    usersToVisit.Enqueue(new KeyValuePair <UserNode, int>(friend, currentUser.Value + 1));
                }
            }
            return(0);
        }
        //NOT WORKING AS EXPECTED
        public List <List <UserNode> > GetShortestPathBetweenTwoUsers(UserNode userOne, UserNode userTwo)
        {
            if (userOne.Equals(userTwo))
            {
                return(null);
            }

            List <KeyValuePair <UserNode, List <UserNode> > >  visitedUsers = new List <KeyValuePair <UserNode, List <UserNode> > >();
            Queue <KeyValuePair <UserNode, List <UserNode> > > usersToVisit = new Queue <KeyValuePair <UserNode, List <UserNode> > >();
            int shortestDistance = GetDistanceBetweenTwoUsers(userOne, userTwo);

            usersToVisit.Enqueue(new KeyValuePair <UserNode, List <UserNode> >(userOne, new List <UserNode> {
                userOne
            }));

            while (usersToVisit.Count != 0)
            {
                KeyValuePair <UserNode, List <UserNode> > currentUser = usersToVisit.Dequeue();

                visitedUsers.Add(currentUser);

                foreach (UserNode friend in currentUser.Key.Friends)
                {
                    if (CanContinueListingVisitedUsers(userTwo, friend, visitedUsers, usersToVisit, currentUser, shortestDistance))
                    {
                        continue;
                    }
                    List <UserNode> path = currentUser.Value.ToList();
                    path.Add(friend);
                    usersToVisit.Enqueue(new KeyValuePair <UserNode, List <UserNode> >(friend, path));
                }
            }

            return(visitedUsers.Where(u => u.Key.Equals(userTwo)).Select(u => u.Value).ToList());
        }
Beispiel #3
0
        private Dictionary <HashSet <UserNode>, int> GetUserDistancesFromUser(UserNode startUser)
        {
            Queue <UserNode>   queue         = new Queue <UserNode>();
            HashSet <UserNode> visited       = new HashSet <UserNode>();
            HashSet <UserNode> dictionaryKey = new HashSet <UserNode>();
            Dictionary <HashSet <UserNode>, int> userDistances = new Dictionary <HashSet <UserNode>, int>();

            HashSet <UserNode> previousUserLayer = new HashSet <UserNode>();
            HashSet <UserNode> currentUserLayer  = new HashSet <UserNode>();

            int distance = -1;

            queue.Enqueue(startUser);
            visited.Add(startUser);
            currentUserLayer.Add(startUser);

            while (queue.Count > 0)
            {
                UserNode currentUser = queue.Dequeue();

                if (!previousUserLayer.Any(user => currentUser.Friends.Contains(user)))
                {
                    if (previousUserLayer.Count > 0)
                    {
                        userDistances = AddKeyToDictionaryWithValue(userDistances, previousUserLayer, distance);
                    }
                    previousUserLayer = new HashSet <UserNode>(currentUserLayer);
                    currentUserLayer.Clear();

                    distance++;
                }

                if (!currentUser.Equals(startUser))
                {
                    currentUserLayer.Add(currentUser);
                }

                foreach (UserNode friend in currentUser.Friends)
                {
                    if (!visited.Contains(friend))
                    {
                        queue.Enqueue(friend);

                        visited.Add(friend);
                    }
                }
            }

            userDistances = AddKeyToDictionaryWithValue(userDistances, previousUserLayer, distance);

            distance++;

            userDistances = AddKeyToDictionaryWithValue(userDistances, currentUserLayer, distance);

            return(userDistances);
        }
 private bool CanContinueListingVisitedUsers(UserNode userTwo,
                                             UserNode friend,
                                             List <KeyValuePair <UserNode,
                                                                 List <UserNode> > > visitedUsers,
                                             Queue <KeyValuePair <UserNode,
                                                                  List <UserNode> > > usersToVisit,
                                             KeyValuePair <UserNode, List <UserNode> > currentUser,
                                             int shortestDistance)
 {
     return(!friend.Equals(userTwo) || visitedUsers.Select(u => u.Key).Contains(friend) ||
            usersToVisit.Select(u => u.Key).Contains(friend) ||
            currentUser.Value.Count > shortestDistance);
 }
        public int DistanceBetweenUsers(UserNode from, UserNode to)
        {
            visited = new List <UserNode>();

            if (from.Equals(to))
            {
                return(0);
            }

            List <UserNode> temp     = new List <UserNode>();
            int             distance = 0;

            searchQueue.Enqueue(from);
            while (searchQueue.Count > 0)
            {
                UserNode nextUser = searchQueue.Dequeue();
                if (nextUser.Equals(to))
                {
                    break;
                }

                if (visited.Contains(nextUser))
                {
                    continue;
                }

                visited.Add(nextUser);

                foreach (var friend in nextUser.Friends)
                {
                    if (!temp.Contains(friend))
                    {
                        temp.Add(friend);
                    }
                }

                if (searchQueue.Count == 0 && temp.Count > 0)
                {
                    distance++;
                    foreach (var user in temp)
                    {
                        searchQueue.Enqueue(user);
                    }
                    temp.Clear();
                }
            }

            return(distance);
        }
        public static int FindPath(List <UserNode> graph, UserNode start, UserNode end)
        {
            if (start.Equals(end))
            {
                return(0);
            }
            InitializeVisited(graph);

            Queue <UserNode> searchQueue = new Queue <UserNode>();

            searchQueue.Enqueue(start);
            visitedNodes[start] = true;
            Dictionary <UserNode, int> nodePerLevel = new Dictionary <UserNode, int>();

            nodePerLevel.Add(start, 0);
            while (searchQueue.Count != 0)
            {
                var currentFirst = searchQueue.Dequeue();


                foreach (var friend in currentFirst.Friends)
                {
                    if (friend.Equals(end))
                    {
                        return(nodePerLevel[currentFirst] + 1);
                    }
                    else
                    {
                        if (visitedNodes[friend] == false)
                        {
                            searchQueue.Enqueue(friend);
                            visitedNodes[friend] = true;
                            nodePerLevel.Add(friend, nodePerLevel[currentFirst] + 1);
                        }
                    }
                }
            }

            return(-1);
        }