Skip to main content

A time-linear Python implementation of Walker's algorithm for level-based tree layouting/drawing

Project description

Walker Layout

This package provides a way to generate level-based tree layouts (i.e. (x, y) coordinates for each node in a tree graph, such that nodes with the same depth from the root node are placed on the same y-height) using Walker's algorithm (https://onlinelibrary.wiley.com/doi/abs/10.1002/spe.4380200705) including improvements by Buchheim et al. (2002) to make it linear in time (https://link.springer.com/content/pdf/10.1007/3-540-36151-0_32.pdf). One can then use these layouts to draw the graph in the xy-plane.

You can either directly supply a networkx graph:

from walkerlayout import WalkerLayouting
import networkx as nx

G = nx.Graph()
G.add_nodes_from(range(0,8))
G.add_edges_from([(0,1), (0,2), (0,3), (1,4), (3,5), (3,6), (5,7)])

layout = WalkerLayouting.layout_networkx(graph=G, root_node=0)

nx.draw(G, pos=layout, with_labels=True)

Or define the tree purely in WalkerLayouting using unique keys:

from walkerlayout import WalkerLayouting
#   0
#  /|\
# 1 2 3
# |   /\
# 4  5  6
#    |
#    7
tree = WalkerLayouting(root_key=0)
tree.add(parent_key=0, child_key=1)
tree.add(parent_key=0, child_key=3)
tree.add(parent_key=0, child_key=2, index=1)
tree.add(parent_key=3, child_key=6)
tree.add_from([(1,4), (3,5,0), (5, 7), (5, 8), (8, 9)]) # (parent, child [,index])
tree.remove(8)

layout = tree.get_layout()
# {
#  0: (0.0, 0.0), 
#  1: (-1.0, 1.0), 
#  2: (0.0, 1.0), 
#  3: (1.0, 1.0), 
#  4: (-1.0, 2.0), 
#  5: (0.5, 2.0), 
#  6: (1.5, 2.0), 
#  7: (0.5, 3.0)
# }

Keys can be of any type. However, you can type-hint them to a fixed type:

tree: WalkerLayouting[str] = WalkerLayouting(root_key='root')

LICENSE (MIT)

Copyright 2023 Elias Foramitti

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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

walkerlayout-1.0.2.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

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

walkerlayout-1.0.2-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file walkerlayout-1.0.2.tar.gz.

File metadata

  • Download URL: walkerlayout-1.0.2.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.5

File hashes

Hashes for walkerlayout-1.0.2.tar.gz
Algorithm Hash digest
SHA256 f97a16015bac0ac9467a2c5666b92e014cd779c678dee2a12fe36719ead1b480
MD5 96149604f0d718874bc4aa12deb7236e
BLAKE2b-256 19e658a459cedcc42d90ac1fc0575d09acd6d3b6857a5fcdff1501483ecca5c1

See more details on using hashes here.

File details

Details for the file walkerlayout-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: walkerlayout-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.5

File hashes

Hashes for walkerlayout-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8afc73ce2460d7ae122ae55d318382aa157d94092c098d1dc0ed124b28b6e4ca
MD5 ccfed1b81888c6a75a5aea7000f81942
BLAKE2b-256 ffd2d88c18ca4c84c1a9c31cc468bf9cb94302c877322eca15389d6f28a97dd9

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