private void dfs(EdgeWeightedDigraph G, int v)
 {
     pre.Enqueue(v);
     marked[v] = true;
     foreach (var w in G.Adj(v))
         if (!marked[w.from])
             dfs(G, w.from);
     post.Enqueue(v);
     reversePost.Push(v);
 }
Exemple #2
0
        public CPM(int N, List <string> edgeText)
        {
            var g = new EdgeWeightedDigraph(2 * N + 2);

            int s = 2 * N, t = 2 * N + 1;

            for (int i = 0; i < N; i++)
            {
                var    e        = edgeText[i].Split(' ');
                double duration = Convert.ToDouble(e[0]);
                g.AddEdge(new DirectedEdge(i, i + N, duration));
                g.AddEdge(new DirectedEdge(s, i, 0.0));
                g.AddEdge(new DirectedEdge(i + N, t, 0.0));
                for (int j = 1; j < e.Length; j++)
                {
                    int successor = Convert.ToInt32(e[j]);
                    g.AddEdge(new DirectedEdge(i + N, successor, 0.0));
                }
            }


            AcylicSP acy = new AcylicSP(g, s, AcylicSP.PathType.Lagrest);

            Console.WriteLine("start\ttimes");
            for (int i = 0; i < N; i++)
            {
                Console.WriteLine($"{i}\t{acy.DistTo[i]}");
            }
            Console.WriteLine($"Total Times:{acy.DistTo[t]}");
        }
Exemple #3
0
 public DijkstraAllPairsSP(EdgeWeightedDigraph G)
 {
     all = new DijkstraSP[G.V];
     for (int v = 0; v < G.V; v++)
     {
         all[v] = new DijkstraSP(G, v);
     }
 }
        private bool[] onStack; // vertices on recursive call stack

        #endregion Fields

        #region Constructors

        public DirectedCycle(EdgeWeightedDigraph G)
        {
            onStack = new bool[G.V];
            edgeTo = new int[G.V];
            marked = new bool[G.V];
            for (int v = 0; v < G.V; v++)
                if (!marked[v])
                    dfs(G, v);
        }
Exemple #5
0
        private IEnumerable<int> order; // topological order

        #endregion Fields

        #region Constructors

        public Topological(EdgeWeightedDigraph G)
        {
            DirectedCycle cyclefinder = new DirectedCycle(G);
            if (!cyclefinder.hasCycle())
            {
                DepthFirstOrder dfs = new DepthFirstOrder(G);
                order = dfs.ReversePost();
            }
        }
Exemple #6
0
        private IEnumerable <int> order; // topological order

        public Topological(EdgeWeightedDigraph G)
        {
            DirectedCycle cyclefinder = new DirectedCycle(G);

            if (!cyclefinder.hasCycle())
            {
                DepthFirstOrder dfs = new DepthFirstOrder(G);
                order = dfs.ReversePost();
            }
        }
        private Stack<int> reversePost; // vertices in reverse postorder

        #endregion Fields

        #region Constructors

        public DepthFirstOrder(EdgeWeightedDigraph G)
        {
            pre = new Queue<int>();
            post = new Queue<int>();
            reversePost = new Stack<int>();
            marked = new bool[G.V];
            for (int v = 0; v < G.V; v++)
                if (!marked[v])
                    dfs(G, v);
        }
 private void findNegativeCycle()
 {
     int V = edgeTo.Length;
     EdgeWeightedDigraph spt;
     spt = new EdgeWeightedDigraph(V);
     for (int v = 0; v < V; v++)
         if (edgeTo[v] != null)
             spt.addEdge(edgeTo[v]);
     DirectedCycle cf = new DirectedCycle(spt);
     cycle = cf.Cycle();
 }
Exemple #9
0
 public AcyclicSP(EdgeWeightedDigraph G, int s)
 {
     edgeTo = new DirectedEdge[G.V];
     distTo = new double[G.V];
     for (int v = 0; v < G.V; v++)
         distTo[v] = double.PositiveInfinity;
     distTo[s] = 0.0;
     Topological top = new Topological(G);
     foreach (int v in top.Order)
         relax(G, v);
 }
Exemple #10
0
 public DijkstraSP(EdgeWeightedDigraph G, int s)
 {
     edgeTo = new DirectedEdge[G.V];
     distTo = new double[G.V];
     pq = new IndexPriorityQueue<double>(G.V);
     for (int v = 0; v < G.V; v++)
         distTo[v] = Double.PositiveInfinity;
     distTo[s] = 0.0;
     pq.Insert(s, 0.0);
     while (!pq.isEmpty())
         relax(G, pq.Del());
 }
