Skip to content
Snippets Groups Projects
Commit 4c940477 authored by gsavin's avatar gsavin
Browse files

Update Kruskal and Prim algorithms doc.

parent d58ee001
Branches
Tags
No related merge requests found
......@@ -39,13 +39,68 @@ import java.util.Comparator;
import java.util.Iterator;
/**
* Compute a spanning tree using the Kruskal algorithm.
*
* <p>
* Kruskal's algorithm is a greedy algorithm which allows to find a minimal
* spanning tree in a weighted connected graph. More informations on <a
* href="http://en.wikipedia.org/wiki/Kruskal%27s_algorithm">Wikipedia</a>.
* </p>
*
* @complexity m*(log(m)+3)+n+n<sup>2</sup>, m = |E|, n = |V|
* <h2>Example</h2>
*
* The following example generates a graph with the Dorogovtsev-Mendes generator
* and then compute a spanning-tree using the Kruskal algorithm. The generator
* creates random weights for edges that will be used by the Kruskal algorithm.
*
* If no weight is present, algorithm considers that all weights are set to 1.
*
* When an edge is in the spanning tree, the algorithm will set its "ui.class"
* attribute to "intree", else the attribute is set to "notintree". According to
* the css stylesheet that is defined, spanning will be displayed with thick
* black lines while edges not in the spanning tree will be displayed with thin
* gray lines.
*
* <pre>
* import org.graphstream.graph.Graph;
* import org.graphstream.graph.implementations.DefaultGraph;
*
* import org.graphstream.algorithm.Kruskal;
* import org.graphstream.algorithm.generator.DorogovtsevMendesGenerator;
*
* public class KruskalTest {
*
* public static void main(String .. args) {
* DorogovtsevMendesGenerator gen = new DorogovtsevMendesGenerator();
* Graph graph = new DefaultGraph("Kruskal Test");
*
* @author Guilhelm Savin
* String css = "edge .notintree {size:1px;fill-color:gray;} " +
* "edge .intree {size:3px;fill-color:black;}";
*
* graph.addAttribute("ui.stylesheet", css);
* graph.display();
*
* gen.addEdgeAttribute("weight");
* gen.setEdgeAttributesRange(1, 100);
* gen.addSink(graph);
* gen.begin();
* for (int i = 0; i < 100 && gen.nextEvents(); i++)
* ;
* gen.end();
*
* Kruskal kruskal = new Kruskal("ui.class", "intree", "notintree");
*
* kruskal.init(g);
* kruskal.compute();
* }
* }
* </pre>
*
* @complexity m*(log(m)+3)+n+n<sup>2</sup>, m = |E|, n = |V|
* @reference Joseph. B. Kruskal: On the Shortest Spanning Subtree of a Graph
* and the Traveling Salesman Problem. In: Proceedings of the
* American Mathematical Society, Vol 7, No. 1 (Feb, 1956), pp. 48–50
* @see org.graphstream.algorithm.AbstractSpanningTree
*
*/
public class Kruskal extends AbstractSpanningTree {
......@@ -83,6 +138,22 @@ public class Kruskal extends AbstractSpanningTree {
this(weightAttribute, flagAttribute, true, false);
}
/**
* Create a new Kruskal's algorithm.
*
* @param flagAttribute
* attribute used to set if an edge is in the spanning tree
* @param flagOn
* value of the <i>flagAttribute</i> if edge is in the spanning
* tree
* @param flagOff
* value of the <i>flagAttribute</i> if edge is not in the
* spanning tree
*/
public Kruskal(String flagAttribute, Object flagOn, Object flagOff) {
this("weight", flagAttribute, flagOn, flagOff);
}
/**
* Create a new Kruskal's algorithm.
*
......@@ -187,6 +258,9 @@ public class Kruskal extends AbstractSpanningTree {
*/
@SuppressWarnings({ "rawtypes" })
protected Comparable getWeight(Edge e) {
if (!e.hasAttribute(weightAttribute))
return Integer.valueOf(1);
return (Comparable) e.getAttribute(weightAttribute, Comparable.class);
}
......@@ -268,8 +342,6 @@ public class Kruskal extends AbstractSpanningTree {
/**
* A comparator which uses the <i>weightAttribute</i> of its parent's class
* to compare edges.
*
* @author Guilhelm Savin
*/
private final class WeightEdgeComparator implements Comparator<Edge> {
/**
......
......@@ -39,13 +39,67 @@ import java.util.Comparator;
import java.util.Collections;
/**
* Compute a spanning tree using the Prim algorithm.
*
* <p>
* Prim's algorithm is an algorithm which allows to find a minimal spanning tree
* in a weighted connected graph. More informations on <a
* href="http://en.wikipedia.org/wiki/Prim%27s_algorithm">Wikipedia</a>.
* </p>
*
* @complexity 0(m+m<sup>2</sup>log(m)), where m = |E|
* <h2>Example</h2>
*
* The following example generates a graph with the Dorogovtsev-Mendes generator
* and then compute a spanning-tree using the Kruskal algorithm. The generator
* creates random weights for edges that will be used by the Kruskal algorithm.
*
* If no weight is present, algorithm considers that all weights are set to 1.
*
* When an edge is in the spanning tree, the algorithm will set its "ui.class"
* attribute to "intree", else the attribute is set to "notintree". According to
* the css stylesheet that is defined, spanning will be displayed with thick
* black lines while edges not in the spanning tree will be displayed with thin
* gray lines.
*
* <pre>
* import org.graphstream.graph.Graph;
* import org.graphstream.graph.implementations.DefaultGraph;
*
* import org.graphstream.algorithm.Prim;
* import org.graphstream.algorithm.generator.DorogovtsevMendesGenerator;
*
* @author Guilhelm Savin
* public class PrimTest {
*
* public static void main(String .. args) {
* DorogovtsevMendesGenerator gen = new DorogovtsevMendesGenerator();
* Graph graph = new DefaultGraph("Prim Test");
*
* String css = "edge .notintree {size:1px;fill-color:gray;} " +
* "edge .intree {size:3px;fill-color:black;}";
*
* graph.addAttribute("ui.stylesheet", css);
* graph.display();
*
* gen.addEdgeAttribute("weight");
* gen.setEdgeAttributesRange(1, 100);
* gen.addSink(graph);
* gen.begin();
* for (int i = 0; i < 100 && gen.nextEvents(); i++)
* ;
* gen.end();
*
* Prim prim = new Prim("ui.class", "intree", "notintree");
*
* prim.init(g);
* prim.compute();
* }
* }
* </pre>
*
* @complexity 0(m+m<sup>2</sup>log(m)), where m = |E|
* @reference R. C. Prim: Shortest connection networks and some generalizations.
* In: Bell System Technical Journal, 36 (1957), pp. 1389–1401
* @see org.graphstream.algorithm.AbstractSpanningTree
*
*/
public class Prim extends AbstractSpanningTree {
......@@ -73,6 +127,22 @@ public class Prim extends AbstractSpanningTree {
this(weightAttribute, flagAttribute, true, false);
}
/**
* Create a new Prim's algorithm.
*
* @param flagAttribute
* attribute used to set if an edge is in the spanning tree
* @param flagOn
* value of the <i>flagAttribute</i> if edge is in the spanning
* tree
* @param flagOff
* value of the <i>flagAttribute</i> if edge is not in the
* spanning tree
*/
public Prim(String flagAttribute, Object flagOn, Object flagOff) {
this("weight", flagAttribute, flagOn, flagOff);
}
/**
* Create a new Prim's algorithm.
*
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment