Skip to content

Commit 8a68809

Browse files
author
lck6055
committed
Added time and space complexities of knn
1 parent b60de8d commit 8a68809

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

Python/machine_learning/knn.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@
2121
4. Use majority voting to determine the predicted class.
2222
5. Return the predicted label.
2323
24+
Time Complexity (per prediction):
25+
1 Distance calculation → O(n * d)
26+
2 Sorting distances → O(n log n)
27+
3 Selecting k nearest points → O(k)
28+
4 Majority voting (Counter) → O(k)
29+
🔹 Overall → O(n * d + n log n)
30+
31+
Space Complexity:
32+
O(n * d) → to store all distances and intermediate computations
33+
2434
Contributor:
2535
---------------------
2636
💻 Contributed by: **Lakhinana Chaturvedi Kashyap**

0 commit comments

Comments
 (0)