Created
January 19, 2016 12:35
-
-
Save sudipto80/4442c9683f7c454e3fa4 to your computer and use it in GitHub Desktop.
BFS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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