{"id":13528877,"url":"https://github.com/earlygrey/simple-graphs","last_synced_at":"2025-04-01T14:33:30.743Z","repository":{"id":37896528,"uuid":"274937205","full_name":"earlygrey/simple-graphs","owner":"earlygrey","description":"A simple graph library for java","archived":false,"fork":false,"pushed_at":"2023-01-10T19:25:32.000Z","size":397,"stargazers_count":44,"open_issues_count":1,"forks_count":8,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-11-02T15:36:32.453Z","etag":null,"topics":["graph","graph-algorithms","graphs","java","lightweight","no-dependencies"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/earlygrey.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-06-25T14:26:37.000Z","updated_at":"2024-10-05T09:06:57.000Z","dependencies_parsed_at":"2023-02-08T20:00:54.528Z","dependency_job_id":null,"html_url":"https://github.com/earlygrey/simple-graphs","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/earlygrey%2Fsimple-graphs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/earlygrey%2Fsimple-graphs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/earlygrey%2Fsimple-graphs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/earlygrey%2Fsimple-graphs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/earlygrey","download_url":"https://codeload.github.com/earlygrey/simple-graphs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246655401,"owners_count":20812627,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["graph","graph-algorithms","graphs","java","lightweight","no-dependencies"],"created_at":"2024-08-01T07:00:27.018Z","updated_at":"2025-04-01T14:33:25.716Z","avatar_url":"https://github.com/earlygrey.png","language":"Java","funding_links":[],"categories":["Resources"],"sub_categories":["Algorithms and AI"],"readme":"\n[![](https://jitpack.io/v/earlygrey/simple-graphs.svg)](https://jitpack.io/#space.earlygrey/simple-graphs)\n\n# Simple Graphs\n\nSimple graphs is a Java library containing basic graph data structures and algorithms. It is lightweight, fast, and intuitive to use.\n\nIt 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.\n\nAlgorithms implemented are:\n- [breadth first search](https://en.wikipedia.org/wiki/Breadth-first_search)\n- [depth first search](https://en.wikipedia.org/wiki/Depth-first_search)\n- shortest path using the [A* search algorithm](https://en.wikipedia.org/wiki/A*_search_algorithm) ([Dijkstra's algorithm](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm) when no heuristic is provided)\n- [cycle detection](https://en.wikipedia.org/wiki/Cycle_(graph_theory)#Cycle_detection)\n- [topological sort](https://en.wikipedia.org/wiki/Topological_sorting) for directed graphs (performed in-place)\n- [minimum weight spanning tree](https://en.wikipedia.org/wiki/Minimum_spanning_tree) for undirected graphs using [Kruskal's algorithm](https://en.wikipedia.org/wiki/Kruskal%27s_algorithm)\n\nSimple 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](https://github.com/libgdx/libgdx).\n\nIf 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](https://jgrapht.org/). It essentially does everything this library does, plus a lot more (though it's not really \"simple\").\n\n---\n\n## Installation\nTo include Simple Graphs in your project, follow the instructions on the [jitpack website](https://jitpack.io/#space.earlygrey/simple-graphs).\n\nIf you use GWT, your .gwt.xml file should inherit simple-graphs using:\n\n```xml\n\u003cinherits name=\"simple_graphs\" /\u003e\n```\n\n\n## Usage\n\n### Construction\n\nUsage looks something like:\n```java\nGraph\u003cInteger\u003e graph = new UndirectedGraph\u003c\u003e(); // or new DirectedGraph\u003c\u003e();\n\nfor (int i = 0; i \u003c 5; i++) {\n  graph.addVertex(i);\n}\nboolean containsVertex = graph.contains(0);\n\ngraph.addEdge(1, 2);\nboolean containsEdge = graph.edgeExists(1, 2)\n```\n\n### Edge Weights\n\nEdge weights can be assigned a fixed float value, or can be dynamically calculated via a `WeightFunction`.\n\n```java\n// fixed value\ngraph.addEdge(vertexA, vertexB, 1.5f);\ngraph.getEdge(vertexA, vertexB).setWeight(1.5f);\ngraph.setDefaultEdgeWeight(1.5f);\n\n// weight functions (assuming vertex class implements a distance method dst())\ngraph.addEdge(vertexA, vertexB, (a, b) -\u003e a.dst(b));\ngraph.getEdge(vertexA, vertexB).setWeight((a, b) -\u003e a.dst(b));\ngraph.setDefaultEdgeWeight((a, b) -\u003e a.dst(b);\n```\n\n### Collections\nYou can obtain `Collection\u003cV\u003e`s of the vertices and edges:\n```java\nCollection\u003cInteger\u003e vertices = graph.getVertices();\nCollection\u003cEdge\u003cInteger\u003e\u003e edges = graph.getEdges();\n```\nNeither 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:\n\n```java\ngraph.sortVertices((v1, v2) -\u003e ...);\n```\nIf 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\u003c\u003e(graph.getVertices())`.\n\n### Algorithms\n\nTo 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.\n```java\nV u, v;\nGraph\u003cV\u003e graph;\nUndirectedGraph\u003cV\u003e undirectedGraph;\nDirectedGraph\u003cV\u003e directedGraph;\n\nPath\u003cV\u003e path = graph.algorithms().findShortestPath(u, v);\nUndirectedGraph\u003cV\u003e tree = undirectedGraph.algorithms().findMinimumWeightSpanningTree();\ndirectedGraph.algorithms().topologicalSort();\n```\n\nAdditionally, 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:\n```java\ngraph.algorithms().breadthFirstSearch(u, step -\u003e System.out.println(\"processing \" + step.vertex()));\n\nGraph\u003cInteger\u003e tree = graph.createNew();\ngraph.algorithms().depthFirstSearch(u, step -\u003e {\n    tree.addVertex(step.vertex());\n    if (step.count() \u003e 0) {\n        tree.addEdge(step.edge());\n    }\n    if (step.depth() \u003e 4) {\n        step.ignore();\n    }\n});\n```\n\n## Technical Considerations\n\nWhile 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`.\n\n---\n## Wiki\n\n[See the wiki](https://github.com/earlygrey/simple-graphs/wiki) for more info.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fearlygrey%2Fsimple-graphs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fearlygrey%2Fsimple-graphs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fearlygrey%2Fsimple-graphs/lists"}