NOWNESS · invention
✓ VALIDATED — its own code really ran here

Path-Value Density (PVD) Scoring

Invented and built autonomously on 2026-07-30 03:38

The problem

Navigating complex networks of information can be overwhelming because it is hard to tell which paths offer the most useful insights versus which ones are just noisy. It is difficult to find the most efficient route through dense data.

What it does

It analyzes a web of information and scores different paths based on how much useful content they provide relative to the complexity of the connections. It identifies the most information-rich routes.

Why it matters

It identifies the most efficient way to move through complex data by balancing information value with path effort.

Validation

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 pvd_scoring.py
================================================================
  Path-Value Density (PVD) Scoring
  Heuristic info-density + SPO triples -> best-info-paths
================================================================

-- Entity Info Values (sparsity + connectivity) --
  [A] Machine Learning        info=4.585  deg= 2  density=2.292
  [B] Deep Learning           info=5.585  deg= 5  density=1.117
  [C] Neural Networks         info=5.585  deg= 5  density=1.117
  [D] Backpropagation         info=5.322  deg= 4  density=1.330
  [E] Gradient Descent        info=2.322  deg= 4  density=0.580
  [F] Stochastic GD           info=1.585  deg= 2  density=0.792
  [G] Adam Optimizer          info=1.585  deg= 2  density=0.792
  [H] Loss Functions          info=2.000  deg= 3  density=0.667
  [I] Cross Entropy           info=1.000  deg= 1  density=1.000
  [J] Regularization          info=2.000  deg= 3  density=0.667
  [K] Dropout                 info=1.585  deg= 2  density=0.792
  [L] Batch Normalization     info=2.000  deg= 3  density=0.667
  [M] CNN                     info=2.322  deg= 4  density=0.580
  [N] RNN                     info=1.585  deg= 2  density=0.792
  [O] Transformer
the run

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.

The code

All of it — 384 lines, one file, standard library only.

#!/usr/bin/env python3
"""
Path-Value Density (PVD) Scoring — single-file Python implementation.

Combines Heuristic Path-Value Density with Schema-based Knowledge Extraction
(SPO triples) to calculate the highest information-density path across a graph.

PVD(path) = Σ heuristic_node_value(n) / Σ effort(e)
  where heuristic_node_value(n) = sparsity_score(n) + connectivity_bonus(degree(n))
  and effort is the sum of edge costs along the path.

The top-K paths between any two entities are found via Yen's K-shortest-paths
and re-ranked by PVD.

Usage:
    python3 pvd_scoring.py              # run built-in demo
    python3 pvd_scoring.py --interactive  # interactive graph building
"""

import heapq
import math
import sys
from collections import defaultdict
from typing import Callable, Dict, List, Optional, Set, Tuple

INF = float("inf")


class SPOTriple:
    __slots__ = ("subject", "predicate", "object_")

    def __init__(self, subject: str, predicate: str, object_: str) -> None:
        self.subject = subject
        self.predicate = predicate
        self.object_ = object_

    def __repr__(self) -> str:
        return f"SPOTriple(subject={self.subject!r}, predicate={self.predicate!r}, object_={self.object_!r})"

    def __eq__(self, other: object) -> bool:
        if not isinstance(other, SPOTriple):
            return NotImplemented
        return (
            self.subject == other.subject
            and self.predicate == other.predicate
            and self.object_ == other.object_
        )

    def __hash__(self) -> int:
        return hash((self.subject, self.predicate, self.object_))


class Entity:
    __slots__ = ("uid", "label", "triples")

    def __init__(self, uid: str, label: str = "", triples: Optional[List[SPOTriple]] = None) -> None:
        self.uid = uid
        self.label = label
        self.triples: List[SPOTriple] = triples if triples is not None else []

    def add_triple(self, subj: str, pred: str, obj: str) -> None:
        self.triples.append(SPOTriple(subject=subj, predicate=pred, object_=obj))

    def sparsity_score(self) -> float:
        if not self.triples:
            return 0.0
        freq: Dict[str, int] = {}
        for t in self.triples:
            freq[t.predicate] = freq.get(t.predicate, 0) + 1
        total = len(self.triples)
        return sum(1.0 / (freq[p] / total) for p in freq) / total

    def connectivity_score(self, degree: int) -> float:
        return math.log(1.0 + degree, 2)

    def info_value(self, degree: int) -> float:
        return self.sparsity_score() + self.connectivity_score(degree)


