Ejemplo n.º 1
0
        ///<summary>
        /// Finds path from start to end by building a graph using BFS traversal
        ///</summary>
        public async Task <Models.Race> FindPath(string startUrl, string endUrl)
        {
            var visited = new HashSet <string>();

            var startArticleTitle = ParseTitle(startUrl);
            var endArticleTitle   = ParseTitle(endUrl);


            visited.Add(startArticleTitle);
            var queue = new Queue <Models.Article>();

            queue.Enqueue(new Models.Article()
            {
                Title  = startArticleTitle,
                Parent = null
            });

            Models.Article currentArticle            = null;
            int            numberOfArticlesTraversed = 0;

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

                visited.Add(currentArticle.Title);

                if (++numberOfArticlesTraversed >= _maxNumberOfArticlesTraversed)
                {
                    throw new Exception("Maximm number of articles traversed reached");
                }

                var links = await _proxy.GetArticleLinks(currentArticle.Title);

                foreach (var link in links)
                {
                    if (!visited.Contains(link))
                    {
                        var linkedArticle = new Models.Article()
                        {
                            Title  = link,
                            Parent = currentArticle
                        };

                        queue.Enqueue(linkedArticle);

                        if (link == endArticleTitle)
                        {
                            return(new Models.Race()
                            {
                                Start = startUrl,
                                End = endUrl,
                                Path = _pathConstructor.ConstructPath(linkedArticle)
                            });
                        }
                    }
                }
            }

            throw new Exception("Path does not exist");
        }
Ejemplo n.º 2
0
        public List <string> ConstructPath(Models.Article currentArticle)
        {
            var path = new List <string>();

            while (currentArticle != null)
            {
                path.Add(_ArticleUrl + currentArticle.Title);
                currentArticle = currentArticle.Parent;
            }

            //since we traverse from the end back to start we need to reverse the path
            path.Reverse();

            return(path);
        }