Ejemplo n.º 1
0
    private void relax(EdgeWeightedDigraph edgeWeightedDigraph, int num)
    {
        Iterator iterator = edgeWeightedDigraph.adj(num).iterator();

        while (iterator.hasNext())
        {
            DirectedEdge directedEdge = (DirectedEdge)iterator.next();
            int          num2         = directedEdge.to();
            if (this.distTo[num2] > this.distTo[num] + directedEdge.weight())
            {
                this.distTo[num2] = this.distTo[num] + directedEdge.weight();
                this.edgeTo[num2] = directedEdge;
                if (!this.onQueue[num2])
                {
                    this.queue.enqueue(Integer.valueOf(num2));
                    this.onQueue[num2] = true;
                }
            }
            int  num3    = this.cost;
            bool expr_93 = num3 != 0;
            this.cost = num3 + 1;
            int expr_A4 = edgeWeightedDigraph.V();
            if (expr_A4 == -1 || (expr_93 ? 1 : 0) % expr_A4 == 0)
            {
                this.findNegativeCycle();
            }
        }
    }
    private void relax(DirectedEdge directedEdge)
    {
        int num  = directedEdge.from();
        int num2 = directedEdge.to();

        if (this.distTo[num2] < this.distTo[num] + directedEdge.weight())
        {
            this.distTo[num2] = this.distTo[num] + directedEdge.weight();
            this.edgeTo[num2] = directedEdge;
        }
    }
    private void relax(DirectedEdge directedEdge)
    {
        int num  = directedEdge.from();
        int num2 = directedEdge.to();

        if (this.distTo[num2] > this.distTo[num] + directedEdge.weight())
        {
            this.distTo[num2] = this.distTo[num] + directedEdge.weight();
            this.edgeTo[num2] = directedEdge;
            if (this.pq.contains(num2))
            {
                this.pq.decreaseKey(num2, java.lang.Double.valueOf(this.distTo[num2]));
            }
            else
            {
                this.pq.insert(num2, java.lang.Double.valueOf(this.distTo[num2]));
            }
        }
    }
    public DijkstraSP(EdgeWeightedDigraph ewd, int i)
    {
        Iterator iterator = ewd.edges().iterator();

        while (iterator.hasNext())
        {
            DirectedEdge directedEdge = (DirectedEdge)iterator.next();
            if (directedEdge.weight() < (double)0f)
            {
                string arg_5E_0 = new StringBuilder().append("edge ").append(directedEdge).append(" has negative weight").toString();

                throw new ArgumentException(arg_5E_0);
            }
        }
        this.distTo = new double[ewd.V()];
        this.edgeTo = new DirectedEdge[ewd.V()];
        for (int j = 0; j < ewd.V(); j++)
        {
            this.distTo[j] = double.PositiveInfinity;
        }
        this.distTo[i] = (double)0f;
        this.pq        = new IndexMinPQ(ewd.V());
        this.pq.insert(i, java.lang.Double.valueOf(this.distTo[i]));
        while (!this.pq.isEmpty())
        {
            int      j         = this.pq.delMin();
            Iterator iterator2 = ewd.adj(j).iterator();
            while (iterator2.hasNext())
            {
                DirectedEdge directedEdge2 = (DirectedEdge)iterator2.next();
                this.relax(directedEdge2);
            }
        }
        if (!DijkstraSP.s_assertionsDisabled && !this.check(ewd, i))
        {
            throw new AssertionError();
        }
    }
    private bool check(EdgeWeightedDigraph edgeWeightedDigraph, int num)
    {
        Iterator iterator = edgeWeightedDigraph.edges().iterator();

        while (iterator.hasNext())
        {
            DirectedEdge directedEdge = (DirectedEdge)iterator.next();
            if (directedEdge.weight() < (double)0f)
            {
                System.err.println("negative edge weight detected");
                return(false);
            }
        }
        if (this.distTo[num] != (double)0f || this.edgeTo[num] != null)
        {
            System.err.println("distTo[s] and edgeTo[s] inconsistent");
            return(false);
        }
        for (int i = 0; i < edgeWeightedDigraph.V(); i++)
        {
            if (i != num)
            {
                if (this.edgeTo[i] == null && this.distTo[i] != double.PositiveInfinity)
                {
                    System.err.println("distTo[] and edgeTo[] inconsistent");
                    return(false);
                }
            }
        }
        for (int i = 0; i < edgeWeightedDigraph.V(); i++)
        {
            Iterator iterator2 = edgeWeightedDigraph.adj(i).iterator();
            while (iterator2.hasNext())
            {
                DirectedEdge directedEdge2 = (DirectedEdge)iterator2.next();
                int          num2          = directedEdge2.to();
                if (this.distTo[i] + directedEdge2.weight() < this.distTo[num2])
                {
                    System.err.println(new StringBuilder().append("edge ").append(directedEdge2).append(" not relaxed").toString());
                    return(false);
                }
            }
        }
        for (int i = 0; i < edgeWeightedDigraph.V(); i++)
        {
            if (this.edgeTo[i] != null)
            {
                DirectedEdge directedEdge = this.edgeTo[i];
                int          num3         = directedEdge.from();
                if (i != directedEdge.to())
                {
                    return(false);
                }
                if (this.distTo[num3] + directedEdge.weight() != this.distTo[i])
                {
                    System.err.println(new StringBuilder().append("edge ").append(directedEdge).append(" on shortest path not tight").toString());
                    return(false);
                }
            }
        }
        return(true);
    }
