Beispiel #1
0
        public Dictionary<string, PersonData> ReadInput(StreamReader _reader, StreamWriter _writer)
        {
            var inputString = _reader.ReadLine().Split('\t');
            string[] meAndMyFriends;

            Name1 = inputString[0];
            Name2 = inputString[1];

            while (!_reader.EndOfStream)
            {
                meAndMyFriends = _reader.ReadLine().Split('\t');
                int size = meAndMyFriends.Length;
                if (size > 1) // There are some friends
                {
                    Person person = new Person();
                    person.Name = meAndMyFriends[0];

                    List<Person> friends = new List<Person>();
                    for (int i = 1; i < size; i++)
                    {
                        if (string.IsNullOrEmpty(meAndMyFriends[i]))
                        {
                            continue;
                        }

                        Person friend = new Person();
                        friend.Name = meAndMyFriends[i];
                        friends.Add(friend);
                    }

                    person.children = friends;
                    PersonData personData = new PersonData();
                    personData.Friend = person;
                    graph.Add(person.Name, personData);
                }
            }
            return graph;
        }
Beispiel #2
0
        public void BFS()
        {
            InputInformation information = new InputInformation();
            graph = information.ReadInput(_reader, _writer);

            Person person = new Person();
            person.Name = information.Name1;
            person.children = graph[person.Name].Friend.children;

            queue.Enqueue(person);

            graph[person.Name].Was = true;

            while (queue.Count > 0)
            {

                Person p = queue.Peek();

                //Thread.Sleep(1000);

                queue.Dequeue();

                if (!graph.ContainsKey(p.Name)) // If graph doesn't contain Key = p.Name
                {
                    continue;
                }
                for (int i = 0; i < graph[p.Name].Friend.children.Count; i++)
                {
                    Person to = graph[p.Name].Friend.children[i];
                    if (graph.ContainsKey(to.Name))
                    {
                        if (graph[to.Name].Was == false)
                        {
                            graph[to.Name].Was = true;
                            queue.Enqueue(to);
                            graph[to.Name].Distance = graph[p.Name].Distance + 1;
                        }
                    }
                    else
                    {
                        PersonData personData = new PersonData();
                        //personData.Friend = new Person();
                        personData.Was = true;
                        graph.Add(to.Name, personData);

                        graph[to.Name].Distance = graph[p.Name].Distance + 1;
                    }
                }
            }
            Message(information.Name1, information.Name2, graph[information.Name2].Distance);

            _reader.Close();
            _writer.Close();
        }