public int Connectionlevel(Panda panda1, Panda panda2) { var inQueue = new Queue<PandaWithLevel>(); var visited = new List<int>(); inQueue.Enqueue(new PandaWithLevel { Level = 0, Panda = panda1.GetHashCode() }); while (inQueue.Count > 0) { var currPandaWithLevel = inQueue.Dequeue(); if (currPandaWithLevel.Panda == panda2.GetHashCode()) { return currPandaWithLevel.Level; } if (!visited.Contains(currPandaWithLevel.Panda)) { visited.Add(currPandaWithLevel.Panda); } var currPanda = _pandaUsers.FirstOrDefault(p => p.GetHashCode() == currPandaWithLevel.Panda.GetHashCode()); if (currPanda != null && currPanda.Friends != null && currPanda.Friends.Count > 0) { foreach (var friend in currPanda.Friends) { if (!visited.Contains(friend.GetHashCode())) { inQueue.Enqueue(new PandaWithLevel { Level = currPandaWithLevel.Level + 1, Panda = friend.GetHashCode() }); } } } } return -1; }
public int HowManyGenderInNetwork(int level, Panda panda, GenderType gender) { var inQueue = new Queue<PandaWithLevel>(); var visited = new List<int>(); var genderCounter = 0; inQueue.Enqueue(new PandaWithLevel { Level = 0, Panda = panda.GetHashCode() }); foreach (var friend in panda.Friends) { inQueue.Enqueue(new PandaWithLevel { Level = 1, Panda = friend.GetHashCode() }); } while (inQueue.Count > 0) { var currPandaWithLevel = inQueue.Dequeue(); var currPanda = _pandaUsers.FirstOrDefault(p => p.GetHashCode() == currPandaWithLevel.Panda.GetHashCode()); if (!visited.Contains(currPanda.GetHashCode()) && currPanda.gender == gender) { visited.Add(currPandaWithLevel.Panda); genderCounter++; } if (currPandaWithLevel.Level < level) { foreach (var friend in currPanda.Friends) { if (!visited.Contains(friend.GetHashCode())) { inQueue.Enqueue(new PandaWithLevel { Level = currPandaWithLevel.Level + 1, Panda = friend.GetHashCode() }); } } } } return genderCounter; }