Skip to content

Commit 876be37

Browse files
committed
fixed ArgumentOutOfRange in Acyclic property check
1 parent 41a3aa2 commit 876be37

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

BinaryRelations/Binary/Properties.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,13 @@ public static bool IsAcyclic(this bool[,] matrix1)
245245
{
246246
ThrowIfNull_NotQuad(matrix1);
247247
var node = 0;
248+
var lastNode = matrix1.GetLength(1) - 1;
248249
var children = GetConnectedEdges(matrix1, node).ToArray();
249-
while (!children.Any() || node != matrix1.GetLength(1) - 1)
250+
while (!children.Any())
250251
{
251-
children = GetConnectedEdges(matrix1, ++node).ToArray();
252+
if (node >= lastNode)
253+
break;
254+
children = GetConnectedEdges(matrix1, node++).ToArray();
252255
}
253256
return !HasCycle(matrix1, node, new HashSet<int>());
254257
}
@@ -259,13 +262,8 @@ private static bool HasCycle(in bool[,] matrix1, int node, in HashSet<int> path)
259262
return true;
260263
var current = new HashSet<int>(path) { node };
261264

262-
foreach (var child in GetConnectedEdges(matrix1, node))
263-
{
264-
if (HasCycle(matrix1, child, current))
265-
return true;
266-
}
267-
268-
return false;
265+
var shade = matrix1;
266+
return GetConnectedEdges(matrix1, node).Any(child => HasCycle(shade, child, current));
269267
}
270268

271269
/// <summary>

BinaryRelationsTests/PropertiesTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ public void IsAntiSymmetric()
136136
public void IsAcyclic()
137137
{
138138
var m = new[,]
139+
{
140+
{0, 1, 0},
141+
{0, 0, 1},
142+
{0, 0, 0}
143+
}.Cast<int, bool>();
144+
Assert.True(m.IsAcyclic());
145+
146+
m = new[,]
139147
{
140148
{0, 1, 0, 0},
141149
{0, 0, 0, 1},

0 commit comments

Comments
 (0)