Simple and minimal Python module for unordered graphs
Project description
simpgraph: Minimal Python Module for Unordered Graphs
Introduction
simpgraph is a simple and minimal Python module for unordered graphs. An unordered graph, simply called a graph, is a pair of a set of elements, called vertices, and a set of (unordered) vertex pairs, called edges. In this module, a graph is assumed to have no loop, an edge whose vertices are identical with each other, and all edges have no direction.
Installation
$ pip install simpgraph
Usage
Let us import simpgraph module, create an empty graph, and add edges (and vertices if necessary).
from simpgraph import SimpGraph
# empty graph
g = SimpGraph()
# 1
# |
# 4---3
g.add_edge(4,1)
g.add_edge(4,3)
assert set(g.all_vertices()) == {1,3,4}
assert g.num_vertices() == 3
assert g.max_vertex() == 4 # not always equal to the number of vertices
assert set(g.adj_vertices(1)) == {4}
assert set(g.adj_vertices(3)) == {4}
assert set(g.adj_vertices(4)) == {1,3}
assert g.num_edges() == 2
g.add_edge(1,4) # the edge between 1 and 4 already added and nothing done
assert g.num_edges() == 2
As above, add_edge(u,v) adds, to the edge set, the edge between vertices u,v given in its arguments in an arbitrary order. New vertices, if included in the added edge, are also added to the vertex set. If the edge is present in the edge set, the method does nothing.
#
# 1 2
# |
# 4---3
g.add_vertex(2)
assert set(g.all_vertices()) == {1,2,3,4}
assert set(g.all_edges()) == {(1,4),(3,4)}
assert set(g.adj_vertices(2)) == set() # isolated vertex
assert g.num_vertices() == 4
g.add_vertex(1) # vertex 1 already present and nothing done
assert g.num_vertices() == 4
Similary, add_vertex(u) adds vertex u to the vertex set. If it is present in the vertex set, nothing is done.
# before after
# 1 2 1 2
# | => |
# 4---3 4
g.discard_vertex(3)
assert set(g.all_vertices()) == {1,2,4}
assert set(g.all_edges()) == {(1,4)}
assert g.num_vertices() == 3
g.discard_vertex(3) # vertex 3 already discarded and nothing done
assert g.num_vertices() == 3
As above, discard_vertex(u) discards not only vertex u but also all edges incident to u. If the vertex is not present in the graph, nothing is done and no exception raises
# before after
# 1 2 1 2
# | =>
# 4 4
g.discard_edge(1,4)
assert set(g.all_vertices()) == {1,2,4}
assert set(g.all_edges()) == set()
assert g.num_edges() == 0
g.discard_edge(1,4)
assert g.num_edges() == 0
Similary, discard_edge(u,v) discards the edge between u and v If the edge is not present in the graph, nothing is done and no exception raises.
# before after
# 1 2
# =>
# 4
g.clear()
assert g.num_vertices() == 0
assert g.num_edges() == 0
clear() clears all variables of graph g and makes it an empty graph.
# 1 2:B
# |A |
# 4 3:C
g.add_edge(2,3)
g.add_edge(1,4, label="A")
g.add_vertex(2, label="B")
g.add_vertex(3, label="C")
assert g.vertex_label(1) is None
assert g.vertex_label(2) == "B"
assert g.vertex_label(3) == "C"
assert g.vertex_label(4) is None
assert g.edge_label(2,3) is None
assert g.edge_label(1,4) == "A"
# before after
# 1 2:B 1 2:B
# |A | => |A |D
# 4 3:C 4 3:C
g.add_edge(2,3, label="D") # label updated
Optional labels can be assigned to vertices and edges, and the graph can be rendered as follows.
g.render(filename="sample", format="png")
As a result, sample.png will be generated. The arguments of render() are the same as those of render() of Graphviz. See User Guide of Graphviz .
# 1 2:B
# |A |D
# 4 3:C
g.add_edge(1,2, label="D")
with open("sample.col", "w") as f:
f.write(f"p {g.max_vertex()} {g.num_edges()}\n")
for u in g.all_vertices():
f.write(f"n {u}\n")
for u,v in g.all_edges():
f.write(f"e {u} {v}\n")
f.seek(0)
gg = SimpGraph.read(filename="sample.col", format="DIMACS")
assert(g == gg) # the vertex set and the edge set are determined to be equal.
# However, no label is preserved in the constructed graph.
# 1 2
# | |
# 4 3
assert gg.vertex_label(2) is None
assert gg.vertex_label(3) is None
assert gg.edge_label(1,4) is None
assert gg.edge_label(2,3) is None
As above, a graph can be wrote to an external file in DIMACS graph format, but vertex labels and edge labels are not recorded. Class method SimpGraph.read() reads such a file and construct a graph, and the resulted graph g is equal to the original graph g in that they have the same vertex set and the same edge set. (Note: the vertex labels and the edge labels are not considered).
Bugs/Requests/Discussions
Please report bugs and requests from GitHub Issues , and ask questions from GitHub Discussions .
License
Please see LICENSE .
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 simpgraph-1.0.0.tar.gz
.
File metadata
- Download URL: simpgraph-1.0.0.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2e355d9d22f6604b7e3ec8addc44434e2834cb1139dad91798e930170d8ba691 |
|
MD5 | 41a00a4cbd0b0e29c46b66df0103589e |
|
BLAKE2b-256 | 0cf0ecf7be20e5e5cebda4981f30a706697ade243bc3353491793a178b77a80c |
File details
Details for the file simpgraph-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: simpgraph-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 15ce625e35d91b5b7ed69b317e5574701a0b3eecf5efaf19d5d0f9ffd77563cf |
|
MD5 | 0832f311b61c1eccb98a8648714c6ed0 |
|
BLAKE2b-256 | 582e75e94d3047a503aff2debd942f612027523033241451ef1cfa8785f4305d |