Skip to main content

A simple and minimal graph library based on Python dictionaries.

Project description

Kladia 🌿

Python

A simple and minimal graph library based on Python dictionaries

Kladia is a library that provides a simple and easy way to work with graphs data structures in Python, by representing them as dictionaries. Kladia graphs are simple enough to be used and adapted to any other libraries and frameworks, including Pandas, Numpy, Scikit-learn, etc.

Installation

pip install kladia

How it works?

Using ONLY Python dictionaries we can easily construct a simple two node directed graph in 4 steps:

  1. Create a graph {'graph': None}
  2. Add a node {'graph': {0: None}}
  3. Add another node {'graph': {0: None, 1: None}}
  4. Add a link from node 0 to node 1 {'graph': {0: {1: None}, 1: None}}

Then, we can replace the None values with desired properties or data. For example, we can add a node with a property {'graph': {0: {'color': 'red'}}} or a link with a property {'graph': {0: {1: {'weight': 1.0}}}}.

Kladia helps you to create and manipulate these graphs in a simple way.

from kladia.graph import graph

g = graph()  # create an empty graph
g.add(0, {'color': 'red'})  # add a node with a property
g.add(1, {'color': 'blue'})  # add another node with a property
g.add((0, 1), {'weight': 1.0})  # add a looping link with a property

print(g.to_dict())

Which will print (without the comments):

{
    'graph': {
        0: {                    # node 0
            'color': 'red',     # node 0 property
            1: {'weight': 1.0}  # link to node 1
        }, 
        1: {                    # node 1
            'color': 'blue'     # node 1 property
        }
    }
}

Just remember, color and weight are just examples of properties and are NOT implemented in kladia.graph. But, this is a very simple, minimal and flexible library, so you can adapt it to your needs. More examples can be found in the examples folder.

Why everything is a dictionary?

For convenience, the Graph class only manage dictionaries for graphs, nodes and links. By design Properties has no restrictions whatsoever.

Using dictionaries and integer labels, we can traverse the graph faster than using an object-oriented approach. You can work only with the structure of the graph, without worrying about the properties for faster operations. For example, this is a directed binary tree of 3 levels:

{
    'graph': {
        0: {1: None, 2: None},      # Node 0 is linked to nodes 1 and 2
        1: {3: None, 4: None},      # Node 1 is linked to nodes 3 and 4
        2: {5: None, 6: None},      # Node 2 is linked to nodes 5 and 6
        3: None,                    # Node 3 is a leaf
        4: None,                    # Node 4 is a leaf
        5: None,                    # Node 5 is a leaf
        6: None                     # Node 6 is a leaf
    }
}

Feel free to review the notes in NOTES.rst for more information about the graph structure.

Other features:

Kladia graphs are:

  • Up to order 2 nested Python dictionaries (dicts, of dicts, of dicts)
  • Graphs, nodes and links can have custom properties dictionary to store any kind of data, such as weights, labels, etc.
  • Represented as a dictionary of nodes (vertices), where each node is a dictionary of links (edges), commonly known as adjacency list
  • Directed Graphs or DiGraphs by default. To create Bidirected graphs, just add the same link in both directions.
  • Weighted graphs can be represented by adding a property to the link, like {'weight': 1.0}
  • MultiGraphs can be represented by adding a property to the link, like {type: 'B', name: 'user1'}
  • Undirected graphs can be represented by adding the same link in both directions or by adding a property to the link, like {'undirected': True}

For example, here is an Undirected Graph with 3 nodes:

{
    'graph': {
        0: {1: {'undirected': True}}, # Node 0 is undirectedly linked to node 1
        1: {2: {'undirected': True}}, # Node 1 is undirectedly linked to node 2
        2: {0: {'undirected': False}}, # Node 2 is directedly linked to node 0
        'graph_type': 'undirected'     # This flag helps to traverse the graph looking for the "undirected" property on each link
    }
}

Please, take this example as a suggestion. graph_type and undirected are not implemented as special keys in kladia.graph. But, this is a very simple, minimal and flexible library, so you can adapt it to your needs.

Why the name Kladia?

Kladia 🌿 is the latinization of the greek word κλαδιά, that means branch or twig. It is also the name of a genus of plants that includes the olive tree.

TODOs

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kladia-0.0.1.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kladia-0.0.1-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file kladia-0.0.1.tar.gz.

File metadata

  • Download URL: kladia-0.0.1.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.9

File hashes

Hashes for kladia-0.0.1.tar.gz
Algorithm Hash digest
SHA256 17f2734cbb6ba17a8a0f41b23d2ddb6e6a7fa257e1c8fe9fb63b8e06e9d07192
MD5 e8d6475b295c76829a0e973aa1201745
BLAKE2b-256 a75801092ff0e4766d2f42b25b5e05051046fa4de388fae4936441f90186b806

See more details on using hashes here.

File details

Details for the file kladia-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: kladia-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.9

File hashes

Hashes for kladia-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 873a9b8e7a621f0be7518dc72be34e23e5578909ec2f39b110f04d2b2faaace0
MD5 71c51fea3d6c9b7641505be1ac60a8d7
BLAKE2b-256 c792756b174079c5bd9716cfa4d78d601a67985a7364c9d929a7f5cdcbedb1c6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page