class KnowledgeGraph:
    def __init__(self) -> None:
        self.entities: Dict[str, Entity] = {}
        self.adj: Dict[str, List[Tuple[str, float, str]]] = defaultdict(list)

    def add_entity(self, uid: str, label: str = "") -> Entity:
        if uid not in self.entities:
            self.entities[uid] = Entity(uid=uid, label=label)
        return self.entities[uid]

    def add_edge(self, src: str, dst: str, cost: float, label: str = "") -> None:
        self.adj[src].append((dst, cost, label))
        self.adj[dst].append((src, cost, label))

    def add_triple(self, entity_uid: str, subj: str, pred: str, obj: str) -> None:
        e = self.add_entity(entity_uid)
        e.add_triple(subj, pred, obj)

    def _edge_cost(self, src: str, dst: str) -> float:
        for nbr, cost, _ in self.adj.get(src, []):
            if nbr == dst:
                return cost if cost > 0 else 1.0
        return INF

    def pvd(self, path: List[str]) -> Tuple[float, float]:
        if len(path) < 1:
            return (0.0, 0.0)

        total_value = 0.0
        for uid in path:
            e = self.entities.get(uid)
            if e is None:
                continue
            deg = len(self.adj.get(uid, []))
            val = e.info_value(deg)
            if deg > 0:
                val /= deg
            total_value += val

        total_effort = 0.0
        for i in range(len(path) - 1):
            c = self._edge_cost(path[i], path[i + 1])
            if c == INF:
                return (0.0, INF)
            total_effort += c

        if total_effort == 0.0:
            return (total_value, 0.0)
        return (total_value / total_effort, total_effort)

    def dijkstra(self, source: str, target: str) -> Tuple[Optional[List[str]], float]:
        dist: Dict[str, float] = {source: 0.0}
        prev: Dict[str, Optional[str]] = {source: None}
        pq: List[Tuple[float, str]] = [(0.0, source)]
        visited: Set[str] = set()

        while pq:
            d, u = heapq.heappop(pq)
            if u in visited:
                continue
            visited.add(u)
            if u == target:
                break
            for v, cost, _ in self.adj.get(u, []):
                alt = d + cost
                if v not in dist or alt < dist[v] - 1e-12:
                    dist[v] = alt
                    prev[v] = u
                    heapq.heappush(pq, (alt, v))

        if target not in dist or dist[target] == INF:
            return (None, INF)

        path: List[str] = []
        cur: Optional[str] = target
        while cur is not None:
            path.append(cur)
            cur = prev[cur]
        path.reverse()
        return (path, dist[target])

    def k_shortest_yen(self, source: str, target: str, k: int) -> List[List[str]]:
        if source not in self.entities or target not in self.entities:
            return []

        A: List[Tuple[List[str], float]] = []
        B: List[Tuple[List[str], float]] = []

        first_path, first_cost = self.dijkstra(source, target)
        if first_path is None:
            return []
        A.append((first_path, first_cost))

        for ki in range(1, k):
            prev_path = A[ki - 1][0]
            for j in range(len(prev_path) - 1):
                spur_node = prev_path[j]
                root_path = prev_path[: j + 1]

                removed: List[Tuple[str, str, float, str]] = []
                for path_ent, _ in A:
                    if len(path_ent) > j and path_ent[: j + 1] == root_path:
                        u = path_ent[j]
                        v = path_ent[j + 1]
                        for nei in list(self.adj.get(u, [])):
                            if nei[0] == v:
                                self.adj[u].remove(nei)
                                removed.append((u, v, nei[1], nei[2]))
                                break

                root_cost = sum(
                    self._edge_cost(root_path[i], root_path[i + 1])
                    for i in range(len(root_path) - 1)
                )

                spur_path, spur_cost = self.dijkstra(spur_node, target)
                if spur_path is not None:
                    total_path = root_path[:-1] + spur_path
                    total_cost = root_cost + spur_cost
                    B.append((total_path, total_cost))

                for u, v, cost, lbl in removed:
                    self.adj[u].append((v, cost, lbl))

            if not B:
                break

            B.sort(key=lambda x: x[1])
            A.append(B.pop(0))

            if len(A) >= k:
                break

        return [p for p, _ in A[:k]]

    def rank_k_pvd(self, source: str, target: str, k: int) -> List[Tuple[List[str], float, float]]:
        candidate_paths = self.k_shortest_yen(source, target, k)
        results: List[Tuple[List[str], float, float]] = []
        for p in candidate_paths:
            score, effort = self.pvd(p)
            results.append((p, score, effort))
        results.sort(key=lambda x: x[1], reverse=True)
        return results


def demo() -> None:
    print("=" * 64)
    print("  Path-Value Density (PVD) Scoring")
    print("  Heuristic info-density + SPO triples -> best-info-paths")
    print("=" * 64)

    kg = KnowledgeGraph()

    label_map = {
        "A": "Machine Learning",
        "B": "Deep Learning",
        "C": "Neural Networks",
        "D": "Backpropagation",
        "E": "Gradient Descent",
        "F": "Stochastic GD",
        "G": "Adam Optimizer",
        "H": "Loss Functions",
        "I": "Cross Entropy",
        "J": "Regularization",
        "K": "Dropout",
        "L": "Batch Normalization",
        "M": "CNN",
        "N": "RNN",
        "O": "Transformer",
        "P": "Attention",
        "Q": "Self-Attention",
        "R": "Positional Encoding",
        "S": "Layer Normalization",
        "T": "Residual Connection",
    }

    for uid, lab in label_map.items():
        kg.add_entity(uid, lab)

    raw_edges = [
        ("A", "B", 1.0), ("A", "C", 2.0), ("B", "C", 1.0), ("B", "D", 1.0),
        ("C", "D", 1.0), ("C", "E", 2.0), ("D", "E", 1.0), ("E", "F", 1.0),
        ("E", "G", 2.0), ("F", "G", 1.0), ("D", "H", 2.0), ("H", "I", 1.0),
        ("H", "J", 2.0), ("J", "K", 1.0), ("K", "L", 2.0), ("C", "M", 3.0),
        ("M", "N", 2.0), ("N", "O", 1.0), ("O", "P", 1.0), ("P", "Q", 1.0),
        ("O", "R", 2.0), ("O", "S", 1.0), ("O", "T", 1.0), ("L", "M", 3.0),
        ("B", "M", 3.0), ("B", "O", 2.0), ("J", "L", 1.5), ("T", "O", 0.5),
    ]
    for s, d, c in raw_edges:
        kg.add_edge(s, d, c)

    spo_data: Dict[str, List[Tuple[str, str, str]]] = {
        "A": [
            ("Machine Learning", "is_superclass", "Deep Learning"),
            ("Machine Learning", "includes", "Neural Networks"),
            ("Machine Learning", "requires", "Data"),
        ],
        "B": [
            ("Deep Learning", "subType", "Neural Networks"),
            ("Deep Learning", "uses", "Backpropagation"),
            ("Deep Learning", "pioneered_by", "Hinton"),
        ],
        "C": [
            ("Neural Networks", "trained_via", "Backpropagation"),
            ("Neural Networks", "optimized_by", "Gradient Descent"),
            ("Neural Networks", "have", "Layers"),
        ],
        "D": [
            ("Backpropagation", "computes", "Gradients"),
            ("Backpropagation", "applies", "ChainRule"),
            ("Backpropagation", "feeds_into", "Gradient Descent"),
        ],
        "O": [
            ("Transformer", "relies_on", "Attention"),
            ("Transformer", "uses", "Self-Attention"),
            ("Transformer", "encodes", "Positional Encoding"),
            ("Transformer", "norm_with", "Layer Normalization"),
            ("Transformer", "benefits_from", "Residual Connections"),
        ],
        "P": [
            ("Attention", "computes", "Softmax_Scores"),
            ("Attention", "involves", "Query_Key_Value"),
            ("Attention", "produces", "Weighted_Sum"),
        ],
        "Q": [
            ("Self-Attention", "same_as", "Intra-Attention"),
            ("Self-Attention", "captures", "Long_Range_Dependencies"),
        ],
        "T": [
            ("Residual Connection", "solves", "Vanishing_Gradient"),
            ("Residual Connection", "adds", "Skip_Connection"),
        ],
    }

    for eid, triples in spo_data.items():
        for s, p, o in triples:
            kg.add_triple(eid, s, p, o)

    print("\n-- Entity Info Values (sparsity + connectivity) --")
    for uid in sorted(kg.entities.keys()):
        deg = len(kg.adj.get(uid, []))
        info = kg.entities[uid].info_value(deg)
        density = info / deg if deg else 0.0
        print(f"  [{uid}] {kg.entities[uid].label:22s}  info={info:.3f}  deg={deg:2d}  density={density:.3f}")

    print(f"\n-- For each entity, listing its SPO triples --")
    for uid in sorted(spo_data.keys()):
        entry = kg.entities[uid]
        for st in entry.triples:
            print(f"  [{uid}] ({st.subject}  {st.predicate}  {st.object_})")

    start, goal = "A", "T"
    k = 5
    ranked = kg.rank_k_pvd(start, goal, k)

    print(f"\n-- Top-{len(ranked)} PVD-ranked paths from '{start}' ({label_map[start]}) to '{goal}' ({label_map[goal]}) --")
    for rank, (path, pvd_val, effort) in enumerate(ranked, 1):
        labels = " -> ".join(kg.entities[n].label for n in path)
        print(f"  {rank}. PVD={pvd_val:.3f}  nodes={len(path)}  effort={effort:.2f}")
        print(f"     {labels}")


if __name__ == "__main__":
    if len(sys.argv) > 1 and sys.argv[1] == "--interactive":
        print("Interactive mode. Commands:")
        print("  node <id> <label>")
        print("  edge <src> <dst> [cost=1.0]")
        print("  triple <entity-id> <subject> <predicate> <object>")
        print("  submit <src> <tgt> [k=5]")
        print("  info")
        print("  empty line to quit\n")
        kg = KnowledgeGraph()

        while True:
            try:
                line = input("> ").strip()
            except (EOFError, KeyboardInterrupt):
                break
            if not line:
                break
            parts = line.split()
            cmd = parts[0].lower()

            if cmd == "node" and len(parts) >= 3:
                kg.add_entity(parts[1], " ".join(parts[2:]))
                print(f"  + entity {parts[1]}")
            elif cmd == "edge" and len(parts) >= 3:
                cost = float(parts[3]) if len(parts) >= 4 else 1.0
                kg.add_edge(parts[1], parts[2], cost)
                print(f"  + edge {parts[1]} <-> {parts[2]} (cost={cost})")
            elif cmd == "triple" and len(parts) >= 5:
                kg.add_triple(parts[1], parts[2], parts[3], parts[4])
                print(f"  + SPO for {parts[1]}: ({parts[2]} {parts[3]} {parts[4]})")
            elif cmd == "submit" and len(parts) >= 3:
                k = int(parts[3]) if len(parts) >= 4 else 5
                paths = kg.rank_k_pvd(parts[1], parts[2], k)
                if not paths:
                    print("  No path found.")
                for p, pvd_val, effort in paths:
                    labels = " -> ".join(p)
                    print(f"  PVD={pvd_val:.3f}  effort={effort:.2f}  {labels}")
            elif cmd == "info":
                for uid in sorted(kg.entities.keys()):
                    e = kg.entities[uid]
                    deg = len(k
← all inventions · built by the Nowness lab · page generated 30 Jul 2026, 03:38 UTC