views:

296

answers:

6

I am comparing two algorithms, Prim's and Kruskal's.

I understand the basic concept of time complexity and when the two work best (sparse/dense graphs)

I found this on the Internet, but I am struggling 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 bit of a long shot, but could anyone explain what is going on here?

Thanks.

+2  A: 

OK, here goes. O(N2) (2 = squared) means that the speed of the algorithm for large N varies as the square of N - so twice the size of graph will result in four times the time to compute.

The Kruskal rows are merely simplified, and assume that E = c * N2. c here is presumably a constant, that we can assume to be significantly smaller than N as N gets large. You need to know the following laws of logarithms: log(ab) = log a + log b and log(a^n) = n * log a. These two combined with the fact that log c << log N (is much less than and can be ignored) should let you understand the simplifications there.

Now, as for the original expressions and where they were derived from, you'd need to check the page you got these from. But I'm assuming that if you're looking at Prim's and Kruskal's then you will be able to understand the derivation, or at least that if you can't my explaining it to you is not actually going to help you in the long run...

David M
+1  A: 

The two algorithms have big-O defined for different inputs (nodes and edges). So they are converting one to the other to compare them.

N is the number nodes in the graph E is the number of edges.

for a dense graph there are O(N^2) Edges

for a sparse graph there are O(N) Edges.

constants are of course irrelavent for big-O hence the c drops out

jk
+1  A: 

The thought is that in a dense graph, the number of edges is O(N^2) while in sparse graphs, the number of edges is O(N). So they're taking the O(E \lg E) and expanding it with this approximation of E in order to compare it directly to the running time of Prim's O(N^2).

Basically, it's showing that Kruskal's is better for sparse graphs and Prim's is better for dense graphs.

Dave
+1  A: 

Kruskal is sensitive to the number of edges (E) in a graph, not the number of nodes. Prim however is only affected by the number of nodes (N), evaluting to O(N^2).

This means that in dense graphs where the number of edges approaches N^2 (all nodes connected) it's complexity factor of O(E*log(E)) is roughly equivalent to O(N^2*log(N)).

The c is a constant to account for the 'almost' and is irrelevant in O notation. Also log(N^2) is of the same order of magnitude as log(N) as logarithm outweighs the power of 2 by a substantial margin ( log(N^2) => 2*log(N) which in O notation is O(log(N)) ).

In a sparse graph E is closer to N giving you O(N*log(N)).

Kris
+4  A: 

Prim is O(N^2), where N is the number of vertices.

Kruskal is O(E log E), where E is the number of edges. The "E log E" comes from a good algorithm sorting the edges. You can then process it in linear E time.

In a dense graph, E ~ N^2. So Kruskal would be O( N^2 log N^2 ), which is simply O( N^2 log N ).

Timmy
A: 

First: n is the number of vertices.

Prim is O(n^2) that part is easy enough.

Kruskal is O(Elog(E)) where E is the number of edges. in a dense graph, there are as many as N choose 2 edges, which is roughly n^2 (actually it's n(n-1)/2, but who's counting?) so, it's roughly n^2 log (n^2) which is 2n^2 log n which is O(n^2logn) which is bigger than O(n^2)

In a sparse graph, there are as few as n edges, so we have n log n which is less than O(n^2).

Brian Postow