gives the adjacency matrix of edge weights of the graph g.
WeightedAdjacencyMatrix[{vw,…}]
uses rules vw to specify the graph g.
WeightedAdjacencyMatrix
gives the adjacency matrix of edge weights of the graph g.
WeightedAdjacencyMatrix[{vw,…}]
uses rules vw to specify the graph g.
Details and Options
- WeightedAdjacencyMatrix returns a SparseArray object, which can be converted to an ordinary matrix using Normal.
- An entry wij of the weighted adjacency matrix is the weight of a directed edge from vertex νi to vertex νj. If there is no edge the weight is taken to be 0.
- An edge without explicit EdgeWeight specified is taken to have weight 1.
- An undirected edge is interpreted as two directed edges with opposite directions and the same weight.
- The vertices vi are assumed to be in the order given by VertexList[g].
- The weighted adjacency matrix for a graph will have dimensions
×
, where
is the number of vertices.
Examples
open all close allBasic Examples (2)
The weighted adjacency matrix of an undirected graph:
Graph[{12, 23, 31}, EdgeWeight -> RandomReal[{1, 5}, 3]]WeightedAdjacencyMatrix[%]//MatrixFormThe weighted adjacency matrix of a directed graph:
Graph[{12, 23, 31}, EdgeWeight -> RandomReal[{1, 5}, 3]]WeightedAdjacencyMatrix[%]//MatrixFormScope (5)
The weighted adjacency matrix of an undirected graph is symmetric:
Graph[{12, 13, 23, 24, 34}, EdgeWeight -> RandomReal[{1, 5}, 5]]WeightedAdjacencyMatrix[%]//MatrixFormThe weighted adjacency matrix of a directed graph can be unsymmetric:
Graph[{12, 21, 31, 32, 41, 42}, EdgeWeight -> RandomReal[{1, 5}, 6]]WeightedAdjacencyMatrix[%]//MatrixFormUse rules to specify the graph:
WeightedAdjacencyMatrix[{1 -> 3, 2 -> 1}]//MatrixFormThe weighted adjacency matrix of the graph with self-loops has diagonal entries:
Graph[{11, 12, 23, 31}, EdgeWeight -> RandomReal[{1, 5}, 4]]WeightedAdjacencyMatrix[%]//MatrixFormWeightedAdjacencyMatrix works with large graphs:
Graph[Table[i -> Mod[i ^ 2, 10 ^ 3], {i, 0, 10 ^ 3 - 1}]];Timing[m = WeightedAdjacencyMatrix[%]]Use MatrixPlot to visualize the matrix:
MatrixPlot[m]Properties & Relations (4)
Rows and columns of the weighted adjacency matrix follow the order given by VertexList:
g = Graph[{23, 31, 12, 14}, EdgeWeight -> RandomReal[{1, 5}, 4], VertexShapeFunction -> "Name"]VertexList[g]TableForm[Normal@WeightedAdjacencyMatrix[g], TableHeadings -> {c = Style[#, Blue]& /@ %, c}]Use WeightedAdjacencyGraph to construct a graph from a weighted adjacency matrix:
WeightedAdjacencyGraph[{{∞, 1, ∞}, {∞, ∞, 1}, {1, 1, ∞}}]WeightedAdjacencyMatrix[%]//NormalThe number of rows or columns is equal to the number of vertices:
g = CompleteGraph[5, EdgeWeight -> RandomReal[{1, 5}, 10]]Dimensions[AdjacencyMatrix[g]]VertexCount[g]The main diagonals for a loop-free graph are all zeros:
GridGraph[{2, 3}, EdgeWeight -> RandomReal[{1, 5}, 7]]Diagonal[AdjacencyMatrix[%]]//NormalRelated Guides
Text
Wolfram Research (2010), WeightedAdjacencyMatrix, Wolfram Language function, https://reference.wolfram.com/language/ref/WeightedAdjacencyMatrix.html (updated 2015).
CMS
Wolfram Language. 2010. "WeightedAdjacencyMatrix." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2015. https://reference.wolfram.com/language/ref/WeightedAdjacencyMatrix.html.
APA
Wolfram Language. (2010). WeightedAdjacencyMatrix. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/WeightedAdjacencyMatrix.html
BibTeX
@misc{reference.wolfram_2026_weightedadjacencymatrix, author="Wolfram Research", title="{WeightedAdjacencyMatrix}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/WeightedAdjacencyMatrix.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_weightedadjacencymatrix, organization={Wolfram Research}, title={WeightedAdjacencyMatrix}, year={2015}, url={https://reference.wolfram.com/language/ref/WeightedAdjacencyMatrix.html}, note=[Accessed: 13-June-2026]}