| | 1 | | namespace MoreStructures.Graphs; |
| | 2 | |
|
| | 3 | | /// <summary> |
| | 4 | | /// A graph data structure, represented as a matrix: the (i, j) element of the matrix is true if the vertex with id i |
| | 5 | | /// is neighbor of the vertex with id j, and false otherwise. |
| | 6 | | /// </summary> |
| | 7 | | /// <param name="AdjacencyMatrix"> |
| | 8 | | /// A square matrix of boolean, each value representing whether a vertex is neighborhood of another one. |
| | 9 | | /// </param> |
| | 10 | | /// <remarks> |
| | 11 | | /// - <b>This representation doesn't support multigraphs</b>, i.e. graphs which can have multiple parallel edges |
| | 12 | | /// between the same two vertices. |
| | 13 | | /// <br/> |
| | 14 | | /// - If the graph can be considered undirected if all edges come in couples with both directions: i.e. the matrix is |
| | 15 | | /// simmetrix, i.e. <c>M[i, j] == M[j, i] for all (i, j)</c>. |
| | 16 | | /// <br/> |
| | 17 | | /// - The size of this data structure is proportional to the square of the number of vertices of the graph. |
| | 18 | | /// <br/> |
| | 19 | | /// - So, this graph representation is particularly useful when the number is edges is proportional to the square of |
| | 20 | | /// the number of vertices v in the graph, and O(v) retrieval of the incoming and outgoing edges is required. |
| | 21 | | /// <br/> |
| | 22 | | /// - Performance is O(v), whether <c>takeIntoAccountEdgeDirection</c> is true or not. |
| | 23 | | /// <br/> |
| | 24 | | /// - Notice that O(v) is worse than O(avg_e), where avg_e is the average number of edges coming out of a vertex, for |
| | 25 | | /// sparse graphs, and comparable for dense graphs. |
| | 26 | | /// <br/> |
| | 27 | | /// - This representation is particularly convenient when used as a directed graph and traversal has often to be done |
| | 28 | | /// in reversed direction, since <see cref="Reverse"/> is an O(1) operation (it just builds a proxy to the original |
| | 29 | | /// graph) and <see cref="GetAdjacentVerticesAndEdges(int, bool)"/> has comparable O(v) complexities when traversing |
| | 30 | | /// edges according to their direction or in any direction. |
| | 31 | | /// <br/> |
| | 32 | | /// - Notice that <see cref="AdjacencyListGraph"/> has better runtime (O(avg_e)) when edges are traversed according to |
| | 33 | | /// their direction, and worse runtime (O(avg_e + v)) when edges are traversed in any direction. |
| | 34 | | /// <br/> |
| | 35 | | /// - <see cref="EdgeListGraph"/> has consistent runtime in both traversal (O(e)), but e is O(v^2) in dense graphs, |
| | 36 | | /// leading to sensibly worse performance in such scenarios. |
| | 37 | | /// </remarks> |
| | 38 | | /// <example> |
| | 39 | | /// The followin graph: |
| | 40 | | /// <code> |
| | 41 | | /// 0 --> 1 <==> 3 |
| | 42 | | /// | ^ ^ / |
| | 43 | | /// | | / / |
| | 44 | | /// | | / / |
| | 45 | | /// v |/ / |
| | 46 | | /// 2 <----- |
| | 47 | | /// </code> |
| | 48 | | /// is represented as <c>AdjacencyMatrixGraph(new {{ F, T, T, F }, { F, F, F, T }, { T, T, F, F }, {F, T, T, F }})</c>. |
| | 49 | | /// </example> |
| 15586 | 50 | | public record AdjacencyMatrixGraph(bool[,] AdjacencyMatrix) : IGraph |
| 184 | 51 | | { |
| 184 | 52 | | /// <inheritdoc path="//*[not(self::remarks)]" /> |
| 184 | 53 | | /// <remarks> |
| 184 | 54 | | /// In the <see cref="AdjacencyMatrixGraph"/> representation, corresponds to the edge of the square matrix. |
| 184 | 55 | | /// <br/> |
| 184 | 56 | | /// Time and Space Complexity are O(1). |
| 184 | 57 | | /// </remarks> |
| 126 | 58 | | public int GetNumberOfVertices() => AdjacencyMatrix.GetLength(0); |
| 184 | 59 | |
|
| 184 | 60 | | /// <inheritdoc path="//*[not(self::remarks)]" /> |
| 184 | 61 | | /// <remarks> |
| 184 | 62 | | /// <para id="algorithm"> |
| 184 | 63 | | /// ALGORITHM |
| 184 | 64 | | /// <br/> |
| 184 | 65 | | /// - Iterates over all the cells of the adjacency matrix M. |
| 184 | 66 | | /// <br/> |
| 184 | 67 | | /// - For each adjacency M[u, v] set in M, the edge (u, v) is returned. |
| 184 | 68 | | /// </para> |
| 184 | 69 | | /// <para id="complexity"> |
| 184 | 70 | | /// COMPLEXITY |
| 184 | 71 | | /// <br/> |
| 184 | 72 | | /// - The adjacency matrix has v rows and v columns, where v is the number of vertices in the graph. |
| 184 | 73 | | /// <br/> |
| 184 | 74 | | /// - Therefore Time Complexity is O(v^2). Space Complexity is O(1), since the iteration uses a constant |
| 184 | 75 | | /// amount of space. |
| 184 | 76 | | /// </para> |
| 184 | 77 | | /// </remarks> |
| 184 | 78 | | public IEnumerable<(int edgeStart, int edgeEnd)> GetAllEdges() => |
| 40 | 79 | | from u in Enumerable.Range(0, AdjacencyMatrix.GetLength(0)) |
| 790 | 80 | | from v in Enumerable.Range(0, AdjacencyMatrix.GetLength(1)) |
| 660 | 81 | | where AdjacencyMatrix[u, v] |
| 155 | 82 | | select (u, v); |
| 184 | 83 | |
|
| 184 | 84 | | /// <inheritdoc path="//*[not(self::remarks)]" /> |
| 184 | 85 | | /// <remarks> |
| 184 | 86 | | /// <para id="algorithm"> |
| 184 | 87 | | /// ALGORITHM |
| 184 | 88 | | /// <br/> |
| 184 | 89 | | /// - Unlike the adjacency list representation, the matrix representation allows to access neighborhoods based |
| 184 | 90 | | /// on both outgoing and incoming edges of a given vertex (the first is a row, the second is a column). |
| 184 | 91 | | /// <br/> |
| 184 | 92 | | /// - Therefore, unlike the adjacency list representation, when the value of |
| 184 | 93 | | /// <paramref name="takeIntoAccountEdgeDirection"/> is <see langword="false"/>, a lookup of all neighborhoods |
| 184 | 94 | | /// defined in the matrix (i.e. a full matrix lookup) is not required. |
| 184 | 95 | | /// <br/> |
| 184 | 96 | | /// - Instead, a single additional lookup of the neighborhood of incoming edges, is required, in addition to |
| 184 | 97 | | /// the lookup of the of the neighborhood of outgoing edges. |
| 184 | 98 | | /// <br/> |
| 184 | 99 | | /// - Notice that, while in the adjacency list representation the neighborhood precisely contains the number of |
| 184 | 100 | | /// neighboring vertices, avg_e, in the adjacency matrix representation the neighborhood is in the form of a |
| 184 | 101 | | /// boolean array of v items, where v is the number of vertices of the graph. |
| 184 | 102 | | /// </para> |
| 184 | 103 | | /// <para id="complexity"> |
| 184 | 104 | | /// COMPLEXITY |
| 184 | 105 | | /// <br/> |
| 184 | 106 | | /// - Direct accesses to the two neighborhoods of interest are constant time operations, since it is about |
| 184 | 107 | | /// retrieving a row and a column given their index, respectively. |
| 184 | 108 | | /// <br/> |
| 184 | 109 | | /// - The matrix is a square matrix of v rows and columns, so each of the neighborhoods to check has v |
| 184 | 110 | | /// elements. |
| 184 | 111 | | /// <br/> |
| 184 | 112 | | /// - Each neighborhood has to be linearly scanned, looking for <see langword="true"/> values. |
| 184 | 113 | | /// <br/> |
| 184 | 114 | | /// - Therefore, Time and Space Complexity (when enumerated) are O(v). |
| 184 | 115 | | /// </para> |
| 184 | 116 | | /// </remarks> |
| 184 | 117 | | public IEnumerable<IGraph.Adjacency> GetAdjacentVerticesAndEdges( |
| 184 | 118 | | int start, bool takeIntoAccountEdgeDirection) |
| 1076 | 119 | | { |
| 11490 | 120 | | for (var j = 0; j < AdjacencyMatrix.GetLength(1); j++) |
| 4669 | 121 | | { |
| 4669 | 122 | | if (AdjacencyMatrix[start, j]) |
| 1050 | 123 | | yield return new(j, start, j); |
| 4669 | 124 | | } |
| 184 | 125 | |
|
| 1076 | 126 | | if (takeIntoAccountEdgeDirection) |
| 680 | 127 | | yield break; |
| 184 | 128 | |
|
| 4192 | 129 | | for (var i = 0; i < AdjacencyMatrix.GetLength(0); i++) |
| 1700 | 130 | | { |
| 1700 | 131 | | if (AdjacencyMatrix[i, start]) |
| 404 | 132 | | yield return new(i, i, start); |
| 1700 | 133 | | } |
| 396 | 134 | | } |
| 184 | 135 | |
|
| 184 | 136 | | /// <inheritdoc path="//*[not(self::remarks)]" /> |
| 184 | 137 | | /// <remarks> |
| 184 | 138 | | /// <para id="algorithm"> |
| 184 | 139 | | /// ALGORITHM |
| 184 | 140 | | /// <br/> |
| 184 | 141 | | /// - An <see cref="IGraph"/> proxy is created, wrapping this instance of <see cref="IGraph"/>. |
| 184 | 142 | | /// <br/> |
| 184 | 143 | | /// - <see cref="IGraph.GetNumberOfVertices"/> is dispatched to the proxied graph. |
| 184 | 144 | | /// <br/> |
| 184 | 145 | | /// - <see cref="IGraph.GetAdjacentVerticesAndEdges(int, bool)"/> is directly implemented, accessing |
| 184 | 146 | | /// <see cref="AdjacencyMatrix"/> directly. |
| 184 | 147 | | /// <br/> |
| 184 | 148 | | /// - The implementation is very similar to the one of <see cref="AdjacencyMatrixGraph"/>: the only difference |
| 184 | 149 | | /// is that columns and rows are inverted. |
| 184 | 150 | | /// </para> |
| 184 | 151 | | /// <para id="complexity"> |
| 184 | 152 | | /// COMPLEXITY |
| 184 | 153 | | /// <br/> |
| 184 | 154 | | /// - Since this method just creates a proxy, Time and Space Complexity are O(1). |
| 184 | 155 | | /// <br/> |
| 184 | 156 | | /// - All operations on the proxy have the same Time and Space Complexity as the corresponding methods in |
| 184 | 157 | | /// <see cref="AdjacencyMatrixGraph"/>. |
| 184 | 158 | | /// </para> |
| 184 | 159 | | /// </remarks> |
| 24 | 160 | | public IGraph Reverse() => new ReverseGraph(this); |
| 184 | 161 | |
|
| 184 | 162 | | sealed private class ReverseGraph : ReverseProxyGraph<AdjacencyMatrixGraph> |
| 184 | 163 | | { |
| 24 | 164 | | public ReverseGraph(AdjacencyMatrixGraph graph) : base(graph) |
| 24 | 165 | | { |
| 24 | 166 | | } |
| 184 | 167 | |
|
| 184 | 168 | | public override IEnumerable<IGraph.Adjacency> GetAdjacentVerticesAndEdges( |
| 184 | 169 | | int start, bool takeIntoAccountEdgeDirection) |
| 52 | 170 | | { |
| 52 | 171 | | var adjacencyMatrix = Proxied.AdjacencyMatrix; |
| 184 | 172 | |
|
| 632 | 173 | | for (var i = 0; i < adjacencyMatrix.GetLength(0); i++) |
| 264 | 174 | | { |
| 264 | 175 | | if (adjacencyMatrix[i, start]) |
| 46 | 176 | | yield return new(i, start, i); |
| 264 | 177 | | } |
| 184 | 178 | |
|
| 52 | 179 | | if (takeIntoAccountEdgeDirection) |
| 26 | 180 | | yield break; |
| 184 | 181 | |
|
| 316 | 182 | | for (var j = 0; j < adjacencyMatrix.GetLength(1); j++) |
| 132 | 183 | | { |
| 132 | 184 | | if (adjacencyMatrix[start, j]) |
| 23 | 185 | | yield return new(j, j, start); |
| 132 | 186 | | } |
| 26 | 187 | | } |
| 184 | 188 | | } |
| 184 | 189 | | } |