-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from Timmoth/features/dfs
Added DFS implementation
- Loading branch information
Showing
9 changed files
with
154 additions
and
32 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using BenchmarkDotNet.Attributes; | ||
using DsaDotnet.Graphs; | ||
|
||
namespace Benchmarks.Graphs; | ||
|
||
public class DfsBenchmarks | ||
{ | ||
private static readonly RandomNetworkGenerator<int> _randomNetworkGenerator = new(); | ||
private Graph<int> _graph = null!; | ||
[Params(100, 1000)] public int N { get; set; } | ||
|
||
[IterationSetup] | ||
public void Setup() | ||
{ | ||
_graph = new Graph<int>(); | ||
_graph.AddNodes(_randomNetworkGenerator.GenerateRandomNetwork(N, n => n)); | ||
} | ||
|
||
[Benchmark] | ||
[BenchmarkCategory("Graph")] | ||
public void Bfs() | ||
{ | ||
_graph.DepthFirstSearch(0, i => i == N - 1); | ||
} | ||
|
||
[IterationCleanup] | ||
public void Cleanup() | ||
{ | ||
_graph = null!; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,4 @@ | |
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
</PropertyGroup> | ||
|
||
</Project> | ||
</Project> |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
namespace DsaDotnet.Graphs; | ||
|
||
public static partial class Traversal | ||
{ | ||
public static Node<T>? DepthFirstSearch<T>(this Graph<T> graph, T start, Predicate<T> predicate) where T : notnull | ||
{ | ||
if (!graph.Nodes.ContainsKey(start)) | ||
{ | ||
throw new ArgumentException("Start node does not exist in the graph."); | ||
} | ||
|
||
var visited = new HashSet<T>(); | ||
var stack = new Stack<Node<T>>(); | ||
|
||
var startNode = graph.Nodes[start]; | ||
stack.Push(startNode); | ||
visited.Add(start); | ||
|
||
if (predicate(startNode.Data)) | ||
{ | ||
return startNode; | ||
} | ||
|
||
while (stack.Count > 0) | ||
{ | ||
var currentNode = stack.Pop(); | ||
|
||
foreach (var neighbor in currentNode.Neighbors) | ||
{ | ||
if (visited.Contains(neighbor.Data)) | ||
{ | ||
continue; | ||
} | ||
|
||
if (predicate(neighbor.Data)) | ||
{ | ||
return neighbor; | ||
} | ||
|
||
stack.Push(neighbor); | ||
visited.Add(neighbor.Data); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using DsaDotnet.Graphs; | ||
using FluentAssertions; | ||
|
||
namespace Tests.Graphs; | ||
|
||
public class DfsTests | ||
{ | ||
[Theory] | ||
[ClassData(typeof(GraphSearchTestData))] | ||
public void Dfs_Finds_ShortestPath_From_Source_To_Destination((int a, int b)[] edges, Predicate<int> predicate, | ||
int start, int? expected) | ||
{ | ||
// Arrange | ||
var graph = new Graph<int>(); | ||
graph.AddEdges(edges); | ||
|
||
// Act | ||
var node = graph.DepthFirstSearch(start, predicate); | ||
|
||
// Assert | ||
node?.Data.Should().Be(expected); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace Tests.Graphs; | ||
|
||
public class GraphSearchTestData : TheoryData<(int a, int b)[], Predicate<int>, int, int?> | ||
{ | ||
public GraphSearchTestData() | ||
{ | ||
Add(new[] { (0, 1), (1, 2) }, i => i == 3, 0, null); | ||
Add(new[] { (0, 1) }, i => i == 0, 0, 0); | ||
Add(new[] { (0, 1), (1, 2), (2, 3) }, i => i == 3, 0, 3); | ||
Add(new[] { (0, 1), (1, 2), (2, 3) }, i => i == 3, 2, 3); | ||
Add(new[] { (0, 1), (0, 2), (1, 3), (2, 4), (3, 4) }, i => i == 4, 0, 4); | ||
Add(new[] { (0, 1), (0, 2), (2, 1), (2, 2), (1, 3), (2, 4), (3, 4) }, i => i == 4, 0, 4); | ||
} | ||
} |