예제 #1
0
        public void Run()
        {
            Console.WriteLine("Choose file:");   // Prompt
            Console.WriteLine("1 - tinyDG.txt"); // Prompt
            Console.WriteLine("or quit");        // Prompt

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

            switch (fileNumber)
            {
            case "1":
                fileName = "tinyDG.txt";
                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 <EdgeD>();

            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 edge         = new EdgeD(ve, we);
                    edges.Add(edge);
                }

                lineIterator++;
            }

            var digraph = new Digraph(v, e, edges);

            Console.WriteLine(digraph);
            Console.ReadLine();
        }
예제 #2
0
        public void Run()
        {
            Console.WriteLine("Choose file:");      // Prompt
            Console.WriteLine("1 - tinyBTree.txt"); // Prompt
            Console.WriteLine("or quit");           // Prompt

            var fileNumber = Console.ReadLine();
            var fieName    = string.Empty;

            switch (fileNumber)
            {
            case "1":
                fieName = "tinyBTree.txt";
                break;

            case "quit":
                return;

            default:
                return;
            }


            var @in       = new In($"Files\\Context\\{fieName}");
            var keyValues = @in.ReadAllLines();

            //var list = words.Select(word => new StringComparable(word)).ToList();

            //var listComparable = list.Cast<IComparable>().ToList();
            //var arrayComparable = list.Cast<IComparable>().ToArray();
            //var listStrings = words.ToList();


            var bree = new BTree <string, string>();


            foreach (var keyValue in keyValues)
            {
                var splittedKeyValue = keyValue.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                var key   = splittedKeyValue[0];
                var value = splittedKeyValue[1];
                bree.Put(key, value);
            }

            Console.WriteLine("cs.princeton.edu:  " + bree.Get("www.cs.princeton.edu"));
            Console.WriteLine("hardvardsucks.com: " + bree.Get("www.harvardsucks.com"));
            Console.WriteLine("simpsons.com:      " + bree.Get("www.simpsons.com"));
            Console.WriteLine("apple.com:         " + bree.Get("www.apple.com"));
            Console.WriteLine("ebay.com:          " + bree.Get("www.ebay.com"));
            Console.WriteLine("dell.com:          " + bree.Get("www.dell.com"));
            Console.WriteLine();

            Console.WriteLine("size:    " + bree.Size());
            Console.WriteLine("height:  " + bree.Height());
            Console.WriteLine(bree);

            Console.ReadLine();
        }
예제 #3
0
        public void Run()
        {
            Console.WriteLine("Choose file:");   // Prompt
            Console.WriteLine("1 - routes.txt"); // Prompt
            Console.WriteLine("2 - movies.txt"); // Prompt
            //Console.WriteLine("3 - largeG.zip"); // Prompt
            Console.WriteLine("or quit");        // Prompt

            var           fileNumber = Console.ReadLine();
            string        fileName;
            char          delimiter;
            string        source;
            List <string> names;

            switch (fileNumber)
            {
            case "1":
                fileName  = "routes.txt";
                delimiter = '\u0020';
                source    = "JFK";
                names     = new List <string> {
                    "LAS", "DFW", "EWR"
                };
                break;

            case "2":
                fileName  = "movies.txt";
                delimiter = '/';
                source    = "Bacon, Kevin";
                names     = new List <string> {
                    "Kidman, Nicole", "Grant, Cary"
                };
                break;

            //case "3":
            //    fieName = "largeG.zip";
            //    break;
            case "quit":
                return;

            default:
                return;
            }


            var @in   = new In($"Files\\Graphs\\{fileName}");
            var lines = !fileName.EndsWith("zip") ? @in.ReadAllLines() : @in.ReadAllLinesFromZip();

            var degreesOfSeparation = new DegreesOfSeparation(lines, delimiter, source);

            Console.WriteLine($"{source}");
            foreach (var name in names)
            {
                Console.WriteLine($"{name}");
                degreesOfSeparation.Find(name);
            }
            Console.ReadLine();
        }
예제 #4
0
        public void Run()
        {
            Console.WriteLine("Choose file:");   // Prompt
            Console.WriteLine("1 - tinyST.txt"); // Prompt
            Console.WriteLine("or quit");        // Prompt

            var fileNumber = Console.ReadLine();
            var fieName    = string.Empty;

            switch (fileNumber)
            {
            case "1":
                fieName = "tinyST.txt";
                break;

            case "quit":
                return;

            default:
                return;
            }


            var @in       = new In($"Files\\Searching\\{fieName}");
            var keyValues = @in.ReadAllLines();

            //var list = words.Select(word => new StringComparable(word)).ToList();

            //var listComparable = list.Cast<IComparable>().ToList();
            //var arrayComparable = list.Cast<IComparable>().ToArray();
            //var listStrings = words.ToList();


            var st = new BinarySearchST <string, string>();


            foreach (var keyValue in keyValues)
            {
                var splittedKeyValue = keyValue.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                var key   = splittedKeyValue[0];
                var value = splittedKeyValue[1];
                st.Put(key, value);
            }
            // print results
            foreach (var item in st.Keys())
            {
                Console.WriteLine(st.Get(item));
            }
            Console.WriteLine(st.Max());
            Console.WriteLine(st.Min());
            Console.ReadLine();
        }
예제 #5
0
        public void Run()
        {
            Console.WriteLine("Choose file:"); // Prompt
            Console.WriteLine("1 - jobs.txt"); // Prompt
            Console.WriteLine("or quit");      // Prompt

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

            switch (fileNumber)
            {
            case "1":
                fileName  = "jobs.txt";
                delimiter = '/';
                break;

            case "quit":
                return;

            default:
                return;
            }


            var @in   = new In($"Files\\Graphs\\{fileName}");
            var lines = !fileName.EndsWith("zip") ? @in.ReadAllLines() : @in.ReadAllLinesFromZip();

            var sg = new SymbolDigraph(lines, delimiter);

            Console.WriteLine(sg.G);

            var topological = new Topological(sg.G);

            foreach (int v in topological.Order())
            {
                Console.WriteLine(sg.Name(v));
            }

            Console.ReadLine();
        }
예제 #6
0
        public void Run()
        {
            Console.WriteLine("Choose file:");      // Prompt
            Console.WriteLine("1 - tinyBatch.txt"); // Prompt
            Console.WriteLine("or quit");           // Prompt

            var fileNumber = Console.ReadLine();
            var fieName    = string.Empty;

            switch (fileNumber)
            {
            case "1":
                fieName = "tinyBatch.txt";
                break;

            case "quit":
                return;

            default:
                return;
            }


            var @in   = new In($"Files\\Sorting\\{fieName}");
            var words = @in.ReadAllLines();

            //var list = words.Select(word => new StringComparable(word)).ToList();

            //var listComparable = list.Cast<IComparable>().ToList();
            //var arrayComparable = list.Cast<IComparable>().ToArray();
            var listStrings = words.ToList();

            var topM = new TopM(listStrings);

            topM.ShowTransactions();


            Console.ReadLine();
        }
예제 #7
0
        public void Run()
        {
            Console.WriteLine("Choose file:");    // Prompt
            Console.WriteLine("1 - tinyG.txt");   // Prompt
            Console.WriteLine("2 - mediumG.txt"); // Prompt
            //Console.WriteLine("3 - largeG.zip"); // Prompt
            Console.WriteLine("or quit");         // Prompt

            var fileNumber = Console.ReadLine();
            var fieName    = string.Empty;

            switch (fileNumber)
            {
            case "1":
                fieName = "tinyG.txt";
                break;

            case "2":
                fieName = "mediumG.txt";
                break;

            //case "3":
            //    fieName = "largeG.zip";
            //    break;
            case "quit":
                return;

            default:
                return;
            }


            var @in   = new In($"Files\\Graphs\\{fieName}");
            var lines = !fieName.EndsWith("zip") ? @in.ReadAllLines() : @in.ReadAllLinesFromZip();

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

            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 edge         = new EdgeU(ve, we);
                    edges.Add(edge);
                }

                lineIterator++;
            }

            var graph = new Graph(v, e, edges);

            if (fileNumber != "3")
            {
                Console.WriteLine(graph);
            }

            var cc = new CC(graph);

            // number of connected components
            var m = cc.Count();

            Console.WriteLine($"{m} components");

            // compute list of vertices in each connected component
            var components = new Core.Collections.Queue <Integer> [m];

            for (var i = 0; i < m; i++)
            {
                components[i] = new Core.Collections.Queue <Integer>();
            }
            for (var vi = 0; vi < graph.V; vi++)
            {
                components[cc.Id(vi)].Enqueue(vi);
            }

            // print results
            for (var i = 0; i < m; i++)
            {
                foreach (int vi in components[i])
                {
                    Console.Write($"{vi} ");
                }
                Console.WriteLine();
            }

            Console.ReadLine();
        }
