Skip to content

Commit

Permalink
Add DFS
Browse files Browse the repository at this point in the history
  • Loading branch information
James Lieu committed Dec 9, 2021
1 parent 688580a commit 615c9c1
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 0 deletions.
38 changes: 38 additions & 0 deletions DepthFirstSearch/DepthFirstSearch.Tests/DFSTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using NUnit.Framework;
using System.Collections.Generic;

namespace DepthFirstSearch.Tests
{
public class DFSTests
{
[Test]
public void Test_Search()
{
// arrange
var root = new Node(0);
var one = new Node(1);
var two = new Node(2);
var three = new Node(3);
var four = new Node(4);
var five = new Node(5);

root.Children.Add(one);
root.Children.Add(four);
root.Children.Add(five);

one.Children.Add(four);
one.Children.Add(three);

three.Children.Add(four);
three.Children.Add(two);

two.Children.Add(one);
// act
var actual = new DFS().Search(root, new List<int>());

// assert
var expected = new List<int> { 0, 1, 4, 3, 2, 5 };
Assert.That(actual, Is.EqualTo(expected));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="NUnit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DepthFirstSearch\DepthFirstSearch.csproj" />
</ItemGroup>

</Project>
31 changes: 31 additions & 0 deletions DepthFirstSearch/DepthFirstSearch.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31919.166
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DepthFirstSearch", "DepthFirstSearch\DepthFirstSearch.csproj", "{852EDCD0-DFD7-4397-B3E2-53A8E3C1D8AF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DepthFirstSearch.Tests", "DepthFirstSearch.Tests\DepthFirstSearch.Tests.csproj", "{98DC9FB0-1D20-473A-B544-EF6A036A04B9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{852EDCD0-DFD7-4397-B3E2-53A8E3C1D8AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{852EDCD0-DFD7-4397-B3E2-53A8E3C1D8AF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{852EDCD0-DFD7-4397-B3E2-53A8E3C1D8AF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{852EDCD0-DFD7-4397-B3E2-53A8E3C1D8AF}.Release|Any CPU.Build.0 = Release|Any CPU
{98DC9FB0-1D20-473A-B544-EF6A036A04B9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{98DC9FB0-1D20-473A-B544-EF6A036A04B9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{98DC9FB0-1D20-473A-B544-EF6A036A04B9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{98DC9FB0-1D20-473A-B544-EF6A036A04B9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {68942BB8-96CA-43E2-96E7-6298A59ADDB0}
EndGlobalSection
EndGlobal
41 changes: 41 additions & 0 deletions DepthFirstSearch/DepthFirstSearch/DFS.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace DepthFirstSearch
{
public class Node
{
public bool Visited { get; set; }
public List<Node> Children { get; set; }
public int Value { get; set; }

public Node(int value)
{
Children = new List<Node>();
Value = value;
Visited = false;
}

public void Visit()
{
Visited = true;
}
}
public class DFS
{
public List<int> Search(Node root, List<int> result)
{
if (root == null)
{
return result;
}
root.Visit();
result.Add(root.Value);
for (int i = 0; i < root.Children.Count; i++)
{
if (!root.Children[i].Visited)
{
Search(root.Children[i], result);
}
}
return result;
}
}
}
9 changes: 9 additions & 0 deletions DepthFirstSearch/DepthFirstSearch/DepthFirstSearch.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>

0 comments on commit 615c9c1

Please sign in to comment.