| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using System.Threading; |
| | | 3 | | using Itinero.Network.Enumerators.Edges; |
| | | 4 | | |
| | | 5 | | namespace Itinero.Network.Search.Islands; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// A directed meta-graph for island detection. |
| | | 9 | | /// Each vertex represents one or more routing edges (merged via union-find). |
| | | 10 | | /// Directed links represent travel direction between edge groups. |
| | | 11 | | /// </summary> |
| | | 12 | | internal class IslandDirectedGraph |
| | | 13 | | { |
| | 1 | 14 | | internal static readonly EdgeId MainNetworkSentinel = new(uint.MaxValue - 1, uint.MaxValue - 1); |
| | | 15 | | |
| | 83 | 16 | | private readonly Dictionary<EdgeId, EdgeId> _parent = new(); |
| | 83 | 17 | | private readonly Dictionary<EdgeId, int> _rank = new(); |
| | 83 | 18 | | private readonly Dictionary<EdgeId, int> _size = new(); |
| | 83 | 19 | | private readonly Dictionary<EdgeId, HashSet<EdgeId>> _outgoing = new(); |
| | 83 | 20 | | private readonly Dictionary<EdgeId, HashSet<EdgeId>> _incoming = new(); |
| | 83 | 21 | | private readonly Dictionary<EdgeId, List<EdgeId>> _members = new(); |
| | 83 | 22 | | private readonly HashSet<EdgeId> _processed = new(); |
| | | 23 | | |
| | | 24 | | // The graph is built incrementally (mutations) and queried concurrently from |
| | | 25 | | // many snap operations. The underlying Dictionaries / HashSets are not safe |
| | | 26 | | // for read-during-write — concurrent IsNotIsland calls during an in-flight |
| | | 27 | | // ProcessEdge corrupt the dict and throw "concurrent update". A reader-writer |
| | | 28 | | // lock keeps reads concurrent against each other (cheap on the snap path) |
| | | 29 | | // and exclusive against any mutation. |
| | 83 | 30 | | private readonly ReaderWriterLockSlim _lock = new(LockRecursionPolicy.SupportsRecursion); |
| | | 31 | | |
| | 83 | 32 | | public IslandDirectedGraph() |
| | 83 | 33 | | { |
| | 83 | 34 | | _parent[MainNetworkSentinel] = MainNetworkSentinel; |
| | 83 | 35 | | _rank[MainNetworkSentinel] = int.MaxValue; |
| | 83 | 36 | | _size[MainNetworkSentinel] = int.MaxValue; |
| | 83 | 37 | | } |
| | | 38 | | |
| | | 39 | | public void AddVertex(EdgeId edgeId) |
| | 6996 | 40 | | { |
| | 6996 | 41 | | _lock.EnterWriteLock(); |
| | | 42 | | try |
| | 6996 | 43 | | { |
| | 11007 | 44 | | if (_parent.ContainsKey(edgeId)) return; |
| | 2985 | 45 | | _parent[edgeId] = edgeId; |
| | 2985 | 46 | | _rank[edgeId] = 0; |
| | 2985 | 47 | | _size[edgeId] = 1; |
| | 2985 | 48 | | _members[edgeId] = new List<EdgeId> { edgeId }; |
| | 2985 | 49 | | } |
| | 20988 | 50 | | finally { _lock.ExitWriteLock(); } |
| | 6996 | 51 | | } |
| | | 52 | | |
| | | 53 | | public bool IsInGraph(EdgeId edgeId) |
| | 1 | 54 | | { |
| | 1 | 55 | | _lock.EnterReadLock(); |
| | 2 | 56 | | try { return _parent.ContainsKey(edgeId); } |
| | 3 | 57 | | finally { _lock.ExitReadLock(); } |
| | 1 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Returns a snapshot of every edge currently in the graph, excluding the |
| | | 62 | | /// <see cref="MainNetworkSentinel"/>. Intended for callers that need to |
| | | 63 | | /// feed every known edge into a global resolution pass (e.g. Tarjan SCC |
| | | 64 | | /// over the full set of one-way singletons forming a cycle). |
| | | 65 | | /// </summary> |
| | | 66 | | public List<EdgeId> GetAllEdges() |
| | 0 | 67 | | { |
| | 0 | 68 | | _lock.EnterReadLock(); |
| | | 69 | | try |
| | 0 | 70 | | { |
| | 0 | 71 | | var result = new List<EdgeId>(_parent.Count); |
| | 0 | 72 | | foreach (var k in _parent.Keys) |
| | 0 | 73 | | { |
| | 0 | 74 | | if (k == MainNetworkSentinel) continue; |
| | 0 | 75 | | result.Add(k); |
| | 0 | 76 | | } |
| | 0 | 77 | | return result; |
| | | 78 | | } |
| | 0 | 79 | | finally { _lock.ExitReadLock(); } |
| | 0 | 80 | | } |
| | | 81 | | |
| | | 82 | | public bool IsProcessed(EdgeId edgeId) |
| | 266817 | 83 | | { |
| | 266817 | 84 | | _lock.EnterReadLock(); |
| | 533634 | 85 | | try { return _processed.Contains(edgeId); } |
| | 800451 | 86 | | finally { _lock.ExitReadLock(); } |
| | 266817 | 87 | | } |
| | | 88 | | |
| | | 89 | | public void SetProcessed(EdgeId edgeId) |
| | 1095 | 90 | | { |
| | 1095 | 91 | | _lock.EnterWriteLock(); |
| | 3285 | 92 | | try { _processed.Add(edgeId); } |
| | 3285 | 93 | | finally { _lock.ExitWriteLock(); } |
| | 1095 | 94 | | } |
| | | 95 | | |
| | | 96 | | public bool IsNotIsland(EdgeId edgeId) |
| | 10236 | 97 | | { |
| | 10236 | 98 | | _lock.EnterReadLock(); |
| | | 99 | | try |
| | 10236 | 100 | | { |
| | 13048 | 101 | | if (!_parent.ContainsKey(edgeId)) return false; |
| | 7424 | 102 | | return this.FindNoLock(edgeId) == this.FindNoLock(MainNetworkSentinel); |
| | | 103 | | } |
| | 30708 | 104 | | finally { _lock.ExitReadLock(); } |
| | 10236 | 105 | | } |
| | | 106 | | |
| | | 107 | | public int GetSize(EdgeId edgeId) |
| | 1571 | 108 | | { |
| | 1571 | 109 | | _lock.EnterReadLock(); |
| | 3142 | 110 | | try { return _size[this.FindNoLock(edgeId)]; } |
| | 4713 | 111 | | finally { _lock.ExitReadLock(); } |
| | 1571 | 112 | | } |
| | | 113 | | |
| | | 114 | | public List<EdgeId>? GetMembers(EdgeId edgeId) |
| | 4604 | 115 | | { |
| | 4604 | 116 | | _lock.EnterReadLock(); |
| | | 117 | | try |
| | 4604 | 118 | | { |
| | 4604 | 119 | | var root = this.FindNoLock(edgeId); |
| | | 120 | | // Snapshot — callers iterate outside the lock and concurrent |
| | | 121 | | // merges / RemoveEdge would otherwise mutate the list out from |
| | | 122 | | // under them. |
| | 4604 | 123 | | return _members.TryGetValue(root, out var m) ? new List<EdgeId>(m) : null; |
| | | 124 | | } |
| | 13812 | 125 | | finally { _lock.ExitReadLock(); } |
| | 4604 | 126 | | } |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Adds a directed link <paramref name="from"/> → <paramref name="to"/> to the |
| | | 130 | | /// graph, with **eager cycle detection**: if a path already exists in the dg |
| | | 131 | | /// from <paramref name="to"/>'s component back to <paramref name="from"/>'s |
| | | 132 | | /// component, the new link closes a strongly-connected component. All |
| | | 133 | | /// components on every path back are merged into one node (F ∩ R: forward |
| | | 134 | | /// reachable from <paramref name="to"/> ∩ backward reachable from |
| | | 135 | | /// <paramref name="from"/>). Returns <c>true</c> when this happens, so the |
| | | 136 | | /// caller can size-check the merged component for MainNet graduation. |
| | | 137 | | /// Returns <c>false</c> when the link was a regular edge (no cycle closed). |
| | | 138 | | /// </summary> |
| | | 139 | | public bool AddDirectedLink(EdgeId from, EdgeId to) |
| | 3535 | 140 | | { |
| | 3535 | 141 | | _lock.EnterWriteLock(); |
| | | 142 | | try |
| | 3535 | 143 | | { |
| | 3535 | 144 | | var fromRoot = this.FindNoLock(from); |
| | 3535 | 145 | | var toRoot = this.FindNoLock(to); |
| | 3535 | 146 | | if (fromRoot == toRoot) return false; |
| | | 147 | | |
| | | 148 | | // Eager cycle-merge: if there's already a path toRoot ↝ fromRoot |
| | | 149 | | // in the existing graph, adding from→to closes an SCC. Collapse |
| | | 150 | | // every component on a closing path into one node. |
| | 3535 | 151 | | if (this.PathExistsNoLock(toRoot, fromRoot)) |
| | 1572 | 152 | | { |
| | 1572 | 153 | | var sccRoots = this.IntersectReachableNoLock(toRoot, fromRoot); |
| | 1572 | 154 | | sccRoots.Add(fromRoot); |
| | 1572 | 155 | | sccRoots.Add(toRoot); |
| | 1572 | 156 | | EdgeId target = fromRoot; |
| | 11634 | 157 | | foreach (var r in sccRoots) |
| | 3459 | 158 | | { |
| | 3459 | 159 | | if (this.FindNoLock(r) != this.FindNoLock(target)) |
| | 1887 | 160 | | this.MergeNoLock(target, r); |
| | 3459 | 161 | | } |
| | 1572 | 162 | | return true; |
| | | 163 | | } |
| | | 164 | | |
| | 1963 | 165 | | if (!_outgoing.TryGetValue(fromRoot, out var targets)) |
| | 494 | 166 | | { |
| | 494 | 167 | | targets = new HashSet<EdgeId>(); |
| | 494 | 168 | | _outgoing[fromRoot] = targets; |
| | 494 | 169 | | } |
| | | 170 | | |
| | 1963 | 171 | | if (targets.Add(toRoot)) |
| | 1912 | 172 | | { |
| | | 173 | | // also update incoming index |
| | 1912 | 174 | | if (!_incoming.TryGetValue(toRoot, out var sources)) |
| | 1780 | 175 | | { |
| | 1780 | 176 | | sources = new HashSet<EdgeId>(); |
| | 1780 | 177 | | _incoming[toRoot] = sources; |
| | 1780 | 178 | | } |
| | 1912 | 179 | | sources.Add(fromRoot); |
| | 1912 | 180 | | } |
| | | 181 | | |
| | 1963 | 182 | | return false; |
| | | 183 | | } |
| | 10605 | 184 | | finally { _lock.ExitWriteLock(); } |
| | 3535 | 185 | | } |
| | | 186 | | |
| | | 187 | | private bool PathExistsNoLock(EdgeId from, EdgeId to) |
| | 3535 | 188 | | { |
| | 3535 | 189 | | if (this.FindNoLock(from) == this.FindNoLock(to)) return true; |
| | 3535 | 190 | | var visited = new HashSet<EdgeId>(); |
| | 3535 | 191 | | var stack = new Stack<EdgeId>(); |
| | 3535 | 192 | | stack.Push(this.FindNoLock(from)); |
| | 7551 | 193 | | while (stack.Count > 0) |
| | 5588 | 194 | | { |
| | 5588 | 195 | | var current = this.FindNoLock(stack.Pop()); |
| | 7160 | 196 | | if (current == this.FindNoLock(to)) return true; |
| | 4025 | 197 | | if (!visited.Add(current)) continue; |
| | 4007 | 198 | | if (_outgoing.TryGetValue(current, out var outs)) |
| | 2131 | 199 | | { |
| | 272593 | 200 | | foreach (var o in outs) |
| | 133100 | 201 | | { |
| | 133100 | 202 | | var r = this.FindNoLock(o); |
| | 135328 | 203 | | if (!visited.Contains(r)) stack.Push(r); |
| | 133100 | 204 | | } |
| | 2131 | 205 | | } |
| | 4007 | 206 | | } |
| | 1963 | 207 | | return false; |
| | 3535 | 208 | | } |
| | | 209 | | |
| | | 210 | | /// <summary> |
| | | 211 | | /// Returns components that are both forward-reachable from <paramref name="forwardStart"/> |
| | | 212 | | /// AND backward-reachable from <paramref name="backwardStart"/>. Used to find the |
| | | 213 | | /// SCC that closes when a new link <c>backwardStart → forwardStart</c> is about |
| | | 214 | | /// to be added. |
| | | 215 | | /// </summary> |
| | | 216 | | private HashSet<EdgeId> IntersectReachableNoLock(EdgeId forwardStart, EdgeId backwardStart) |
| | 1572 | 217 | | { |
| | 1572 | 218 | | var forward = new HashSet<EdgeId>(); |
| | 1572 | 219 | | { |
| | 1572 | 220 | | var stack = new Stack<EdgeId>(); |
| | 1572 | 221 | | stack.Push(this.FindNoLock(forwardStart)); |
| | 5265 | 222 | | while (stack.Count > 0) |
| | 3693 | 223 | | { |
| | 3693 | 224 | | var current = this.FindNoLock(stack.Pop()); |
| | 3703 | 225 | | if (!forward.Add(current)) continue; |
| | 3683 | 226 | | if (_outgoing.TryGetValue(current, out var outs)) |
| | 2052 | 227 | | { |
| | 270320 | 228 | | foreach (var o in outs) |
| | 132082 | 229 | | { |
| | 132082 | 230 | | var r = this.FindNoLock(o); |
| | 134203 | 231 | | if (!forward.Contains(r)) stack.Push(r); |
| | 132082 | 232 | | } |
| | 2052 | 233 | | } |
| | 3683 | 234 | | } |
| | 1572 | 235 | | } |
| | 1572 | 236 | | var result = new HashSet<EdgeId>(); |
| | 1572 | 237 | | var bStack = new Stack<EdgeId>(); |
| | 1572 | 238 | | var bVisited = new HashSet<EdgeId>(); |
| | 1572 | 239 | | bStack.Push(this.FindNoLock(backwardStart)); |
| | 5234 | 240 | | while (bStack.Count > 0) |
| | 3662 | 241 | | { |
| | 3662 | 242 | | var current = this.FindNoLock(bStack.Pop()); |
| | 3662 | 243 | | if (!bVisited.Add(current)) continue; |
| | 7121 | 244 | | if (forward.Contains(current)) result.Add(current); |
| | 3662 | 245 | | if (_incoming.TryGetValue(current, out var ins)) |
| | 3320 | 246 | | { |
| | 27042 | 247 | | foreach (var i in ins) |
| | 8541 | 248 | | { |
| | 8541 | 249 | | var r = this.FindNoLock(i); |
| | 10631 | 250 | | if (!bVisited.Contains(r)) bStack.Push(r); |
| | 8541 | 251 | | } |
| | 3320 | 252 | | } |
| | 3662 | 253 | | } |
| | 1572 | 254 | | return result; |
| | 1572 | 255 | | } |
| | | 256 | | |
| | | 257 | | public bool HasDirectedLink(EdgeId from, EdgeId to) |
| | 0 | 258 | | { |
| | 0 | 259 | | _lock.EnterReadLock(); |
| | | 260 | | try |
| | 0 | 261 | | { |
| | 0 | 262 | | var fromRoot = this.FindNoLock(from); |
| | 0 | 263 | | var toRoot = this.FindNoLock(to); |
| | 0 | 264 | | return _outgoing.TryGetValue(fromRoot, out var targets) && targets.Contains(toRoot); |
| | | 265 | | } |
| | 0 | 266 | | finally { _lock.ExitReadLock(); } |
| | 0 | 267 | | } |
| | | 268 | | |
| | | 269 | | public void Merge(EdgeId a, EdgeId b) |
| | 0 | 270 | | { |
| | 0 | 271 | | _lock.EnterWriteLock(); |
| | 0 | 272 | | try { this.MergeNoLock(a, b); } |
| | 0 | 273 | | finally { _lock.ExitWriteLock(); } |
| | 0 | 274 | | } |
| | | 275 | | |
| | | 276 | | private void MergeNoLock(EdgeId a, EdgeId b) |
| | 2945 | 277 | | { |
| | 2945 | 278 | | var rootA = this.FindNoLock(a); |
| | 2945 | 279 | | var rootB = this.FindNoLock(b); |
| | 2945 | 280 | | if (rootA == rootB) return; |
| | | 281 | | |
| | | 282 | | // sentinel always wins |
| | 2945 | 283 | | var sentinelRoot = this.FindNoLock(MainNetworkSentinel); |
| | 2945 | 284 | | if (rootB == sentinelRoot) |
| | 42 | 285 | | (rootA, rootB) = (rootB, rootA); |
| | 2903 | 286 | | else if (rootA != sentinelRoot && _rank[rootA] < _rank[rootB]) |
| | 1248 | 287 | | (rootA, rootB) = (rootB, rootA); |
| | | 288 | | |
| | 2945 | 289 | | _parent[rootB] = rootA; |
| | 2945 | 290 | | if (rootA != sentinelRoot) |
| | 1720 | 291 | | { |
| | 1720 | 292 | | _size[rootA] += _size[rootB]; |
| | 1877 | 293 | | if (_rank[rootA] == _rank[rootB]) _rank[rootA]++; |
| | 1720 | 294 | | } |
| | | 295 | | |
| | | 296 | | // merge outgoing |
| | 2945 | 297 | | if (_outgoing.TryGetValue(rootB, out var bOut)) |
| | 628 | 298 | | { |
| | 628 | 299 | | if (!_outgoing.TryGetValue(rootA, out var aOut)) |
| | 178 | 300 | | { |
| | 178 | 301 | | aOut = new HashSet<EdgeId>(); |
| | 178 | 302 | | _outgoing[rootA] = aOut; |
| | 178 | 303 | | } |
| | | 304 | | |
| | 5616 | 305 | | foreach (var t in bOut) |
| | 1866 | 306 | | { |
| | 1866 | 307 | | var tRoot = this.FindNoLock(t); |
| | 1866 | 308 | | if (tRoot != rootA) |
| | 21 | 309 | | { |
| | 21 | 310 | | aOut.Add(tRoot); |
| | | 311 | | // update incoming: t's incoming should point to rootA not rootB |
| | 21 | 312 | | if (_incoming.TryGetValue(tRoot, out var tInc)) |
| | 21 | 313 | | { |
| | 21 | 314 | | tInc.Remove(rootB); |
| | 21 | 315 | | tInc.Add(rootA); |
| | 21 | 316 | | } |
| | 21 | 317 | | } |
| | 1866 | 318 | | } |
| | | 319 | | |
| | 628 | 320 | | _outgoing.Remove(rootB); |
| | 628 | 321 | | } |
| | | 322 | | |
| | | 323 | | // merge incoming |
| | 2945 | 324 | | if (_incoming.TryGetValue(rootB, out var bInc)) |
| | 1755 | 325 | | { |
| | 1755 | 326 | | if (!_incoming.TryGetValue(rootA, out var aInc)) |
| | 21 | 327 | | { |
| | 21 | 328 | | aInc = new HashSet<EdgeId>(); |
| | 21 | 329 | | _incoming[rootA] = aInc; |
| | 21 | 330 | | } |
| | | 331 | | |
| | 8831 | 332 | | foreach (var s in bInc) |
| | 1783 | 333 | | { |
| | 1783 | 334 | | var sRoot = this.FindNoLock(s); |
| | 1783 | 335 | | if (sRoot != rootA) |
| | 330 | 336 | | { |
| | 330 | 337 | | aInc.Add(sRoot); |
| | | 338 | | // update outgoing: s's outgoing should point to rootA not rootB |
| | 330 | 339 | | if (_outgoing.TryGetValue(sRoot, out var sOut)) |
| | 330 | 340 | | { |
| | 330 | 341 | | sOut.Remove(rootB); |
| | 330 | 342 | | sOut.Add(rootA); |
| | 330 | 343 | | } |
| | 330 | 344 | | } |
| | 1783 | 345 | | } |
| | | 346 | | |
| | 1755 | 347 | | _incoming.Remove(rootB); |
| | 1755 | 348 | | } |
| | | 349 | | |
| | | 350 | | // remove self-loops |
| | 2945 | 351 | | if (_outgoing.TryGetValue(rootA, out var aOutFinal)) |
| | 1912 | 352 | | aOutFinal.Remove(rootA); |
| | 2945 | 353 | | if (_incoming.TryGetValue(rootA, out var aIncFinal)) |
| | 1912 | 354 | | aIncFinal.Remove(rootA); |
| | | 355 | | |
| | | 356 | | // merge members |
| | 2945 | 357 | | if (_members.TryGetValue(rootB, out var bMembers)) |
| | 2945 | 358 | | { |
| | 2945 | 359 | | if (rootA == sentinelRoot) |
| | 1225 | 360 | | { |
| | 1225 | 361 | | _members.Remove(rootB); |
| | 1225 | 362 | | } |
| | | 363 | | else |
| | 1720 | 364 | | { |
| | 1720 | 365 | | if (!_members.TryGetValue(rootA, out var aMembers)) |
| | 0 | 366 | | { |
| | 0 | 367 | | aMembers = new List<EdgeId>(); |
| | 0 | 368 | | _members[rootA] = aMembers; |
| | 0 | 369 | | } |
| | 1720 | 370 | | aMembers.AddRange(bMembers); |
| | 1720 | 371 | | _members.Remove(rootB); |
| | 1720 | 372 | | } |
| | 2945 | 373 | | } |
| | 2945 | 374 | | } |
| | | 375 | | |
| | | 376 | | public void CollapseToMainNetwork(EdgeId root) |
| | 1222 | 377 | | { |
| | 1222 | 378 | | _lock.EnterWriteLock(); |
| | | 379 | | try |
| | 1222 | 380 | | { |
| | 1222 | 381 | | root = this.FindNoLock(root); |
| | 1386 | 382 | | if (root == this.FindNoLock(MainNetworkSentinel)) return; |
| | 1058 | 383 | | this.MergeNoLock(MainNetworkSentinel, root); |
| | 1058 | 384 | | } |
| | 3666 | 385 | | finally { _lock.ExitWriteLock(); } |
| | 1222 | 386 | | } |
| | | 387 | | |
| | | 388 | | public void RemoveEdge(EdgeId edgeId) |
| | 0 | 389 | | { |
| | 0 | 390 | | _lock.EnterWriteLock(); |
| | | 391 | | try |
| | 0 | 392 | | { |
| | 0 | 393 | | var root = this.FindNoLock(edgeId); |
| | | 394 | | |
| | 0 | 395 | | if (_members.TryGetValue(root, out var members)) |
| | 0 | 396 | | { |
| | 0 | 397 | | members.Remove(edgeId); |
| | 0 | 398 | | if (members.Count == 0) |
| | 0 | 399 | | { |
| | 0 | 400 | | _members.Remove(root); |
| | | 401 | | |
| | | 402 | | // clean up adjacency |
| | 0 | 403 | | if (_outgoing.TryGetValue(root, out var targets)) |
| | 0 | 404 | | { |
| | 0 | 405 | | foreach (var t in targets) |
| | 0 | 406 | | { |
| | 0 | 407 | | if (_incoming.TryGetValue(this.FindNoLock(t), out var tInc)) |
| | 0 | 408 | | tInc.Remove(root); |
| | 0 | 409 | | } |
| | 0 | 410 | | _outgoing.Remove(root); |
| | 0 | 411 | | } |
| | | 412 | | |
| | 0 | 413 | | if (_incoming.TryGetValue(root, out var sources)) |
| | 0 | 414 | | { |
| | 0 | 415 | | foreach (var s in sources) |
| | 0 | 416 | | { |
| | 0 | 417 | | if (_outgoing.TryGetValue(this.FindNoLock(s), out var sOut)) |
| | 0 | 418 | | sOut.Remove(root); |
| | 0 | 419 | | } |
| | 0 | 420 | | _incoming.Remove(root); |
| | 0 | 421 | | } |
| | 0 | 422 | | } |
| | 0 | 423 | | } |
| | | 424 | | |
| | 0 | 425 | | _parent.Remove(edgeId); |
| | 0 | 426 | | _processed.Remove(edgeId); |
| | 0 | 427 | | } |
| | 0 | 428 | | finally { _lock.ExitWriteLock(); } |
| | 0 | 429 | | } |
| | | 430 | | |
| | | 431 | | /// <summary> |
| | | 432 | | /// O(1) dead-end check using incoming index. |
| | | 433 | | /// </summary> |
| | | 434 | | public bool IsDeadEnd(EdgeId edgeId) |
| | 0 | 435 | | { |
| | 0 | 436 | | _lock.EnterReadLock(); |
| | | 437 | | try |
| | 0 | 438 | | { |
| | 0 | 439 | | var root = this.FindNoLock(edgeId); |
| | 0 | 440 | | if (root == this.FindNoLock(MainNetworkSentinel)) return false; |
| | | 441 | | |
| | 0 | 442 | | var hasOutgoing = _outgoing.TryGetValue(root, out var targets) && targets.Count > 0; |
| | 0 | 443 | | if (!hasOutgoing) return true; |
| | | 444 | | |
| | 0 | 445 | | var hasIncoming = _incoming.TryGetValue(root, out var sources) && sources.Count > 0; |
| | 0 | 446 | | return !hasIncoming; |
| | | 447 | | } |
| | 0 | 448 | | finally { _lock.ExitReadLock(); } |
| | 0 | 449 | | } |
| | | 450 | | |
| | | 451 | | /// <summary> |
| | | 452 | | /// Checks if this edge can reach the sentinel in BOTH directions. |
| | | 453 | | /// </summary> |
| | | 454 | | public bool CanReachMainNetwork(EdgeId edgeId) |
| | 0 | 455 | | { |
| | 0 | 456 | | _lock.EnterReadLock(); |
| | | 457 | | try |
| | 0 | 458 | | { |
| | 0 | 459 | | var root = this.FindNoLock(edgeId); |
| | 0 | 460 | | var sentinel = this.FindNoLock(MainNetworkSentinel); |
| | 0 | 461 | | if (root == sentinel) return true; |
| | | 462 | | |
| | 0 | 463 | | var visited = new HashSet<EdgeId>(); |
| | 0 | 464 | | var canForward = this.DfsCanReach(root, sentinel, visited, true); |
| | 0 | 465 | | if (!canForward) return false; |
| | | 466 | | |
| | 0 | 467 | | visited.Clear(); |
| | 0 | 468 | | return this.DfsCanReach(root, sentinel, visited, false); |
| | | 469 | | } |
| | 0 | 470 | | finally { _lock.ExitReadLock(); } |
| | 0 | 471 | | } |
| | | 472 | | |
| | | 473 | | /// <summary> |
| | | 474 | | /// Diagnostic: returns (canForward, canBackward) one-direction reachability |
| | | 475 | | /// to <see cref="MainNetworkSentinel"/>. <c>(true,true)</c> matches |
| | | 476 | | /// <see cref="CanReachMainNetwork"/>; the other combinations expose the |
| | | 477 | | /// asymmetric cases. |
| | | 478 | | /// </summary> |
| | | 479 | | public (bool canForward, bool canBackward) ReachMainNetworkDirections(EdgeId edgeId) |
| | 0 | 480 | | { |
| | 0 | 481 | | _lock.EnterReadLock(); |
| | | 482 | | try |
| | 0 | 483 | | { |
| | 0 | 484 | | var root = this.FindNoLock(edgeId); |
| | 0 | 485 | | var sentinel = this.FindNoLock(MainNetworkSentinel); |
| | 0 | 486 | | if (root == sentinel) return (true, true); |
| | | 487 | | |
| | 0 | 488 | | var visited = new HashSet<EdgeId>(); |
| | 0 | 489 | | var canForward = this.DfsCanReach(root, sentinel, visited, true); |
| | 0 | 490 | | visited.Clear(); |
| | 0 | 491 | | var canBackward = this.DfsCanReach(root, sentinel, visited, false); |
| | 0 | 492 | | return (canForward, canBackward); |
| | | 493 | | } |
| | 0 | 494 | | finally { _lock.ExitReadLock(); } |
| | 0 | 495 | | } |
| | | 496 | | |
| | | 497 | | /// <summary> |
| | | 498 | | /// Returns the union-find roots this edge's component has outgoing dg links |
| | | 499 | | /// to (after <see cref="Find"/> canonicalisation). Includes the sentinel if |
| | | 500 | | /// the component links to it. |
| | | 501 | | /// </summary> |
| | | 502 | | public List<EdgeId> GetOutgoingRoots(EdgeId edgeId) |
| | 3128 | 503 | | { |
| | 3128 | 504 | | var result = new List<EdgeId>(); |
| | 3128 | 505 | | _lock.EnterReadLock(); |
| | | 506 | | try |
| | 3128 | 507 | | { |
| | 3128 | 508 | | if (!_parent.ContainsKey(edgeId)) return result; |
| | 3128 | 509 | | var root = this.FindNoLock(edgeId); |
| | 3128 | 510 | | if (_outgoing.TryGetValue(root, out var outs)) |
| | 1673 | 511 | | { |
| | 1673 | 512 | | var seen = new HashSet<EdgeId>(); |
| | 267155 | 513 | | foreach (var o in outs) |
| | 131068 | 514 | | { |
| | 131068 | 515 | | var r = this.FindNoLock(o); |
| | 261959 | 516 | | if (r == root) continue; |
| | 354 | 517 | | if (seen.Add(r)) result.Add(r); |
| | 177 | 518 | | } |
| | 1673 | 519 | | } |
| | 3128 | 520 | | } |
| | 9384 | 521 | | finally { _lock.ExitReadLock(); } |
| | 3128 | 522 | | return result; |
| | 3128 | 523 | | } |
| | | 524 | | |
| | | 525 | | /// <summary> |
| | | 526 | | /// Returns the union-find roots this edge's component has incoming dg links |
| | | 527 | | /// from (after <see cref="Find"/> canonicalisation). Includes the sentinel |
| | | 528 | | /// if the sentinel links to the component. |
| | | 529 | | /// </summary> |
| | | 530 | | public List<EdgeId> GetIncomingRoots(EdgeId edgeId) |
| | 1602 | 531 | | { |
| | 1602 | 532 | | var result = new List<EdgeId>(); |
| | 1602 | 533 | | _lock.EnterReadLock(); |
| | | 534 | | try |
| | 1602 | 535 | | { |
| | 1602 | 536 | | if (!_parent.ContainsKey(edgeId)) return result; |
| | 1602 | 537 | | var root = this.FindNoLock(edgeId); |
| | 1602 | 538 | | if (_incoming.TryGetValue(root, out var ins)) |
| | 1571 | 539 | | { |
| | 1571 | 540 | | var seen = new HashSet<EdgeId>(); |
| | 18289 | 541 | | foreach (var i in ins) |
| | 6788 | 542 | | { |
| | 6788 | 543 | | var r = this.FindNoLock(i); |
| | 13385 | 544 | | if (r == root) continue; |
| | 382 | 545 | | if (seen.Add(r)) result.Add(r); |
| | 191 | 546 | | } |
| | 1571 | 547 | | } |
| | 1602 | 548 | | } |
| | 4806 | 549 | | finally { _lock.ExitReadLock(); } |
| | 1602 | 550 | | return result; |
| | 1602 | 551 | | } |
| | | 552 | | |
| | | 553 | | /// <summary> |
| | | 554 | | /// Returns the union-find roots that this edge's component is directionally |
| | | 555 | | /// linked to (outgoing ∪ incoming), excluding the sentinel. Used by the |
| | | 556 | | /// edge-frontier BFS to keep expanding through already-processed edges: |
| | | 557 | | /// the dg knows their links from prior calls' processing, so the BFS can |
| | | 558 | | /// continue without re-doing the merge work. |
| | | 559 | | /// </summary> |
| | | 560 | | public List<EdgeId> GetLinkedNeighbourRoots(EdgeId edgeId) |
| | 0 | 561 | | { |
| | 0 | 562 | | var result = new List<EdgeId>(); |
| | 0 | 563 | | _lock.EnterReadLock(); |
| | | 564 | | try |
| | 0 | 565 | | { |
| | 0 | 566 | | if (!_parent.ContainsKey(edgeId)) return result; |
| | 0 | 567 | | var root = this.FindNoLock(edgeId); |
| | 0 | 568 | | var sentinel = this.FindNoLock(MainNetworkSentinel); |
| | 0 | 569 | | if (_outgoing.TryGetValue(root, out var outs)) |
| | 0 | 570 | | { |
| | 0 | 571 | | foreach (var o in outs) |
| | 0 | 572 | | { |
| | 0 | 573 | | var r = this.FindNoLock(o); |
| | 0 | 574 | | if (r != sentinel) result.Add(r); |
| | 0 | 575 | | } |
| | 0 | 576 | | } |
| | 0 | 577 | | if (_incoming.TryGetValue(root, out var ins)) |
| | 0 | 578 | | { |
| | 0 | 579 | | foreach (var i in ins) |
| | 0 | 580 | | { |
| | 0 | 581 | | var r = this.FindNoLock(i); |
| | 0 | 582 | | if (r != sentinel && !result.Contains(r)) result.Add(r); |
| | 0 | 583 | | } |
| | 0 | 584 | | } |
| | 0 | 585 | | } |
| | 0 | 586 | | finally { _lock.ExitReadLock(); } |
| | 0 | 587 | | return result; |
| | 0 | 588 | | } |
| | | 589 | | |
| | | 590 | | private bool DfsCanReach(EdgeId current, EdgeId target, HashSet<EdgeId> visited, bool forward) |
| | 0 | 591 | | { |
| | 0 | 592 | | current = this.FindNoLock(current); |
| | 0 | 593 | | if (current == target) return true; |
| | 0 | 594 | | if (!visited.Add(current)) return false; |
| | | 595 | | |
| | 0 | 596 | | var adj = forward |
| | 0 | 597 | | ? (_outgoing.TryGetValue(current, out var o) ? o : null) |
| | 0 | 598 | | : (_incoming.TryGetValue(current, out var i) ? i : null); |
| | | 599 | | |
| | 0 | 600 | | if (adj == null) return false; |
| | | 601 | | |
| | 0 | 602 | | foreach (var next in adj) |
| | 0 | 603 | | { |
| | 0 | 604 | | if (this.DfsCanReach(this.FindNoLock(next), target, visited, forward)) return true; |
| | 0 | 605 | | } |
| | | 606 | | |
| | 0 | 607 | | return false; |
| | 0 | 608 | | } |
| | | 609 | | |
| | | 610 | | /// <summary> |
| | | 611 | | /// Detects cycles among the given candidate roots and merges them. |
| | | 612 | | /// Uses Tarjan's SCC algorithm on the subgraph of candidates. |
| | | 613 | | /// Returns true if any merges happened. |
| | | 614 | | /// </summary> |
| | | 615 | | public bool DetectAndMergeCycles(List<EdgeId> candidateRoots) |
| | 0 | 616 | | { |
| | 0 | 617 | | _lock.EnterWriteLock(); |
| | | 618 | | try |
| | 0 | 619 | | { |
| | | 620 | | // build the set of roots to consider |
| | 0 | 621 | | var rootSet = new HashSet<EdgeId>(); |
| | 0 | 622 | | foreach (var c in candidateRoots) |
| | 0 | 623 | | { |
| | 0 | 624 | | var r = this.FindNoLock(c); |
| | 0 | 625 | | if (r != this.FindNoLock(MainNetworkSentinel)) |
| | 0 | 626 | | rootSet.Add(r); |
| | 0 | 627 | | } |
| | | 628 | | |
| | 0 | 629 | | if (rootSet.Count < 2) return false; |
| | | 630 | | |
| | | 631 | | // Tarjan's SCC |
| | 0 | 632 | | var index = 0; |
| | 0 | 633 | | var stack = new Stack<EdgeId>(); |
| | 0 | 634 | | var onStack = new HashSet<EdgeId>(); |
| | 0 | 635 | | var indices = new Dictionary<EdgeId, int>(); |
| | 0 | 636 | | var lowLinks = new Dictionary<EdgeId, int>(); |
| | 0 | 637 | | var sccs = new List<List<EdgeId>>(); |
| | | 638 | | |
| | 0 | 639 | | foreach (var v in rootSet) |
| | 0 | 640 | | { |
| | 0 | 641 | | if (!indices.ContainsKey(v)) |
| | 0 | 642 | | this.Strongconnect(v, rootSet, ref index, stack, onStack, indices, lowLinks, sccs); |
| | 0 | 643 | | } |
| | | 644 | | |
| | | 645 | | // merge SCCs with more than one vertex |
| | 0 | 646 | | var merged = false; |
| | 0 | 647 | | foreach (var scc in sccs) |
| | 0 | 648 | | { |
| | 0 | 649 | | if (scc.Count < 2) continue; |
| | 0 | 650 | | for (var i = 1; i < scc.Count; i++) |
| | 0 | 651 | | { |
| | 0 | 652 | | this.MergeNoLock(scc[0], scc[i]); |
| | 0 | 653 | | } |
| | 0 | 654 | | merged = true; |
| | 0 | 655 | | } |
| | | 656 | | |
| | 0 | 657 | | return merged; |
| | | 658 | | } |
| | 0 | 659 | | finally { _lock.ExitWriteLock(); } |
| | 0 | 660 | | } |
| | | 661 | | |
| | | 662 | | private void Strongconnect(EdgeId v, HashSet<EdgeId> rootSet, |
| | | 663 | | ref int index, Stack<EdgeId> stack, HashSet<EdgeId> onStack, |
| | | 664 | | Dictionary<EdgeId, int> indices, Dictionary<EdgeId, int> lowLinks, |
| | | 665 | | List<List<EdgeId>> sccs) |
| | 0 | 666 | | { |
| | 0 | 667 | | indices[v] = index; |
| | 0 | 668 | | lowLinks[v] = index; |
| | 0 | 669 | | index++; |
| | 0 | 670 | | stack.Push(v); |
| | 0 | 671 | | onStack.Add(v); |
| | | 672 | | |
| | 0 | 673 | | if (_outgoing.TryGetValue(v, out var targets)) |
| | 0 | 674 | | { |
| | 0 | 675 | | foreach (var t in targets) |
| | 0 | 676 | | { |
| | 0 | 677 | | var w = this.FindNoLock(t); |
| | 0 | 678 | | if (!rootSet.Contains(w)) continue; // only consider candidates |
| | | 679 | | |
| | 0 | 680 | | if (!indices.ContainsKey(w)) |
| | 0 | 681 | | { |
| | 0 | 682 | | this.Strongconnect(w, rootSet, ref index, stack, onStack, indices, lowLinks, sccs); |
| | 0 | 683 | | lowLinks[v] = System.Math.Min(lowLinks[v], lowLinks[w]); |
| | 0 | 684 | | } |
| | 0 | 685 | | else if (onStack.Contains(w)) |
| | 0 | 686 | | { |
| | 0 | 687 | | lowLinks[v] = System.Math.Min(lowLinks[v], indices[w]); |
| | 0 | 688 | | } |
| | 0 | 689 | | } |
| | 0 | 690 | | } |
| | | 691 | | |
| | 0 | 692 | | if (lowLinks[v] == indices[v]) |
| | 0 | 693 | | { |
| | 0 | 694 | | var scc = new List<EdgeId>(); |
| | | 695 | | EdgeId w; |
| | | 696 | | do |
| | 0 | 697 | | { |
| | 0 | 698 | | w = stack.Pop(); |
| | 0 | 699 | | onStack.Remove(w); |
| | 0 | 700 | | scc.Add(w); |
| | 0 | 701 | | } while (w != v); |
| | | 702 | | |
| | 0 | 703 | | sccs.Add(scc); |
| | 0 | 704 | | } |
| | 0 | 705 | | } |
| | | 706 | | |
| | | 707 | | /// <summary> |
| | | 708 | | /// Walks up the union-find chain to the root. Acquires the read lock; for callers |
| | | 709 | | /// that already hold the lock (read or write), use <see cref="FindNoLock"/>. |
| | | 710 | | /// </summary> |
| | | 711 | | public EdgeId Find(EdgeId x) |
| | 33928 | 712 | | { |
| | 33928 | 713 | | _lock.EnterReadLock(); |
| | 67856 | 714 | | try { return this.FindNoLock(x); } |
| | 101784 | 715 | | finally { _lock.ExitReadLock(); } |
| | 33928 | 716 | | } |
| | | 717 | | |
| | | 718 | | /// <summary> |
| | | 719 | | /// Lock-free Find for use inside methods that already hold _lock. Intentionally |
| | | 720 | | /// does NOT path-compress: the graph is built once then queried from many concurrent |
| | | 721 | | /// snap calls; compressing would mutate <see cref="_parent"/> during reads and |
| | | 722 | | /// require an exclusive lock for every Find. Without compression each Find is |
| | | 723 | | /// O(log n) thanks to rank-balanced unions in <see cref="MergeNoLock"/> — fast |
| | | 724 | | /// enough for the snap path. |
| | | 725 | | /// </summary> |
| | | 726 | | private EdgeId FindNoLock(EdgeId x) |
| | 532456 | 727 | | { |
| | 532456 | 728 | | if (!_parent.TryGetValue(x, out var parent)) return x; |
| | 976422 | 729 | | while (parent != x) |
| | 443966 | 730 | | { |
| | 443966 | 731 | | x = parent; |
| | 443966 | 732 | | if (!_parent.TryGetValue(x, out parent)) return x; |
| | 443966 | 733 | | } |
| | 532456 | 734 | | return x; |
| | 532456 | 735 | | } |
| | | 736 | | } |