JGraphT:LabeledEdges
From Eigenpedia
A simple example for dealing with labeling edges. Each edge has one of two labels - "friend" and "enemy". A custom RelationshipEdge class takes care of the nitty gritty. Comments/suggestions are welcome.
import java.util.ArrayList;
import org.jgrapht.*;
import org.jgrapht.graph.*;
public class LabeledEdges {
private static final String friend = "friend";
private static final String enemy = "enemy";
public static void main(String[] args) {
DirectedGraph<String, RelationshipEdge> graph =
new DirectedMultigraph<String, RelationshipEdge>(
new ClassBasedEdgeFactory<String, RelationshipEdge>(RelationshipEdge.class));
ArrayList<String> people = new ArrayList<String>();
people.add("John");
people.add("James");
people.add("Sarah");
people.add("Jessica");
// John is everyone's friend
for (String person : people) {
graph.addVertex(person);
graph.addEdge(people.get(0), person, new RelationshipEdge<String>(people.get(0), person, friend));
}
// Apparently James doesn't really like John
graph.addEdge("James", "John", new RelationshipEdge<String>("James", "John", enemy));
// Jessica is Sarah and James's friend
graph.addEdge("Jessica", "Sarah", new RelationshipEdge<String>("Jessica", "Sarah", friend));
graph.addEdge("Jessica", "James", new RelationshipEdge<String>("Jessica", "James", friend));
// But Sarah doesn't really like James
graph.addEdge("Sarah", "James", new RelationshipEdge<String>("Sarah", "James", enemy));
for (RelationshipEdge edge : graph.edgeSet()) {
if (edge.equals(enemy)) {
System.out.printf("%s is an enemy of %s\n", edge.getV1(), edge.getV2());
} else if (edge.equals(friend)) {
System.out.printf("%s is a friend of %s\n", edge.getV1(), edge.getV2());
}
}
}
public static class RelationshipEdge<V> extends DefaultEdge {
private V v1;
private V v2;
private String label;
public RelationshipEdge(V v1, V v2, String label) {
this.v1 = v1;
this.v2 = v2;
this.label = label;
}
public V getV1() {
return v1;
}
public V getV2() {
return v2;
}
public String toString() {
return label;
}
}
}

