Example #1
0
        public static void MainTest(string[] args)
        {
            TextInput           input = new TextInput(args[0]);
            EdgeWeightedDigraph G     = new EdgeWeightedDigraph(input);
            int s = int.Parse(args[1]);

            // compute shortest paths
            DijkstraSP sp = new DijkstraSP(G, s);

            // print shortest path
            for (int t = 0; t < G.V; t++)
            {
                if (sp.HasPathTo(t))
                {
                    Console.Write("{0} to {1} ({2:F2})  ", s, t, sp.DistTo(t));
                    foreach (DirectedEdge e in sp.PathTo(t))
                    {
                        Console.Write(e + "   ");
                    }
                    Console.WriteLine();
                }
                else
                {
                    Console.Write("{0} to {1} no path\n", s, t);
                }
            }
        }
Example #2
0
        // find shortest augmenting path and upate
        private void augment()
        {
            // build residual graph
            EdgeWeightedDigraph G = new EdgeWeightedDigraph(2 * N + 2);
            int s = 2 * N, t = 2 * N + 1;

            for (int i = 0; i < N; i++)
            {
                if (xy[i] == UNMATCHED)
                {
                    G.AddEdge(new DirectedEdge(s, i, 0.0));
                }
            }
            for (int j = 0; j < N; j++)
            {
                if (yx[j] == UNMATCHED)
                {
                    G.AddEdge(new DirectedEdge(N + j, t, py[j]));
                }
            }
            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < N; j++)
                {
                    if (xy[i] == j)
                    {
                        G.AddEdge(new DirectedEdge(N + j, i, 0.0));
                    }
                    else
                    {
                        G.AddEdge(new DirectedEdge(i, N + j, reducedCost(i, j)));
                    }
                }
            }

            // compute shortest path from s to every other vertex
            DijkstraSP spt = new DijkstraSP(G, s);

            // augment along alternating path
            foreach (DirectedEdge e in spt.PathTo(t))
            {
                int i = e.From, j = e.To - N;
                if (i < N)
                {
                    xy[i] = j;
                    yx[j] = i;
                }
            }

            // update dual variables
            for (int i = 0; i < N; i++)
            {
                px[i] += spt.DistTo(i);
            }
            for (int j = 0; j < N; j++)
            {
                py[j] += spt.DistTo(N + j);
            }
        }
Example #3
0
 /// <summary>
 /// Computes a shortest paths tree from each vertex to to every other vertex in
 /// the edge-weighted digraph <c>G</c>.</summary>
 /// <param name="G">the edge-weighted digraph</param>
 /// <exception cref="ArgumentException">if an edge weight is negative</exception>
 /// <exception cref="ArgumentException">unless 0 &lt;= <c>s</c> &lt;= <c>V</c> - 1</exception>
 ///
 public DijkstraAllPairsSP(EdgeWeightedDigraph G)
 {
     all = new DijkstraSP[G.V];
     for (int v = 0; v < G.V; v++)
     {
         all[v] = new DijkstraSP(G, v);
     }
 }