/** * Beispiel aus * * - Algorithmen und Datenstrukturen für Dummies * - von Andreas Gogol-Döring und Thomas Letschert * - Verlag Wiley-VCH; Oktober 2019 * - Kapitel 6, Graphen: Jeder mit jedem * * @author A. Gogol-Döring, Th. Letschert */ import Foundation struct AuD_06_04_VampInfluencer { static let V = ["Vlad", "Brunhilde", "Olga", "Rudiger", "Hildegard", "Anna"] static let E = [ ("Vlad", ["Olga", "Hildegard"]), ("Brunhilde", ["Rudiger"]), ("Olga", ["Hildegard", "Anna"]), ("Rudiger", []), ("Hildegard", []), ("Anna", ["Rudiger", "Hildegard"]) ] static func MaxReachable(E: [ (String, [String]) ] ) -> String { func R(v: String) -> [String] { var M: [String] = [] func Reach(E: [(String, [String])], v: String) -> [String] { M = [v] + M var result = [v] for (x, ww) in E { if (x == v) { for w in ww { if (!M.contains(w)) { result = result + Reach(E: E, v: w) } } return result } } return [] } return Reach(E: E, v: v) } return E . map ( { (p: (String, [String]) ) in p.0 } ) // vertices . map( { (s: String) -> [String] in R(v: s) } ) // R of vertices . max( by: { (a:[String], b:[String]) -> Bool in a.count < b.count } )![0] } static func run() { print(MaxReachable(E:E)) } }