For each point, you have to find the nearest neighbor. (you get the first
n
)Computing the distance between two points get you a factor
1
, because it runs inO(1)
.So, computing the distance between one point and all the other points gets you a factor
O(n)
.
In total you get
O(n²)
. Sure, in step 3 you do not have to compute the distance to visited points, but this saves only a constant factor.The nearest neighbor info of a point
A
is the nearest point toA
. So you have to compute forn
points the distance to all other points, so you get here alsoO(n²)
Adding this together results in O(n² + n²) = O(2n²) = O(n²)
.