Ejemplo n.º 1
0
        // Relax(u, v, w):
        // if(d[v] > d[u] + w(u, v))
        //    d[v] = d[u] + w(u, v)

        private void dijkstra(EdgeWeightedDigraph graph, int vertex)
        {
            pq.Insert(vertex, 0.0);

            while (!pq.IsEmpty())
            {
                int v = pq.DelMin();
                foreach (DirectedWeightedEdge edge in graph.Adj(v))
                {
                    Relax(edge);
                }
            }
        }
Ejemplo n.º 2
0
 private void SPBellmanFord(EdgeWeightedDigraph graph)
 {
     for (int pass = 0; pass < graph.V(); pass++)
     {
         for (int v = 0; v < graph.V(); v++)
         {
             foreach (var edge in graph.Adj(v))
             {
                 Relax(edge);
             }
         }
     }
 }
Ejemplo n.º 3
0
        public static void DisplayEWDGraph(EdgeWeightedDigraph g)
        {
            Console.WriteLine("{0} verticies, {1} edges", g.V(), g.E());

            for (int i = 0; i < g.V(); ++i)
            {
                Console.Write("{0}: ", i);
                foreach (var u in g.Adj(i))
                {
                    Console.Write("[{0}-{1},{2}] ", u.From(), u.To(), u.Weight());
                }
                Console.WriteLine();
            }
        }
Ejemplo n.º 4
0
        private void AstarSP(EdgeWeightedDigraph graph, int vertex, int target)
        {
            pq.Insert(vertex, 0.0);

            while (!pq.IsEmpty())
            {
                int v = pq.DelMin();

                if (v == target)
                {
                    break;
                }

                foreach (DirectedWeightedEdge edge in graph.Adj(v))
                {
                    Relax(edge, target);
                }
            }
        }