I compare two algorithms: Prim and Kruskal's.
I understand the basic concept of time complexity and when both work better (sparse / dense graphs)
I found this on the Internet, but I try to convert it to English.
dense graph: Prim = O(N2) Kruskal = O(N2*log(N)) sparse graph: Prim = O(N2) Kruskal = O(N log(N))
It's a little long shot, but can anyone explain what is going on here?
Prim is O (N ^ 2), where N is the number of vertices.
Kruskal - O (E log E), where E is the number of edges. "E log E" comes from a good edge sorting algorithm. You can then process it in linear time E.
In the dense graph E ~ N ^ 2. So Kruskal would be O (N ^ 2 log N ^ 2), which is just O (N ^ 2 log N).
, . O (N2) (2 = ) , N N - .
, E = c * N2. c , -, , , N, N . : log(ab) = log a + log b log(a^n) = n * log a. , log c < log N ( ) .
E = c * N2
c
log(ab) = log a + log b
log(a^n) = n * log a
, , , . , Prim Kruskal, , , , , ...
Kruskal (E) , . Prim (N), evaluting to O(N^2).
O(N^2)
, , N ^ 2 ( ), O(E*log(E)) O(N^2*log(N)).
O(E*log(E))
O(N^2*log(N))
- , "" O. , log (N ^ 2) , log (N), 2 (log(N^2) => 2*log(N), O O(log(N))).
log(N^2) => 2*log(N)
O(log(N))
E N, O(N*log(N)).
O(N*log(N))
-O, ( ). , .
N - E - .
O (N ^ 2) Edges
O (N) Edges.
, O, c
, O (N ^ 2), - O (N). O (E\lg E) E, Prim O (N ^ 2).
, , Kruskal , Prim .
-: n - .
Prim - O (n ^ 2), .
Kruskal - O (Elog (E)), E - . N, 2 , n ^ 2 ( n (n-1)/2, ?), n ^ 2 log (n ^ 2), 2n ^ 2 log n, O (n ^ 2logn), O (n ^ 2)
The sparse graph has only n edges, so we have n log n that is less than O (n ^ 2).