-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphical_pos.py
More file actions
55 lines (44 loc) · 1.91 KB
/
graphical_pos.py
File metadata and controls
55 lines (44 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import networkx as nx
def graphical_pos(G, width=1., vert_gap = 0.2, vert_loc = 0, xcenter = 0.5):
"""
Creates a tree based representation of a graph from the bottom up specifically for the Collatz Conjecture, starting at node 4.
Example:
5 32
\\ /
16
|
8
|
4
/ \\
2 - 1
G: the graph
width: horizontal space allocated for this branch - avoids overlap with other branches
vert_gap: gap between levels of hierarchy
vert_loc: vertical location of root
xcenter: horizontal location of root
"""
# Root is always set to the graph value of 4
root = 4
def _graphical_pos(G, root, width=1., vert_gap = 0.2, vert_loc = 0, xcenter = 0.5, pos = None, parent = None):
'''
pos: a dict saying where all nodes go if they have been assigned
parent: parent of this branch. - only affects it if non-directed
'''
if pos is None:
pos = {root:(xcenter,vert_loc)}
else:
pos[root] = (xcenter, vert_loc)
children = list(G.neighbors(root))
if not isinstance(G, nx.DiGraph) and parent is not None:
children.remove(parent)
if len(children)!=0:
dx = width/len(children)
nextx = xcenter - width/2 - dx/2
for child in children:
nextx += dx
pos = _graphical_pos(G,child, width = dx, vert_gap = vert_gap,
vert_loc = vert_loc-vert_gap, xcenter=nextx,
pos=pos, parent = root)
return pos
return _graphical_pos(G, root, width, vert_gap, vert_loc, xcenter)