Qu

add_edge

Builtin index · Collections & strings

add_node(g, name)

Input: g, name · Output: no output

What it is

g is a graph handle; name is a Str/number node id; add_edge(g, a, b, [weight=1.0]) also takes a second node id and an optional weight (number, default 1.0). Nodes are named; an edge carries an optional weight, defaulting to 1.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
add_node(g, name)
g —g is a graph handle.
name Str or numbername is a Str/number node id.
no outputNodes are named; an edge carries an optional weight, defaulting to 1.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

h = graph()
add_node(h, "X")          # an isolated node, no edges yet

h.add_edge("X", "Y")
print(has_edge(h, "X", "Y"))   # true

print(has_edge(h, "X", "Z"))   # false -- Z isn't even a node

print(neighbors(h, "X"))       # ("Y")

output
true
false
("Y")

from collections-strings

How it works

graph([directed=]) (§ data structures pass, 2026-09-01, algorithm, Dijkstra's shortest_path; see that module's own doc comment for the node-id normalization rule and why Dijkstra over plain BFS.

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.

See also

add_node — documented in the same entry.