예제 #1
0
        // TODO: What happens if two nodes are at the same "lowest cost"?
        private string FindLowestCostNodeId(FastestPathTable fastestPathTable, HashSet <string> visitedNodeIds)
        {
            int    lowestCost       = int.MaxValue;
            string lowestCostNodeId = null;

            foreach (var row in fastestPathTable.Rows)
            {
                if (!visitedNodeIds.Contains(row.NodeId) && row.LowestCost < lowestCost)
                {
                    lowestCost       = row.LowestCost;
                    lowestCostNodeId = row.NodeId;
                }
            }

            return(lowestCostNodeId);
        }
            public void ShouldGenerateValidTable_WhenGivenUnconnectedGraphNodes()
            {
                // Arrange
                var nodes = GenerateUndirectedGraphNodes();

                nodes.Add(new Node {
                    Id = "F", Neighbors = null
                });

                var expectedResult = new FastestPathTable()
                {
                    PrimaryNodeId = "A",
                    Rows          = new List <FastestPathRow>
                    {
                        new FastestPathRow {
                            NodeId = "A", LowestCost = 0, NeighborNodeId = null
                        },
                        new FastestPathRow {
                            NodeId = "B", LowestCost = 3, NeighborNodeId = "D"
                        },
                        new FastestPathRow {
                            NodeId = "C", LowestCost = 7, NeighborNodeId = "E"
                        },
                        new FastestPathRow {
                            NodeId = "D", LowestCost = 1, NeighborNodeId = "A"
                        },
                        new FastestPathRow {
                            NodeId = "E", LowestCost = 2, NeighborNodeId = "D"
                        },
                        new FastestPathRow {
                            NodeId = "F", LowestCost = int.MaxValue, NeighborNodeId = null
                        }
                    }
                };

                var dijkstraService = CreateService();
                var request         = new FindShortestPathsRequest {
                    Nodes = nodes, PrimaryNodeId = "A"
                };

                // Act
                var results = dijkstraService.FindShortestPaths(request);

                // Assert
                results.Should().BeEquivalentTo(expectedResult);
            }
예제 #3
0
        private FastestPathTable CreateFastestPathTable(List <Node> nodes, string primaryNodeId)
        {
            var fastestPathTable = new FastestPathTable();

            foreach (var node in nodes)
            {
                fastestPathTable.Rows.Add(new FastestPathRow
                {
                    NodeId         = node.Id,
                    LowestCost     = node.Id == primaryNodeId ? 0 : int.MaxValue,
                    NeighborNodeId = null
                });
            }

            fastestPathTable.PrimaryNodeId = primaryNodeId;

            return(fastestPathTable);
        }
            public void ShouldGenerateValidTable_WhenGivenDirectedGraphNodes()
            {
                // Arrange
                var nodes = GenerateUndirectedGraphNodes();

                // Remove the neighbor E from node D to make it directed (D->E)...
                var nodeDNeighbors = nodes.FirstOrDefault(n => n.Id == "D").Neighbors;

                nodeDNeighbors.RemoveAll(n => n.NodeId == "E");

                var expectedResult = new FastestPathTable()
                {
                    PrimaryNodeId = "A",
                    Rows          = new List <FastestPathRow>
                    {
                        new FastestPathRow {
                            NodeId = "A", LowestCost = 0, NeighborNodeId = null
                        },
                        new FastestPathRow {
                            NodeId = "B", LowestCost = 3, NeighborNodeId = "D"
                        },
                        new FastestPathRow {
                            NodeId = "C", LowestCost = 8, NeighborNodeId = "B"
                        },
                        new FastestPathRow {
                            NodeId = "D", LowestCost = 1, NeighborNodeId = "A"
                        },
                        new FastestPathRow {
                            NodeId = "E", LowestCost = 5, NeighborNodeId = "B"
                        }
                    }
                };

                var dijkstraService = CreateService();
                var request         = new FindShortestPathsRequest {
                    Nodes = nodes, PrimaryNodeId = "A"
                };

                // Act
                var results = dijkstraService.FindShortestPaths(request);

                // Assert
                results.Should().BeEquivalentTo(expectedResult);
            }
예제 #5
0
        private List <string> FindPath(FastestPathTable table, string startNodeId, string endNodeId)
        {
            var path = new List <string> {
                startNodeId
            };
            var rowsByNodeId = table.Rows.ToDictionary(r => r.NodeId);

            var currentNodeId = startNodeId;

            while (currentNodeId != endNodeId && currentNodeId != null)
            {
                currentNodeId = rowsByNodeId[currentNodeId].NeighborNodeId;
                path.Add(currentNodeId);
            }

            if (currentNodeId == null)
            {
                path = null;
            }

            return(path);
        }