コード例 #1
0
ファイル: CPM.cs プロジェクト: kismy/Algorithms
    void Start()
    {
        string[] lines = txt.text.Split('\n');
        // number of jobs
        int n = int.Parse(lines[0]);

        // source and sink
        int source = 2 * n;
        int sink   = 2 * n + 1;

        // build network
        EdgeWeightedDigraph G = new EdgeWeightedDigraph(2 * n + 2);

        for (int i = 0; i < n; i++)
        {
            string[] strs = lines[i + 1].Split(new char[1] {
                ' '
            }, StringSplitOptions.RemoveEmptyEntries);
            double duration = double.Parse(strs[0]);
            G.addEdge(new DirectedEdge(source, i, 0.0));
            G.addEdge(new DirectedEdge(i + n, sink, 0.0));
            G.addEdge(new DirectedEdge(i, i + n, duration));

            // precedence constraints
            int m = int.Parse(strs[1]);
            for (int j = 0; j < m; j++)
            {
                int precedent = int.Parse(strs[2 + j]);
                G.addEdge(new DirectedEdge(n + i, precedent, 0.0));
            }
        }

        // compute longest path
        AcyclicLP lp = new AcyclicLP(G, source);

        // print results
        print("jobID \t startTime \t finishTime");

        for (int i = 0; i < n; i++)
        {
            print(i + "  \t " + lp.DistTo(i) + " \t " + lp.DistTo(i + n));
        }
        print("Finish time:" + lp.DistTo(sink));
    }
コード例 #2
0
ファイル: AcyclicLP.cs プロジェクト: kismy/Algorithms
    void Start()
    {
        int s = 5;
        EdgeWeightedDigraph G = new EdgeWeightedDigraph(txt);

        AcyclicLP lp = new AcyclicLP(G, s);

        for (int v = 0; v < G.V(); v++)
        {
            if (lp.hasPathTo(v))
            {
                string str = (s + " to " + v + "  distance= " + lp.DistTo(v));
                foreach (DirectedEdge e in lp.PathTo(v))
                {
                    str += ("     " + e);
                }
                print(str);
            }
            else
            {
                print(s + " to " + v + "         no path\n");
            }
        }
    }
コード例 #3
0
        public void Run()
        {
            Console.WriteLine("Choose file:");      // Prompt
            Console.WriteLine("1 - tinyEWDAG.txt"); // Prompt
            //Console.WriteLine("2 - mediumEWD.txt"); // Prompt
            //Console.WriteLine("3 - mediumEWG.txt"); // Prompt

            Console.WriteLine("or quit"); // Prompt

            var    fileNumber = Console.ReadLine();
            string fileName;

            switch (fileNumber)
            {
            case "1":
                fileName = "tinyEWDAG.txt";
                break;

            //case "2":
            //    fileName = "mediumEWD.txt";
            //    break;
            //case "3":
            //    fileName = "largeEWG.zip";
            //    break;
            case "quit":
                return;

            default:
                return;
            }


            var @in   = new In($"Files\\Graphs\\{fileName}");
            var lines = @in.ReadAllLines();

            var lineIterator = 0;
            var v            = 0;
            var e            = 0;
            var edges        = new List <DirectedEdge>();

            foreach (var line in lines)
            {
                if (lineIterator == 0)
                {
                    v = Convert.ToInt32(line);
                }
                if (lineIterator == 1)
                {
                    e = Convert.ToInt32(line);
                }
                if (lineIterator > 1)
                {
                    var lineSplitted = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    var ve           = Convert.ToInt32(lineSplitted[0]);
                    var we           = Convert.ToInt32(lineSplitted[1]);
                    var weight       = Convert.ToDouble(lineSplitted[2], CultureInfo.InvariantCulture);
                    var edge         = new DirectedEdge(ve, we, weight);
                    edges.Add(edge);
                }

                lineIterator++;
            }

            var edgeWeightedDigraph = new EdgeWeightedDigraph(v, e, edges);

            Console.WriteLine(edgeWeightedDigraph);

            const int s = 5;

            // find shortest path from s to each other vertex in DAG
            var lp = new AcyclicLP(edgeWeightedDigraph, s);

            for (var vi = 0; vi < edgeWeightedDigraph.V; vi++)
            {
                if (lp.HasPathTo(vi))
                {
                    Console.Write($"{s} to {vi} {$"{lp.DistTo(vi):0.00}"}  ");
                    foreach (var ei in lp.PathTo(vi))
                    {
                        Console.Write($"{ei}   ");
                    }
                    Console.WriteLine();
                }
                else
                {
                    Console.Write($"{s} to {vi}         no path{Environment.NewLine}");
                }
            }
            Console.ReadLine();
        }
コード例 #4
0
        public void Run()
        {
            Console.WriteLine("Choose file:");   // Prompt
            Console.WriteLine("1 - jobsPC.txt"); // Prompt
            //Console.WriteLine("2 - mediumEWD.txt"); // Prompt
            //Console.WriteLine("3 - mediumEWG.txt"); // Prompt

            Console.WriteLine("or quit"); // Prompt

            var    fileNumber = Console.ReadLine();
            string fileName;

            switch (fileNumber)
            {
            case "1":
                fileName = "jobsPC.txt";
                break;

            //case "2":
            //    fileName = "mediumEWD.txt";
            //    break;
            //case "3":
            //    fileName = "largeEWG.zip";
            //    break;
            case "quit":
                return;

            default:
                return;
            }


            var @in   = new In($"Files\\Graphs\\{fileName}");
            var lines = @in.ReadAllLines();



            var lineIterator = 0;
            var i            = 0;
            // number of jobs
            var n = 0;

            // source and sink
            var source = 0;
            var sink   = 0;

            var v     = 0;
            var e     = 0;
            var edges = new List <DirectedEdge>();

            foreach (var line in lines)
            {
                if (lineIterator == 0)
                {
                    n      = Convert.ToInt32(line);
                    source = 2 * n;
                    sink   = 2 * n + 1;
                    v      = sink + 1;
                }



                if (lineIterator > 0)
                {
                    var lineSplitted = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    var duration = Convert.ToDouble(lineSplitted[0], CultureInfo.InvariantCulture);

                    edges.Add(new DirectedEdge(source, i, 0.0));
                    e++;
                    edges.Add(new DirectedEdge(i + n, sink, 0.0));
                    e++;
                    edges.Add(new DirectedEdge(i, i + n, duration));
                    e++;

                    // precedence constraints
                    var m = Convert.ToInt32(lineSplitted[1], CultureInfo.InvariantCulture);
                    for (var j = 0; j < m; j++)
                    {
                        var precedent = Convert.ToInt32(lineSplitted[1 + (j + 1)], CultureInfo.InvariantCulture);
                        edges.Add(new DirectedEdge(n + i, precedent, 0.0));
                        e++;
                    }

                    i++;
                }

                lineIterator++;
            }

            var edgeWeightedDigraph = new EdgeWeightedDigraph(v, e, edges);

            Console.WriteLine(edgeWeightedDigraph);


            // find shortest path from s to each other vertex in DAG
            var lp = new AcyclicLP(edgeWeightedDigraph, source);

            // print results
            Console.WriteLine(" job   start  finish");
            Console.WriteLine("--------------------");
            for (var k = 0; k < n; k++)
            {
                Console.Write($"{k} {lp.DistTo(k)} {lp.DistTo(k + n)}{Environment.NewLine}");
            }
            Console.Write($"Finish time: {lp.DistTo(sink)}{Environment.NewLine}");

            Console.ReadLine();
        }