Skip to content

Commit af33007

Browse files
author
Kapil Borle
committed
Create a digraph base class
1 parent 6363d4c commit af33007

File tree

2 files changed

+38
-79
lines changed

2 files changed

+38
-79
lines changed

Engine/Helper.cs

Lines changed: 27 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3557,81 +3557,58 @@ public object VisitUsingExpression(UsingExpressionAst usingExpressionAst)
35573557
}
35583558

35593559
/// Class to represent a directed graph
3560-
public class Digraph<T>
3560+
public class Digraph
35613561
{
3562-
public int NumVertices
3563-
{
3564-
get { return graph.Count; }
3565-
}
3566-
35673562
private List<List<int>> graph;
3568-
private Dictionary<T, int> vertexIndexMap;
3569-
35703563

3571-
private int GetIndex(T vertex)
3564+
public Digraph()
35723565
{
3573-
int idx;
3574-
return vertexIndexMap.TryGetValue(vertex, out idx) ? idx : -1;
3566+
graph = new List<List<int>> ();
35753567
}
35763568

3577-
public IEnumerable<T> GetNeighbors(T vertex)
3569+
public int GetNumVertices()
35783570
{
3579-
ValidateVertexArgument(vertex);
3580-
var idx = GetIndex(vertex);
3581-
var idxVertexMap = vertexIndexMap.ToDictionary(x => x.Value, x => x.Key);
3582-
foreach (var neighbor in graph[idx])
3583-
{
3584-
yield return idxVertexMap[neighbor];
3585-
}
3571+
return graph.Count;
35863572
}
35873573

3588-
public int GetNumNeighbors(T vertex)
3574+
public int GetNumNeighbors(int vertex)
35893575
{
3590-
ValidateVertexArgument(vertex);
3591-
return graph[GetIndex(vertex)].Count;
3576+
return GetNeighbors(vertex).Count();
35923577
}
35933578

3594-
public Digraph()
3579+
public IEnumerable<int> GetNeighbors(int vertex)
35953580
{
3596-
graph = new List<List<int>> ();
3597-
vertexIndexMap = new Dictionary<T, int>();
3581+
ValidateVertex(vertex);
3582+
return graph[vertex];
35983583
}
35993584

3600-
public void AddVertex(T vertex)
3585+
public bool ContainsVertex(int vertex)
36013586
{
3602-
if (vertex == null)
3603-
{
3604-
throw new ArgumentNullException("vertex");
3605-
}
3606-
3607-
if (GetIndex(vertex) != -1)
3608-
{
3609-
throw new ArgumentException("Vertex already present! Cannot add it to the Digraph", "vertex");
3610-
}
3587+
return vertex >= 0 && vertex < graph.Count;
3588+
}
36113589

3612-
vertexIndexMap.Add(vertex, graph.Count);
3590+
public void AddVertex()
3591+
{
36133592
graph.Add(new List<int>());
36143593
}
36153594

3616-
public void AddEdge(T fromVertex, T toVertex)
3595+
public void AddEdge(int fromVertex, int toVertex)
36173596
{
3618-
ValidateVertexArgument(fromVertex);
3619-
ValidateVertexArgument(toVertex);
3620-
3621-
var toIdx = GetIndex(toVertex);
3622-
var fromVertexList = graph[GetIndex(fromVertex)];
3623-
if (!fromVertexList.Contains(toIdx))
3597+
ValidateVertex(fromVertex);
3598+
ValidateVertex(toVertex);
3599+
if (graph[fromVertex].Contains(toVertex))
36243600
{
3625-
fromVertexList.Add(toIdx);
3601+
throw new ArgumentException(String.Format("Edge from {0} to {1} already present", fromVertex, toVertex));
36263602
}
3603+
graph[fromVertex].Add(toVertex);
36273604
}
36283605

3629-
public bool IsConnected(T vertex1, T vertex2)
3606+
public bool IsConnected(int vertex1, int vertex2)
36303607
{
3631-
ValidateVertexArgument(vertex1);
3632-
ValidateVertexArgument(vertex2);
3608+
ValidateVertex(vertex1);
3609+
ValidateVertex(vertex2);
36333610
var visited = new bool[graph.Count];
3634-
return IsConnected(GetIndex(vertex1), GetIndex(vertex2), ref visited);
3611+
return IsConnected(vertex1, vertex2, ref visited);
36353612
}
36363613

36373614
private bool IsConnected(int fromIdx, int toIdx, ref bool[] visited)
@@ -3659,26 +3636,12 @@ private bool IsConnected(int fromIdx, int toIdx, ref bool[] visited)
36593636
return isConnected;
36603637
}
36613638

3662-
private void ValidateNotNull(T vertex)
3639+
private void ValidateVertex(int vertex)
36633640
{
3664-
if (vertex == null)
3665-
{
3666-
throw new ArgumentNullException("vertex");
3667-
}
3668-
}
3669-
3670-
private void ValidateVertexPresence(T vertex)
3671-
{
3672-
if (GetIndex(vertex) == -1)
3641+
if (!ContainsVertex(vertex))
36733642
{
36743643
throw new ArgumentOutOfRangeException("vertex not present in the Digraph.", "vertex");
36753644
}
36763645
}
3677-
3678-
private void ValidateVertexArgument(T vertex)
3679-
{
3680-
ValidateNotNull(vertex);
3681-
ValidateVertexPresence(vertex);
3682-
}
36833646
}
36843647
}

Tests/Engine/Helpers.tests.ps1

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,26 @@ Import-Module PSScriptAnalyzer
22

33
Describe "Test Directed Graph" {
44
Context "When a graph is created" {
5-
$digraph = New-Object -TypeName 'Microsoft.Windows.PowerShell.ScriptAnalyzer.DiGraph[string]'
6-
$digraph.AddVertex('v1');
7-
$digraph.AddVertex('v2');
8-
$digraph.AddVertex('v3');
9-
$digraph.AddVertex('v4');
10-
$digraph.AddVertex('v5');
5+
$digraph = New-Object -TypeName 'Microsoft.Windows.PowerShell.ScriptAnalyzer.DiGraph'
6+
0..4 | ForEach-Object {$digraph.AddVertex()}
117

12-
$digraph.AddEdge('v1', 'v2');
13-
$digraph.AddEdge('v1', 'v5');
14-
$digraph.AddEdge('v2', 'v4');
8+
$digraph.AddEdge(0, 1);
9+
$digraph.AddEdge(0, 4);
10+
$digraph.AddEdge(1, 3);
1511

1612
It "correctly adds the vertices" {
17-
$digraph.NumVertices | Should Be 5
13+
$digraph.GetNumVertices() | Should Be 5
1814
}
1915

2016
It "correctly adds the edges" {
21-
$digraph.GetNumNeighbors('v1') | Should Be 2
22-
$neighbors = $digraph.GetNeighbors('v1')
23-
$neighbors -contains 'v2' | Should Be $true
24-
$neighbors -contains 'v5' | Should Be $true
17+
$digraph.GetNumNeighbors(0) | Should Be 2
18+
$neighbors = $digraph.GetNeighbors(0)
19+
$neighbors -contains 1 | Should Be $true
20+
$neighbors -contains 4 | Should Be $true
2521
}
2622

2723
It "finds the connection" {
28-
$digraph.IsConnected('v1', 'v4') | Should Be $true
24+
$digraph.IsConnected(0, 3) | Should Be $true
2925
}
3026
}
3127
}

0 commit comments

Comments
 (0)