| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using Itinero.Network.Enumerators.Edges; |
| | | 3 | | |
| | | 4 | | namespace Itinero.Network.Search.Islands; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// A directed meta-graph for island detection. |
| | | 8 | | /// Each vertex represents one or more routing edges (merged via union-find). |
| | | 9 | | /// Directed links represent travel direction between edge groups. |
| | | 10 | | /// </summary> |
| | | 11 | | internal class IslandDirectedGraph |
| | | 12 | | { |
| | 1 | 13 | | internal static readonly EdgeId MainNetworkSentinel = new(uint.MaxValue - 1, uint.MaxValue - 1); |
| | | 14 | | |
| | 9 | 15 | | private readonly Dictionary<EdgeId, EdgeId> _parent = new(); |
| | 9 | 16 | | private readonly Dictionary<EdgeId, int> _rank = new(); |
| | 9 | 17 | | private readonly Dictionary<EdgeId, int> _size = new(); |
| | 9 | 18 | | private readonly Dictionary<EdgeId, HashSet<EdgeId>> _outgoing = new(); |
| | 9 | 19 | | private readonly Dictionary<EdgeId, HashSet<EdgeId>> _incoming = new(); |
| | 9 | 20 | | private readonly Dictionary<EdgeId, List<EdgeId>> _members = new(); |
| | 9 | 21 | | private readonly HashSet<EdgeId> _processed = new(); |
| | | 22 | | |
| | 9 | 23 | | public IslandDirectedGraph() |
| | 9 | 24 | | { |
| | 9 | 25 | | _parent[MainNetworkSentinel] = MainNetworkSentinel; |
| | 9 | 26 | | _rank[MainNetworkSentinel] = int.MaxValue; |
| | 9 | 27 | | _size[MainNetworkSentinel] = int.MaxValue; |
| | 9 | 28 | | } |
| | | 29 | | |
| | | 30 | | public void AddVertex(EdgeId edgeId) |
| | 5530 | 31 | | { |
| | 10018 | 32 | | if (_parent.ContainsKey(edgeId)) return; |
| | 1042 | 33 | | _parent[edgeId] = edgeId; |
| | 1042 | 34 | | _rank[edgeId] = 0; |
| | 1042 | 35 | | _size[edgeId] = 1; |
| | 1042 | 36 | | _members[edgeId] = new List<EdgeId> { edgeId }; |
| | 5530 | 37 | | } |
| | | 38 | | |
| | 257 | 39 | | public bool IsInGraph(EdgeId edgeId) => _parent.ContainsKey(edgeId); |
| | 841 | 40 | | public bool IsProcessed(EdgeId edgeId) => _processed.Contains(edgeId); |
| | 826 | 41 | | public void SetProcessed(EdgeId edgeId) => _processed.Add(edgeId); |
| | | 42 | | |
| | | 43 | | public bool IsNotIsland(EdgeId edgeId) |
| | 13097 | 44 | | { |
| | 14145 | 45 | | if (!_parent.ContainsKey(edgeId)) return false; |
| | 12049 | 46 | | return this.Find(edgeId) == this.Find(MainNetworkSentinel); |
| | 13097 | 47 | | } |
| | | 48 | | |
| | 1027 | 49 | | public int GetSize(EdgeId edgeId) => _size[this.Find(edgeId)]; |
| | | 50 | | |
| | | 51 | | public List<EdgeId>? GetMembers(EdgeId edgeId) |
| | 13 | 52 | | { |
| | 13 | 53 | | var root = this.Find(edgeId); |
| | 13 | 54 | | return _members.TryGetValue(root, out var m) ? m : null; |
| | 13 | 55 | | } |
| | | 56 | | |
| | | 57 | | public void AddDirectedLink(EdgeId from, EdgeId to) |
| | 9406 | 58 | | { |
| | 9406 | 59 | | var fromRoot = this.Find(from); |
| | 9406 | 60 | | var toRoot = this.Find(to); |
| | 16756 | 61 | | if (fromRoot == toRoot) return; |
| | | 62 | | |
| | 2056 | 63 | | if (!_outgoing.TryGetValue(fromRoot, out var targets)) |
| | 1038 | 64 | | { |
| | 1038 | 65 | | targets = new HashSet<EdgeId>(); |
| | 1038 | 66 | | _outgoing[fromRoot] = targets; |
| | 1038 | 67 | | } |
| | | 68 | | |
| | 2056 | 69 | | if (targets.Add(toRoot)) |
| | 2056 | 70 | | { |
| | | 71 | | // also update incoming index |
| | 2056 | 72 | | if (!_incoming.TryGetValue(toRoot, out var sources)) |
| | 1038 | 73 | | { |
| | 1038 | 74 | | sources = new HashSet<EdgeId>(); |
| | 1038 | 75 | | _incoming[toRoot] = sources; |
| | 1038 | 76 | | } |
| | 2056 | 77 | | sources.Add(fromRoot); |
| | 2056 | 78 | | } |
| | 9406 | 79 | | } |
| | | 80 | | |
| | | 81 | | public bool HasDirectedLink(EdgeId from, EdgeId to) |
| | 9406 | 82 | | { |
| | 9406 | 83 | | var fromRoot = this.Find(from); |
| | 9406 | 84 | | var toRoot = this.Find(to); |
| | 9406 | 85 | | return _outgoing.TryGetValue(fromRoot, out var targets) && targets.Contains(toRoot); |
| | 9406 | 86 | | } |
| | | 87 | | |
| | | 88 | | public void Merge(EdgeId a, EdgeId b) |
| | 1036 | 89 | | { |
| | 1036 | 90 | | var rootA = this.Find(a); |
| | 1036 | 91 | | var rootB = this.Find(b); |
| | 1036 | 92 | | if (rootA == rootB) return; |
| | | 93 | | |
| | | 94 | | // sentinel always wins |
| | 1036 | 95 | | var sentinelRoot = this.Find(MainNetworkSentinel); |
| | 1036 | 96 | | if (rootB == sentinelRoot) |
| | 0 | 97 | | (rootA, rootB) = (rootB, rootA); |
| | 1036 | 98 | | else if (rootA != sentinelRoot && _rank[rootA] < _rank[rootB]) |
| | 1 | 99 | | (rootA, rootB) = (rootB, rootA); |
| | | 100 | | |
| | 1036 | 101 | | _parent[rootB] = rootA; |
| | 1036 | 102 | | if (rootA != sentinelRoot) |
| | 1027 | 103 | | { |
| | 1027 | 104 | | _size[rootA] += _size[rootB]; |
| | 1044 | 105 | | if (_rank[rootA] == _rank[rootB]) _rank[rootA]++; |
| | 1027 | 106 | | } |
| | | 107 | | |
| | | 108 | | // merge outgoing |
| | 1036 | 109 | | if (_outgoing.TryGetValue(rootB, out var bOut)) |
| | 1036 | 110 | | { |
| | 1036 | 111 | | if (!_outgoing.TryGetValue(rootA, out var aOut)) |
| | 6 | 112 | | { |
| | 6 | 113 | | aOut = new HashSet<EdgeId>(); |
| | 6 | 114 | | _outgoing[rootA] = aOut; |
| | 6 | 115 | | } |
| | | 116 | | |
| | 7216 | 117 | | foreach (var t in bOut) |
| | 2054 | 118 | | { |
| | 2054 | 119 | | var tRoot = this.Find(t); |
| | 2054 | 120 | | if (tRoot != rootA) |
| | 1 | 121 | | { |
| | 1 | 122 | | aOut.Add(tRoot); |
| | | 123 | | // update incoming: t's incoming should point to rootA not rootB |
| | 1 | 124 | | if (_incoming.TryGetValue(tRoot, out var tInc)) |
| | 1 | 125 | | { |
| | 1 | 126 | | tInc.Remove(rootB); |
| | 1 | 127 | | tInc.Add(rootA); |
| | 1 | 128 | | } |
| | 1 | 129 | | } |
| | 2054 | 130 | | } |
| | | 131 | | |
| | 1036 | 132 | | _outgoing.Remove(rootB); |
| | 1036 | 133 | | } |
| | | 134 | | |
| | | 135 | | // merge incoming |
| | 1036 | 136 | | if (_incoming.TryGetValue(rootB, out var bInc)) |
| | 1036 | 137 | | { |
| | 1036 | 138 | | if (!_incoming.TryGetValue(rootA, out var aInc)) |
| | 6 | 139 | | { |
| | 6 | 140 | | aInc = new HashSet<EdgeId>(); |
| | 6 | 141 | | _incoming[rootA] = aInc; |
| | 6 | 142 | | } |
| | | 143 | | |
| | 7216 | 144 | | foreach (var s in bInc) |
| | 2054 | 145 | | { |
| | 2054 | 146 | | var sRoot = this.Find(s); |
| | 2054 | 147 | | if (sRoot != rootA) |
| | 0 | 148 | | { |
| | 0 | 149 | | aInc.Add(sRoot); |
| | | 150 | | // update outgoing: s's outgoing should point to rootA not rootB |
| | 0 | 151 | | if (_outgoing.TryGetValue(sRoot, out var sOut)) |
| | 0 | 152 | | { |
| | 0 | 153 | | sOut.Remove(rootB); |
| | 0 | 154 | | sOut.Add(rootA); |
| | 0 | 155 | | } |
| | 0 | 156 | | } |
| | 2054 | 157 | | } |
| | | 158 | | |
| | 1036 | 159 | | _incoming.Remove(rootB); |
| | 1036 | 160 | | } |
| | | 161 | | |
| | | 162 | | // remove self-loops |
| | 1036 | 163 | | if (_outgoing.TryGetValue(rootA, out var aOutFinal)) |
| | 1036 | 164 | | aOutFinal.Remove(rootA); |
| | 1036 | 165 | | if (_incoming.TryGetValue(rootA, out var aIncFinal)) |
| | 1036 | 166 | | aIncFinal.Remove(rootA); |
| | | 167 | | |
| | | 168 | | // merge members |
| | 1036 | 169 | | if (_members.TryGetValue(rootB, out var bMembers)) |
| | 1036 | 170 | | { |
| | 1036 | 171 | | if (rootA == sentinelRoot) |
| | 9 | 172 | | { |
| | 9 | 173 | | _members.Remove(rootB); |
| | 9 | 174 | | } |
| | | 175 | | else |
| | 1027 | 176 | | { |
| | 1027 | 177 | | if (!_members.TryGetValue(rootA, out var aMembers)) |
| | 0 | 178 | | { |
| | 0 | 179 | | aMembers = new List<EdgeId>(); |
| | 0 | 180 | | _members[rootA] = aMembers; |
| | 0 | 181 | | } |
| | 1027 | 182 | | aMembers.AddRange(bMembers); |
| | 1027 | 183 | | _members.Remove(rootB); |
| | 1027 | 184 | | } |
| | 1036 | 185 | | } |
| | 1036 | 186 | | } |
| | | 187 | | |
| | | 188 | | public void CollapseToMainNetwork(EdgeId root) |
| | 9 | 189 | | { |
| | 9 | 190 | | root = this.Find(root); |
| | 9 | 191 | | if (root == this.Find(MainNetworkSentinel)) return; |
| | 9 | 192 | | this.Merge(MainNetworkSentinel, root); |
| | 9 | 193 | | } |
| | | 194 | | |
| | | 195 | | public void RemoveEdge(EdgeId edgeId) |
| | 5 | 196 | | { |
| | 5 | 197 | | var root = this.Find(edgeId); |
| | | 198 | | |
| | 5 | 199 | | if (_members.TryGetValue(root, out var members)) |
| | 5 | 200 | | { |
| | 5 | 201 | | members.Remove(edgeId); |
| | 5 | 202 | | if (members.Count == 0) |
| | 5 | 203 | | { |
| | 5 | 204 | | _members.Remove(root); |
| | | 205 | | |
| | | 206 | | // clean up adjacency |
| | 5 | 207 | | if (_outgoing.TryGetValue(root, out var targets)) |
| | 1 | 208 | | { |
| | 3 | 209 | | foreach (var t in targets) |
| | 0 | 210 | | { |
| | 0 | 211 | | if (_incoming.TryGetValue(this.Find(t), out var tInc)) |
| | 0 | 212 | | tInc.Remove(root); |
| | 0 | 213 | | } |
| | 1 | 214 | | _outgoing.Remove(root); |
| | 1 | 215 | | } |
| | | 216 | | |
| | 5 | 217 | | if (_incoming.TryGetValue(root, out var sources)) |
| | 1 | 218 | | { |
| | 5 | 219 | | foreach (var s in sources) |
| | 1 | 220 | | { |
| | 1 | 221 | | if (_outgoing.TryGetValue(this.Find(s), out var sOut)) |
| | 1 | 222 | | sOut.Remove(root); |
| | 1 | 223 | | } |
| | 1 | 224 | | _incoming.Remove(root); |
| | 1 | 225 | | } |
| | 5 | 226 | | } |
| | 5 | 227 | | } |
| | | 228 | | |
| | 5 | 229 | | _parent.Remove(edgeId); |
| | 5 | 230 | | _processed.Remove(edgeId); |
| | 5 | 231 | | } |
| | | 232 | | |
| | | 233 | | /// <summary> |
| | | 234 | | /// O(1) dead-end check using incoming index. |
| | | 235 | | /// </summary> |
| | | 236 | | public bool IsDeadEnd(EdgeId edgeId) |
| | 115 | 237 | | { |
| | 115 | 238 | | var root = this.Find(edgeId); |
| | 115 | 239 | | if (root == this.Find(MainNetworkSentinel)) return false; |
| | | 240 | | |
| | 115 | 241 | | var hasOutgoing = _outgoing.TryGetValue(root, out var targets) && targets.Count > 0; |
| | 120 | 242 | | if (!hasOutgoing) return true; |
| | | 243 | | |
| | 110 | 244 | | var hasIncoming = _incoming.TryGetValue(root, out var sources) && sources.Count > 0; |
| | 110 | 245 | | return !hasIncoming; |
| | 115 | 246 | | } |
| | | 247 | | |
| | | 248 | | /// <summary> |
| | | 249 | | /// Checks if this edge can reach the sentinel in BOTH directions. |
| | | 250 | | /// </summary> |
| | | 251 | | public bool CanReachMainNetwork(EdgeId edgeId) |
| | 110 | 252 | | { |
| | 110 | 253 | | var root = this.Find(edgeId); |
| | 110 | 254 | | var sentinel = this.Find(MainNetworkSentinel); |
| | 110 | 255 | | if (root == sentinel) return true; |
| | | 256 | | |
| | 110 | 257 | | var visited = new HashSet<EdgeId>(); |
| | 110 | 258 | | var canForward = this.DfsCanReach(root, sentinel, visited, true); |
| | 220 | 259 | | if (!canForward) return false; |
| | | 260 | | |
| | 0 | 261 | | visited.Clear(); |
| | 0 | 262 | | return this.DfsCanReach(root, sentinel, visited, false); |
| | 110 | 263 | | } |
| | | 264 | | |
| | | 265 | | private bool DfsCanReach(EdgeId current, EdgeId target, HashSet<EdgeId> visited, bool forward) |
| | 9520 | 266 | | { |
| | 9520 | 267 | | current = this.Find(current); |
| | 9520 | 268 | | if (current == target) return true; |
| | 18930 | 269 | | if (!visited.Add(current)) return false; |
| | | 270 | | |
| | 110 | 271 | | var adj = forward |
| | 110 | 272 | | ? (_outgoing.TryGetValue(current, out var o) ? o : null) |
| | 110 | 273 | | : (_incoming.TryGetValue(current, out var i) ? i : null); |
| | | 274 | | |
| | 110 | 275 | | if (adj == null) return false; |
| | | 276 | | |
| | 19150 | 277 | | foreach (var next in adj) |
| | 9410 | 278 | | { |
| | 9410 | 279 | | if (this.DfsCanReach(this.Find(next), target, visited, forward)) return true; |
| | 9410 | 280 | | } |
| | | 281 | | |
| | 110 | 282 | | return false; |
| | 9520 | 283 | | } |
| | | 284 | | |
| | | 285 | | /// <summary> |
| | | 286 | | /// Detects cycles among the given candidate roots and merges them. |
| | | 287 | | /// Uses Tarjan's SCC algorithm on the subgraph of candidates. |
| | | 288 | | /// Returns true if any merges happened. |
| | | 289 | | /// </summary> |
| | | 290 | | public bool DetectAndMergeCycles(List<EdgeId> candidateRoots) |
| | 6 | 291 | | { |
| | | 292 | | // build the set of roots to consider |
| | 6 | 293 | | var rootSet = new HashSet<EdgeId>(); |
| | 238 | 294 | | foreach (var c in candidateRoots) |
| | 110 | 295 | | { |
| | 110 | 296 | | var r = this.Find(c); |
| | 110 | 297 | | if (r != this.Find(MainNetworkSentinel)) |
| | 110 | 298 | | rootSet.Add(r); |
| | 110 | 299 | | } |
| | | 300 | | |
| | 12 | 301 | | if (rootSet.Count < 2) return false; |
| | | 302 | | |
| | | 303 | | // Tarjan's SCC |
| | 0 | 304 | | var index = 0; |
| | 0 | 305 | | var stack = new Stack<EdgeId>(); |
| | 0 | 306 | | var onStack = new HashSet<EdgeId>(); |
| | 0 | 307 | | var indices = new Dictionary<EdgeId, int>(); |
| | 0 | 308 | | var lowLinks = new Dictionary<EdgeId, int>(); |
| | 0 | 309 | | var sccs = new List<List<EdgeId>>(); |
| | | 310 | | |
| | 0 | 311 | | foreach (var v in rootSet) |
| | 0 | 312 | | { |
| | 0 | 313 | | if (!indices.ContainsKey(v)) |
| | 0 | 314 | | this.Strongconnect(v, rootSet, ref index, stack, onStack, indices, lowLinks, sccs); |
| | 0 | 315 | | } |
| | | 316 | | |
| | | 317 | | // merge SCCs with more than one vertex |
| | 0 | 318 | | var merged = false; |
| | 0 | 319 | | foreach (var scc in sccs) |
| | 0 | 320 | | { |
| | 0 | 321 | | if (scc.Count < 2) continue; |
| | 0 | 322 | | for (var i = 1; i < scc.Count; i++) |
| | 0 | 323 | | { |
| | 0 | 324 | | this.Merge(scc[0], scc[i]); |
| | 0 | 325 | | } |
| | 0 | 326 | | merged = true; |
| | 0 | 327 | | } |
| | | 328 | | |
| | 0 | 329 | | return merged; |
| | 6 | 330 | | } |
| | | 331 | | |
| | | 332 | | private void Strongconnect(EdgeId v, HashSet<EdgeId> rootSet, |
| | | 333 | | ref int index, Stack<EdgeId> stack, HashSet<EdgeId> onStack, |
| | | 334 | | Dictionary<EdgeId, int> indices, Dictionary<EdgeId, int> lowLinks, |
| | | 335 | | List<List<EdgeId>> sccs) |
| | 0 | 336 | | { |
| | 0 | 337 | | indices[v] = index; |
| | 0 | 338 | | lowLinks[v] = index; |
| | 0 | 339 | | index++; |
| | 0 | 340 | | stack.Push(v); |
| | 0 | 341 | | onStack.Add(v); |
| | | 342 | | |
| | 0 | 343 | | if (_outgoing.TryGetValue(v, out var targets)) |
| | 0 | 344 | | { |
| | 0 | 345 | | foreach (var t in targets) |
| | 0 | 346 | | { |
| | 0 | 347 | | var w = this.Find(t); |
| | 0 | 348 | | if (!rootSet.Contains(w)) continue; // only consider candidates |
| | | 349 | | |
| | 0 | 350 | | if (!indices.ContainsKey(w)) |
| | 0 | 351 | | { |
| | 0 | 352 | | this.Strongconnect(w, rootSet, ref index, stack, onStack, indices, lowLinks, sccs); |
| | 0 | 353 | | lowLinks[v] = System.Math.Min(lowLinks[v], lowLinks[w]); |
| | 0 | 354 | | } |
| | 0 | 355 | | else if (onStack.Contains(w)) |
| | 0 | 356 | | { |
| | 0 | 357 | | lowLinks[v] = System.Math.Min(lowLinks[v], indices[w]); |
| | 0 | 358 | | } |
| | 0 | 359 | | } |
| | 0 | 360 | | } |
| | | 361 | | |
| | 0 | 362 | | if (lowLinks[v] == indices[v]) |
| | 0 | 363 | | { |
| | 0 | 364 | | var scc = new List<EdgeId>(); |
| | | 365 | | EdgeId w; |
| | | 366 | | do |
| | 0 | 367 | | { |
| | 0 | 368 | | w = stack.Pop(); |
| | 0 | 369 | | onStack.Remove(w); |
| | 0 | 370 | | scc.Add(w); |
| | 0 | 371 | | } while (w != v); |
| | | 372 | | |
| | 0 | 373 | | sccs.Add(scc); |
| | 0 | 374 | | } |
| | 0 | 375 | | } |
| | | 376 | | |
| | | 377 | | public EdgeId Find(EdgeId x) |
| | 150330 | 378 | | { |
| | 150330 | 379 | | if (!_parent.ContainsKey(x)) return x; |
| | 210018 | 380 | | if (_parent[x] != x) _parent[x] = this.Find(_parent[x]); |
| | 150330 | 381 | | return _parent[x]; |
| | 150330 | 382 | | } |
| | | 383 | | } |