public void BuildGraph_InvalidValuesInRow_EmptyGraph()
        {
            string[] rows = { "1", "random random2", "1 5 9", "4 5 2 3" };

            var actualGraph = _graphBuilder.BuildGraph(rows);

            Assert.IsNull(actualGraph.RootNode); // if the graph is empty, the root node will always be Null
        }
Пример #2
0
        public ICollection <int> GetShortestPath(string idFromValue, string idToValue)
        {
            Activator.Initialise();

            int idFrom, idTo;

            if (!int.TryParse(idFromValue, out idFrom) ||
                !int.TryParse(idToValue, out idTo))
            {
                return(new List <int>());
            }

            IGraphStorageService  storage      = ServiceLocator.Current.GetInstance <IGraphStorageService>();
            IGraphBuilder         builder      = ServiceLocator.Current.GetInstance <IGraphBuilder>();
            IShortestPathStrategy pathStrategy = ServiceLocator.Current.GetInstance <IShortestPathStrategy>();

            Domain.IGraph graph    = builder.BuildGraph(storage.RetrieveGraph());
            IGraphNode    fromNode = graph.GetNode(idFrom);
            IGraphNode    toNode   = graph.GetNode(idTo);

            if (fromNode == null || toNode == null)
            {
                return(new List <int>());
            }

            return(pathStrategy.FindShortestPath(graph, fromNode, toNode));
        }
Пример #3
0
        public void Run(string[] args)
        {
            if (args.Length != 4)
            {
                Console.WriteLine($"Only {args.Length} arguments supplied on the command line.");
                Console.WriteLine("Please use: WordLadder DictionaryFile StartWord EndWord ResultFile");
            }

            try
            {
                string dictionaryFileName = args[0];
                string startWord          = args[1];
                string endWord            = args[2];
                string ladderFileName     = args[3];

                var  dictionary = _fileIO.ParseFile(dictionaryFileName);
                var  graph      = _grapgBuilder.BuildGraph(dictionary);
                var  distances  = _ladderBuilder.FindDistances(startWord, endWord, graph);
                var  path       = _ladderBuilder.BuildPath(startWord, endWord, distances);
                bool saved      = _fileIO.SaveLadder(path, ladderFileName);
                if (saved)
                {
                    Console.WriteLine($"Saved the word ladder to {ladderFileName}.");
                }
                else
                {
                    Console.WriteLine("Failed to create the ladder.");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error calculating the ladder.");
                Console.WriteLine($"{e.Message} at {e.StackTrace}");
            }
        }