Ejemplo n.º 6
0
 private bool check(EdgeWeightedDigraph edgeWeightedDigraph, int num)
 {
     if (this.hasNegativeCycle())
     {
         double   num2     = (double)0f;
         Iterator iterator = this.negativeCycle().iterator();
         while (iterator.hasNext())
         {
             DirectedEdge directedEdge = (DirectedEdge)iterator.next();
             num2 += directedEdge.weight();
         }
         if (num2 >= (double)0f)
         {
             System.err.println(new StringBuilder().append("error: weight of negative cycle = ").append(num2).toString());
             return(false);
         }
     }
     else
     {
         if (this.distTo[num] != (double)0f || this.edgeTo[num] != null)
         {
             System.err.println("distanceTo[s] and edgeTo[s] inconsistent");
             return(false);
         }
         for (int i = 0; i < edgeWeightedDigraph.V(); i++)
         {
             if (i != num)
             {
                 if (this.edgeTo[i] == null && this.distTo[i] != double.PositiveInfinity)
                 {
                     System.err.println("distTo[] and edgeTo[] inconsistent");
                     return(false);
                 }
             }
         }
         for (int i = 0; i < edgeWeightedDigraph.V(); i++)
         {
             Iterator iterator2 = edgeWeightedDigraph.adj(i).iterator();
             while (iterator2.hasNext())
             {
                 DirectedEdge directedEdge2 = (DirectedEdge)iterator2.next();
                 int          num3          = directedEdge2.to();
                 if (this.distTo[i] + directedEdge2.weight() < this.distTo[num3])
                 {
                     System.err.println(new StringBuilder().append("edge ").append(directedEdge2).append(" not relaxed").toString());
                     return(false);
                 }
             }
         }
         for (int i = 0; i < edgeWeightedDigraph.V(); i++)
         {
             if (this.edgeTo[i] != null)
             {
                 DirectedEdge directedEdge3 = this.edgeTo[i];
                 int          num4          = directedEdge3.from();
                 if (i != directedEdge3.to())
                 {
                     return(false);
                 }
                 if (this.distTo[num4] + directedEdge3.weight() != this.distTo[i])
                 {
                     System.err.println(new StringBuilder().append("edge ").append(directedEdge3).append(" on shortest path not tight").toString());
                     return(false);
                 }
             }
         }
     }
     StdOut.println("Satisfies optimality conditions");
     StdOut.println();
     return(true);
 }