-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTransitiveClosureFloydWarshall.cs
More file actions
39 lines (31 loc) · 1.26 KB
/
Copy pathTransitiveClosureFloydWarshall.cs
File metadata and controls
39 lines (31 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System;
using AlgorithmsAndDataStructures.Algorithms.Graph.Common;
namespace AlgorithmsAndDataStructures.Algorithms.Graph.Misc;
public class TransitiveClosureFloydWarshall
{
#pragma warning disable CA1822 // Mark members as static
public bool[][] GetReachabilityMatrix(WeightedGraphVertex[] graph)
#pragma warning restore CA1822 // Mark members as static
{
if (graph is null) return Array.Empty<bool[]>();
var reachabillityMatrix = new bool[graph.Length][];
for (var i = 0; i < reachabillityMatrix.Length; i++)
{
reachabillityMatrix[i] = new bool[graph.Length];
reachabillityMatrix[i][i] = true;
for (var j = 0; j < graph[i].Edges.Count; j++)
{
var edge = graph[i].Edges[j];
reachabillityMatrix[i][edge.To] = true;
}
}
for (var i = 0; i < graph.Length; i++)
for (var j = 0; j < graph.Length; j++)
for (var k = 0; k < graph.Length; k++)
{
var isReachableThroughCurrentlyInspectedVertex = reachabillityMatrix[i][k] && reachabillityMatrix[k][j];
if (isReachableThroughCurrentlyInspectedVertex) reachabillityMatrix[i][j] = true;
}
return reachabillityMatrix;
}
}