private void dfs(EdgeWeightedDigraph edgeWeightedDigraph, int num)
    {
        this.onStack[num] = true;
        this.marked[num]  = true;
        Iterator iterator = edgeWeightedDigraph.adj(num).iterator();

        while (iterator.hasNext())
        {
            DirectedEdge directedEdge = (DirectedEdge)iterator.next();
            int          num2         = directedEdge.to();
            if (this.cycle != null)
            {
                return;
            }
            if (!this.marked[num2])
            {
                this.edgeTo[num2] = directedEdge;
                this.dfs(edgeWeightedDigraph, num2);
            }
            else if (this.onStack[num2])
            {
                this.cycle = new Stack();
                while (directedEdge.from() != num2)
                {
                    this.cycle.push(directedEdge);
                    directedEdge = this.edgeTo[directedEdge.from()];
                }
                this.cycle.push(directedEdge);
            }
        }
        this.onStack[num] = false;
    }
    public virtual void addEdge(DirectedEdge de)
    {
        int num = de.from();

        this.adj[num].add(de);
        this.E++;
    }
Exemplo n.º 3
0
    // certify that digraph is either acyclic or has a directed cycle
    private bool check()
    {
        // edge-weighted digraph is cyclic
        if (hasCycle())
        {
            // verify cycle
            DirectedEdge first = null, last = null;
            foreach (DirectedEdge e in Cycle())
            {
                if (first == null)
                {
                    first = e;
                }
                if (last != null)
                {
                    if (last.to() != e.from())
                    {
                        throw new System.Exception("cycle edges " + last + " and " + e + " not incident\n");
                        return(false);
                    }
                }
                last = e;
            }

            if (last.to() != first.from())
            {
                throw new System.Exception("cycle edges " + last + " and " + first + " not incident\n");
                return(false);
            }
        }


        return(true);
    }
Exemplo n.º 4
0
    // relax edge e
    private void relax(DirectedEdge e)
    {
        int v = e.from(), w = e.to();

        if (distTo[w] > distTo[v] + e.Weight())
        {
            distTo[w] = distTo[v] + e.Weight();
            edgeTo[w] = e;
        }
    }
Exemplo n.º 5
0
    public void addEdge(DirectedEdge e)
    {
        int v = e.from();
        int w = e.to();

        validateVertex(v);
        validateVertex(w);
        adj[v].Add(e);
        indegree[w]++;
        this.e++;
    }
    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;
        }
    }
    public virtual void addEdge(DirectedEdge de)
    {
        int num  = de.from();
        int num2 = de.to();

        if (this.adj[num][num2] == null)
        {
            this.E++;
            this.adj[num][num2] = de;
        }
    }
Exemplo n.º 8
0
    private void dfs(EdgeWeightedDigraph G, int v)
    {
        onStack[v] = true;
        marked[v]  = true;
        foreach (DirectedEdge e in G.Adj(v))
        {
            int w = e.to();

            // short circuit if directed cycle found
            if (cycle != null)
            {
                return;
            }

            // found new vertex, so recur
            else if (!marked[w])
            {
                edgeTo[w] = e;
                dfs(G, w);
            }

            // trace back directed cycle
            else if (onStack[w])
            {
                cycle = new Stack <DirectedEdge>();

                DirectedEdge f = e;
                while (f.from() != w)
                {
                    cycle.push(f);
                    f = edgeTo[f.from()];
                }
                cycle.push(f);

                return;
            }
        }

        onStack[v] = false;
    }
Exemplo n.º 9
0
 /**
  * Returns a shortest path from the source {@code s} to vertex {@code v}.
  * @param  v the destination vertex
  * @return a shortest path from the source {@code s} to vertex {@code v}
  *         as an iterable of edges, and {@code null} if no such path
  * @throws UnsupportedOperationException if there is a negative cost cycle reachable
  *         from the source vertex {@code s}
  * @throws IllegalArgumentException unless {@code 0 <= v < V}
  */
 public Stack<DirectedEdge> PathTo(int v)
 {
     validateVertex(v);
     if (hasNegativeCycle())
         throw new System.Exception("Negative cost cycle exists");
     if (!hasPathTo(v)) return null;
     Stack<DirectedEdge> path = new Stack<DirectedEdge>();
     for (DirectedEdge e = edgeTo[v]; e != null; e = edgeTo[e.from()])
     {
         path.push(e);
     }
     return path;
 }
Exemplo n.º 10
0
    public Stack <DirectedEdge> pathTo(int v)
    {
        validateVertex(v);
        if (!hasPathTo(v))
        {
            return(null);
        }
        Stack <DirectedEdge> path = new Stack <DirectedEdge>();

        for (DirectedEdge e = edgeTo[v]; e != null; e = edgeTo[e.from()])
        {
            path.push(e);
        }
        return(path);
    }
