diff --git a/algorithms/CSharp/README.md b/algorithms/CSharp/README.md index 1ad10476..ebe9e827 100644 --- a/algorithms/CSharp/README.md +++ b/algorithms/CSharp/README.md @@ -35,6 +35,7 @@ For running the `.cs` file please using [.Net Finddle](https://dotnetfiddle.net/ - [Factorial](src/Recursion/factorial.cs) ## Graph +- [Breadth First Search](src/Graph/breadth-first-search.cs) - [Depth First Search](src/Graph/depth-first-search.cs) - [Kruskals Algorithm to Find Minimum Spanning Tree](src/Graph/kruskals-algorithm.cs) - [Dijkstras Algorithm to Find Shortest Path](src/Graph/dijkstra.cs) diff --git a/algorithms/CSharp/src/Graph/breadth-first-search.cs b/algorithms/CSharp/src/Graph/breadth-first-search.cs new file mode 100644 index 00000000..60a31bbb --- /dev/null +++ b/algorithms/CSharp/src/Graph/breadth-first-search.cs @@ -0,0 +1,91 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Algorithms.Graph +{ + public class BreadthFirstSearch + { + public class Graph + { + private List> _edges; + private bool _isDirected; + private int _totalNodes; + + public Graph(int totalNodes, bool isDirected) + { + _isDirected = isDirected; + _totalNodes = totalNodes; + _edges = new List>(); + for(int i = 0; i <= totalNodes; i++) + { + _edges.Add(new List()); + } + } + + public void AddEdge(int source, int destination) + { + _edges[source].Add(destination); + if(!_isDirected) + { + _edges[destination].Add(source); + } + } + + public List BFS(int source) + { + List path = new List(); + Queue q = new Queue(); + bool[] visited = Enumerable.Repeat(false, _totalNodes + 1).ToArray(); + + q.Enqueue(source); + visited[source] = true; + + while(q.Count() > 0) + { + int u = q.Dequeue(); + path.Add(u); + + foreach(int v in _edges[u]) + { + if(!visited[v]) + { + q.Enqueue(v); + visited[v] = true; + } + } + } + + return path; + } + } + + public static List BFSDriver(int totalNodes, bool isDirected, int source, List> edges) + { + Graph graph = new Graph(totalNodes, isDirected); + foreach(var edge in edges) + { + graph.AddEdge(edge.Item1, edge.Item2); + } + + return graph.BFS(source); + } + + public static void Main() + { + List> edges = new List>() + { + new Tuple(1, 2), + new Tuple(1, 3), + new Tuple(2, 3), + new Tuple(3, 1), + new Tuple(3, 4), + new Tuple(4, 4), + }; + + var results = BFSDriver(4, true, 3, edges); + Console.WriteLine(string.Join("->", results)); + } + } +} diff --git a/algorithms/CSharp/test/Graph/breadth-first-search.cs b/algorithms/CSharp/test/Graph/breadth-first-search.cs new file mode 100644 index 00000000..237474fd --- /dev/null +++ b/algorithms/CSharp/test/Graph/breadth-first-search.cs @@ -0,0 +1,31 @@ +using System; +using NUnit.Framework; +using System.Collections.Generic; + +namespace Algorithms.Tests.Graph +{ + [TestFixture] + public class BreadthFirstSearch + { + static object[] DivideCasesForBFS = + { + new object[] { 4, true, 3, new List>() { + new Tuple(1, 2), + new Tuple(1, 3), + new Tuple(2, 3), + new Tuple(3, 1), + new Tuple(3, 4), + new Tuple(4, 4), + }, "3->1->4->2" + + }, + }; + + [TestCaseSource(nameof(DivideCasesForBFS))] + public void TestDFS_ShouldGetExpectedResult(int totalNodes, bool isDirected, int source, List> edges, string expected) + { + List result = Algorithms.Graph.BreadthFirstSearch.BFSDriver(totalNodes, isDirected, source, edges); + Assert.AreEqual(expected, string.Join("->", result)); + } + } +}