예제 #8
0
        public void Run()
        {
            Console.WriteLine("Choose file:");     // Prompt
            Console.WriteLine("1 - tinyDG.txt");   // Prompt
            Console.WriteLine("2 - mediumDG.txt"); // Prompt
            Console.WriteLine("or quit");          // Prompt

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

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

            case "2":
                fileName = "mediumDG.txt";
                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 <EdgeD>();

            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 edge         = new EdgeD(ve, we);
                    edges.Add(edge);
                }

                lineIterator++;
            }

            var digraph = new Digraph(v, e, edges);

            Console.WriteLine(digraph);
            Console.WriteLine("------------------------------------------------------");


            const int s   = 3;
            var       bfs = new BreadthFirstDirectedPaths(digraph, s);

            for (var i = 0; i < digraph.V; i++)
            {
                if (bfs.HasPathTo(i))
                {
                    Console.Write($"{s} to {i} ({bfs.DistTo(i)}):  ");
                    foreach (int x in bfs.PathTo(i))
                    {
                        if (x == s)
                        {
                            Console.Write(x);
                        }
                        else
                        {
                            Console.Write("->" + x);
                        }
                    }
                    Console.WriteLine();
                }

                else
                {
                    Console.Write($"{s} to {i} (-):  not connected{Environment.NewLine}");
                }
            }

            Console.ReadLine();
        }
예제 #9
0
        public void Run()
        {
            Console.WriteLine("Choose file:");      // Prompt
            Console.WriteLine("1 - tinyEWG.txt");   // Prompt
            Console.WriteLine("2 - mediumEWG.txt"); // Prompt
            //Console.WriteLine("3 - mediumEWG.txt"); // Prompt

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

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

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

            case "2":
                fileName = "mediumEWG.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 <EdgeW>();

            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 EdgeW(ve, we, weight);
                    edges.Add(edge);
                }

                lineIterator++;
            }

            var edgeWeightedGraph = new EdgeWeightedGraph(v, e, edges);

            Console.WriteLine(edgeWeightedGraph);
            Console.ReadLine();
        }
예제 #10
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 sp = new AcyclicSP(edgeWeightedDigraph, s);

            for (var vi = 0; vi < edgeWeightedDigraph.V; vi++)
            {
                if (sp.HasPathTo(vi))
                {
                    Console.Write($"{s} to {vi} {$"{sp.DistTo(vi):0.00}"}  ");
                    foreach (var ei in sp.PathTo(vi))
                    {
                        Console.Write($"{ei}   ");
                    }
                    Console.WriteLine();
                }
                else
                {
                    Console.Write($"{s} to {vi}         no path{Environment.NewLine}");
                }
            }
            Console.ReadLine();
        }
예제 #11
0
        public void Run()
        {
            Console.WriteLine("Choose file:");    // Prompt
            Console.WriteLine("1 - tinyG.txt");   // Prompt
            Console.WriteLine("2 - mediumG.txt"); // Prompt
            Console.WriteLine("or quit");         // Prompt

            var fileNumber = Console.ReadLine();
            var fieName    = string.Empty;

            switch (fileNumber)
            {
            case "1":
                fieName = "tinyG.txt";
                break;

            case "2":
                fieName = "mediumG.txt";
                break;

            case "quit":
                return;

            default:
                return;
            }


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

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

            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 edge         = new EdgeU(ve, we);
                    edges.Add(edge);
                }

                lineIterator++;
            }

            var graph = new Graph(v, e, edges);

            Console.WriteLine(graph);

            var search1 = new NonrecursiveDFS(graph, 0);

            for (var vg = 0; vg < graph.V; vg++)
            {
                if (search1.Marked(vg))
                {
                    Console.WriteLine($"{vg} ");
                }
            }

            Console.WriteLine("------------------------------------------------");

            var search2 = new NonrecursiveDFS(graph, 9);

            for (var vg = 0; vg < graph.V; vg++)
            {
                if (search2.Marked(vg))
                {
                    Console.WriteLine($"{vg} ");
                }
            }

            Console.ReadLine();
        }
