예제 #1
0
        public IDictionary <T, DistanceInfo <T> > BuildDistanceTable(IGraph <T> graph, T source)
        {
            if (graph == null)
            {
                throw new ArgumentNullException(nameof(graph));
            }

            var distances = InitializeDistanceTable(graph, source);

            for (var iteration = 0; iteration < graph.Count; iteration++)
            {
                foreach (var sourceVertex in graph)
                {
                    foreach (var destinationVertex in graph.GetAdjacentVertices(sourceVertex))
                    {
                        var step = StepMetadata <T> .ForPath(sourceVertex, destinationVertex, distances);

                        if (step.FoundShorterDistance() ||
                            step.SameDistanceButLessEdges())
                        {
                            if (CheckingForCycles(graph, iteration))
                            {
                                return(InitializeDistanceTable(graph, source));
                            }

                            distances[destinationVertex] = step.ToSource();
                        }
                    }
                }
            }

            return(distances);
        }
예제 #2
0
        public IDictionary <T, DistanceInfo <T> > BuildDistanceTable(IGraph <T> graph, T source)
        {
            if (graph == null)
            {
                throw new ArgumentNullException(nameof(graph));
            }

            var distances         = InitializeDistanceTable(graph, source);
            var verticesToProcess = new MinBinaryHeap <DistanceInfo <T> >(DistanceInfo <T> .DistanceAndEdgeComparer);
            var processedVertices = new HashSet <T>(graph.Comparer);

            verticesToProcess.Push(DistanceInfo <T> .Zero(source));

            while (verticesToProcess.Count > 0)
            {
                var sourceVertex = verticesToProcess.Pop().Single().PreviousVertex;

                processedVertices.Add(sourceVertex);

                foreach (var destinationVertex in graph.GetAdjacentVertices(sourceVertex))
                {
                    if (processedVertices.Contains(destinationVertex))
                    {
                        continue;
                    }

                    var step = StepMetadata <T> .ForPath(sourceVertex, destinationVertex, distances);

                    if (step.FoundShorterDistance() ||
                        step.SameDistanceButLessEdges())
                    {
                        distances[destinationVertex] = step.ToSource();
                        verticesToProcess.Push(step.ToDestination());
                    }
                }
            }

            return(distances);
        }