Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
gs-algo
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
graphstream
gs-algo
Commits
4c940477
Commit
4c940477
authored
13 years ago
by
gsavin
Browse files
Options
Downloads
Patches
Plain Diff
Update Kruskal and Prim algorithms doc.
parent
d58ee001
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/org/graphstream/algorithm/Kruskal.java
+76
-4
76 additions, 4 deletions
src/org/graphstream/algorithm/Kruskal.java
src/org/graphstream/algorithm/Prim.java
+72
-2
72 additions, 2 deletions
src/org/graphstream/algorithm/Prim.java
with
148 additions
and
6 deletions
src/org/graphstream/algorithm/Kruskal.java
+
76
−
4
View file @
4c940477
...
...
@@ -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
>
{
/**
...
...
This diff is collapsed.
Click to expand it.
src/org/graphstream/algorithm/Prim.java
+
72
−
2
View file @
4c940477
...
...
@@ -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.
*
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment