chore(CSharp): add breadth first search (#525)

pull/504/head^2
Waqar Hassan Khan 2021-10-07 00:38:05 +06:00 committed by GitHub
parent a131e2e14a
commit ce53af33ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 123 additions and 0 deletions

View File

@ -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)

View File

@ -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<List<int>> _edges;
private bool _isDirected;
private int _totalNodes;
public Graph(int totalNodes, bool isDirected)
{
_isDirected = isDirected;
_totalNodes = totalNodes;
_edges = new List<List<int>>();
for(int i = 0; i <= totalNodes; i++)
{
_edges.Add(new List<int>());
}
}
public void AddEdge(int source, int destination)
{
_edges[source].Add(destination);
if(!_isDirected)
{
_edges[destination].Add(source);
}
}
public List<int> BFS(int source)
{
List<int> path = new List<int>();
Queue<int> q = new Queue<int>();
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<int> BFSDriver(int totalNodes, bool isDirected, int source, List<Tuple<int, int>> 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<Tuple<int, int>> edges = new List<Tuple<int, int>>()
{
new Tuple<int, int>(1, 2),
new Tuple<int, int>(1, 3),
new Tuple<int, int>(2, 3),
new Tuple<int, int>(3, 1),
new Tuple<int, int>(3, 4),
new Tuple<int, int>(4, 4),
};
var results = BFSDriver(4, true, 3, edges);
Console.WriteLine(string.Join("->", results));
}
}
}

View File

@ -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<Tuple<int, int>>() {
new Tuple<int, int>(1, 2),
new Tuple<int, int>(1, 3),
new Tuple<int, int>(2, 3),
new Tuple<int, int>(3, 1),
new Tuple<int, int>(3, 4),
new Tuple<int, int>(4, 4),
}, "3->1->4->2"
},
};
[TestCaseSource(nameof(DivideCasesForBFS))]
public void TestDFS_ShouldGetExpectedResult(int totalNodes, bool isDirected, int source, List<Tuple<int, int>> edges, string expected)
{
List<int> result = Algorithms.Graph.BreadthFirstSearch.BFSDriver(totalNodes, isDirected, source, edges);
Assert.AreEqual(expected, string.Join("->", result));
}
}
}