Check identity, direction, duplicates and degree before placing nodes on a canvas.
Network graphs: define nodes and edges before trusting the layout
The edge is the claim
A node-link graph represents entities and relationships. Before running a force layout, specify whether an edge means a message, a transaction, a dependency or a co-occurrence, and whether direction matters. Two messages from depot A to depot B may be two events or one weighted relationship. Resolve that choice at the declared grain. Stable node IDs prevent two accounts with the same display name from merging. The unit of analysis extends to graph edges.
Layout distance is not a metric
Force layouts position nodes by an algorithm balancing constraints. Nearby nodes may be strongly connected, but the distance is not automatically a business quantity; changing a random seed, viewport or graph subset can move them. Pin important positions or show a fixed comparison layout when inspecting versions. Report quantitative graph measures separately with their definitions. Comparison panels should preserve a stable node identity and comparable viewport.
Count what selection removes
A filtered network can make a high-degree node appear isolated if many of its neighbors are hidden. Show selected and total degree separately or state that the graph describes only the visible subgraph. Validate dangling edges whose endpoints are absent; do not let the renderer silently discard them. For large graphs, aggregate by a declared community or show a ranked adjacency table, because a dense hairball hides paths and edge direction. Resolution limits apply to edges and nodes as much as to points in a chart.
Design for inspection
Hover-only relationships are inaccessible on touch and keyboard. Provide a searchable node list, ordered neighbor table, edge direction labels and a way to inspect an entity without relying on color. Keep private identifiers out of public graph exports. A link of width 8 should name the weight unit and time interval; do not use width for one measure and tooltip for another. Text alternatives carry exact relationships that an overview cannot show.
Worked contract
from collections import defaultdict
def directed_degrees(
node_ids: set[str], links: list[tuple[str, str, int]]
) -> dict[str, tuple[int, int]]:
if not node_ids:
raise ValueError("empty node set")
incoming = defaultdict(int)
outgoing = defaultdict(int)
for source, target, weight in links:
if source not in node_ids or target not in node_ids or weight <= 0:
raise ValueError("dangling edge or nonpositive weight")
outgoing[source] += weight
incoming[target] += weight
return {node: (incoming[node], outgoing[node]) for node in node_ids}
degrees = directed_degrees(
{"depot-47", "depot-81", "depot-94"},
[("depot-47", "depot-81", 31),
("depot-81", "depot-94", 8)])
assert degrees["depot-81"] == (31, 8)
assert sum(value[0] for value in degrees.values()) == 39Performance and operating cost
For V nodes and E links, endpoint validation and weighted-degree counting take O(V + E) expected time and O(V) space. Layout may take many iterative passes and dominate CPU; this accounting step should run first so bad graph data does not get disguised by an attractive arrangement. The function sums weights and permits repeated directed pairs by design. If the chart promises one edge per pair, aggregate and count duplicates explicitly before layout.
Common Mistakes
- Treating screen distance from a force layout as an exact relationship strength.
- Discarding edges with missing endpoints without an audit count.
- Mixing event count with unique-neighbor degree.
- Comparing two filtered layouts without identifying removed nodes and edges.
Validation drill
Map handoffs among 31 service teams. Add a duplicate label, a dangling edge and a filtered-out hub; preserve stable IDs and report both visible and full-graph weighted degrees.
Read next
- Sankey diagrams: reconcile flows before sizing links
- Aggregation grain and denominator: make every plotted mark auditable
- Large chart series: reduce marks without erasing spikes
- Accessible charts: encode meaning without relying on color alone
