{"id":24151097,"url":"https://github.com/childsish/graph","last_synced_at":"2026-02-18T01:02:39.780Z","repository":{"id":142010935,"uuid":"94110861","full_name":"childsish/graph","owner":"childsish","description":"A simple header library of basic C++ graph classes","archived":false,"fork":false,"pushed_at":"2020-02-12T07:05:47.000Z","size":218,"stargazers_count":10,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-09T02:04:26.623Z","etag":null,"topics":["graphs","header-only","library"],"latest_commit_sha":null,"homepage":"","language":"C++","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/childsish.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2017-06-12T15:20:04.000Z","updated_at":"2023-07-08T08:26:18.000Z","dependencies_parsed_at":null,"dependency_job_id":"ce5e97e9-12a6-4913-b525-2e070fa61e36","html_url":"https://github.com/childsish/graph","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/childsish/graph","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/childsish%2Fgraph","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/childsish%2Fgraph/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/childsish%2Fgraph/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/childsish%2Fgraph/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/childsish","download_url":"https://codeload.github.com/childsish/graph/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/childsish%2Fgraph/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279000773,"owners_count":26082906,"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","status":"online","status_checked_at":"2025-10-09T02:00:07.460Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["graphs","header-only","library"],"created_at":"2025-01-12T09:13:55.924Z","updated_at":"2025-10-09T02:09:20.604Z","avatar_url":"https://github.com/childsish.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# graph\n\n[![Build Status](https://travis-ci.org/childsish/graph.svg?branch=master)](https://travis-ci.org/childsish/graph)\n\n*graph* is a simple header library of basic C++ graph (as in networks, not plots) classes.\nA couple of basic classes are implemented that allow most other graph types to be simulated; `Graph` and `PartiteGraph`.\n \n`Graph` is a directed graph with only one edge per vertex pair and no edge weights.\nVertices do not need to be predefined and can be automatically created when adding edges.\nCurrently cyclic/acyclic restrictions needs to be enforced by the user.\n  \n`PartiteGraph` is a subclass of `Graph` that splits vertices into separate partitions and prevents vertices from belonging to multiple partitions or edges among vertices from the same partition.\n\n## `Graph` example\n\n### Specifying type\n\nWhen declaring a `Graph`, the type of the vertex identifiers needs to be defined using the template parameter.\nInternally vertices are stored in an [`std::unordered_map`][1] and thus must follow the specifications outlined for [`UnorderedAssociativeContainers`][2].\n\n```cpp\n#include \u003cstring\u003e\n#include \u003cgraph/Graph.h\u003e\n\ngraph::Graph\u003cint\u003e int_graph;\ngraph::Graph\u003cstd::string\u003e string_graph;\n```\n\n### Adding edges\n\nEdges are added using `Graph::add_edge`. Unlike the `PartiteGraph`, vertices will be created automatically if they don't already exist. \n\n```cpp\n#include \u003cgraph/Graph.h\u003e\n\ngraph::Graph\u003cint\u003e graph;\ngraph.add_edge(0, 1);\ngraph.add_edge(0, 2);\n\ngraph.get_children(0);  // {0, 1, 2}\n```\n\n### Querying neighbours\n\n`Graph` has five methods for getting the various types of neighbours:\n* `Graph::get_children`\n* `Graph::get_parents`\n* `Graph::get_neightbours`\n* `Graph::get_descendents`\n* `Graph::get_ancestors` \n\n```cpp\n#include \u003cstring\u003e\n#include \u003cgraph/Graph.h\u003e\n\ngraph::Graph\u003cstd::string\u003e\ngraph.add_edge(\"A\", \"B\");\ngraph.add_edge(\"B\", \"C\");\ngraph.add_edge(\"B\", \"D\");\n\ngraph.get_children(\"B\");     // {\"C\", \"D\"}\ngraph.get_parents(\"B\");      // {\"A\"}\ngraph.get_neightbours(\"B\");  // {\"A\", \"C\", \"D\"}\ngraph.get_descendents(\"A\");  // {\"B\", \"C\", \"D\"}\ngraph.get_ancestors(\"C\")     // {\"A\", \"B\"}\n```\n\n## `PartiteGraph` example\n\n### Specifying types\n\nWhen declaring a `PartiteGraph`, the type of the vertex identifiers and all partitions need to be defined.\n\n```cpp\n#include \u003cgraph/PartiteGraph.h\u003e\n\nstruct Vertex { std::string name; };\nstruct Edge { float weight; };\n\ngraph::PartiteGraph\u003cint, Vertex, Edge\u003e graph;\n```\n\n### Adding edges\n\nEdges are also added using `PartiteGraph::add_edge`. However, unlike `Graph`, vertices are not created automatically and first need to be added using `PartiteGraph::add_vertex`.\nAdditionally, an edge can not be created between two vertices from the same partition.\n\n```cpp\n#include \u003cgraph/PartiteGraph.h\u003e\n\nenum Partition { vertex_partition, edge_partition };\nstruct Vertex { std::string name; };\nstruct Edge { float weight; };\n\n// Add vertices\ngraph::PartiteGraph\u003cint, Vertex, Edge\u003e graph;\ngraph.add_vertex\u003cvertex_partition\u003e(0, {\"A\"});\ngraph.add_vertex\u003cvertex_partition\u003e(1, {\"B\"});\ngraph.add_vertex\u003cvertex_partition\u003e(2, {\"C\"});\ngraph.add_vertex\u003cvertex_partition\u003e(3, {\"D\"});\ngraph.add_vertex\u003cedge_partition\u003e(4, {1.0});\ngraph.add_vertex\u003cedge_partition\u003e(5, {0.5});\ngraph.add_vertex\u003cedge_partition\u003e(6, {0.0});\n\n// Add edges\ngraph.add_edge(0, 4);\ngraph.add_edge(4, 1);\ngraph.add_edge(1, 5);\ngraph.add_edge(1, 6);\ngraph.add_edge(5, 2);\ngraph.add_edge(6, 3);\n```\n\n## Different graph types\n\n* **Undirected graph.** Simply use `Graph::get_neighbours`.\n* **Multigraph.** Use a `PartiteGraph` with two partitions. One partition is for vertex vertices and the other is for\n edge vertices. This will allow vertices in the vertex partition to be connected with each other via vertices in the\n edge partition. A subclass could be used to simplify getting neighbours. See example [Adding edges](#adding-edges-1).\n* **Edge weights.** Use a `PartiteGraph` and add edge weights to one of the partition types. Also see example\n[Adding edges](#adding-edges-1).\n\n\n[1]: http://en.cppreference.com/w/cpp/container/unordered_map\n[2]: http://en.cppreference.com/w/cpp/concept/UnorderedAssociativeContainer\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchildsish%2Fgraph","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchildsish%2Fgraph","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchildsish%2Fgraph/lists"}