File: plot_lca.py

package info (click to toggle)
networkx 3.4.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,700 kB
  • sloc: python: 105,310; xml: 544; makefile: 131; javascript: 120; sh: 34
file content (59 lines) | stat: -rw-r--r-- 1,588 bytes parent folder | download
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
56
57
58
59
"""
=======================
Lowest Common Ancestors
=======================

Compute and visualize LCA for node pairs

In a randomly generated directed tree, the lowest common
ancestors are computed for certain node pairs. These node
pairs and their LCA are then visualized with a chosen
color scheme.

"""

import networkx as nx
import matplotlib.pyplot as plt

G = nx.DiGraph(
    [
        (0, 2),
        (2, 7),
        (2, 11),
        (5, 6),
        (6, 9),
        (6, 8),
        (7, 1),
        (7, 3),
        (8, 12),
        (11, 10),
        (11, 5),
        (12, 4),
        (12, 13),
    ]
)
pos = nx.nx_agraph.graphviz_layout(G, prog="dot")

# Compute lowest-common ancestors for certain node pairs
ancestors = list(nx.all_pairs_lowest_common_ancestor(G, ((1, 3), (4, 9), (13, 10))))

# Create node color and edge color lists
node_colors = ["#D5D7D8" for _ in G]
node_edge_colors = ["None" for _ in G]
node_seq = list(G.nodes)
clr_pairs = (("cyan", "tab:blue"), ("moccasin", "tab:orange"), ("lime", "tab:green"))
for (children, ancestor), (child_clr, anc_clr) in zip(ancestors, clr_pairs):
    for c in children:
        node_colors[node_seq.index(c)] = child_clr
    node_colors[node_seq.index(ancestor)] = anc_clr
    node_edge_colors[node_seq.index(ancestor)] = "black"

# Plot tree
plt.figure(figsize=(15, 15))
plt.title("Visualize Lowest Common Ancestors of node pairs")
nx.draw_networkx_nodes(
    G, pos, node_color=node_colors, node_size=2000, edgecolors=node_edge_colors
)
nx.draw_networkx_edges(G, pos)
nx.draw_networkx_labels(G, pos, font_size=15)
plt.show()