예제 #12
0
        public void Run()
        {
            Console.WriteLine("Choose file:");  // Prompt
            Console.WriteLine("1 - rates.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 = "rates.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 currencyIterator = 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>();

            string[] names = {};
            foreach (var line in lines)
            {
                if (lineIterator == 0)
                {
                    v     = Convert.ToInt32(line);
                    names = new string[v];
                }



                if (lineIterator > 0)
                {
                    var wordsIterator = 0;
                    var ratesIterator = 0;
                    var lineSplitted  = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (var word in lineSplitted)
                    {
                        if (wordsIterator == 0)
                        {
                            names[wordsIterator] = word;
                        }

                        if (wordsIterator > 0)
                        {
                            double rate = Convert.ToSingle(word, CultureInfo.InvariantCulture);
                            var    edge = new DirectedEdge(currencyIterator, ratesIterator, -Math.Log(rate));
                            edges.Add(edge);
                            e++;
                            ratesIterator++;
                        }
                        wordsIterator++;
                    }

                    currencyIterator++;
                }

                lineIterator++;
            }

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

            Console.WriteLine(edgeWeightedDigraph);


            // find shortest path from s to each other vertex in DAG
            var arbitrage = new Arbitrage(edgeWeightedDigraph, source);
            var spt       = arbitrage.GetShotestPath();

            // print results
            Console.WriteLine(" job   start  finish");
            Console.WriteLine("--------------------");
            if (spt.HasNegativeCycle())
            {
                var stake = 1000.0;
                foreach (var edge in spt.NegativeCycle())
                {
                    Console.Write($"{stake:0.00000} {names[edge.From()]} ");
                    stake *= Math.Exp(-edge.Weight);
                    Console.Write($"= {stake:0.00000} {names[edge.To()]}{Environment.NewLine}");
                }
            }
            else
            {
                Console.WriteLine("No arbitrage opportunity");
            }

            Console.ReadLine();
        }
예제 #13
0
        public void Run()
        {
            Console.WriteLine("Choose file:");    // Prompt
            Console.WriteLine("1 - tinyDG.txt");  // Prompt
            Console.WriteLine("2 - tinyDAG.txt"); // Prompt
            //Console.WriteLine("3 - largeG.zip"); // Prompt
            Console.WriteLine("or quit");         // Prompt

            var fileNumber = Console.ReadLine();
            var fieName    = string.Empty;

            switch (fileNumber)
            {
            case "1":
                fieName = "tinyDG.txt";
                break;

            case "2":
                fieName = "tinyDAG.txt";
                break;

            //case "3":
            //    fieName = "largeG.zip";
            //    break;
            case "quit":
                return;

            default:
                return;
            }


            var @in   = new In($"Files\\Graphs\\{fieName}");
            var lines = !fieName.EndsWith("zip") ? @in.ReadAllLines() : @in.ReadAllLinesFromZip();

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

            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 edge         = new EdgeD(ve, we);
                    edges.Add(edge);
                }

                lineIterator++;
            }

            var graph = new Digraph(v, e, edges);

            if (fileNumber != "3")
            {
                Console.WriteLine(graph);
            }


            var finder = new DirectedCycle(graph);

            if (finder.HasCycle())
            {
                Console.Write("Directed cycle: ");
                foreach (int vi in finder.Cycle())
                {
                    Console.Write($"{vi} ");
                }
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("No directed cycle");
            }


            Console.ReadLine();
        }
예제 #14
0
        public void Run()
        {
            Console.WriteLine("Choose file:");    // Prompt
            Console.WriteLine("1 - tinySET.txt"); // Prompt
            Console.WriteLine("or quit");         // Prompt

            var fileNumber = Console.ReadLine();
            var fieName    = string.Empty;

            switch (fileNumber)
            {
            case "1":
                fieName = "tinySET.txt";
                break;

            case "quit":
                return;

            default:
                return;
            }


            var @in  = new In($"Files\\Searching\\{fieName}");
            var keys = @in.ReadAllLines();

            //var list = words.Select(word => new StringComparable(word)).ToList();

            //var listComparable = list.Cast<IComparable>().ToList();
            //var arrayComparable = list.Cast<IComparable>().ToArray();
            //var listStrings = words.ToList();


            var set = new SET <string>();


            foreach (var key in keys)
            {
                set.Add(key);
            }
            // print results
            foreach (var item in set)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("-----------------------------------------------------------");
            Console.WriteLine(set.Contains("www.cs.princeton.edu"));
            Console.WriteLine(!set.Contains("www.harvardsucks.com"));
            Console.WriteLine(set.Contains("www.simpsons.com"));
            Console.WriteLine("-----------------------------------------------------------");
            Console.WriteLine();


            Console.WriteLine("-----------------------------------------------------------");
            Console.WriteLine("ceiling(www.simpsonr.com) = " + set.Ceiling("www.simpsonr.com"));
            Console.WriteLine("ceiling(www.simpsons.com) = " + set.Ceiling("www.simpsons.com"));
            Console.WriteLine("ceiling(www.simpsont.com) = " + set.Ceiling("www.simpsont.com"));
            Console.WriteLine("floor(www.simpsonr.com)   = " + set.Floor("www.simpsonr.com"));
            Console.WriteLine("floor(www.simpsons.com)   = " + set.Floor("www.simpsons.com"));
            Console.WriteLine("floor(www.simpsont.com)   = " + set.Floor("www.simpsont.com"));
            Console.WriteLine("-----------------------------------------------------------");
            Console.WriteLine();

            Console.WriteLine("-----------------------------------------------------------");
            Console.WriteLine(set.Max());
            Console.WriteLine(set.Min());
            Console.WriteLine();

            Console.ReadLine();
        }
