1// SPDX-FileCopyrightText: 2025 The Project Pterodactyl Developers 2// 3// SPDX-License-Identifier: MPL-2.0 4 5import Foundation 6 7struct Graph<Vertex: Hashable> { 8 var edges: [Vertex: Set<Vertex>] 9} 10 11extension Graph: Codable where Vertex: Codable {} 12extension Graph: Sendable where Vertex: Sendable {} 13 14extension Graph { 15 func verticesReachableFrom(_ start: Vertex) -> Set<Vertex> { 16 var visited: Set<Vertex> = [] 17 var stack: [Vertex] = [] 18 19 if let neighbors = edges[start] { 20 stack.append(contentsOf: neighbors) 21 } 22 23 while let vertex = stack.popLast() { 24 guard !visited.contains(vertex) else { continue } 25 visited.insert(vertex) 26 if let neighbors = edges[vertex] { 27 for neighbor in neighbors where !visited.contains(neighbor) { 28 stack.append(neighbor) 29 } 30 } 31 } 32 33 return visited 34 } 35}