Exemple #11
0
 private void relax(EdgeWeightedDigraph G, int v)
 {
     foreach (DirectedEdge e in G.Adj(v))
     {
         int w = e.to;
         if (distTo[w] > distTo[v] + e.Weight)
         {
             distTo[w] = distTo[v] + e.Weight;
             edgeTo[w] = e;
         }
     }
 }
Exemple #12
0
 private void relax(EdgeWeightedDigraph G, int v)
 {
     foreach (DirectedEdge e in G.Adj(v))
     {
         int w = e.to;
         if (distTo[w] > distTo[v] + e.Weight)
         {
             distTo[w] = distTo[v] + e.Weight;
             edgeTo[w] = e;
         }
     }
 }
 public EdgeWeightedCycleFinder(EdgeWeightedDigraph g)
 {
     _onStack = new bool[g.V];
     _edgeTo  = new DirectedEdge[g.V];
     _marked  = new bool[g.V];
     for (int v = 0; v < g.V; v++)
     {
         if (!_marked[v])
         {
             Dfs(g, v);
         }
     }
 }
 private bool[] onStack;             // vertices on recursive call stack
 public DirectedCycle(EdgeWeightedDigraph G)
 {
     onStack = new bool[G.V];
     edgeTo  = new int[G.V];
     marked  = new bool[G.V];
     for (int v = 0; v < G.V; v++)
     {
         if (!marked[v])
         {
             dfs(G, v);
         }
     }
 }
 public DepthFirstOrder(EdgeWeightedDigraph dg)
 {
     preQueue    = new Queue <int>();
     postQueue   = new Queue <int>();
     reversePost = new Stack <int>();
     marked      = new bool[dg.V];
     for (int s = 0; s < dg.V; s++)
     {
         if (!marked[s])
         {
             Dfs(dg, s);
         }
     }
 }
Exemple #16
0
 private void dfs(EdgeWeightedDigraph G, int v)
 {
     pre.Enqueue(v);
     marked[v] = true;
     foreach (var w in G.Adj(v))
     {
         if (!marked[w.from])
         {
             dfs(G, w.from);
         }
     }
     post.Enqueue(v);
     reversePost.Push(v);
 }
Exemple #17
0
 private Stack <int> reversePost; // vertices in reverse postorder
 public DepthFirstOrder(EdgeWeightedDigraph G)
 {
     pre         = new Queue <int>();
     post        = new Queue <int>();
     reversePost = new Stack <int>();
     marked      = new bool[G.V];
     for (int v = 0; v < G.V; v++)
     {
         if (!marked[v])
         {
             dfs(G, v);
         }
     }
 }
Exemple #18
0
 public DijkstraSP(EdgeWeightedDigraph g, int s)
 {
     _edgeTo = new DirectedEdge[g.V];
     _distTo = new double[g.V];
     _pq     = new IndexMinPQ <double>(g.V * g.V);
     for (int i = 0; i < g.V; i++)
     {
         _distTo[i] = double.PositiveInfinity;
     }
     _distTo[s] = 0;
     _pq.Insert(s, 0);
     while (_pq.Count > 0)
     {
         Relax(g, _pq.DelMin());
     }
 }
Exemple #19
0
        public static void Filling(this EdgeWeightedDigraph ewg, int count)
        {
            for (int i = 0; i < count - 1; i++)
            {
                int    k      = i + 1;
                double weight = (i + k) * (double)count / 100;
                ewg.addEdge(new DirectedEdge(i, i + 1, weight));
            }

            for (int i = 0; i < count - 2; i++)
            {
                int    k      = i + 2;
                double weight = (i + k) * (double)count / 100;
                ewg.addEdge(new DirectedEdge(i, i + 2, weight));
            }
        }
Exemple #20
0
 public DijkstraSP(EdgeWeightedDigraph G, int s)
 {
     edgeTo = new DirectedEdge[G.V];
     distTo = new double[G.V];
     pq     = new IndexPriorityQueue <double>(G.V);
     for (int v = 0; v < G.V; v++)
     {
         distTo[v] = Double.PositiveInfinity;
     }
     distTo[s] = 0.0;
     pq.Insert(s, 0.0);
     while (!pq.isEmpty())
     {
         relax(G, pq.Del());
     }
 }