예제 #15
0
        public void Run()
        {
            Console.WriteLine("Choose file:");   // Prompt
            Console.WriteLine("1 - tinyDG.txt"); // Prompt
            Console.WriteLine("or quit");        // Prompt

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

            switch (fileNumber)
            {
            case "1":
                fileName = "tinyDG.txt";
                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 <EdgeD>();

            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 edge         = new EdgeD(ve, we);
                    edges.Add(edge);
                }

                lineIterator++;
            }

            var digraph = new Digraph(v, e, edges);

            Console.WriteLine(digraph);
            Console.WriteLine("------------------------------------------------------");


            var bag1 = new Core.Collections.Bag <Integer> {
                new Integer(1)
            };
            var bag2 = new Core.Collections.Bag <Integer> {
                new Integer(2)
            };
            var bag3 = new Core.Collections.Bag <Integer> {
                new Integer(6)
            };
            var listSources = new List <Core.Collections.Bag <Integer> > {
                bag1, bag2, bag3
            };

            foreach (var sources in listSources)

            {
                foreach (var source in sources)
                {
                    // multiple-source reachability
                    var dfs = new NonrecursiveDirectedDFS(digraph, source.Value);
                    // print out vertices reachable from sources
                    for (var i = 0; i < digraph.V; i++)
                    {
                        if (dfs.Marked(i))
                        {
                            Console.Write($"{i} ");
                        }
                    }
                    Console.WriteLine();
                }
                Console.WriteLine();
                Console.WriteLine("---------------------------------------------------------");
            }

            Console.ReadLine();
        }
예제 #16
0
        public void Run()
        {
            Console.WriteLine("Choose file:");      // Prompt
            Console.WriteLine("1 - tinyEWD.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 = "tinyEWD.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 adjMatrixEdgeWeightedDigraph = new AdjMatrixEdgeWeightedDigraph(v, e, edges);

            Console.WriteLine(adjMatrixEdgeWeightedDigraph);
            // run Floyd-Warshall algorithm
            var spt = new FloydWarshall(adjMatrixEdgeWeightedDigraph);

            // print all-pairs shortest path distances
            Console.Write("  ");
            for (var vv = 0; vv < adjMatrixEdgeWeightedDigraph.V; vv++)
            {
                Console.Write($"{vv} ");
            }
            Console.WriteLine();
            for (var vv = 0; vv < adjMatrixEdgeWeightedDigraph.V; vv++)
            {
                Console.Write($"{vv}: ");
                for (var w = 0; w < adjMatrixEdgeWeightedDigraph.V; w++)
                {
                    Console.Write(spt.HasPath(vv, w) ? $"{spt.Dist(vv, w):00} " : "  Inf ");
                }
                Console.WriteLine();
            }

            // print negative cycle
            if (spt.HasNegativeCycle)
            {
                Console.WriteLine("Negative cost cycle:");
                foreach (var edge in spt.NegativeCycle())
                {
                    Console.WriteLine(edge);
                }

                Console.WriteLine();
            }

            // print all-pairs shortest paths
            else
            {
                for (var vv = 0; vv < adjMatrixEdgeWeightedDigraph.V; vv++)
                {
                    for (var w = 0; w < adjMatrixEdgeWeightedDigraph.V; w++)
                    {
                        if (spt.HasPath(vv, w))
                        {
                            Console.Write($"{vv} to {w} {spt.Dist(vv, w):00}  ");
                            foreach (var edge in spt.Path(vv, w))
                            {
                                Console.Write($"{edge}  ");
                            }
                            Console.WriteLine();
                        }
                        else
                        {
                            Console.Write($"{vv} to {w} no path{Environment.NewLine}");
                        }
                    }
                }
            }

            Console.ReadLine();
        }
예제 #17
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();
        }
예제 #18
0
        public void Run()
        {
            Console.WriteLine("Choose file:");     // Prompt
            Console.WriteLine("1 - tinyDG.txt");   // Prompt
            Console.WriteLine("2 - mediumDG.txt"); // Prompt
            Console.WriteLine("or quit");          // Prompt

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

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

            case "2":
                fileName = "mediumDG.txt";
                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 <EdgeD>();

            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 edge         = new EdgeD(ve, we);
                    edges.Add(edge);
                }

                lineIterator++;
            }

            var digraph = new Digraph(v, e, edges);

            Console.WriteLine(digraph);

            var scc = new GabowSCC(digraph);

            // number of connected components
            var m = scc.Count();

            Console.WriteLine($"{m} components");

            // compute list of vertices in each strong component
            var components = new Core.Collections.Queue <Integer> [m];

            for (var i = 0; i < m; i++)
            {
                components[i] = new Core.Collections.Queue <Integer>();
            }
            for (var i = 0; i < digraph.V; i++)
            {
                components[scc.Id(i)].Enqueue(i);
            }

            // print results
            for (var i = 0; i < m; i++)
            {
                foreach (int j in components[i])
                {
                    Console.Write($"{j} ");
                }
                Console.WriteLine();
            }

            Console.ReadLine();
        }
예제 #19
0
        public void Run()
        {
            Console.WriteLine("Choose file:");    // Prompt
            Console.WriteLine("1 - tinyDAG.txt"); // Prompt
            Console.WriteLine("or quit");         // Prompt

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

            switch (fileNumber)
            {
            case "1":
                fileName = "tinyDAG.txt";
                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 <EdgeD>();

            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 edge         = new EdgeD(ve, we);
                    edges.Add(edge);
                }

                lineIterator++;
            }

            var digraph = new Digraph(v, e, edges);

            Console.WriteLine(digraph);

            var dfs = new DepthFirstOrder(digraph);

            Console.WriteLine("   v  pre post");
            Console.WriteLine("--------------");
            for (var vi = 0; vi < digraph.V; vi++)
            {
                Console.Write($"{vi} {dfs.Pre(vi)} {dfs.Post(vi)}{Environment.NewLine}");
            }

            Console.Write("Preorder:  ");
            foreach (int vi in dfs.Pre())
            {
                Console.Write($"{vi} ");
            }
            Console.WriteLine();

            Console.Write("Postorder: ");
            foreach (int vi in dfs.Post())
            {
                Console.Write($"{vi} ");
            }
            Console.WriteLine();

            Console.Write("Reverse postorder: ");
            foreach (int vi in dfs.ReversePost())
            {
                Console.Write($"{vi} ");
            }
            Console.ReadLine();
        }
예제 #20
0
        /// <summary>
        /// Reads in a sequence of pairs of integers (between 0 and N-1) from standard input,
        /// where each integer represents some object;
        /// if the objects are in different components, merge the two components
        /// and print the pair to standard output.
        /// </summary>
        public void Run()
        {
            Console.WriteLine("Choose file:");     // Prompt
            Console.WriteLine("1 - tinyUF.txt");   // Prompt
            Console.WriteLine("2 - mediumUF.txt"); // Prompt
            Console.WriteLine("3 - largeUF.txt");  // Prompt
            Console.WriteLine("or quit");          // Prompt

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

            switch (fileNumber)
            {
            case "1":
                fieName = "tinyUF.txt";
                break;

            case "2":
                fieName = "mediumUF.txt";
                break;

            case "3":
                fieName = "largeUF.txt";
                break;

            case "quit":
                return;

            default:
                return;
            }


            var @in        = new In($"Files\\QuickUnionUF\\{fieName}");
            var lines      = @in.ReadAllLines();
            var iterator   = 0;
            var n          = 0;
            var unionsList = new List <UnionUF>();

            foreach (var line in lines)
            {
                if (iterator == 0)
                {
                    n = Convert.ToInt32(line);
                }
                else
                {
                    var lineSplitted = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    if (lineSplitted.Length == 2)
                    {
                        var p = Convert.ToInt32(lineSplitted[0]);
                        var q = Convert.ToInt32(lineSplitted[1]);
                        unionsList.Add(new UnionUF {
                            P = p, Q = q
                        });
                    }
                }

                iterator++;
            }

            var uf = new Core.QuickUnionUF.QuickUnionUF(n);

            iterator = 0;
            foreach (var union in unionsList)
            {
                if (uf.Connected(union.P, union.Q))
                {
                    continue;
                }
                uf.Union(union.P, union.Q);
                //Console.WriteLine("{0} {1}", union.P, union.Q);
                iterator++;
                Console.WriteLine("#{0} {1}", iterator, unionsList.Count);
            }
            Console.WriteLine("{0} components", uf.Count());
            Console.ReadLine();
        }
예제 #21
0
        public void Run()
        {
            Console.WriteLine("Choose file:");  // Prompt
            Console.WriteLine("1 - ip.csv");    // Prompt
            Console.WriteLine("2 - DJIA.csv");  // Prompt
            Console.WriteLine("3 - amino.csv"); // Prompt
            Console.WriteLine("4 - UPC.zip");   // Prompt
            Console.WriteLine("or quit");       // Prompt

            var fileNumber = Console.ReadLine();
            var fieName    = string.Empty;

            switch (fileNumber)
            {
            case "1":
                fieName = "ip.csv";
                break;

            case "2":
                fieName = "DJIA.csv";
                break;

            case "3":
                fieName = "amino.csv";
                break;

            case "4":
                fieName = "UPC.zip";
                break;

            case "quit":
                return;

            default:
                return;
            }


            var @in   = new In($"Files\\Searching\\{fieName}");
            var lines = !fieName.EndsWith("zip") ? @in.ReadAllLines() : @in.ReadAllLinesFromZip();


            //var list = words.Select(word => new StringComparable(word)).ToList();
            //var listComparable = list.Cast<IComparable>().ToList();
            //var arrayComparable = list.Cast<IComparable>().ToArray();
            //var listStrings = words.ToList();

            var lookupCSV = new LookupCSV();

            foreach (var line in lines)
            {
                var lineSplitted = line.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                if (lineSplitted.Length >= 2)
                {
                    var key   = lineSplitted[0];
                    var value = lineSplitted[1];
                    lookupCSV.InsertItem(key, value);
                }
            }

            lookupCSV.Check("cnzz.com");
            lookupCSV.Check("americanexpress.com");
            lookupCSV.Check("www.1gl.ru");
            Console.WriteLine("----------------------------------------------------------------------");
            lookupCSV.Check("26-Oct-28");
            lookupCSV.Check("22-Oct-28");
            lookupCSV.Check("32-Oct-28");
            Console.WriteLine("----------------------------------------------------------------------");
            lookupCSV.Check("CAA");
            lookupCSV.Check("ACT");
            lookupCSV.Check("GRR");
            Console.WriteLine("----------------------------------------------------------------------");
            lookupCSV.Check("0000000010344");
            lookupCSV.Check("0000000492515");
            lookupCSV.Check("5024425379607");

            Console.ReadLine();
        }
예제 #22
0
        public void Run()
        {
            Console.WriteLine("Choose file:");   // Prompt
            Console.WriteLine("1 - aminol.csv"); // Prompt
            Console.WriteLine("2 - movies.txt"); // Prompt
            Console.WriteLine("or quit");        // Prompt

            var fileNumber    = Console.ReadLine();
            var fieName       = string.Empty;
            var lineSeparator = ',';

            switch (fileNumber)
            {
            case "1":
                fieName       = "aminol.csv";
                lineSeparator = ',';
                break;

            case "2":
                fieName       = "movies.txt";
                lineSeparator = '/';
                break;

            case "quit":
                return;

            default:
                return;
            }


            var @in   = new In($"Files\\Searching\\{fieName}");
            var lines = !fieName.EndsWith("zip") ? @in.ReadAllLines() : @in.ReadAllLinesFromZip();


            //var list = words.Select(word => new StringComparable(word)).ToList();
            //var listComparable = list.Cast<IComparable>().ToList();
            //var arrayComparable = list.Cast<IComparable>().ToArray();
            //var listStrings = words.ToList();

            var lookupIndex = new LookupIndex();


            foreach (var line in lines)
            {
                var lineSplitted  = line.Split(new[] { lineSeparator }, StringSplitOptions.RemoveEmptyEntries);
                var key           = string.Empty;
                var fields        = new List <string>();
                var fieldIterator = 0;
                foreach (var field in lineSplitted)
                {
                    if (fieldIterator == 0)
                    {
                        key = field;
                    }
                    else
                    {
                        fields.Add(field);
                    }
                    fieldIterator++;
                }

                lookupIndex.CreateLookup(key, fields);
            }

            lookupIndex.Check("Alanine");
            lookupIndex.Check("Glycine");
            lookupIndex.Check("CGA");
            Console.WriteLine("----------------------------------------------------------------------");
            lookupIndex.Check("Amants du Pont-Neuf, Les (1991)");
            lookupIndex.Check("Bad Boys (1983)");
            lookupIndex.Check("Sheen, Martin");
            Console.WriteLine("----------------------------------------------------------------------");

            Console.ReadLine();
        }