Exemplo n.º 11
0
    void Start()
    {
        string[] lines = txt.text.Split(new char[] { '\n' });

        // V currencies
        int V = int.Parse(lines[0]);

        string[] name = new string[V];

        // create complete network
        EdgeWeightedDigraph G = new EdgeWeightedDigraph(V);

        for (int v = 0; v < V; v++)
        {
            string[] lineStrs = lines[v + 1].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            name[v] = lineStrs[0];
            for (int w = 0; w < V; w++)
            {
                double       rate = double.Parse(lineStrs[w + 1]);
                DirectedEdge e    = new DirectedEdge(v, w, -Math.Log(rate));
                G.addEdge(e);
            }
        }

        // find negative cycle
        BellmanFordSP spt = new BellmanFordSP(G, 0);

        if (spt.hasNegativeCycle())
        {
            double stake = 1000.0;
            foreach (DirectedEdge e in spt.negativeCycle())
            {
                string str = (stake + " " + name[e.from()] + " ");
                stake *= Math.Exp(-e.Weight());
                str   += ("= " + stake + " " + name[e.to()] + "\n");
                print(str);
            }

            print("以1000元为本金,该负权重环一圈套利" + (stake - 1000));
        }
        else
        {
            print("No arbitrage opportunity");
        }
    }
Exemplo n.º 12
0
    // relax edge e and update pq if changed
    private void relax(DirectedEdge e)
    {
        int v = e.from(), w = e.to();

        if (distTo[w] > distTo[v] + e.Weight())
        {
            distTo[w] = distTo[v] + e.Weight();
            edgeTo[w] = e;
            if (pq.contains(w))
            {
                pq.decreaseKey(w, distTo[w]);
            }
            else
            {
                pq.insert(w, distTo[w]);
            }
        }
    }
    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]));
            }
        }
    }
 private bool check(EdgeWeightedDigraph edgeWeightedDigraph)
 {
     if (this.hasCycle())
     {
         DirectedEdge directedEdge  = null;
         DirectedEdge directedEdge2 = null;
         Iterator     iterator      = this.cycle().iterator();
         while (iterator.hasNext())
         {
             DirectedEdge directedEdge3 = (DirectedEdge)iterator.next();
             if (directedEdge == null)
             {
                 directedEdge = directedEdge3;
             }
             if (directedEdge2 != null && directedEdge2.to() != directedEdge3.from())
             {
                 System.err.printf("cycle edges %s and %s not incident\n", new object[]
                 {
                     directedEdge2,
                     directedEdge3
                 });
                 return(false);
             }
             directedEdge2 = directedEdge3;
         }
         if (directedEdge2.to() != directedEdge.from())
         {
             System.err.printf("cycle edges %s and %s not incident\n", new object[]
             {
                 directedEdge2,
                 directedEdge
             });
             return(false);
         }
     }
     return(true);
 }
/*	[Signature("(I)Ljava/lang/Iterable<LDirectedEdge;>;")]*/

    public virtual Iterable pathTo(int i)
    {
        if (!this.hasPathTo(i))
        {
            return(null);
        }
        Stack stack = new Stack();

        for (DirectedEdge directedEdge = this.edgeTo[i]; directedEdge != null; directedEdge = this.edgeTo[directedEdge.from()])
        {
            stack.push(directedEdge);
        }
        return(stack);
    }
Exemplo n.º 16
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);
 }
Exemplo n.º 17
0
/*	[Signature("(I)Ljava/lang/Iterable<LDirectedEdge;>;")]*/

    public virtual Iterable pathTo(int i)
    {
        if (this.hasNegativeCycle())
        {
            string arg_12_0 = "Negative cost cycle exists";

            throw new NotSupportedException(arg_12_0);
        }
        if (!this.hasPathTo(i))
        {
            return(null);
        }
        Stack stack = new Stack();

        for (DirectedEdge directedEdge = this.edgeTo[i]; directedEdge != null; directedEdge = this.edgeTo[directedEdge.from()])
        {
            stack.push(directedEdge);
        }
        return(stack);
    }
Exemplo n.º 18
0
    // check optimality conditions: either 
    // (i) there exists a negative cycle reacheable from s
    //     or 
    // (ii)  for all edges e = v->w:            distTo[w] <= distTo[v] + e.weight()
    // (ii') for all edges e = v->w on the SPT: distTo[w] == distTo[v] + e.weight()
    private bool check(EdgeWeightedDigraph G, int s)
    {

        // has a negative cycle
        if (hasNegativeCycle())
        {
            double weight = 0.0;
            foreach (DirectedEdge e in negativeCycle())
            {
                weight += e.Weight();
            }
            if (weight >= 0.0)
            {
               throw new System.Exception("error: weight of negative cycle = " + weight);
                return false;
            }
        }

        // no negative cycle reachable from source
        else
        {

            // check that distTo[v] and edgeTo[v] are consistent
            if (distTo[s] != 0.0 || edgeTo[s] != null)
            {
                throw new System.Exception("distanceTo[s] and edgeTo[s] inconsistent");
                return false;
            }
            for (int v = 0; v < G.V(); v++)
            {
                if (v == s) continue;
                if (edgeTo[v] == null && distTo[v] != double.PositiveInfinity)
                {
                    throw new System.Exception("distTo[] and edgeTo[] inconsistent");
                    return false;
                }
            }

            // check that all edges e = v->w satisfy distTo[w] <= distTo[v] + e.weight()
            for (int v = 0; v < G.V(); v++)
            {
                foreach (DirectedEdge e in G.Adj(v))
                {
                    int w = e.to();
                    if (distTo[v] + e.Weight() < distTo[w])
                    {
                        throw new System.Exception("edge " + e + " not relaxed");
                        return false;
                    }
                }
            }

            // check that all edges e = v->w on SPT satisfy distTo[w] == distTo[v] + e.weight()
            for (int w = 0; w < G.V(); w++)
            {
                if (edgeTo[w] == null) continue;
                DirectedEdge e = edgeTo[w];
                int v = e.from();
                if (w != e.to()) return false;
                if (distTo[v] + e.Weight() != distTo[w])
                {
                    throw new System.Exception("edge " + e + " on shortest path not tight");
                    return false;
                }
            }
        }

        print("Satisfies optimality conditions");
        return true;
    }
