Skip to content

Commit 19101a3

Browse files
authored
Merge pull request #92 from automaciej/master
Don't call indexOfVertex(head) per neighbor.
2 parents d1de55a + efd8c37 commit 19101a3

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

Sources/SwiftGraph/Cycle.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,15 @@ public extension Graph {
3535
while openPaths.count > 0 {
3636
let openPath = openPaths.removeFirst() // queue pop()
3737
if openPath.count > maxK { return cycles } // do we want to stop at a certain length k
38-
if let tail = openPath.last, let head = openPath.first, let neighbors = neighborsForVertex(tail) {
38+
if let tail = openPath.last,
39+
let head = openPath.first,
40+
let headIndex = indexOfVertex(head),
41+
let neighbors = neighborsForVertex(tail) {
3942
for neighbor in neighbors {
4043
if neighbor == head {
4144
cycles.append(openPath + [neighbor]) // found a cycle
42-
} else if !openPath.contains(neighbor) && indexOfVertex(neighbor)! > indexOfVertex(head)! {
45+
} else if let neighborIndex = indexOfVertex(neighbor),
46+
!openPath.contains(neighbor) && neighborIndex > headIndex {
4347
openPaths.append(openPath + [neighbor]) // another open path to explore
4448
}
4549
}

0 commit comments

Comments
 (0)