Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Sources/SwiftGraph/Search.swift
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ public extension Graph {
var visited: [Bool] = [Bool](repeating: false, count: vertexCount)
let queue: Queue<Int> = Queue<Int>()
var pathDict: [Int: Edge] = [Int: Edge]()
visited[fromIndex] = true
queue.push(fromIndex)
while !queue.isEmpty {
let v: Int = queue.pop()
Expand Down Expand Up @@ -415,6 +416,7 @@ public extension Graph {
var visited: [Bool] = [Bool](repeating: false, count: vertexCount)
let queue: Queue<Int> = Queue<Int>()
var pathDict: [Int: Edge] = [Int: Edge]()
visited[fromIndex] = true
queue.push(fromIndex)
while !queue.isEmpty {
let v: Int = queue.pop()
Expand Down Expand Up @@ -460,6 +462,7 @@ public extension Graph {
let queue: Queue<Int> = Queue<Int>()
var pathDict: [Int: Edge] = [Int: Edge]()
var paths: [[Edge]] = [[Edge]]()
visited[fromIndex] = true
queue.push(fromIndex)
while !queue.isEmpty {
let v: Int = queue.pop()
Expand Down
9 changes: 9 additions & 0 deletions Tests/SwiftGraphTests/SwiftGraphSearchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -477,4 +477,13 @@ class SwiftGraphSearchTests: XCTestCase {
XCTAssertTrue(endCities.contains("San Francisco"), "Should contain a route to San Francisco")
}
}

func testFindAllBfsDoesNotRevisitStartVertex() {
let g = UnweightedGraph<String>(vertices: ["A", "B", "C"])
g.addEdge(from: "A", to: "B", directed: false)
g.addEdge(from: "B", to: "C", directed: false)
let result = g.findAllBfs(from: "A") { _ in true }
XCTAssertEqual(result.count, 3, "Each vertex must be reported exactly once")
XCTAssertEqual(result.first?.count, 0, "The path to the start vertex is empty")
}
}
1 change: 1 addition & 0 deletions Tests/SwiftGraphTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ extension SwiftGraphSearchTests {
("testDfsVisitOrderWithCycle", testDfsVisitOrderWithCycle),
("testDFSWithCycle", testDFSWithCycle),
("testFindAllBfs", testFindAllBfs),
("testFindAllBfsDoesNotRevisitStartVertex", testFindAllBfsDoesNotRevisitStartVertex),
("testFindAllDfs", testFindAllDfs),
("testTraverseBfsOnCycle", testTraverseBfsOnCycle),
("testTraverseDfsOnCycle", testTraverseDfsOnCycle),
Expand Down
Loading