Java library containing basic graph algorithms. Fast and easy to use.
Usage is pretty pretty straightforward. Graph construction
Graph graph = new LinkedGraph(0); // or new MatrixGraph<>();
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 5; j++) {
graph.addEdge(i, j, 1); // vertex, vertex, weight
}
}Remove Edge
graph.removeEdge(1, 2);Graph size
int size = graph.size();Edge count for whole graph, or particular vertex
int edges = graph.edgeCount();
int sevenEdges = graph.edgeCount(7);Check weight, edge existance
boolean containsVertex = graph.hasEdge(1, 3);
int weight = graph.getWeight(1, 3);Iterate over edges
Iterable<Edge> edges = graph.edges(3);- Traversal
- Sorting
- Path Finding
- Minimum Spanning Tree
- Coloring
- Detection
BFS algorithm recibes the graph to be traversed, starting vertex and a callback function to be executed on every node.
import algorithms.Traversal.BFS;
BFS.solve(g, 0, (arg) -> {
System.out.println(arg);
});DFS algorithm recibes the graph to be traversed, starting vertex and a callback function to be executed on every node.
import algorithms.Traversal.DFS;
DFS.solve(g, 0, (arg) -> {
System.out.println(arg);
});Topological sort algorithm recibes a graph, and returns a sorted array representing one possible topological sort.
import algorithms.Sorting.TopoSort;
int[] result = TopoSort.solve(g);Dijkstra's algorithm recibes a graph, starting index, and returns an array representing minimum travelling cost to every node.
import algorithms.PathFinding.Dijkstra;
int[] result = Dijkstra.solve(g, 0);Floyd's algorithm recibes a graph and returns a matrix representing minimum travelling between every node.
import algorithms.PathFinding.Floyd;
int[][] result = Floyd.solve(g);Warshall's algorithm recibes a graph and returns a matrix representing wether there is a path between nodes.
import algorithms.PathFinding.Warshall;
boolean[][] result = Warshall.solve(g);Prim's algorithm recibes a graph, and returns an array in which every node (position) contains its parent. Therefore MST can be constructed.
import algorithms.MST.Prim;
int[] result = Prim.solve(g);Greedy Coloring algorithm recibes an undirected graph, and returns an array in which every node (position) contains its corresponding color (represented as integer). Coloring makes no sense for directed graphs.
import algorithms.Coloring.GreedyColoring;
int[] result = GreedyColoring.solve(g);Cycle Detection algorithm recibes a directed graph, and returns wether it contains a cycle.
import algorithms.Detection.CycleDetection;
boolean result = CycleDetection.solve(g);Connected Components algorithm recibes a graph, and returns a list of connected components (Edge list)
import adt.Edge;
import algorithms.Detection.ConnectedComponents;
List<List<Edge>> result = ConnectedComponents.solve(g);
totalComponents = result.size();Connected Components algorithm recibes a graph, and returns a list of bridges (Edge list). If graph is directed, algorithm solves strongly connected components problem.
import adt.Edge;
import algorithms.Detection.BridgeDetection;
List<Edge> result = BridgeDetection.solve(g);