Graph analysis – the lazy (evaluation) way: Analysis on the fly,for graphs, that are computed and/or adapted on the fly.
Project description
NoGraphs: Graph analysis on the fly
NoGraphs simplifies the analysis of graphs that can not or should not be fully computed, stored or adapted, e.g. infinite graphs, large graphs and graphs with expensive computations. (Here, the word graph denotes the thing with vertices and edges, not with diagrams.)
The approach: Graphs are computed and/or adapted in application code on the fly (when needed and as far as needed). Also, the analysis and the reporting of results by the library happen on the fly (when, and as far as, results can already be derived).
Think of it as graph analysis - the lazy (evaluation) way.
Feature overview
Algorithms: DFS, BFS, topological search, Dijkstra, A* and MST.
Flexible graph notion: Infinite directed multigraphs with loops and attributes (this includes multiple adjacency, cycles, self-loops, directed edges, weighted edges and edges with application specific attributes). Your vertices can be nearly anything. Currently, all algorithms are limited to locally finite graphs (i.e., a vertex has only finitely many outgoing edges).
Results: Reachability, depth, distance, paths and trees. Paths can be calculated with vertices or edges or attributed edges and can be iterated in both directions.
Flexible API: It eases operations like graph pruning, graph product and graph abstraction, the computation of search-aware graphs and traversals of vertex equivalence classes on the fly. It is even possible to replace some of the internal data structures and to interfere with them during the search.
Implementation: Pure Python, no dependencies, runtime and memory performance have been goals.
Documentation
Example
Our graph is directed, weighted and has infinitely many edges. These edges are defined in application code by the following function. For a vertex i (first parameter, here: an integer), it yields the edges that start at i as tuples (end_vertex, edge_weight). What a strange graph - we do not know how it looks like…
>>> def next_edges(i, _):
... j = (i + i // 6) % 6
... yield i + 1, j * 2 + 1
... if i % 2 == 0:
... yield i + 6, 7 - j
... elif i > 5:
... yield i - 6, 1
We would like to find out the distance of vertex 5 from vertex 0, i.e., the minimal necessary sum of edge weights of any path from 0 to 5, and (one of) the shortest paths from 0 to 5.
We do not know which part of the graph is necessary to look at in order to find the shortest path and to make sure, it is really the shortest. So, we use the traversal strategy TraversalShortestPaths of NoGraphs. It implements the well-known Dijkstra graph algorithm in the lazy evaluation style of NoGraphs.
>>> import nographs as nog
>>> traversal = nog.TraversalShortestPaths(next_edges)
We ask NoGraphs to traverse the graph starting at vertex 0, to calculate paths while doing so, and to stop when visiting vertex 5.
>>> traversal.start_from(0, build_paths=True).go_to(5)
5
The state data of this vertex visit contains our results:
>>> traversal.distance
24
>>> traversal.paths[5]
(0, 1, 2, 3, 4, 10, 16, 17, 11, 5)
We learn that we need to examine the graph at least till vertex 17 to find the shortest path from 0 to 5. It is not easy to see that from the definition of the graph…
A part of the graph, the vertices up to 41, is shown in the following picture. Arrows denote directed edges. The edges in red show shortest paths from 0 to other vertices.
And now?
Can you imagine…
An infinite generator of primes, defined by just a graph and a call to a standard graph algorithm?
Or a graph that defines an infinite set of Towers of Hanoi problems in a generic way, without fixing the number of towers, disk sizes, and the start and goal configuration - and a specific problem instance is solved by just one library call?
Or graphs that are dynamically computed based on other graphs, or on analysis results about other graphs, or even on partial analysis results from already processed parts of the same graph?
Let’s build it.
Welcome to NoGraphs!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file nographs-2.5.0.dev1.tar.gz
.
File metadata
- Download URL: nographs-2.5.0.dev1.tar.gz
- Upload date:
- Size: 89.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f27555f8c9f03cc33cbc9da14d2238e9f224bec30a2fbb81cb55743034feb7f3 |
|
MD5 | f3ad09b2aeb84771bcad7f41974dd8a3 |
|
BLAKE2b-256 | 0888833ef87dc108d26aadab565db86db6b463d5d086776c193230ad29a83ccb |
File details
Details for the file nographs-2.5.0.dev1-py3-none-any.whl
.
File metadata
- Download URL: nographs-2.5.0.dev1-py3-none-any.whl
- Upload date:
- Size: 24.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5f67396a14c9cf5d90ee303934ce9a3767890681d6428a3b387934e055c3d719 |
|
MD5 | 9e5ea026f9480a0e76886e62a6b0793f |
|
BLAKE2b-256 | e61bfce3347be17dc2ec4c0496639dfd10b990509c4c4cc99ddd6c7837383335 |