Chapter 37: Graphs Challenges

Last updated

Last updated
extension Graph where Element: Hashable {
public func numberOfPaths(from source: Vertex<Element>, to destination: Vertex<Element>) -> Int {
// Implement Solution Here.
var numberOfPaths = 0
var visited: Set<Vertex<Element>> = []
// DFS
paths(from: source, to: destination, visited: &visited, pathCounts: &numberOfPaths)
return numberOfPaths
}
func paths(from source: Vertex<Element>, to destination: Vertex<Element>, visited: inout Set<Vertex<Element>>, pathCounts: inout Int) {
visited.insert(source)
if source == destination {
// 找到一個path
pathCounts += 1
} else {
let neighbors = edges(from: source)
for edge in neighbors {
// 要走之前沒走過的點
if !visited.contains(edge.destination) {
paths(from: edge.destination, to: destination, visited: &visited, pathCounts: &pathCounts)
}
}
}
// Remove the source vertex from the visited set, so you can continue to find other paths to that node
visited.remove(source)
}
}let graph = AdjacencyList<String>()
let vincent = graph.createVertex(data: "vincent")
let chesley = graph.createVertex(data: "chesley")
let ruiz = graph.createVertex(data: "ruiz")
let patrick = graph.createVertex(data: "patrick")
let ray = graph.createVertex(data: "ray")
let sun = graph.createVertex(data: "sun")
let cole = graph.createVertex(data: "cole")
let kerry = graph.createVertex(data: "kerry")
graph.add(.undirected, from: vincent, to: chesley, weight: 0)
graph.add(.undirected, from: vincent, to: ruiz, weight: 0)
graph.add(.undirected, from: vincent, to: patrick, weight: 0)
graph.add(.undirected, from: ruiz, to: ray, weight: 0)
graph.add(.undirected, from: ruiz, to: sun, weight: 0)
graph.add(.undirected, from: patrick, to: cole, weight: 0)
graph.add(.undirected, from: patrick, to: kerry, weight: 0)
graph.add(.undirected, from: cole, to: ruiz, weight: 0)
graph.add(.undirected, from: cole, to: vincent, weight: 0)
print(graph)
print("Ruiz and Vincent both share a friend name Cole")