public class Kruskal extends AbstractSpanningTree
Kruskal's algorithm is a greedy algorithm which allows to find a minimal spanning tree in a weighted connected graph. More informations on Wikipedia.
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"); 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(); } }
AbstractSpanningTree
Modifier and Type | Class and Description |
---|---|
protected class |
Kruskal.EdgeComparator |
protected class |
Kruskal.TreeIterator<T extends Edge> |
Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_WEIGHT_ATTRIBUTE
Default weight attribute
|
protected List<Edge> |
treeEdges
List of the tree edges.
|
protected double |
treeWeight
The weight of the spanning tree
|
protected String |
weightAttribute
Attribute where the weights of the edges are stored
|
flagAttribute, flagOff, flagOn, graph
Constructor and Description |
---|
Kruskal()
Create a new Kruskal's algorithm.
|
Kruskal(String flagAttribute,
Object flagOn,
Object flagOff)
Create a new Kruskal's algorithm.
|
Kruskal(String weightAttribute,
String flagAttribute)
Create a new Kruskal's algorithm.
|
Kruskal(String weightAttribute,
String flagAttribute,
Object flagOn,
Object flagOff)
Create a new Kruskal's algorithm.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Removes the tags of all edges.
|
<T extends Edge> |
getTreeEdgesIterator()
An iterator on the tree edges.
|
double |
getTreeWeight()
Returns the total weight of the minimum spanning tree
|
protected double |
getWeight(Edge e) |
String |
getWeightAttribute()
Get weight attribute used to compare edges.
|
protected void |
makeTree()
Method that will be implemented by spanning tree's algorithms to build
the tree.
|
void |
setWeightAttribute(String newWeightAttribute)
Set the weight attribute.
|
compute, edgeOff, edgeOn, getFlagAttribute, getFlagOff, getFlagOn, getTreeEdges, init, resetFlags, setFlagAttribute, setFlagOff, setFlagOn
public static final String DEFAULT_WEIGHT_ATTRIBUTE
protected String weightAttribute
protected double treeWeight
public Kruskal()
public Kruskal(String weightAttribute, String flagAttribute)
true
for the tree edges and false for the non-tree edges.weightAttribute
- attribute used to compare edgesflagAttribute
- attribute used to set if an edge is in the spanning treepublic Kruskal(String flagAttribute, Object flagOn, Object flagOff)
flagAttribute
- attribute used to set if an edge is in the spanning treeflagOn
- value of the flagAttribute if edge is in the spanning
treeflagOff
- value of the flagAttribute if edge is not in the
spanning treepublic Kruskal(String weightAttribute, String flagAttribute, Object flagOn, Object flagOff)
weightAttribute
- attribute used to compare edgesflagAttribute
- attribute used to set if an edge is in the spanning treeflagOn
- value of the flagAttribute if edge is in the spanning
treeflagOff
- value of the flagAttribute if edge is not in the
spanning treepublic String getWeightAttribute()
public void setWeightAttribute(String newWeightAttribute)
newWeightAttribute
- new attribute usedprotected void makeTree()
AbstractSpanningTree
makeTree
in class AbstractSpanningTree
public <T extends Edge> Iterator<T> getTreeEdgesIterator()
SpanningTree
getTreeEdgesIterator
in interface SpanningTree
getTreeEdgesIterator
in class AbstractSpanningTree
public void clear()
SpanningTree
clear
in interface SpanningTree
clear
in class AbstractSpanningTree
public double getTreeWeight()
WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses