WeightedGraphQ
Details
- WeightedGraphQ will give True if there is an explicit VertexWeight or EdgeWeight property defined.
- In cases where a weighted graph is expected, the default vertex and edge weights are taken to be 1.
- Weights are often taken to mean cost, capacity, etc.
Examples
open all close allBasic Examples (2)
Test whether a graph is a weighted graph:
{CompleteGraph[5], CompleteGraph[5, EdgeWeight -> RandomReal[{0, 10}, {10}]]}WeightedGraphQ /@ %{AdjacencyGraph[(| | | |
| - | - | - |
| 0 | 1 | 0 |
| 0 | 0 | 1 |
| 1 | 0 | 0 |)], WeightedAdjacencyGraph[(| | | |
| - | - | - |
| ∞ | 1 | ∞ |
| ∞ | ∞ | 3 |
| 2 | ∞ | ∞ |)]}WeightedGraphQ /@ %Scope (4)
Test undirected and directed graphs:
{WeightedAdjacencyGraph[(| | | |
| - | - | - |
| ∞ | 1 | 2 |
| 1 | ∞ | 1 |
| 2 | 1 | ∞ |)], WeightedAdjacencyGraph[(| | | |
| - | - | - |
| ∞ | 2 | ∞ |
| ∞ | ∞ | 3 |
| 1 | ∞ | ∞ |)]}WeightedGraphQ /@ %GridGraph[{2, 3}, EdgeWeight -> RandomReal[{0, 1}, {7}]]WeightedGraphQ[%]GridGraph[{2, 3}, VertexWeight -> RandomReal[{0, 1}, {6}]]WeightedGraphQ[%]WeightedGraphQ gives False for anything that is not a weighted graph:
WeightedGraphQ[x]WeightedGraphQ[Graph[garbage]]g = GridGraph[{10, 10, 10, 10}, VertexWeight -> RandomReal[{0, 1}, {10^4}]];WeightedGraphQ[g]//Timing{VertexCount[g], EdgeCount[g]}Possible Issues (1)
WeightedGraphQ gives False for non-explicit graphs:
WeightedGraphQ[CompleteGraph[n]]Related Guides
History
Text
Wolfram Research (2010), WeightedGraphQ, Wolfram Language function, https://reference.wolfram.com/language/ref/WeightedGraphQ.html.
CMS
Wolfram Language. 2010. "WeightedGraphQ." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/WeightedGraphQ.html.
APA
Wolfram Language. (2010). WeightedGraphQ. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/WeightedGraphQ.html
BibTeX
@misc{reference.wolfram_2026_weightedgraphq, author="Wolfram Research", title="{WeightedGraphQ}", year="2010", howpublished="\url{https://reference.wolfram.com/language/ref/WeightedGraphQ.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_weightedgraphq, organization={Wolfram Research}, title={WeightedGraphQ}, year={2010}, url={https://reference.wolfram.com/language/ref/WeightedGraphQ.html}, note=[Accessed: 13-June-2026]}