Skip to content

WeightedAdjacencyMatrix ​

The vertex-by-vertex matrix of edge weights of a weighted Graph, 0 where no edge exists.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguageWeightedAdjacencyMatrix!
WeightedAdjacencyMatrix(g)entry (i,j)(i, j) is the weight of the edge from vertex ii to vertex jj (both directions, for an undirected edge) — 1, AdjacencyMatrix's own default, when gg carries no EdgeWeight at all.
Typed signatures
TypePackageApplies
(value) -> list<list<any>>combinatoricsalways

Domain: Collections

Details
  • gg carries its weights via EdgeWeight -> {w1, w2, …}, one weight per edge in EdgeList order, given as a Graph(edges, EdgeWeight -> {…}) option — see Graph's own note. The Property/PropertyValue per-edge form is not supported.
  • A multi-edge (two edges between the same ordered pair) sums its parallel edges' weights, matching how AdjacencyMatrix sums parallel edges' counts.
  • Returned as a plain matrix (a list of lists) rather than Wolfram's default SparseArray — the same simplification AdjacencyMatrix documents, for the same reason.

Examples

1 more kept as data, checked against the oracles
Basic #

The undirected edge's weight is symmetric; the directed edge's is not

live ↗
InWeightedAdjacencyMatrix(Graph([1,2,3],[UndirectedEdge(1,2),DirectedEdge(2,3)],Rule(EdgeWeight,[5,7])))\mathrm{WeightedAdjacencyMatrix}(\mathrm{Graph}(\bigl\lbrack1, 2, 3\bigr\rbrack, \bigl\lbrack\mathrm{UndirectedEdge}(1, 2), \mathrm{DirectedEdge}(2, 3)\bigr\rbrack, \mathrm{Rule}(\mathrm{EdgeWeight}, \bigl\lbrack5, 7\bigr\rbrack)))Out[[0,5,0],[5,0,7],[0,0,0]]\bigl\lbrack\bigl\lbrack0, 5, 0\bigr\rbrack, \bigl\lbrack5, 0, 7\bigr\rbrack, \bigl\lbrack0, 0, 0\bigr\rbrack\bigr\rbrack
Scope #

A directed edge fills only the row of its source

live ↗
InWeightedAdjacencyMatrix(Graph([1,2],[DirectedEdge(1,2)],Rule(EdgeWeight,[3])))\mathrm{WeightedAdjacencyMatrix}(\mathrm{Graph}(\bigl\lbrack1, 2\bigr\rbrack, \bigl\lbrack\mathrm{DirectedEdge}(1, 2)\bigr\rbrack, \mathrm{Rule}(\mathrm{EdgeWeight}, \bigl\lbrack3\bigr\rbrack)))Out[[0,3],[0,0]]\bigl\lbrack\bigl\lbrack0, 3\bigr\rbrack, \bigl\lbrack0, 0\bigr\rbrack\bigr\rbrack

Weights stay exact

live ↗
InWeightedAdjacencyMatrix(Graph([1,2],[UndirectedEdge(1,2)],Rule(EdgeWeight,[1/2])))\mathrm{WeightedAdjacencyMatrix}(\mathrm{Graph}(\bigl\lbrack1, 2\bigr\rbrack, \bigl\lbrack\mathrm{UndirectedEdge}(1, 2)\bigr\rbrack, \mathrm{Rule}(\mathrm{EdgeWeight}, \bigl\lbrack1/2\bigr\rbrack)))Out[[0,12],[12,0]]\bigl\lbrack\bigl\lbrack0, \frac{1}{2}\bigr\rbrack, \bigl\lbrack\frac{1}{2}, 0\bigr\rbrack\bigr\rbrack

Parallel edges sum, and a float weight makes the entry a float

live ↗
InWeightedAdjacencyMatrix(Graph([1,2],[UndirectedEdge(1,2),UndirectedEdge(1,2)],Rule(EdgeWeight,[5,0.25])))\mathrm{WeightedAdjacencyMatrix}(\mathrm{Graph}(\bigl\lbrack1, 2\bigr\rbrack, \bigl\lbrack\mathrm{UndirectedEdge}(1, 2), \mathrm{UndirectedEdge}(1, 2)\bigr\rbrack, \mathrm{Rule}(\mathrm{EdgeWeight}, \bigl\lbrack5, 0.25\bigr\rbrack)))Out[[0,5.25],[5.25,0]]\bigl\lbrack\bigl\lbrack0, 5.25\bigr\rbrack, \bigl\lbrack5.25, 0\bigr\rbrack\bigr\rbrack
live ↗
InWeightedAdjacencyMatrix(Graph([1,2],[UndirectedEdge(1,2)],Rule(EdgeWeight,[w])))\mathrm{WeightedAdjacencyMatrix}(\mathrm{Graph}(\bigl\lbrack1, 2\bigr\rbrack, \bigl\lbrack\mathrm{UndirectedEdge}(1, 2)\bigr\rbrack, \mathrm{Rule}(\mathrm{EdgeWeight}, \bigl\lbrack w\bigr\rbrack)))Out[[0,w],[w,0]]\bigl\lbrack\bigl\lbrack0, w\bigr\rbrack, \bigl\lbrack w, 0\bigr\rbrack\bigr\rbrack
Properties #

A loop at vertex 1 puts its weight at (1, 1)

live ↗
InWeightedAdjacencyMatrix(Graph([1,2],[DirectedEdge(1,1)],Rule(EdgeWeight,[4])))\mathrm{WeightedAdjacencyMatrix}(\mathrm{Graph}(\bigl\lbrack1, 2\bigr\rbrack, \bigl\lbrack\mathrm{DirectedEdge}(1, 1)\bigr\rbrack, \mathrm{Rule}(\mathrm{EdgeWeight}, \bigl\lbrack4\bigr\rbrack)))Out[[4,0],[0,0]]\bigl\lbrack\bigl\lbrack4, 0\bigr\rbrack, \bigl\lbrack0, 0\bigr\rbrack\bigr\rbrack

An undirected loop is one edge, so its weight sits on the diagonal once, not twice

live ↗
InWeightedAdjacencyMatrix(Graph([1,2],[UndirectedEdge(1,1)],Rule(EdgeWeight,[3])))\mathrm{WeightedAdjacencyMatrix}(\mathrm{Graph}(\bigl\lbrack1, 2\bigr\rbrack, \bigl\lbrack\mathrm{UndirectedEdge}(1, 1)\bigr\rbrack, \mathrm{Rule}(\mathrm{EdgeWeight}, \bigl\lbrack3\bigr\rbrack)))Out[[3,0],[0,0]]\bigl\lbrack\bigl\lbrack3, 0\bigr\rbrack, \bigl\lbrack0, 0\bigr\rbrack\bigr\rbrack
About
Examples8
Checked againstWolfram Language 1 agree7 differ
CrosswalkWolfram Language

See also: AdjacencyMatrix, Graph, GraphDistance