예제 #23
0
        public void Run()
        {
            Console.WriteLine("Choose file:");    // Prompt
            Console.WriteLine("1 - tinyCG.txt");  // Prompt
            Console.WriteLine("2 - mediumG.txt"); // Prompt
            Console.WriteLine("3 - largeG.zip");  // Prompt
            Console.WriteLine("or quit");         // Prompt

            var fileNumber = Console.ReadLine();
            var fieName    = string.Empty;

            switch (fileNumber)
            {
            case "1":
                fieName = "tinyCG.txt";
                break;

            case "2":
                fieName = "mediumG.txt";
                break;

            case "3":
                fieName = "largeG.zip";
                break;

            case "quit":
                return;

            default:
                return;
            }


            var @in   = new In($"Files\\Graphs\\{fieName}");
            var lines = !fieName.EndsWith("zip") ? @in.ReadAllLines() : @in.ReadAllLinesFromZip();

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

            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 edge         = new EdgeU(ve, we);
                    edges.Add(edge);
                }

                lineIterator++;
            }

            var graph = new Graph(v, e, edges);

            if (fileNumber != "3")
            {
                Console.WriteLine(graph);
            }


            const int s    = 0;
            var       bfs1 = new BreadthFirstPaths(graph, s);

            for (var vi = 0; vi < graph.V; vi++)
            {
                if (bfs1.HasPathTo(vi))
                {
                    Console.Write($"{s} to {vi}:  ");
                    foreach (int x in bfs1.PathTo(vi))
                    {
                        if (x == s)
                        {
                            Console.Write(x);
                        }
                        else
                        {
                            Console.Write($"-{x}");
                        }
                    }
                    Console.WriteLine();
                }

                else
                {
                    Console.WriteLine($"{s} to {v}:  not connected{Environment.NewLine}");
                }
                if (vi >= 1 && fileNumber == "3")
                {
                    break;
                }
            }

            //Console.WriteLine("------------------------------------------------");

            Console.ReadLine();
        }
예제 #24
0
        public void Run()
        {
            Console.WriteLine("Choose file:");   // Prompt
            Console.WriteLine("1 - routes.txt"); // Prompt
            Console.WriteLine("2 - movies.txt"); // Prompt
            //Console.WriteLine("3 - largeG.zip"); // Prompt
            Console.WriteLine("or quit");        // Prompt

            var           fileNumber = Console.ReadLine();
            string        fileName;
            char          delimiter;
            List <string> names;

            switch (fileNumber)
            {
            case "1":
                fileName  = "routes.txt";
                delimiter = '\u0020';
                names     = new List <string> {
                    "JFK", "LAX"
                };
                break;

            case "2":
                fileName  = "movies.txt";
                delimiter = '/';
                names     = new List <string> {
                    "Tin Men (1987)", "Bacon, Kevin"
                };
                break;

            //case "3":
            //    fieName = "largeG.zip";
            //    break;
            case "quit":
                return;

            default:
                return;
            }


            var @in   = new In($"Files\\Graphs\\{fileName}");
            var lines = !fileName.EndsWith("zip") ? @in.ReadAllLines() : @in.ReadAllLinesFromZip();

            var sg    = new SymbolGraph(lines, delimiter);
            var graph = sg.G;


            foreach (var name in names)
            {
                Console.WriteLine($"{name}");
                if (sg.Contains(name))
                {
                    var s = sg.Index(name);
                    foreach (int v in graph.Adj(s))
                    {
                        Console.WriteLine($"   {sg.Name(v)}");
                    }
                }
                else
                {
                    Console.WriteLine($"input not contain '{name}'");
                }
            }
            Console.ReadLine();
        }
예제 #25
0
        public void Run()
        {
            Console.WriteLine("Choose file:");      // Prompt
            Console.WriteLine("1 - tinyEWG.txt");   // Prompt
            Console.WriteLine("2 - mediumEWG.txt"); // Prompt
            //Console.WriteLine("3 - mediumEWG.txt"); // Prompt

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

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

            switch (fileNumber)
            {
            case "1":
                fileName = "tinyEWG.txt";
                source   = 6;
                break;

            case "2":
                fileName = "mediumEWG.txt";
                source   = 6;
                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 <EdgeW>();

            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 EdgeW(ve, we, weight);
                    edges.Add(edge);
                }

                lineIterator++;
            }

            var edgeWeightedGraph = new EdgeWeightedGraph(v, e, edges);

            Console.WriteLine(edgeWeightedGraph);

            // compute shortest paths
            var sp = new DijkstraUndirectedSP(edgeWeightedGraph, source);


            // print shortest path
            for (var t = 0; t < edgeWeightedGraph.V; t++)
            {
                if (sp.HasPathTo(t))
                {
                    Console.Write($"{source} to {t} {$"{sp.DistTo(t):0.00000}"}  ");
                    foreach (var edge in sp.PathTo(t))
                    {
                        Console.Write($"{edge}   ");
                    }
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine($"{source} to {t}         no path{Environment.NewLine}");
                }
            }

            Console.ReadLine();
        }