Creating this topic to catch any typos and bugs in the 5th Edition of Data Structures & Algorithms in Swift.
Hi @jellodiil
I found a potential mistake in one of the challenges, but I’m not sure if I might have missed something from the lesson. In Graph Chapter → DFS lesson → Challenge 3 (Checking for a Cycle in a Graph), the provided solution seems to always return true, even when the graph does not contain a cycle.
let graph = AdjacencyList()
let a = graph.createVertex(data: “A”)
let b = graph.createVertex(data: “B”)
let c = graph.createVertex(data: “C”)
let d = graph.createVertex(data: “D”)
graph.add(.directed, from: a, to: b, weight: nil)
graph.add(.directed, from: b, to: c, weight: nil)
graph.add(.directed, from: c, to: a, weight: nil)
graph.add(.directed, from: c, to: d, weight: nil)
//graph.add(.directed, from: d, to: c, weight: nil) // This will cause a cycle
print(“Has cycle: (graph.hasCycle(from: a))”)
Based on this setup, the graph should not contain a cycle unless we explicitly add the edge from d to c. However, graph.hasCycle(from: a) always returns true, which seems incorrect.
Since this is my first time posting here, I’d appreciate any guidance on whether I should report this differently. Let me know if you need more details!