internal static void PerformWarshall(BitArray2D adj_matrix) { if (adj_matrix == null) { throw new System.ArgumentNullException(nameof(adj_matrix)); } if (adj_matrix.Width != adj_matrix.Height) { const string msg = "Adjacency Matrix width must equal its height"; throw new System.ArgumentException(msg); } for (int k = 0; k < adj_matrix.Width; k++) { for (int row = 0; row < adj_matrix.Height; row++) { for (int col = 0; col < adj_matrix.Width; col++) { bool v = adj_matrix.Get(row, col) | (adj_matrix.Get(row, k) & adj_matrix.Get(k, col)); adj_matrix[row, col] = v; } } } }
public static IEnumerable <DirectedEdge <TNode, object> > GetClosureFromEdges <TNode, TData>( IEnumerable <DirectedEdge <TNode, TData> > edges) { if (edges == null) { throw new System.ArgumentNullException(nameof(edges)); } var dicof_obj_to_id = new Dictionary <TNode, int>(); var dicof_id_to_obj = new Dictionary <int, TNode>(); foreach (var edge in edges) { if (!dicof_obj_to_id.ContainsKey(edge.From)) { dicof_obj_to_id[edge.From] = dicof_obj_to_id.Count; } if (!dicof_obj_to_id.ContainsKey(edge.To)) { dicof_obj_to_id[edge.To] = dicof_obj_to_id.Count; } } foreach (var kv in dicof_obj_to_id) { dicof_id_to_obj[kv.Value] = kv.Key; } var internal_edges = new List <DirectedEdge <int, object> >(); foreach (var edge in edges) { int fromid = dicof_obj_to_id[edge.From]; int toid = dicof_obj_to_id[edge.To]; object data = null; var directed_edge = new DirectedEdge <int, object>(fromid, toid, data); internal_edges.Add(directed_edge); } if (internal_edges.Count == 0) { yield break; } int num_vertices = dicof_obj_to_id.Count; var adj_matrix = new BitArray2D(num_vertices, num_vertices); foreach (var internal_edge in internal_edges) { adj_matrix[internal_edge.From, internal_edge.To] = true; } var warshall_result = adj_matrix.Clone(); ConnectionAnalyzer.PerformWarshall(warshall_result); for (int row = 0; row < adj_matrix.Width; row++) { for (int col = 0; col < adj_matrix.Height; col++) { if (warshall_result.Get(row, col) && (row != col)) { var de = new DirectedEdge <TNode, object>(dicof_id_to_obj[row], dicof_id_to_obj[col], null); yield return(de); } } } }