Exemplo n.º 19
0
    private bool Check(EdgeWeightedDigraph G, int s)
    {
        // check that edge weights are nonnegative
        foreach (DirectedEdge e in G.edges())
        {
            if (e.Weight() < 0)
            {
                throw new System.Exception("negative edge weight detected");
                return(false);
            }
        }

        // check that distTo[v] and edgeTo[v] are consistent
        if (distTo[s] != 0.0 || edgeTo[s] != null)
        {
            throw new System.Exception("distTo[s] and edgeTo[s] inconsistent");
            return(false);
        }
        for (int v = 0; v < G.V(); v++)
        {
            if (v == s)
            {
                continue;
            }
            if (edgeTo[v] == null && distTo[v] != double.PositiveInfinity)
            {
                throw new System.Exception("distTo[] and edgeTo[] inconsistent");
                return(false);
            }
        }

        // check that all edges e = v->w satisfy distTo[w] <= distTo[v] + e.weight()
        for (int v = 0; v < G.V(); v++)
        {
            foreach (DirectedEdge e in G.Adj(v))
            {
                int w = e.to();
                if (distTo[v] + e.Weight() < distTo[w])
                {
                    throw new System.Exception("edge " + e + " not relaxed");
                    return(false);
                }
            }
        }

        // check that all edges e = v->w on SPT satisfy distTo[w] == distTo[v] + e.weight()
        for (int w = 0; w < G.V(); w++)
        {
            if (edgeTo[w] == null)
            {
                continue;
            }
            DirectedEdge e = edgeTo[w];
            int          v = e.from();
            if (w != e.to())
            {
                return(false);
            }
            if (distTo[v] + e.Weight() != distTo[w])
            {
                throw new System.Exception("edge " + e + " on shortest path not tight");
                return(false);
            }
        }
        return(true);
    }
    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);
    }
    private void augment()
    {
        EdgeWeightedDigraph edgeWeightedDigraph = new EdgeWeightedDigraph(2 * this.N + 2);
        int num  = 2 * this.N;
        int num2 = 2 * this.N + 1;

        for (int i = 0; i < this.N; i++)
        {
            if (this.xy[i] == -1)
            {
                edgeWeightedDigraph.addEdge(new DirectedEdge(num, i, (double)0f));
            }
        }
        for (int i = 0; i < this.N; i++)
        {
            if (this.yx[i] == -1)
            {
                edgeWeightedDigraph.addEdge(new DirectedEdge(this.N + i, num2, this.py[i]));
            }
        }
        for (int i = 0; i < this.N; i++)
        {
            for (int j = 0; j < this.N; j++)
            {
                if (this.xy[i] == j)
                {
                    edgeWeightedDigraph.addEdge(new DirectedEdge(this.N + j, i, (double)0f));
                }
                else
                {
                    edgeWeightedDigraph.addEdge(new DirectedEdge(i, this.N + j, this.reduced(i, j)));
                }
            }
        }
        DijkstraSP dijkstraSP = new DijkstraSP(edgeWeightedDigraph, num);
        Iterator   iterator   = dijkstraSP.pathTo(num2).iterator();

        while (iterator.hasNext())
        {
            DirectedEdge directedEdge = (DirectedEdge)iterator.next();
            int          num3         = directedEdge.from();
            int          num4         = directedEdge.to() - this.N;
            if (num3 < this.N)
            {
                this.xy[num3] = num4;
                this.yx[num4] = num3;
            }
        }
        for (int j = 0; j < this.N; j++)
        {
            double[] arg_180_0 = this.px;
            int      num5      = j;
            double[] array     = arg_180_0;
            array[num5] += dijkstraSP.distTo(j);
        }
        for (int j = 0; j < this.N; j++)
        {
            double[] arg_1B6_0 = this.py;
            int      num5      = j;
            double[] array     = arg_1B6_0;
            array[num5] += dijkstraSP.distTo(this.N + j);
        }
    }