It is difficult to see if a set of paths covers a wide variety of information or if they all just follow the same route. This makes it hard to measure how much unique ground is being covered.
It looks at the unique items in different paths and calculates a score based on how much they differ from one another. It provides a single number that represents the variety across a network.
It provides a clear way to measure how much unique information is being explored across different paths.
It was run inside an isolated container with no network access. This is the exact command and the real output it produced — captured process output, not written by a model.
$ python3 path_diversity.py Path Diversity Score (Jaccard Distance): 0.3333
A screenshot of that run.
A clean run proves this does what is shown above, in a CPU-only sandbox. It is a small research demo — not a production tool, and nothing here was published anywhere.
All of it — 64 lines, one file, standard library only.
import sys
from itertools import combinations
def jaccard_distance(set1, set2):
"""Calculate Jaccard Distance between two sets"""
intersection = set1.intersection(set2)
union = set1.union(set2)
return 1 - len(intersection)/len(union) if union else 0
def calculate_path_diversity(paths):
"""Calculate average Jaccard Distance across all path pairs"""
node_sets = [set(path) for path in paths]
distances = []
# Generate all unique path pairs
for set_a, set_b in combinations(node_sets, 2):
distances.append(jaccard_distance(set_a, set_b))
return sum(distances)/len(distances) if distances else 0
def cluster_paths(paths, threshold=0.3):
"""Cluster paths using Jaccard Distance threshold"""
if not paths:
return []
clusters = []
sets = [set(path) for path in paths]
# Start first cluster with initial path
clusters.append([0])
for i in range(1, len(sets)):
assigned = False
for cluster in clusters:
# Compare with first member of cluster
rep_index = cluster[0]
dist = jaccard_distance(sets[rep_index], sets[i])
if dist <= threshold:
cluster.append(i)
assigned = True
break
if not assigned:
clusters.append([i])
# Convert indices to paths
return [[paths[idx] for idx in cluster] for cluster in clusters]
def main():
# Example usage with predefined paths
if len(sys.argv) > 1:
# Could implement file input parsing here
print("Error: Custom input not implemented in this example")
else:
# Sample paths in a graph
paths = [
['A', 'B', 'D'],
['A', 'C', 'D'],
['A', 'B', 'C', 'D']
]
diversity_score = calculate_path_diversity(paths)
print(f"Path Diversity Score (Jaccard Distance): {diversity_score:.4f}")
# Perform path clustering
clusters = cluster_paths(paths)
print("\nPath Clusters (Structural Similarity > 0.3 threshold):")
for i, cluster in enumerate(clusters, 1):
print(f"Cluster {i}: {{', '.join([' -> '.join(path) for path in cluster])}}")
if __name__ == "__main__":
main()