public DagShortestPaths(EdgeWeightedDigraph g, int source) : base(g, source)
        {
            TopologicalSort topological = new TopologicalSort(g);
            List <int>      list        = topological.GetTopoSort();

            foreach (var i in list)
            {
                foreach (DirectedEdge e in g.Adj(i))
                {
                    base.Relax(e);
                }
            }
        }
Exemple #2
0
 private void dfs(EdgeWeightedDigraph g, int s)
 {
     nodes[s].Color      = VertexColor.Gray;
     nodes[s].Discovered = ++time;
     foreach (DirectedEdge e in g.Adj(s))
     {
         int w = e.To;
         if (nodes[w].Color == VertexColor.White)
         {
             nodes[w].Parent = nodes[s];
             dfs(g, w);
         }
     }
     nodes[s].Color    = VertexColor.Black;
     nodes[s].Finished = ++time;
     postOrder.Enqueue(s);
 }
Exemple #3
0
        public DijkstraAlgorithm(EdgeWeightedDigraph digraph, int source) : base(digraph, source)
        {
            //pq maintain a set of vertex need to deal with.
            IndexMinPQ <double> pq = new IndexMinPQ <double>(digraph.V);

            pq.Insert(source, distTo[source]);

            while (!pq.IsEmpty())
            {
                //when v pops up, the distance and path to v have been confirmed
                int v = pq.DelMin();
                foreach (DirectedEdge e in digraph.Adj(v))
                {
                    Relax(pq, e);
                }
            }
        }