1

I have the BFS which prints all paths from a defined node as a sequence (in one line). I want to print each path in a line. For example:if I have a directed graph like

A B
A F
B C
B D

I want to have the path from node A like

BC
BD
F

my current script out put is BCDF, which put the all paths together as a sequence.

Also, how I can change it to read a graph from a text file. since I do not want to copy all graph's edges in the scipt

My BFS script

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BFS
{
    class Graph
    {
        private int _V;                                     //It shows the number of nodes
        private bool _directed;                             //If _directed is true, it means that the graph is a directed one
        LinkedList<int>[] _adj;

        public Graph(int V, bool directed)
        {
            _adj = new LinkedList<int>[V];

            for (int i = 0; i < _adj.Length; i++)
            {
                _adj[i] = new LinkedList<int>();
            }

            _V = V;
            _directed = directed;
        }

        public void AddEdge(int v, int w)
        {
            _adj[v].AddLast(w);

            if (!_directed)
            {
                _adj[w].AddLast(v);
            }
        }

        public void BreadthFirstSearch(int s)               //s is the checked node
        {
            bool[] visited = new bool[_V];
            for (int i = 0; i < _V; i++)
                visited[i] = false;

            // Create a queue for BFS
            LinkedList<int> queue = new LinkedList<int>();

            visited[s] = true;                             //if a node is visited we swith its value to true
            queue.AddLast(s);

            while (queue.Any())
            {
                // Dequeue a vertex from queue and print it
                s = queue.First();
                Console.Write(s + " ");
                queue.RemoveFirst();

                LinkedList<int> list = _adj[s];

                foreach (var val in list)
                {
                    if (!visited[val])
                    {
                        visited[val] = true;
                        queue.AddLast(val);
                    }
                }
            }
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            Graph g = new Graph(1400, true); //the number should be the same or more than the number of nodes
            g.AddEdge(1, 2);
            g.AddEdge(2, 4);
            g.AddEdge(2, 5);
            g.AddEdge(2, 3);
            Console.Write("Breadth First Traversal from vertex 2:\n");
            g.BreadthFirstSearch(2); //The number here shows the starting node  
            Console.Read();

        }
    }
}
raven
  • 2,381
  • 2
  • 20
  • 44
  • One point... why use LinkedList as opposed to just a List ? (List I believe is generally faster... see http://stackoverflow.com/questions/169973/when-should-i-use-a-list-vs-a-linkedlist .. another point look into StyleCop and give your variables meaningful names. – Paul Zahra May 12 '16 at 15:04

0 Answers0