{"id":19501016,"url":"https://github.com/grame-cncm/digraph","last_synced_at":"2025-04-25T23:30:52.115Z","repository":{"id":50408097,"uuid":"81219997","full_name":"grame-cncm/digraph","owner":"grame-cncm","description":"Very simple C++ directed graph library","archived":false,"fork":false,"pushed_at":"2022-08-01T14:47:34.000Z","size":167,"stargazers_count":76,"open_issues_count":1,"forks_count":8,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-04T05:02:23.426Z","etag":null,"topics":["cycle","dag","digraph","graph","graph-algorithms","tarjan"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/grame-cncm.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2017-02-07T14:58:51.000Z","updated_at":"2024-11-09T04:50:01.000Z","dependencies_parsed_at":"2022-08-20T10:30:34.859Z","dependency_job_id":null,"html_url":"https://github.com/grame-cncm/digraph","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grame-cncm%2Fdigraph","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grame-cncm%2Fdigraph/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grame-cncm%2Fdigraph/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grame-cncm%2Fdigraph/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/grame-cncm","download_url":"https://codeload.github.com/grame-cncm/digraph/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250912660,"owners_count":21506865,"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":["cycle","dag","digraph","graph","graph-algorithms","tarjan"],"created_at":"2024-11-10T22:10:49.789Z","updated_at":"2025-04-25T23:30:51.857Z","avatar_url":"https://github.com/grame-cncm.png","language":"C++","readme":"# Digraph library ![Build GH Status](https://github.com/grame-cncm/digraph/workflows/compile-test/badge.svg)\n\n**Digraph** is a very simple, C++ 11 template-based, directed graph library. It is not designed to be general, but to suit the needs of the (next) Faust compiler.\n\nIt is made of five files:\n\n- `arrow.hh` arrows between nodes\n- `digraph.hh` directed graphs made of nodes and arrows\n- `digraphop.hh` basic operations on directed graphs\n- `schedule.hh` various scheduling strategies\n- `stdprinting.hh` utility printing operators for pairs, vectors, maps, etc.\n\n\n## Building a digraph\nA **digraph** is a directed graph. It is composed of a set of nodes `{n_1,n_2,...}` and a set of connections (i.e. arrows) between these nodes `{(n_i -a-\u003e n_j),...}`.\n\nFor a connection `(n_i -a-\u003e n_j)`, the node `n_i` is the source of the connection, the node `n_j` is the destination of the connections, and `a` is the value of the connection. \n\nThe API to create a graph is very simple. You start by creating an empty graph:\n\n\tdigraph\u003cchar,int\u003e g;\n\nThen you add nodes and connections to the graph:\n\n\tg.add('A','B').add('B','C',1).add('D')...\n\nThe method `add()` can be used to add individual nodes like in `add('D')` or connections `add('A','B')`. In this case, the involved nodes are automatically added to the graph. There is no need to add them individually.\n\nBy default, the value of the connection is 0. To create a connection with a different value use: `add('A','B',3)`. If multiple connections between two nodes are created, only the connection with the smallest value is kept.\n\nIt is also possible to `add()` a whole graph with all its nodes and connections. For example if `g1` and `g2` are two graphs, then:\n\n\tg1.add(g2)\n\nWill add all the nodes and connections of `g2` into `g1`. If multiple connections between two nodes are created, only the connection with the smallest value is kept.\n\nPlease note that the only way to modify a digraphs is by adding nodes and connections using the `add()` method. Digraphs are otherwise immutable, and all other transformation implies the creation of a new digraph.\n\n\n\n## Accessing nodes and connections\nTo access the nodes of a graph, one uses the method `g.nodes()`. For example to iterate over the nodes:\n\n\tfor (const auto\u0026 n : g.nodes()) {\n\t\t... do something with node n ...\n\t}\n\nOnce you have a node you can iterate over its connections. To access the connections of a node we use the method `g.connections(n)`:\n\n\tfor (const auto\u0026 n : g.nodes()) {\n    \tfor (const auto\u0026 c : g.connections(n)) {\n\t   \t\t... c.first: destination node of the connection\n\t\t\t... c.second: value of the connection\n\t    }\n\t}\n\n## Algorithms and Operations on digraphs\nPlease note that the following operations never modify the graphs used as arguments.\n\n### Partition\nA partition of a digraph into strongly connected components can be obtained using the Tarjan class, an implementation of [Robert Tarjan's algorithm](https://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm)\n\n\tTarjan\u003cN\u003e t(const digraph\u003cN,A\u003e\u0026);\n\t...t.partition()...\n\nThe Tarjan class has essentially two methods:\n\n#### Partition\n\nThe method `partition()` returns the partition of the graph into strongly connected components. \n\n\tpartition() -\u003e set\u003cset\u003cN\u003e\u003e\u0026\n\nThe result is a set of set on nodes.  Each set of nodes represents a strongly connected component of the graph.\n  \n#### Cycles\nThe method `cycles()` returns the number of cycles of the graph.\n\n\tcycles() -\u003e int\n\n### Number of cycles\nThe function `cycles(mygraph)` return the number of cycles of a graph. It uses internally `Tarjan`.\n\n\tcycles(const digraph\u003cN,A\u003e\u0026) -\u003e int\n\n\n### Direct acyclic graph of graphes\nThe function `graph2dag(mygraph)` uses Tarjan to transform a graph into a direct acyclic graph (DAG):\n\n \tgraph2dag(const digraph\u003cN,A\u003e\u0026) -\u003e digraph\u003cdigraph\u003cN,A\u003e\u003e\n\nThe nodes of the resulting DAG are themselves graphs representing the strongly connected components of the input graph.\n\n### Parallelize\n\nProvided the input graph is a DAG, the function `parallelize()` transforms the input graph into a sequence of parallel nodes\n\n\tparallelize(const digraph\u003cN,A\u003e\u0026) -\u003e vector\u003cvector\u003cN\u003e\u003e\n\n### Serialize\n\nProvided the input graph is a DAG, the function `serialize()` transforms the input graph into a vector of nodes\n\n\tserialize(const digraph\u003cN,A\u003e\u0026) -\u003e vector\u003cN\u003e\n\n\n### Map nodes\nThe function `mapnodes()` creates a copy of the input graph in which each node is transformed by the function `f()`. Existing connections in the input graph are preserved in the resulting graph.\n\n\tmapnodes(const digraph\u003cN,A\u003e\u0026, f:N-\u003eM) -\u003e digraph\u003cM\u003e\n\n\n### Map connections\nThe function `mapconnections()` creates a copy of the input graph in which only the connections that satisfy the predicate `f()` are kept. \n\n\tmapconnections(const digraph\u003cN,A\u003e\u0026, f:(N,N,int)-\u003ebool) -\u003e digraph\u003cN,A\u003e\n\n### Cut high-value connections\n\nThe function `cut()` creates a copy of the input graph in which all connections of value \u003e= `d` are eliminated.\n\n\tcut(const digraph\u003cN,A\u003e\u0026, d) -\u003e digraph\u003cN,A\u003e\n\n### Split graph\n\nThe function `splitgraph()` splits a graph `G` into two subgraphs `L` and `R` according to a predicate `left()`. The nodes satisfying the predicate are copied into `L`, the others into `R`. The connections are kept, unless they concern nodes that are not in the same subgraph.\n\n\tsplitgraph(const digraph\u003cN,A\u003e\u0026 G, function\u003cbool(const N\u0026)\u003e left, digraph\u003cN,A\u003e\u0026 L, digraph\u003cN,A\u003e\u0026 R)","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgrame-cncm%2Fdigraph","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgrame-cncm%2Fdigraph","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgrame-cncm%2Fdigraph/lists"}