Пример #1
0
        /// <summary>
        /// Dijkstra algorithm (shortest path) based on graph g for start vertice s. Positive cycles are allowed in shortest path algorithm
        /// </summary>
        /// <param name="g">Graph for search</param>
        /// <param name="s">Start vertice</param>
        public static PathStats ShortestPath(GraphBase g, int s)
        {
            var ps = new PathStats(g.V);

            for (var i = 0; i < ps.Dist.Length; i++)
            {
                ps.Dist[i] = int.MaxValue;
                ps.Prev[i] = -1;
            }

            ps.Dist[s] = 0;//start vertice

            var pq = new IndexMinPQ<Distance>(ps.Dist.Length);
            for (int i = 0; i < ps.Dist.Length; i++)
            {
                pq.Insert(i, new Distance { V = i, Dist = ps.Dist[i] });
            }

            while (!pq.IsEmpty())
            {
                var v = pq.DelRoot();

                foreach (var e in g.Adjacent(v))
                {
                    if (ps.Dist[e.V2] > ps.Dist[v] + e.Weight)
                    {
                        ps.Dist[e.V2] = ps.Dist[v] + e.Weight;
                        ps.Prev[e.V2] = v;
                        pq.ChangeKey(e.V2, new Distance { V = e.V2, Dist = ps.Dist[e.V2] });
                    }
                }
            }

            return ps;
        }