| | 1 | | namespace MoreStructures.Graphs; |
| | 2 | |
|
| | 3 | | /// <summary> |
| | 4 | | /// A graph data structure, represented as an ordered list of neighborhoods: the i-th item of the list is the set of |
| | 5 | | /// ids of the vertices which are neighbors of the vertex with id i. |
| | 6 | | /// </summary> |
| | 7 | | /// <param name="Neighborhoods"> |
| | 8 | | /// A list of sets of integers, each set representing the neighborhood of the corresponding vertex. |
| | 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. when the |
| | 15 | | /// neighborhoods list L is such that <c>if v2 belongs to L[v1], then v1 belongs to L[v2]</c>. |
| | 16 | | /// <br/> |
| | 17 | | /// - The size of this data structure is proportional to the number of edges of the graph, since a vertex has as many |
| | 18 | | /// neighbors as edges connecting to other vertices (possibly including itself). |
| | 19 | | /// <br/> |
| | 20 | | /// - So, this graph representation is particularly useful when the number is edges is smaller or proportional to the |
| | 21 | | /// number of vertices in the graph, i.e. when the graph is <b>sparse</b> (i.e. when e is O(v)). |
| | 22 | | /// <br/> |
| | 23 | | /// - It becomes an expensive representation when the graph is <b>dense</b> (i.e. when e is O(v^2)). |
| | 24 | | /// <br/> |
| | 25 | | /// - While having size proportional to the number of edges, <see cref="AdjacencyListGraph"/> is more convenient than |
| | 26 | | /// <see cref="EdgeListGraph"/> to run neighborhood-based algorithms, such as discovery, because it makes easier |
| | 27 | | /// and faster to get neighbors of a vertex. |
| | 28 | | /// </remarks> |
| | 29 | | /// <example> |
| | 30 | | /// The followin graph: |
| | 31 | | /// <code> |
| | 32 | | /// 0 --> 1 <==> 3 |
| | 33 | | /// | ^ ^ / |
| | 34 | | /// | | / / |
| | 35 | | /// | | / / |
| | 36 | | /// v |/ / |
| | 37 | | /// 2 <----- |
| | 38 | | /// </code> |
| | 39 | | /// is represented as <c>AdjacencyListGraph(new { new { 1, 2 }, new { 3 }, new { 0, 1 }, new { 1, 2 } })</c>. |
| | 40 | | /// </example> |
| 5526 | 41 | | public record AdjacencyListGraph(IList<ISet<int>> Neighborhoods) : IGraph |
| 216 | 42 | | { |
| 216 | 43 | | /// <inheritdoc path="//*[not(self::remarks)]" /> |
| 216 | 44 | | /// <remarks> |
| 216 | 45 | | /// In the <see cref="AdjacencyListGraph"/> representation, corresponds to number of neighborhoods. |
| 216 | 46 | | /// <br/> |
| 216 | 47 | | /// Time and Space Complexity are O(1). |
| 216 | 48 | | /// </remarks> |
| 126 | 49 | | public int GetNumberOfVertices() => Neighborhoods.Count; |
| 216 | 50 | |
|
| 216 | 51 | | /// <inheritdoc path="//*[not(self::remarks)]" /> |
| 216 | 52 | | /// <remarks> |
| 216 | 53 | | /// <para id="algorithm"> |
| 216 | 54 | | /// ALGORITHM |
| 216 | 55 | | /// <br/> |
| 216 | 56 | | /// - Iterates over all the neighborhoods. |
| 216 | 57 | | /// <br/> |
| 216 | 58 | | /// - For each neighbor v of the vertex u, returns the edge (u, v). |
| 216 | 59 | | /// </para> |
| 216 | 60 | | /// <para id="complexity"> |
| 216 | 61 | | /// COMPLEXITY |
| 216 | 62 | | /// <br/> |
| 216 | 63 | | /// - There are v neighbors in total (as many as the number of vertices in the graph). |
| 216 | 64 | | /// <br/> |
| 216 | 65 | | /// - The total number of neighbors across all neighborhoods is e (as many as the number of edges in the |
| 216 | 66 | | /// graph). |
| 216 | 67 | | /// <br/> |
| 216 | 68 | | /// - Therefore Time Complexity is O(v + e). Space Complexity is O(1), since the iteration uses a constant |
| 216 | 69 | | /// amount of space. |
| 216 | 70 | | /// </para> |
| 216 | 71 | | /// </remarks> |
| 216 | 72 | | public IEnumerable<(int edgeStart, int edgeEnd)> GetAllEdges() => |
| 285 | 73 | | Neighborhoods.SelectMany((v, i) => Enumerable.Repeat(i, v.Count).Zip(v).Select(c => c)); |
| 216 | 74 | |
|
| 216 | 75 | | /// <inheritdoc path="//*[not(self::remarks)]" /> |
| 216 | 76 | | /// <remarks> |
| 216 | 77 | | /// <para id="algorithm"> |
| 216 | 78 | | /// ALGORITHM |
| 216 | 79 | | /// <br/> |
| 216 | 80 | | /// - Because the neighborhoods list is indexed by the vertex id, the algorithm has just to perform a direct |
| 216 | 81 | | /// access to the <paramref name="start"/>-th item: that is the set of neighbors of the vertex with id |
| 216 | 82 | | /// <paramref name="start"/>. |
| 216 | 83 | | /// <br/> |
| 216 | 84 | | /// - When the value of <paramref name="takeIntoAccountEdgeDirection"/> is <see langword="false"/>, a lookup of |
| 216 | 85 | | /// all neighborhoods is required. |
| 216 | 86 | | /// <br/> |
| 216 | 87 | | /// - In such case it would be better to have neighborhoods list already including bi-directional edges, and |
| 216 | 88 | | /// using <paramref name="takeIntoAccountEdgeDirection"/> = <see langword="true"/>. If not, the advantages of |
| 216 | 89 | | /// having O(1) neighborhood lookup would be lost. |
| 216 | 90 | | /// <br/> |
| 216 | 91 | | /// - If bi-directional edges are not included in the graph, and many calls to this method need to be called |
| 216 | 92 | | /// with <paramref name="takeIntoAccountEdgeDirection"/> = <see langword="true"/>, consider reversing the |
| 216 | 93 | | /// graph using <see cref="Reverse"/>, and keep one structure for direct lookup and the other for reversed |
| 216 | 94 | | /// lookup. |
| 216 | 95 | | /// </para> |
| 216 | 96 | | /// <para id="complexity"> |
| 216 | 97 | | /// COMPLEXITY |
| 216 | 98 | | /// <br/> |
| 216 | 99 | | /// - Direct access to the neighborhoods list is a constant time operation. |
| 216 | 100 | | /// <br/> |
| 216 | 101 | | /// - Therefore, when <paramref name="takeIntoAccountEdgeDirection"/> = <see langword="true"/>, Time and Space |
| 216 | 102 | | /// Complexity (when enumerated) are O(avg_e), where avg_e is the average number of edges coming out of the |
| 216 | 103 | | /// <paramref name="start"/> vertex. |
| 216 | 104 | | /// <br/> |
| 216 | 105 | | /// - However, when <paramref name="takeIntoAccountEdgeDirection"/> = <see langword="false"/>, Time Complexity |
| 216 | 106 | | /// becomes O(avg_e + v), where v is the number of vertices of the graph (i.e. the number of total |
| 216 | 107 | | /// neighborhoods defined). |
| 216 | 108 | | /// </para> |
| 216 | 109 | | /// </remarks> |
| 216 | 110 | | public IEnumerable<IGraph.Adjacency> GetAdjacentVerticesAndEdges( |
| 216 | 111 | | int start, bool takeIntoAccountEdgeDirection) |
| 1128 | 112 | | { |
| 5576 | 113 | | foreach (var end in Neighborhoods[start]) |
| 1096 | 114 | | yield return new(end, start, end); |
| 216 | 115 | |
|
| 1128 | 116 | | if (takeIntoAccountEdgeDirection) |
| 706 | 117 | | yield break; |
| 216 | 118 | |
|
| 4508 | 119 | | for (var i = 0; i < Neighborhoods.Count; i++) |
| 1832 | 120 | | { |
| 1832 | 121 | | if (i == start) |
| 422 | 122 | | continue; |
| 216 | 123 | |
|
| 1410 | 124 | | if (Neighborhoods[i].Contains(start)) |
| 378 | 125 | | yield return new(i, i, start); |
| 1410 | 126 | | } |
| 422 | 127 | | } |
| 216 | 128 | |
|
| 216 | 129 | | /// <inheritdoc path="//*[not(self::remarks)]" /> |
| 216 | 130 | | /// <remarks> |
| 216 | 131 | | /// <para id="algorithm"> |
| 216 | 132 | | /// ALGORITHM |
| 216 | 133 | | /// <br/> |
| 216 | 134 | | /// - Because <see cref="AdjacencyListGraph"/> has O(avg_e + v) Time Complexity when edges have to be traversed |
| 216 | 135 | | /// in reverse, rather than O(avg_e), which is much smaller on large graphs, a proxy to the original data |
| 216 | 136 | | /// structure is not used. |
| 216 | 137 | | /// <br/> |
| 216 | 138 | | /// - Instead, reversed neighborhoods RV are calculated, by iterating over all neighbors u of the neighborhood |
| 216 | 139 | | /// N[v] of each vertex v of this graph: if u belongs to N[v] then v is added to RV[u], initially set to an |
| 216 | 140 | | /// empty <see cref="HashSet{T}"/>. |
| 216 | 141 | | /// <br/> |
| 216 | 142 | | /// - Finally a new <see cref="AdjacencyListGraph"/> is built out of RV and returned as result. |
| 216 | 143 | | /// </para> |
| 216 | 144 | | /// <para id="complexity"> |
| 216 | 145 | | /// COMPLEXITY |
| 216 | 146 | | /// <br/> |
| 216 | 147 | | /// - Unlike in <see cref="AdjacencyMatrixGraph.Reverse"/>, a brand new structure is built, and proxies are not |
| 216 | 148 | | /// used at all. |
| 216 | 149 | | /// <br/> |
| 216 | 150 | | /// - There are as many neighborhoods and reversed neighbors as vertices in the graph. |
| 216 | 151 | | /// <br/> |
| 216 | 152 | | /// - The cost of going through all the neighbors in all the neighborhoods is proportional to the number of |
| 216 | 153 | | /// edges in the graph. |
| 216 | 154 | | /// <br/> |
| 216 | 155 | | /// - Therefore Time and Space Complexity are O(v + e), where v is the number of vertices and e the number of |
| 216 | 156 | | /// edges. |
| 216 | 157 | | /// </para> |
| 216 | 158 | | /// </remarks> |
| 216 | 159 | | public IGraph Reverse() |
| 32 | 160 | | { |
| 32 | 161 | | var numberOfVertices = Neighborhoods.Count; |
| 32 | 162 | | var reversedNeighborhoods = new ISet<int>[numberOfVertices]; |
| 216 | 163 | |
|
| 272 | 164 | | for (var vertex = 0; vertex < numberOfVertices; vertex++) |
| 104 | 165 | | reversedNeighborhoods[vertex] = new HashSet<int>(); |
| 216 | 166 | |
|
| 272 | 167 | | for (var vertex = 0; vertex < numberOfVertices; vertex++) |
| 496 | 168 | | foreach (var neighbor in Neighborhoods[vertex]) |
| 92 | 169 | | reversedNeighborhoods[neighbor].Add(vertex); |
| 216 | 170 | |
|
| 32 | 171 | | return new AdjacencyListGraph(reversedNeighborhoods); |
| 32 | 172 | | } |
| 216 | 173 | | } |