Exemplo n.º 1
0
        public static List <Node> Dijkstra(Graph graph, Dictionary <Edge, double> weights, Node start, Node end)
        {
            var notVisited = graph.Nodes.ToList();
            var track      = new Dictionary <Node, DijkstraData>();

            track[start] = new DijkstraData {
                Price = 0, Previous = null
            };

            while (true)
            {
                Node toOpen    = null;
                var  bestPrice = double.PositiveInfinity;
                foreach (var e in notVisited)
                {
                    if (track.ContainsKey(e) && track[e].Price < bestPrice)
                    {
                        bestPrice = track[e].Price;
                        toOpen    = e;
                    }
                }

                if (toOpen == null)
                {
                    return(null);
                }
                if (toOpen.Equals(end))
                {
                    end = toOpen;
                    break;
                }

                foreach (var e in toOpen.IncidentEdges.Where(z => z.From == toOpen))
                {
                    var currentPrice = track[toOpen].Price + weights[e];
                    var nextNode     = e.OtherNode(toOpen);
                    if (!track.ContainsKey(nextNode) || track[nextNode].Price > currentPrice)
                    {
                        track[nextNode] = new DijkstraData {
                            Previous = toOpen, Price = currentPrice
                        };
                    }
                }

                notVisited.Remove(toOpen);
            }

            var result = new List <Node>();

            while (end != null)
            {
                result.Add(end);
                end = track[end].Previous;
            }
            result.Reverse();
            return(result);
        }
Exemplo n.º 2
0
        public static DijkstraAnswer DijkstraAlgo(Graph graph, Node start, Node end, List <Node> eliminated)
        {
            var notVisited = graph.Nodes.ToList();
            var track      = new Dictionary <Node, DijkstraData>();

            track[start] = new DijkstraData {
                Price = 0, Previous = null
            };

            while (true)
            {
                Node toOpen    = null;
                var  bestPrice = double.PositiveInfinity;
                foreach (var e in notVisited)
                {
                    if (!eliminated.Contains(e) && track.ContainsKey(e) && track[e].Price < bestPrice)
                    {
                        bestPrice = track[e].Price;
                        toOpen    = e;
                    }
                }

                if (toOpen == null)
                {
                    return(null);
                }
                if (toOpen == end)
                {
                    break;
                }

                foreach (var e in toOpen.IncidentEdges)
                {
                    var currentPrice = track[toOpen].Price + e.Weight;
                    var nextNode     = e.OtherNode(toOpen);
                    if (!track.ContainsKey(nextNode) || currentPrice < track[nextNode].Price)
                    {
                        track[nextNode] = new DijkstraData {
                            Previous = toOpen, Price = currentPrice
                        };
                    }
                }

                notVisited.Remove(toOpen);
            }

            var result = new List <Node>();
            var price  = track[end].Price;

            while (end != null)
            {
                result.Add(end);
                end = track[end].Previous;
            }
            result.Reverse();
            return(new DijkstraAnswer(result, price));
        }