コード例 #1
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);
    }
コード例 #2
0
    private void dfs(EdgeWeightedDigraph edgeWeightedDigraph, int num)
    {
        this.marked[num] = true;
        int[] arg_23_0 = this.pre;
        int   num2     = this.preCounter;
        int   arg_23_2 = num2;

        this.preCounter = num2 + 1;
        arg_23_0[num]   = arg_23_2;
        this.preorder.enqueue(Integer.valueOf(num));
        Iterator iterator = edgeWeightedDigraph.adj(num).iterator();

        while (iterator.hasNext())
        {
            DirectedEdge directedEdge = (DirectedEdge)iterator.next();
            int          num3         = directedEdge.to();
            if (!this.marked[num3])
            {
                this.dfs(edgeWeightedDigraph, num3);
            }
        }
        this.postorder.enqueue(Integer.valueOf(num));
        int[] arg_9F_0 = this.post;
        num2 = this.postCounter;
        int arg_9F_2 = num2;

        this.postCounter = num2 + 1;
        arg_9F_0[num]    = arg_9F_2;
    }
コード例 #3
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();
            }
        }
    }
コード例 #4
0
    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;
    }
コード例 #5
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;
        }
    }
コード例 #6
0
    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;
        }
    }
コード例 #8
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++;
    }
コード例 #9
0
 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);
 }
コード例 #10
0
ファイル: Arbitrage.cs プロジェクト: kismy/Algorithms
    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");
        }
    }
コード例 #11
0
ファイル: DijkstraSP.cs プロジェクト: kismy/Algorithms
    // 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]);
            }
        }
    }
コード例 #12
0
    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]));
            }
        }
    }
コード例 #13
0
    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);
    }
コード例 #14
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);
 }
コード例 #15
0
ファイル: DijkstraSP.cs プロジェクト: kismy/Algorithms
    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);
    }
コード例 #16
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;
    }
コード例 #17
0
    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);
        }
    }