Steven Barkin
Steven Barkin

Reputation: 99

How to label or color a vertex using jgrapht libraries?

I am interested in writing a graph coloring algorithm using various types of directed and undirected graph classes provided by jgrapht. None of them seem to have an easy ability to do this within the graph class itself (e.g. DirectedSimpleGraph).

My goal is to be able to traverse the graph object, and add/change various labels or colors to the vertices, without the need to store the vertex information outside the object itself - i.e. I would like to use or create methods such as "DirectedSimpleGraph.setColorToVertex(v, c), where v is a vertex. and c is the color which perhaps would be defined as an integer. Any leads or best practices advice would be greatly appreciated.

Upvotes: 3

Views: 2839

Answers (3)

Maria Ines Parnisari
Maria Ines Parnisari

Reputation: 17466

I would like to use or create methods such as "DirectedSimpleGraph.setColorToVertex(v, c), where v is a vertex

Graphs as a data structure don't have the concept of colors. Only visual representation of graphs do.

So, you can use something like the DOTExporter to change the graph in various ways, including coloring edges and vertices:

public void printGraph(Graph<GraphNode, DefaultEdge> graph) {
    PrintWriter writer = new PrintWriter(System.out);
    DOTExporter exp = new DOTExporter<>(
            new NodeIdProvider(),
            new NodeLabelProvider(),
            new StringEdgeNameProvider<DefaultEdge>(),
            new NodeAttributeProvider(),
            new EdgeAttributeProvider());
    exp.exportGraph(graph, writer);
}

The NodeAttributeProvider prints each node:

public class NodeAttributeProvider implements ComponentAttributeProvider<GraphNode>
{
    @Override
    public Map<String, String> getComponentAttributes(GraphNode component) {
        Map<String, String> attrs = new HashMap<>();
        attrs.put("style", "filled");
        attrs.put("fillcolor", component.getFillColor());
        return attrs;
    }
}

Your job is then to implement interface GraphNode and its method getFillColor().

Upvotes: 0

Graeme Ahokas
Graeme Ahokas

Reputation: 36

The typical way to be able to colour or label a vertex is to supply your own Vertex class to jgrapht that stores whatever you need. For example,

public class MyVertex {
  public String colour;
}

SimpleGraph<MyVertex, DefaultEdge> g = 
    new SimpleGraph<MyVertex,DefaultEdge>(DefaultEdge.class);
MyVertex v1 = new MyVertex();
MyVertex v2 = new MyVertex();

g.addVertex(v1);
g.addVertex(v2);

DefaultEdge edge = g.addEdge(v1, v2);

//traverse graph
Graphs.getOppositeVertex(g, edge, v1).colour = "red";

This way, you don't need to use an external mechanism (e.g. hash map) to keep track of vertex colours / labels.

Upvotes: 2

Sid
Sid

Reputation: 463

It looks like the jgrapht library https://github.com/jgrapht/jgrapht is under active development. Perhaps you can contact the developer ? Have a look in the ReadMe at the github link for more info.

You could possibly extend the abstract classes in the library to add custom fields & functions you desire.

Upvotes: 0

Related Questions