-
Notifications
You must be signed in to change notification settings - Fork 0
/
Breadth first search.cs
55 lines (49 loc) · 1.68 KB
/
Breadth first search.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Variables
{
class Program{
private static Dictionary<string, string[]> _graph = new Dictionary<string, string[]>();
static void Main(string[] args)
{
_graph.Add("you", new[] { "alice", "bob", "claire" });
_graph.Add("bob", new[] { "anuj", "peggy" });
_graph.Add("alice", new[] { "peggy" });
_graph.Add("claire", new[] { "thom", "jonny" });
_graph.Add("anuj", Array.Empty<string>());
_graph.Add("peggy", Array.Empty<string>());
_graph.Add("thom", Array.Empty<string>());
_graph.Add("jonny", Array.Empty<string>());
Search("you");
}
private static bool Search(string name)
{
var searchQueue = new Queue<string>(_graph[name]);
var Searched = new List<string>();
while (searchQueue.Any())
{
var Person = searchQueue.Dequeue();
if (!Searched.Contains(Person))
{
if (PersonIsSeller(Person))
{
Console.WriteLine($"{Person} is a mango seller");
return true;
}
else
{
searchQueue = new Queue<string>(searchQueue.Concat(_graph[Person]));
Searched.Add(Person);
}
}
}
return false;
}
private static bool PersonIsSeller(string name)
{
return name.EndsWith("e");
}
}
}