Exemple #21
0
        public AcyclicSP(EdgeWeightedDigraph G, int s)
        {
            edgeTo = new DirectedEdge[G.V];
            distTo = new double[G.V];
            for (int v = 0; v < G.V; v++)
            {
                distTo[v] = double.PositiveInfinity;
            }
            distTo[s] = 0.0;
            Topological top = new Topological(G);

            foreach (int v in top.Order)
            {
                relax(G, v);
            }
        }
Exemple #22
0
        private void FindNegativeCycle()
        {
            int v = _edgeTo.Length;
            EdgeWeightedDigraph spt = new EdgeWeightedDigraph(v);

            for (int s = 0; s < v; s++)
            {
                if (_edgeTo[s] != null)
                {
                    spt.AddEdge(_edgeTo[s]);
                }
            }
            EdgeWeightedCycleFinder finder = new EdgeWeightedCycleFinder(spt);

            _cycle = finder.Cycle;
        }
Exemple #23
0
        private void findNegativeCycle()
        {
            int V = edgeTo.Length;
            EdgeWeightedDigraph spt;

            spt = new EdgeWeightedDigraph(V);
            for (int v = 0; v < V; v++)
            {
                if (edgeTo[v] != null)
                {
                    spt.addEdge(edgeTo[v]);
                }
            }
            DirectedCycle cf = new DirectedCycle(spt);

            cycle = cf.Cycle();
        }
        private void Dfs(EdgeWeightedDigraph dg, int v)
        {
            preQueue.Enqueue(v);
            marked[v] = true;

            foreach (var e in dg.Adj(v))
            {
                int w = e.To;
                if (!marked[w])
                {
                    Dfs(dg, w);
                }
            }

            postQueue.Enqueue(v);
            reversePost.Push(v);
        }
        private Queue<int> queue; // vertices being relaxed

        #endregion Fields

        #region Constructors

        public BellmanFordSP(EdgeWeightedDigraph G, int s)
        {
            distTo = new double[G.V];
            edgeTo = new DirectedEdge[G.V];
            onQ = new bool[G.V];
            queue = new Queue<int>();
            for (int v = 0; v < G.V; v++)
                distTo[v] = double.PositiveInfinity;
            distTo[s] = 0.0;
            queue.Enqueue(s);
            onQ[s] = true;
            while (queue.Count!=0 && !this.hasNegativeCycle())
            {
                int v = queue.Dequeue();
                onQ[v] = false;
                relax(G,v);
            }
        }
 private void relax(EdgeWeightedDigraph G, int v)
 {
     foreach (DirectedEdge e in G.Adj(v))
     {
         int w = e.to;
         if (distTo[w] > distTo[v] + e.Weight)
         {
             distTo[w] = distTo[v] + e.Weight;
             edgeTo[w] = e;
             if (!onQ[w])
             {
                 queue.Enqueue(w);
                 onQ[w] = true;
             }
         }
         if (cost++%G.V == 0)
             findNegativeCycle();
     }
 }
Exemple #27
0
 public static void ShowPathsByAcyclic(this EdgeWeightedDigraph ewg)
 {
     for (int i = 0; i < ewg.V; i++)
     {
         AcyclicSP dapsp = new AcyclicSP(ewg, i);
         for (int j = 0; j < ewg.V; j++)
         {
             if (dapsp.hasPathTo(j) && i != j)
             {
                 Console.WriteLine("{0}-{1} : ", i, j);
                 foreach (var v in dapsp.pathTo(j))
                 {
                     Console.WriteLine(v);
                 }
                 Console.WriteLine("------------------");
             }
         }
     }
 }
Exemple #28
0
        private IEnumerable <DirectedEdge> cycle; // negative cycle in edgeTo[]?

        public BellmanFordSP(EdgeWeightedDigraph G, int s)
        {
            distTo = new double[G.V];
            edgeTo = new DirectedEdge[G.V];
            onQ    = new bool[G.V];
            queue  = new Queue <int>();
            for (int v = 0; v < G.V; v++)
            {
                distTo[v] = double.PositiveInfinity;
            }
            distTo[s] = 0.0;
            queue.Enqueue(s);
            onQ[s] = true;
            while (queue.Count != 0 && !this.hasNegativeCycle())
            {
                int v = queue.Dequeue();
                onQ[v] = false;
                relax(G, v);
            }
        }
Exemple #29
0
 private void relax(EdgeWeightedDigraph G, int v)
 {
     foreach (DirectedEdge e in G.Adj(v))
     {
         int w = e.to;
         if (distTo[w] > distTo[v] + e.Weight)
         {
             distTo[w] = distTo[v] + e.Weight;
             edgeTo[w] = e;
             if (pq.contains(w))
             {
                 pq.change(w, distTo[w]);
             }
             else
             {
                 pq.Insert(w, distTo[w]);
             }
         }
     }
 }
Exemple #30
0
 public BellmanFordSP(EdgeWeightedDigraph g, int s)
 {
     _distTo  = new double[g.V];
     _edgeTo  = new DirectedEdge[g.V];
     _onQueue = new bool[g.V];
     _queue   = new Queue <int>();
     for (int i = 0; i < g.V; i++)
     {
         _distTo[i] = double.PositiveInfinity;
     }
     _distTo[s] = 0d;
     _queue.Enqueue(s);
     _onQueue[s] = true;
     while (_queue.Count > 0 && !HasNegativeCycle())
     {
         int v = _queue.Dequeue();
         _onQueue[v] = false;
         Relax(g, v);
     }
 }
Exemple #31
0
        public static void ShowPathsByDijkstra(this EdgeWeightedDigraph ewg)
        {
            DijkstraAllPairsSP dapsp = new DijkstraAllPairsSP(ewg);

            for (int i = 0; i < ewg.V; i++)
            {
                for (int j = 0; j < ewg.V; j++)
                {
                    if (dapsp.hasPathTo(i, j) && i != j)
                    {
                        Console.WriteLine("{0}-{1} : ", i, j);
                        foreach (var v in dapsp.path(i, j))
                        {
                            Console.WriteLine(v);
                        }
                        Console.WriteLine("------------------");
                    }
                }
            }
        }
Exemple #32
0
 public void Relax(EdgeWeightedDigraph g, int v)
 {
     foreach (DirectedEdge e in g.Adj(v))
     {
         int w = e.To;
         if (_distTo[w] > _distTo[v] + e.Weight)
         {
             _distTo[w] = _distTo[v] + e.Weight;
             _edgeTo[w] = e;
             if (_pq.Contains(w))
             {
                 _pq.Change(w, _distTo[w]);
             }
             else
             {
                 _pq.Insert(w, _distTo[w]);
             }
         }
     }
 }
Exemple #33
0
 private void relax(EdgeWeightedDigraph G, int v)
 {
     foreach (DirectedEdge e in G.Adj(v))
     {
         int w = e.to;
         if (distTo[w] > distTo[v] + e.Weight)
         {
             distTo[w] = distTo[v] + e.Weight;
             edgeTo[w] = e;
             if (!onQ[w])
             {
                 queue.Enqueue(w);
                 onQ[w] = true;
             }
         }
         if (cost++ % G.V == 0)
         {
             findNegativeCycle();
         }
     }
 }
Exemple #34
0
        private static void Main(string[] args)
        {
            EdgeWeightedDigraph ewg = new EdgeWeightedDigraph(5);

            ewg.Filling(5);
            ewg.ShowEdges();
            Console.WriteLine();

            /*Console.WriteLine("Dijkstra Paths :");
             * ewg.ShowPathsByDijkstra();
             * Console.WriteLine();*/

            /*Console.WriteLine("Acyclic Paths");
             * ewg.ShowPathsByAcyclic();
             * Console.WriteLine();*/

            Console.WriteLine("BellmanFord Paths");
            ewg.ShowPathsByBellmanFord();
            Console.WriteLine();

            Console.ReadKey();
        }
 private void dfs(EdgeWeightedDigraph G, int v)
 {
     onStack[v] = true;
     marked[v] = true;
     foreach (var w in G.Adj(v))
         if (this.hasCycle())
             return;
         else if (!marked[w.to])
         {
             edgeTo[w.to] = v;
             dfs(G, w.to);
         }
         else if (onStack[w.to])
         {
             cycle = new Stack<DirectedEdge>();
             for (int x = v; x != w.to; x = edgeTo[x])
                 //cycle.Push(x);
             cycle.Push(w);
             //cycle.Push(v);
         }
     onStack[v] = false;
 }
Exemple #36
0
        private void Relax(EdgeWeightedDigraph g, int v)
        {
            foreach (var e in g.Adj(v))
            {
                int w = e.To;
                if (_distTo[w] > _distTo[v] + e.Weight)
                {
                    _distTo[w] = _distTo[v] + e.Weight;
                    _edgeTo[w] = e;
                    if (!_onQueue[w])
                    {
                        _queue.Enqueue(w);
                        _onQueue[w] = true;
                    }
                }

                if (_cost++ % g.V == 0)
                {
                    FindNegativeCycle();
                }
            }
        }
