Skip to content
openlg edited this page Sep 23, 2024 · 6 revisions

Graphlib is a Java library for creating and modifying directed and undirected graphs. In addition to a core graph API, it also comes with implementations for many common graph algorithms.

Table of Contents

Example

This following code block shows a small example of how to use graphlib in Java:

// Create a new directed graph
Graph<Object, Object> g = new Graph();

// Add node "a" to the graph with no label
g.setNode("a");

System.out.println(g.hasNode("a"));
// => true

// Add node "b" to the graph with a String label
g.setNode("b", "b's value");

// Get the label for node b
System.out.println(g.getNode("b"));
// => "b's value"

// Add node "c" to the graph with an Object label
g.setNode("c", new HashMap<String, Integer>(){{
	put("key", 1234);
}});

// What nodes are in the graph?
System.out.println(g.getNodes());
// => `[ 'a', 'b', 'c' ]`

// Add a directed edge from "a" to "b", but assign no label
g.setEdge("a", "b");

// Add a directed edge from "c" to "d" with an Object label.
// Since "d" did not exist prior to this call it is automatically
// created with an undefined label.
g.setEdge("c", "d", new HashMap<String, String>(){{
	put("key", "value");
}});

// What edges are in the graph?
System.out.println(g.getEdges());
// => `[ [ name = null, source = c, target = d ],
// 	     [ name = null, source = a, target = b ] ]`.

// Which edges leave node "a"?
System.out.println(g.outEdges("a"));
// => `[ [ name = null, source = a, target = b ]]`

// Which edges enter and leave node "d"?
System.out.println(g.nodeEdges("d"));
// => `[ [ name = null, source = c, target = d ]]`

Usage

You can add dependencies to your project through maven and gradle.

Use Maven

Add dependency into you pom.xml

<!-- https://mvnrepository.com/artifact/io.github.openlg/graphlib -->
<dependency>
    <groupId>io.github.openlg</groupId>
    <artifactId>graphlib</artifactId>
    <version>1.0.1</version>
</dependency>

Use Gradle

Add dependency into you build.gradle.

// https://mvnrepository.com/artifact/io.github.openlg/graphlib
implementation 'io.github.openlg:graphlib:1.0.1'

License

Graphlib is licensed under the terms of the Apache License. See LICENSE for details.

Clone this wiki locally