MCPcopy Index your code
hub / github.com/earlygrey/simple-graphs

github.com/earlygrey/simple-graphs @v5.1.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v5.1.1 ↗ · + Follow
366 symbols 1,073 edges 36 files 69 documented · 19%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Simple Graphs

Simple graphs is a Java library containing basic graph data structures and algorithms. It is lightweight, fast, and intuitive to use.

It has two types of graph data structures representing undirected and directed graphs. Each of these provides methods for adding and removing vertices and edges, for retrieving edges, and for accessing collections of its vertices and edges. All graphs in simple graphs are weighted and (of course) simple.

Algorithms implemented are: - breadth first search - depth first search - shortest path using the A* search algorithm (Dijkstra's algorithm when no heuristic is provided) - cycle detection - topological sort for directed graphs (performed in-place) - minimum weight spanning tree for undirected graphs using Kruskal's algorithm

Simple graphs uses Java 8 and Java Collections. It has no dependencies, and is GWT compatible. It uses float for floating point values, so should be a little more compatible with libraries that use floats, such as libgdx.

If you're looking for a broader, more powerful library and don't care about Java 8, GWT, or including a lot of extra dependencies, I'd recommend JGraphT. It essentially does everything this library does, plus a lot more (though it's not really "simple").


Installation

To include Simple Graphs in your project, follow the instructions on the jitpack website.

If you use GWT, your .gwt.xml file should inherit simple-graphs using:

<inherits name="simple_graphs" />

Usage

Construction

Usage looks something like:

Graph<Integer> graph = new UndirectedGraph<>(); // or new DirectedGraph<>();

for (int i = 0; i < 5; i++) {
  graph.addVertex(i);
}
boolean containsVertex = graph.contains(0);

graph.addEdge(1, 2);
boolean containsEdge = graph.edgeExists(1, 2)

Edge Weights

Edge weights can be assigned a fixed float value, or can be dynamically calculated via a WeightFunction.

// fixed value
graph.addEdge(vertexA, vertexB, 1.5f);
graph.getEdge(vertexA, vertexB).setWeight(1.5f);
graph.setDefaultEdgeWeight(1.5f);

// weight functions (assuming vertex class implements a distance method dst())
graph.addEdge(vertexA, vertexB, (a, b) -> a.dst(b));
graph.getEdge(vertexA, vertexB).setWeight((a, b) -> a.dst(b));
graph.setDefaultEdgeWeight((a, b) -> a.dst(b);

Collections

You can obtain Collection<V>s of the vertices and edges:

Collection<Integer> vertices = graph.getVertices();
Collection<Edge<Integer>> edges = graph.getEdges();

Neither collection can be modified directly - they provide a "read-only" iterable view of the vertices and edges, and they are subject to the same restrictions as a Collection returned by Collections#unmodifiableCollection(). The iteration order is guaranteed to be consistent for both collections (default order is insertion order) and both are sortable, though you need to sort using the graph object and not directly on the collection. Something like:

graph.sortVertices((v1, v2) -> ...);

If you want to access elements by index, you need to convert to an array via the toArray() methods or add them to a List via eg new ArrayList<>(graph.getVertices()).

Algorithms

To access algorithms, use Graph#algorithms(). You need to have a specific reference to a subclass of Graph (DirectedGraph or UndirectedGraph) to access algorithms specific to that type of graph.

V u, v;
Graph<V> graph;
UndirectedGraph<V> undirectedGraph;
DirectedGraph<V> directedGraph;

Path<V> path = graph.algorithms().findShortestPath(u, v);
UndirectedGraph<V> tree = undirectedGraph.algorithms().findMinimumWeightSpanningTree();
directedGraph.algorithms().topologicalSort();

Additionally, search algorithms allow a processing step at each step of the algorithm. These can be used for side effects (for example to construct another graph as the algorithm runs), or for deciding whether to skip processing that vertex or terminate the algorithm. For example:

graph.algorithms().breadthFirstSearch(u, step -> System.out.println("processing " + step.vertex()));

Graph<Integer> tree = graph.createNew();
graph.algorithms().depthFirstSearch(u, step -> {
    tree.addVertex(step.vertex());
    if (step.count() > 0) {
        tree.addEdge(step.edge());
    }
    if (step.depth() > 4) {
        step.ignore();
    }
});

Technical Considerations

While vertices can be any type of Object, care must be taken that they are immutable, in the sense that while in the Graph their hashCode() method always returns the same value, and equals is consistent. In general, vertex objects are subject to the same requirements as keys in a java Map.


Wiki

See the wiki for more info.

Extension points exported contracts — how you extend this code

Heuristic (Interface)
A function which estimates the distance of a shortest path between two vertices. A heuristic h should be admissible, [1 …
src/main/java/space/earlygrey/simplegraphs/utils/Heuristic.java
WeightFunction (Interface)
(no doc) [2 implementers]
src/main/java/space/earlygrey/simplegraphs/utils/WeightFunction.java
SearchProcessor (Interface)
(no doc)
src/main/java/space/earlygrey/simplegraphs/utils/SearchProcessor.java

Core symbols most depended-on inside this repo

addEdge
called by 41
src/main/java/space/earlygrey/simplegraphs/Graph.java
size
called by 40
src/main/java/space/earlygrey/simplegraphs/Graph.java
get
called by 29
src/main/java/space/earlygrey/simplegraphs/Array.java
addVertex
called by 24
src/main/java/space/earlygrey/simplegraphs/Graph.java
getEdgeCount
called by 21
src/main/java/space/earlygrey/simplegraphs/Graph.java
add
called by 21
src/main/java/space/earlygrey/simplegraphs/Path.java
algorithms
called by 19
src/main/java/space/earlygrey/simplegraphs/Graph.java
clear
called by 15
src/main/java/space/earlygrey/simplegraphs/Path.java

Shape

Method 324
Class 39
Interface 3

Languages

Java100%

Modules by API surface

src/main/java/space/earlygrey/simplegraphs/Graph.java38 symbols
src/main/java/space/earlygrey/simplegraphs/Node.java34 symbols
src/main/java/space/earlygrey/simplegraphs/NodeMap.java27 symbols
src/main/java/space/earlygrey/simplegraphs/Connection.java24 symbols
src/main/java/space/earlygrey/simplegraphs/VertexCollection.java21 symbols
src/main/java/space/earlygrey/simplegraphs/Array.java21 symbols
src/main/java/space/earlygrey/simplegraphs/Path.java16 symbols
src/main/java/space/earlygrey/simplegraphs/NodeCollection.java15 symbols
src/main/java/space/earlygrey/simplegraphs/BinaryHeap.java15 symbols
src/main/java/space/earlygrey/simplegraphs/algorithms/SearchStep.java12 symbols
src/main/java/space/earlygrey/simplegraphs/Edge.java12 symbols
src/main/java/space/earlygrey/simplegraphs/algorithms/Algorithms.java11 symbols

For agents

$ claude mcp add simple-graphs \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact