Skip to content

Instantly share code, notes, and snippets.

@sudipto80
Created January 19, 2016 12:35
Show Gist options
  • Save sudipto80/4442c9683f7c454e3fa4 to your computer and use it in GitHub Desktop.
Save sudipto80/4442c9683f7c454e3fa4 to your computer and use it in GitHub Desktop.
BFS
Dictionary<string,List<string>> graph =
new Dictionary<string,List<string>>();
graph.Add("A",new List<string>(){"B","C","D"});
graph.Add("B",new List<string>(){"A","E","F"});
graph.Add("E",new List<string>(){"B","G"});
List<string> visited = new List<string>();
Queue<string> qu = new Queue<string>();
qu.Enqueue("A");
visited.Add("A");
while (qu.Count ()!=0)
{
string v = qu.Dequeue();
if(graph.ContainsKey(v))
{
foreach (var adj in graph[v])
{
if(!visited.Contains(adj))
{
qu.Enqueue(adj);
visited.Add(adj);
}
}
}
}
visited.Dump("BFS");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment