r"""
Proximity graphs and their nesting
==================================

A graph turns a cloud of samples into a structure: who is whose neighbor.
The classical proximity graphs differ in how strict "neighbor" is:

* the nearest neighbor graph (NNG) links each sample to its nearest neighbor;
* the minimal spanning tree (MST) connects all samples with minimal total length;
* the relative neighborhood graph (RNG) links two samples when no third
  sample is closer to both of them;
* the Gabriel graph (GG) links two samples when the ball spanned by them as a
  diameter is empty;
* the Delaunay triangulation (DT) is the dual of the Voronoi diagram.

Each graph contains the previous one, NNG ⊆ MST ⊆ RNG ⊆ GG ⊆ DT, so the
chain moves from "only the most obvious relations" to "every geometrically
possible relation".
"""

import matplotlib.pyplot as plt

from graphclust import graphs
from graphclust.datasets import make_variety
from graphclust.plotting import plot_graph

# [snippet:graphs]
X, y = make_variety()
G = {
    "nearest neighbor graph": graphs.nearest_neighbor_graph(X),
    "minimal spanning tree": graphs.minimum_spanning_tree(X),
    "relative neighborhood graph": graphs.relative_neighborhood_graph(X),
    "Gabriel graph": graphs.gabriel_graph(X),
    "Delaunay triangulation": graphs.delaunay_graph(X),
    "symmetric 5-NN graph": graphs.knn_graph(X, k=5),
}
print(graphs.neighborhood_chain(X))
# [/snippet:graphs]

fig, axes = plt.subplots(2, 3, figsize=(12, 7.6))
for ax, (name, A) in zip(axes.flat, G.items()):
    plot_graph(X, A, ax=ax, node_size=10, title=f"{name} ({A.nnz // 2} edges)")
    ax.set_xticks([])
    ax.set_yticks([])
plt.tight_layout()
