Skip to content

Commit b942647

Browse files
authored
PR: Ver 1.2.1-poc
2 parents 18a0cbe + 3c970fc commit b942647

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+3316
-20
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
8+
namespace GraphAlgorithmsLibrary.Algorithms
9+
{
10+
public class CircularEdgesCounter
11+
{
12+
13+
private readonly Dictionary<int, List<int>> _graph;
14+
15+
public CircularEdgesCounter(Dictionary<int, List<int>> graph)
16+
{
17+
_graph = graph;
18+
}
19+
20+
public int CountCircularEdges()
21+
{
22+
int circularEdgeCount = 0;
23+
HashSet<int> visited = new HashSet<int>();
24+
HashSet<int> recursionStack = new HashSet<int>();
25+
26+
bool HasCycle(int node)
27+
{
28+
if (recursionStack.Contains(node))
29+
return true;
30+
if (visited.Contains(node))
31+
return false;
32+
33+
visited.Add(node);
34+
recursionStack.Add(node);
35+
36+
if (_graph.ContainsKey(node))
37+
{
38+
foreach (var neighbor in _graph[node])
39+
{
40+
if (HasCycle(neighbor))
41+
{
42+
circularEdgeCount++;
43+
return true;
44+
}
45+
}
46+
}
47+
48+
recursionStack.Remove(node);
49+
return false;
50+
}
51+
52+
foreach (var node in _graph.Keys)
53+
{
54+
if (HasCycle(node))
55+
{
56+
// Continue searching even after finding a cycle to count all circular edges
57+
}
58+
}
59+
60+
return circularEdgeCount;
61+
}
62+
}
63+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+

2+
using System;
3+
using System.Collections.Concurrent;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
8+
namespace GraphAlgorithmsLibrary.Algorithms
9+
{
10+
public class DeepFirstSearch
11+
{
12+
private readonly Dictionary<int, List<int>> _graph;
13+
private int? maxNodeCache = null; // Cache the max node value
14+
15+
public DeepFirstSearch(Dictionary<int, List<int>> graph)
16+
{
17+
_graph = graph;
18+
}
19+
20+
// Recursive DFS function to find all paths
21+
public async Task<List<List<int>>> Execute(int src, int dst)
22+
{
23+
ConcurrentBag<List<int>> allPaths = new ConcurrentBag<List<int>>();
24+
int maxNode = GetMaxNode();
25+
26+
bool[] visited = new bool[maxNode + 1];
27+
28+
async Task DFSAsync(int current, List<int> path, bool[] localVisited)
29+
{
30+
localVisited[current] = true;
31+
32+
if (current == dst)
33+
{
34+
allPaths.Add(new List<int>(path));
35+
}
36+
else if (_graph.ContainsKey(current))
37+
{
38+
var tasks = new List<Task>();
39+
foreach (var neighbor in _graph[current])
40+
{
41+
if (!localVisited[neighbor])
42+
{
43+
var newPath = new List<int>(path) { neighbor };
44+
var newVisited = (bool[])localVisited.Clone();
45+
tasks.Add(DFSAsync(neighbor, newPath, newVisited));
46+
}
47+
}
48+
await Task.WhenAll(tasks);
49+
}
50+
}
51+
52+
var initialTasks = new List<(int, List<int>, bool[])>();
53+
Stack<(int, List<int>)> stack = new Stack<(int, List<int>)>();
54+
stack.Push((src, new List<int> { src }));
55+
56+
while (stack.Count > 0)
57+
{
58+
var (current, path) = stack.Pop();
59+
if (_graph.ContainsKey(current))
60+
{
61+
foreach (var neighbor in _graph[current])
62+
{
63+
if (!visited[neighbor])
64+
{
65+
var newPath = new List<int>(path) { neighbor };
66+
initialTasks.Add((neighbor, newPath, (bool[])visited.Clone()));
67+
}
68+
}
69+
}
70+
}
71+
72+
// Parallel execution with auto-adjusted chunks
73+
var parallelOptions = new ParallelOptions
74+
{
75+
MaxDegreeOfParallelism = Environment.ProcessorCount
76+
};
77+
78+
var partitioner = Partitioner.Create(initialTasks, EnumerablePartitionerOptions.NoBuffering);
79+
var tasksParallel = new ConcurrentBag<Task>();
80+
81+
Parallel.ForEach(partitioner, parallelOptions, initialTask =>
82+
{
83+
var (current, path, localVisited) = initialTask;
84+
tasksParallel.Add(DFSAsync(current, path, localVisited));
85+
});
86+
87+
await Task.WhenAll(tasksParallel);
88+
89+
return allPaths.ToList();
90+
}
91+
92+
private int GetMaxNode()
93+
{
94+
if (maxNodeCache == null)
95+
{
96+
maxNodeCache = _graph.Keys.Concat(_graph.Values.SelectMany(v => v)).DefaultIfEmpty(-1).Max();
97+
}
98+
return maxNodeCache.Value;
99+
}
100+
}
101+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{F2B1B7B8-8E92-4BA4-8D1A-0D6329134633}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>GraphAlgorithmsLibrary</RootNamespace>
11+
<AssemblyName>GraphAlgorithmsLibrary</AssemblyName>
12+
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<Deterministic>true</Deterministic>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<DebugType>pdbonly</DebugType>
27+
<Optimize>true</Optimize>
28+
<OutputPath>bin\Release\</OutputPath>
29+
<DefineConstants>TRACE</DefineConstants>
30+
<ErrorReport>prompt</ErrorReport>
31+
<WarningLevel>4</WarningLevel>
32+
</PropertyGroup>
33+
<ItemGroup>
34+
<Reference Include="System" />
35+
<Reference Include="System.Core" />
36+
<Reference Include="System.Xml.Linq" />
37+
<Reference Include="System.Data.DataSetExtensions" />
38+
<Reference Include="Microsoft.CSharp" />
39+
<Reference Include="System.Data" />
40+
<Reference Include="System.Net.Http" />
41+
<Reference Include="System.Xml" />
42+
</ItemGroup>
43+
<ItemGroup>
44+
<Compile Include="Algorithms\CircularEdgesCounter.cs" />
45+
<Compile Include="Algorithms\DeepFirstSearch.cs" />
46+
<Compile Include="GraphDirectionalEdges.cs" />
47+
<Compile Include="GraphNonDirectionalEdges.cs" />
48+
<Compile Include="GraphBase.cs" />
49+
<Compile Include="Properties\AssemblyInfo.cs" />
50+
</ItemGroup>
51+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
52+
</Project>

GraphAlgorithmsLibrary/GraphBase.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Ignore Spelling: DFS
2+
3+
using GraphAlgorithmsLibrary.Algorithms;
4+
using System.Collections.Generic;
5+
using System.Threading.Tasks;
6+
7+
namespace GraphAlgorithmsLibrary
8+
{
9+
public abstract class GraphBase
10+
{
11+
internal readonly Dictionary<int, List<int>> graph = new Dictionary<int, List<int>>();
12+
13+
public abstract void AddEdge(int u, int v);
14+
15+
public async Task<List<List<int>>> DFS_FindAllPathsAsync(int src, int dst)
16+
{
17+
DeepFirstSearch deepFirstSearch = new DeepFirstSearch(graph);
18+
return await deepFirstSearch.Execute(src, dst);
19+
}
20+
21+
public Dictionary<int, List<int>> GetGraph()
22+
{
23+
return graph;
24+
}
25+
26+
public int CountCircularEdges()
27+
{
28+
CircularEdgesCounter counter = new CircularEdgesCounter(graph);
29+
30+
return counter.CountCircularEdges();
31+
}
32+
}
33+
34+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Collections.Generic;
2+
3+
namespace GraphAlgorithmsLibrary
4+
{
5+
public class GraphDirectionalEdges : GraphBase
6+
{
7+
public override void AddEdge(int u, int v)
8+
{
9+
if (!graph.ContainsKey(u))
10+
{
11+
graph[u] = new List<int>();
12+
}
13+
graph[u].Add(v);
14+
15+
if (!graph.ContainsKey(v))
16+
{
17+
graph[v] = new List<int>();
18+
}
19+
}
20+
}
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Collections.Generic;
2+
3+
namespace GraphAlgorithmsLibrary
4+
{
5+
public class GraphNonDirectionalEdges : GraphBase
6+
{
7+
public override void AddEdge(int u, int v)
8+
{
9+
if (!graph.ContainsKey(u))
10+
{
11+
graph[u] = new List<int>();
12+
}
13+
graph[u].Add(v);
14+
15+
if (!graph.ContainsKey(v))
16+
{
17+
graph[v] = new List<int>();
18+
}
19+
// line where non directional edge added.
20+
graph[v].Add(u);
21+
}
22+
}
23+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("GraphAlgorithmsLibrary")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("GraphAlgorithmsLibrary")]
13+
[assembly: AssemblyCopyright("Copyright © 2024")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("f2b1b7b8-8e92-4ba4-8d1a-0d6329134633")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

VisJsNetworkLibrary/Exceptions/ExceptionMessage.cs renamed to GraphVisualizationLibrary/Exceptions/ExceptionMessage.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,15 @@ public static string NotAllValuesAreIntegersInCountColumn()
2626
{
2727
return "Selected Count column has non integers values! All values should to be whole numbers.";
2828
}
29+
30+
public static string PathBetweenNodesNotFound()
31+
{
32+
return "There are no path found between source node and destination node.";
33+
}
34+
35+
public static string NodeIdNotFound(string nodeLabel)
36+
{
37+
return $"Node with value {nodeLabel} do not exists in data range. Check if you write it correctly.";
38+
}
2939
}
3040
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Runtime.Serialization;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace VisJsNetworkLibrary.Exceptions
9+
{
10+
internal class NodeIdNotFoundException : Exception
11+
{
12+
public NodeIdNotFoundException()
13+
{
14+
}
15+
16+
public NodeIdNotFoundException(string message) : base(message)
17+
{
18+
}
19+
20+
public NodeIdNotFoundException(string message, Exception innerException) : base(message, innerException)
21+
{
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)