-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFSShortestPath.cs
More file actions
77 lines (60 loc) · 1.63 KB
/
BFSShortestPath.cs
File metadata and controls
77 lines (60 loc) · 1.63 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
static void Display(List<List<int>> edges)
{
foreach(var lst in edges)
{
Console.WriteLine(lst[0] + " " + lst[1]);
}
}
static Dictionary<int, HashSet<int>> BuildGraph(List<List<int>> edges)
{
//Display(edges);
Dictionary<int, HashSet<int>> graph = new Dictionary<int, HashSet<int>>();
foreach(var lst in edges)
{
if(!graph.ContainsKey(lst[0]))
{
graph.Add(lst[0], new HashSet<int>());
}
if(!graph.ContainsKey(lst[1]))
{
graph.Add(lst[1], new HashSet<int>());
}
graph[lst[0]].Add(lst[1]);
graph[lst[1]].Add(lst[0]);
}
return graph;
}
public static List<int> bfs(int n, int m, List<List<int>> edges, int s)
{
var graph = BuildGraph(edges);
int[] result = new int[n];
bool[] visited = new bool[n];
Queue<int> queue = new Queue<int>();
queue.Enqueue(s);
visited[s - 1] = true;
while(queue.Count != 0)
{
int node = queue.Dequeue();
if(graph.ContainsKey(node))
{
foreach(int j in graph[node])
{
if(!visited[j - 1])
{
visited[j - 1] = true;
queue.Enqueue(j);
result[j - 1] = result[node - 1] + 6;
}
}
}
}
List<int> finalResult = new List<int>();
for(int i = 0; i < result.Length; i++)
{
if(i != s - 1)
{
finalResult.Add(result[i] == 0 ? -1 : result[i]);
}
}
return finalResult;
}