Exemple #37
0
 public DijkstraAllPairsSP(EdgeWeightedDigraph G)
 {
     all = new DijkstraSP[G.V];
     for (int v = 0; v < G.V; v++)
         all[v] = new DijkstraSP(G, v);
 }
Exemple #38
0
 private void relax(EdgeWeightedDigraph G, int v)
 {
     foreach (DirectedEdge e in G.Adj(v))
     {
         int w = e.to;
         if (distTo[w] > distTo[v] + e.Weight)
         {
             distTo[w] = distTo[v] + e.Weight;
             edgeTo[w] = e;
             if (pq.contains(w)) pq.change(w, distTo[w]);
             else pq.Insert(w, distTo[w]);
         }
     }
 }
Exemple #39
0
        static void Main(string[] args)
        {
            EdgeWeightedDigraph g = new EdgeWeightedDigraph(8);

            g.AddEdge(new DirectedEdge(4, 5, 0.35));
            g.AddEdge(new DirectedEdge(5, 4, 0.35));
            g.AddEdge(new DirectedEdge(4, 7, 0.37));
            g.AddEdge(new DirectedEdge(5, 7, 0.28));
            g.AddEdge(new DirectedEdge(7, 5, 0.28));
            g.AddEdge(new DirectedEdge(5, 1, 0.32));
            g.AddEdge(new DirectedEdge(0, 4, 0.38));
            g.AddEdge(new DirectedEdge(0, 2, 0.26));
            g.AddEdge(new DirectedEdge(7, 3, 0.39));
            g.AddEdge(new DirectedEdge(1, 3, 0.29));
            g.AddEdge(new DirectedEdge(2, 7, 0.34));
            g.AddEdge(new DirectedEdge(6, 2, 0.40));
            g.AddEdge(new DirectedEdge(3, 6, 0.52));
            g.AddEdge(new DirectedEdge(6, 0, 0.58));
            g.AddEdge(new DirectedEdge(6, 4, 0.93));

            DijkstraSP dsp = new DijkstraSP(g, 0);

            Console.WriteLine("DijkstraSP");
            foreach (var e in dsp.PathTo(6))
            {
                Console.WriteLine($"{e} ");
            }

            var edges = new List <string>()
            {
                "41 1 7 9",
                "51 2",
                "50",
                "36",
                "38",
                "45",
                "21 3 8",
                "32 3 8",
                "32 2",
                "29 4 6"
            };
            var cpm = new CPM(10, edges);

            var dedges = new List <string>()
            {
                "4 5  0.35",
                "5 4 -0.66",
                "4 7  0.37",
                "5 7  0.28",
                "7 5  0.28",
                "5 1  0.32",
                "0 4  0.38",
                "0 2  0.26",
                "7 3  0.39",
                "1 3  0.29",
                "2 7  0.34",
                "6 2  0.40",
                "3 6  0.52",
                "6 0  0.58",
                "6 4  0.93"
            };
            int N = 8;
            EdgeWeightedDigraph spt = new EdgeWeightedDigraph(N);

            for (int i = 0; i < N; i++)
            {
                var    e        = dedges[i].Split(' ');
                double duration = Convert.ToDouble(e[e.Length - 1]);
                int    v        = Convert.ToInt32(e[0]);
                int    w        = Convert.ToInt32(e[1]);
                spt.AddEdge(new DirectedEdge(v, w, duration));
            }

            BellmanFordSP bsp = new BellmanFordSP(spt, 0);

            Console.WriteLine(bsp.HasNegativeCycle());
            Console.WriteLine("Hello World!");
        }
Exemple #40
0
        public Topological(EdgeWeightedDigraph dg)
        {
            DepthFirstOrder dfo = new DepthFirstOrder(dg);

            _order = dfo.Reverse;
        }
Exemple #41
0
        private static void Main(string[] args)
        {
            EdgeWeightedDigraph ewg = new EdgeWeightedDigraph(5);

            ewg.Filling(5);
            ewg.ShowEdges();
            Console.WriteLine();

            /*Console.WriteLine("Dijkstra Paths :");
            ewg.ShowPathsByDijkstra();
            Console.WriteLine();*/

            /*Console.WriteLine("Acyclic Paths");
            ewg.ShowPathsByAcyclic();
            Console.WriteLine();*/

            Console.WriteLine("BellmanFord Paths");
            ewg.ShowPathsByBellmanFord();
            Console.WriteLine();

            Console.ReadKey();
        }
Exemple #42
0
        public static bool hasCycles(this EdgeWeightedDigraph ewg)
        {
            DirectedCycle dc = new DirectedCycle(ewg);

            return(dc.hasCycle());
        }