Example #1
0
        public static void Main(string[] args)
        {
            var file = new FileStream(@"/home/travis/GitHub/RosProbs/c#/Algorithmic Heights/BFS/rosalind_bfs.txt",
                                      FileMode.Open, FileAccess.Read);

            using (var reader = new StreamReader(file))
            {
                // first line is how many nodes, and how many edges
                string line = reader.ReadLine();
                // create array to hold nodes and edges
                int[] data  = new List <int> (Array.ConvertAll(line.Split(' '), int.Parse)).ToArray();
                int   nodes = data [0];
                int   edges = data [1];

                // create new diGraph object
                DiGraph diGraph = new DiGraph(nodes);

                // for each # of edges
                for (int i = 0; i < edges; ++i)
                {
                    // read the line
                    line = reader.ReadLine();
                    // array to hold the node and and connected node
                    int [] tokens = new List <int> (Array.ConvertAll(line.Split(' '), int.Parse)).ToArray();
                    // add the items to the diGraph
                    diGraph.Add(tokens [0], tokens [1]);
                }

                // create a list to hold the results output
                List <int> results = new List <int> ();

                // Breadth First Search on each Node
                for (int i = 1; i <= nodes; ++i)
                {
                    diGraph.BFS(i);
                }

                // get the distance from origin to each node
                // since we init that array to -1: if no way
                // to node we will get -1
                for (int i = 1; i <= nodes; ++i)
                {
                    results.Add(diGraph.distanceTo(i));
                }

                // print the results to the file
                PrintToFile(ref results);
            }
        }