Commit efd8c37
committed
Don't call indexOfVertex(head) per neighbor.
Reduces the cycle detection complexity from O(V²) to O(V).
indexOfVertex(head) takes O(V) time, and if we call it `neighbors` times
(O(V)) then we end up with the O(V²) time complexity of the cycle
detection.
To fix this, we only need to move the head vertex index lookup to the
outside of the loop.1 parent d1de55a commit efd8c37
1 file changed
+6
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
38 | | - | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
39 | 42 | | |
40 | 43 | | |
41 | 44 | | |
42 | | - | |
| 45 | + | |
| 46 | + | |
43 | 47 | | |
44 | 48 | | |
45 | 49 | | |
| |||
0 commit comments