{"id":16519471,"url":"https://github.com/root-11/graph-theory","last_synced_at":"2025-04-09T20:11:13.587Z","repository":{"id":34871610,"uuid":"172258600","full_name":"root-11/graph-theory","owner":"root-11","description":"A simple graph library","archived":false,"fork":false,"pushed_at":"2025-02-02T21:41:21.000Z","size":3607,"stargazers_count":84,"open_issues_count":12,"forks_count":20,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-09T20:11:03.908Z","etag":null,"topics":["assignment-problem","flow-problem","graph","graph-algorithms","graph-library","graph-theory","graphs","minimum-spanning-trees","shortest-path","topological-sort","tsp-solver"],"latest_commit_sha":null,"homepage":"","language":"Python","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/root-11.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-02-23T20:06:45.000Z","updated_at":"2025-02-02T21:32:43.000Z","dependencies_parsed_at":"2023-01-15T10:00:43.755Z","dependency_job_id":"a703ae0a-d2ed-47f9-8bf1-a35d0a32ab1e","html_url":"https://github.com/root-11/graph-theory","commit_stats":{"total_commits":413,"total_committers":8,"mean_commits":51.625,"dds":"0.21791767554479424","last_synced_commit":"6c70286c774d6a3bb4bbb3e1a60e684a2634dec1"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/root-11%2Fgraph-theory","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/root-11%2Fgraph-theory/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/root-11%2Fgraph-theory/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/root-11%2Fgraph-theory/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/root-11","download_url":"https://codeload.github.com/root-11/graph-theory/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248103872,"owners_count":21048245,"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":["assignment-problem","flow-problem","graph","graph-algorithms","graph-library","graph-theory","graphs","minimum-spanning-trees","shortest-path","topological-sort","tsp-solver"],"created_at":"2024-10-11T16:46:52.709Z","updated_at":"2025-04-09T20:11:13.565Z","avatar_url":"https://github.com/root-11.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# graph-theory\n![Build status](https://github.com/root-11/graph-theory/actions/workflows/python-test.yml/badge.svg)\n[![codecov](https://codecov.io/gh/root-11/graph-theory/branch/master/graph/badge.svg?token=hWbKhIXskp)](https://codecov.io/gh/root-11/graph-theory)\n[![Downloads](https://pepy.tech/badge/graph-theory)](https://pepy.tech/project/graph-theory)\n[![Downloads](https://pepy.tech/badge/graph-theory/month)](https://pepy.tech/project/graph-theory/month)\n[![PyPI version](https://badge.fury.io/py/graph-theory.svg)](https://badge.fury.io/py/graph-theory)\n\n\nA simple graph library...\u003cbr\u003e\n*... A bit like networkx, just without the overhead...*\u003cbr\u003e \n*... similar to graph-tool, without the Python 2.7 legacy...*\u003cbr\u003e\n*... with code that you can explain to your boss...*\u003cbr\u003e\n\nDetailed tutorial evolving in the [examples section](https://github.com/root-11/graph-theory/blob/master/examples/readme.md).\n\n---------------------------\nInstall:\n\n    pip install graph-theory\n\nUpgrade:\n\n    pip install graph-theory --upgrade --no-cache\n\nTesting:\n\n    pytest tests\n\n---------------------------\nImport:\n\n    import Graph\n    g = Graph()  \n\n    import Graph3d\n    g3d = Graph3D()\n\n---------------------------\n\nModules:\n\n| module | description |\n|:---|:---|\n| `from graph import Graph, Graph3D` | Elementary methods (see basic methods below) for Graph and Graph3D.|\n| `from graph import ...` | All methods available on Graph (see table below) |\n| `from graph.assignment_problem import ...` | solvers for assignment problem, the Weapons-Target Assignment Problem, ... |\n| `from graph.hash import ...` | graph hash functions: graph hash, merkle tree, flow graph hash | \n| `from graph.random import ...` | graph generators for random, 2D and 3D graphs. |\n| `from graph.transshipment_problem import ...` | solvers for the transshipment problem |\n| `from graph.traffic_scheduling_problem import ...` | solvers for the traffic jams (and slide puzzle) |\n| `from graph.visuals import ...` | methods for creating matplotlib plots |\n| `from graph.finite_state_machine import ...` | finite state machine |\n\n\nAll module functions are available from Graph and Graph3D (where applicable).\n\n| Graph | Graph3D | methods                                          | returns                                                                                                                                                                                                               | example |\n|:---:|:---:|:-------------------------------------------------|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|\n| + | + | `a in g`                                         | assert if g contains node a                                                                                                                                                                                           | |\n| + | + | `g.add_node(n, [obj])`                           | adds a node (with a pointer to object `obj` if given)                                                                                                                                                                 ||\n| + | + | `g.copy()`                                       | returns a shallow copy of `g`                                                                                                                                                                                         ||\n| + | + | `g.node(node1)`                                  | returns object attached to node 1                                                                                                                                                                                     ||\n| + | + | `g.del_node(node1)`                              | deletes node1 and all it's edges                                                                                                                                                                                      ||\n| + | + | `g.nodes()`                                      | returns a list of nodes                                                                                                                                                                                               ||\n| + | + | `len(g.nodes())`                                 | returns the number of nodes                                                                                                                                                                                           ||\n| + | + | `g.nodes(from_node=1)`                           | returns nodes with edges from node 1                                                                                                                                                                                  ||\n| + | + | `g.nodes(to_node=2)`                             | returns nodes with edges to node 2                                                                                                                                                                                    ||\n| + | + | `g.nodes(in_degree=2)`                           | returns nodes with 2 incoming edges                                                                                                                                                                                   ||\n| + | + | `g.nodes(out_degree=2)`                          | returns nodes with 2 outgoing edges                                                                                                                                                                                   ||\n| + | + | `g.add_edge(1,2,3)`                              | adds edge to g for vector `(1,2)` with value `3`                                                                                                                                                                      ||\n| + | + | `g.edge(1,2)`                                    | returns value of edge between nodes 1 and 2                                                                                                                                                                           ||\n| + | + | `g.edge(1,2,default=3)`                          | returns `default=3` if `edge(1,2)` doesn't exist. \u003cbr\u003esimilar to `d.get(key, 3)`                                                                                                                                      ||\n| + | + | `g.del_edge(1,2)`                                | removes edge between nodes 1 and 2                                                                                                                                                                                    ||\n| + | + | `g.edges()`                                      | returns a list of edges                                                                                                                                                                                               ||\n| + | + | `len(g.edges())`                                 | returns the number of edges                                                                                                                                                                                           ||\n| + | + | `g.edges(path=[path])`                           | returns a list of edges (along a path if given).                                                                                                                                                                      ||\n| + | + | `same_path(p1,p2)`                               | compares two paths to determine if they contain same sequences \u003cbr\u003eex.: `[1,2,3] == [2,3,1]`                                                                                                                          ||\n| + | + | `g.edges(from_node=1)`                           | returns edges outgoing from node 1                                                                                                                                                                                    ||\n| + | + | `g.edges(to_node=2)`                             | returns edges incoming to node 2                                                                                                                                                                                      ||\n| + | + | `g.from_dict(d)`                                 | updates the graph from a dictionary                                                                                                                                                                                   ||\n| + | + | `g.to_dict()`                                    | returns the graph as a dictionary                                                                                                                                                                                     ||\n| + | + | `g.from_list(L)`                                 | updates the graph from a list                                                                                                                                                                                         ||\n| + | + | `g.to_list()`                                    | return the graph as a list of edges                                                                                                                                                                                   ||\n| + | + | `g.shortest_path(start,end [, memoize, avoids])` | returns the distance and path for path with smallest edge sum \u003cbr\u003e If `memoize=True`, sub results are cached for faster access if repeated calls.\u003cbr\u003e If `avoids=set()`, then these nodes are not a part of the path. ||\n| + | + | `g.shortest_path_bidirectional(start,end)`       | returns distance and path for the path with smallest edge sum using bidrectional search.                                                                                                                              ||\n| + | + | `g.is_connected(start,end)`                      | determines if there is a path from start to end                                                                                                                                                                       ||\n| + | + | `g.breadth_first_search(start,end)`              | returns the number of edges and path with fewest edges                                                                                                                                                                ||\n| + | + | `g.breadth_first_walk(start,end)`                | returns a generator for a BFS walk                                                                                                                                                                                    ||\n| + | + | `g.degree_of_separation(n1,n2)`                  | returns the distance between two nodes using BFS                                                                                                                                                                      ||\n| + | + | `g.distance_map(starts,ends, reverse)`           | returns a dictionary with the distance from any start to any end (or reverse)                                                                                                                                         ||\n| + | + | `g.network_size(n1, degree_of_separation)`       | returns the nodes within the range given by `degree_of_separation`                                                                                                                                                    ||\n| + | + | `g.topological_sort(key)`                        | returns a generator that yields node in order from a non-cyclic graph.                                                                                                                                                ||\n| + | + | `g.critical_path()`                              | returns the distance of the critical path and a list of Tasks.                                                                                                                                                        | [Example](examples/solving%20search%20problems.ipynb) |\n| + | + | `g.critical_path_minimize_for_slack()`           | returns graph with artificial dependencies that minimises slack.                                                                                                                                                      | [Example](examples/solving%20search%20problems.ipynb)|\n| + | + | `g.phase_lines()`                                | returns a dictionary with the phase_lines for a non-cyclic graph.                                                                                                                                                     ||\n| + | + | `g.sources(n)`                                   | returns the source_tree of node `n`                                                                                                                                                                                   ||\n| + | + | `g.depth_first_search(start,end)`                | returns path using DFS and backtracking                                                                                                                                                                               ||\n| + | + | `g.depth_scan(start, criteria)`                  | returns set of nodes where criteria is True                                                                                                                                                                           ||\n| + | + | `g.distance_from_path(path)`                     | returns the distance for path.                                                                                                                                                                                        ||\n| + | + | `g.maximum_flow(source,sink)`                    | finds the maximum flow between a source and a sink                                                                                                                                                                    ||\n| + | + | `g.maximum_flow_min_cut(source,sink)`            | finds the maximum flow minimum cut between a source and a sink                                                                                                                                                        ||\n| + | + | `g.minimum_cost_flow(inventory, capacity)`       | finds the total cost and flows of the capacitated minimum cost flow.                                                                                                                                                  ||\n| + | + | `g.solve_tsp()`                                  | solves the traveling salesman problem for the graph.\u003cbr\u003eAvailable methods: 'greedy' (default) and 'bnb                                                                                                                ||\n| + | + | `g.subgraph_from_nodes(nodes)`                   | returns the subgraph of `g` involving `nodes`                                                                                                                                                                         ||\n| + | + | `g.is_subgraph(g2)`                              | determines if graph `g2` is a subgraph in g                                                                                                                                                                           ||\n| + | + | `g.is_partite(n)`                                | determines if graph is n-partite                                                                                                                                                                                      ||\n| + | + | `g.has_cycles()`                                 | determines if there are any cycles in the graph                                                                                                                                                                       ||\n| + | + | `g.components()`                                 | returns set of nodes in each component in `g`                                                                                                                                                                         ||\n| + | + | `g.same_path(p1,p2)`                             | compares two paths, returns True if they're the same                                                                                                                                                                  ||\n| + | + | `g.adjacency_matrix()`                           | returns the adjacency matrix for the graph                                                                                                                                                                            ||\n| + | + | `g.all_pairs_shortest_paths()`                   | finds the shortest path between all nodes                                                                                                                                                                             ||\n| + | + | `g.minsum()`                                     | finds the node(s) with shortest total distance to all other nodes                                                                                                                                                     ||\n| + | + | `g.minmax()`                                     | finds the node(s) with shortest maximum distance to all other nodes                                                                                                                                                   ||\n| + | + | `g.shortest_tree_all_pairs()`                    | finds the shortest tree for all pairs                                                                                                                                                                                 ||\n| + | + | `g.has_path(p)`                                  | asserts whether a path `p` exists in g                                                                                                                                                                                ||\n| + | + | `g.all_simple_paths(start,end)`                  | finds all simple paths between 2 nodes                                                                                                                                                                                ||\n| + | + | `g.all_paths(start,end)`                         | finds all combinations of paths between 2 nodes                                                                                                                                                                       ||\n| - | + | `g3d.distance(n1,n2)`                            | returns the spatial distance between `n1` and `n2`                                                                                                                                                                    ||\n| - | + | `g3d.n_nearest_neighbour(n1, [n])`               | returns the `n` nearest neighbours to node `n1`                                                                                                                                                                       ||\n| - | + | `g3d.plot()`                                     | returns matplotlib plot of the graph.                                                                                                                                                                                 ||\n\n\n## FAQ\n\n| want to... | doesn't work... | do instead... | ...but why? |\n|:---|:---|:---|:---|\n| have multiple edges between two nodes | `Graph(from_list=[(1,2,3), (1,2,4)]` | Add dummy nodes\u003cbr\u003e`[(1,a,3), (a,2,0),`\u003cbr\u003e` (1,b,4),(b,2,0)]` | Explicit is better than implicit. |\n| multiple values on an edge | `g.add_edge(1,2,{'a':3, 'b':4})` | Have two graphs\u003cbr\u003e`g_a.add_edge(1,2,3)`\u003cbr\u003e`g_b.add_edge(1,2,4)` | Most graph algorithms don't work with multiple values |\n|do repeated calls to shortest path|`g.shortest_path(a,b)` is slow|Use `g.shortest_path(a,b,memoize=True)` instead|memoize uses bidirectional search and caches sub-results along the shortest path for future retrievals|\n\n## Credits:\n\n- Arturo Soucase for packaging and testing. \n- Peter Norvig for inspiration on TSP from [pytudes](https://github.com/norvig/pytudes/blob/master/ipynb/TSP.ipynb).\n- Harry Darby for the mountain river map.\n- Kyle Downey for depth_scan algorithm.\n- Ross Blandford for munich firebrigade centre -, traffic jam - and slide puzzle - test cases.\n- Avi Kelman for type-tolerant search, and a number of micro optimizations.\n- Joshua Crestone for all simple paths test.\n- CodeMartyLikeYou for detecting a bug in `@memoize` \n- Tom Carroll for detecting the bug in del_edge and inspiration for topological sort.\n- Sappique for discovering bugs in `__eq__`, `copy` and `has_cycles`.\n- joshinils for discovering bug where `graph.edges(from_node=0)` was interpreted as `False`.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Froot-11%2Fgraph-theory","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Froot-11%2Fgraph-theory","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Froot-11%2Fgraph-theory/lists"}