Function Repository Resource:

GraphWeights

Source Notebook

Return the weights of a graph

Contributed by: Alejandra Ortiz Duran

ResourceFunction["GraphWeights"][g]

returns the weights of the edges in graph g.

ResourceFunction["GraphWeights"][g,"Edge"]

returns the weights of the edges in graph g.

ResourceFunction["GraphWeights"][g,"Vertex"]

returns the weights of the vertices in graph g.

Details

GraphWeights will return the VertexWeight or EdgeWeight property defined in the graph.
Weights are often taken to mean cost, capacity, etc.
GraphsWeights works with undirected graphs, directed graphs, weighted graphs, multigraphs, and mixed graphs.

Examples

Basic Examples (2) 

Find the weights in a graph:

In[1]:=
g = Graph[{1 \[UndirectedEdge] 3, 4 \[UndirectedEdge] 3, 1 \[UndirectedEdge] 2}, EdgeWeight -> {1 \[UndirectedEdge] 2 -> 2}]
Out[1]=
In[2]:=
ResourceFunction["GraphWeights"][g]
Out[2]=
In[3]:=
ResourceFunction["GraphWeights"][g, "Edge"]
Out[3]=

Find the weights of the vertices in a graph:

In[4]:=
g = Graph[{1 \[UndirectedEdge] 3, 4 \[UndirectedEdge] 3, 1 \[UndirectedEdge] 2}, VertexWeight -> {1 \[UndirectedEdge] 2 -> 2}]
Out[4]=
In[5]:=
ResourceFunction["GraphWeights"][g, "Vertex"]
Out[5]=

Scope (4) 

Works with undirected weighted graphs:

In[6]:=
ResourceFunction["GraphWeights"][\!\(\*
GraphicsBox[
NamespaceBox["NetworkGraphics",
DynamicModuleBox[{Typeset`graph = HoldComplete[
Graph[{1, 2, 3, 4, 5}, {Null, SparseArray[
         Automatic, {5, 5}, 0, {1, {{0, 3, 6, 8, 9, 10}, {{2}, {3}, {4}, {2}, {3}, {
            5}, {4}, {5}, {5}, {5}}}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}}]}, {EdgeWeight -> {1.2369696639583552`, 4.812258229681952, 8.607929577404715, 7.431845779784997, 8.574488000789195, 8.006160372677755, 2.8301481232102947`, 9.121065878848466, 2.3183507378286023`, 1.7298078679288516`}, FormatType -> TraditionalForm, GraphLayout -> {"Dimension" -> 2}, ImageSize -> {131.4000000000001, Automatic}, VertexLabels -> {
Placed[Automatic, Center]}, VertexSize -> {0.25}, VertexStyle -> {
GrayLevel[1]}}]]}, 
TagBox[GraphicsGroupBox[{
{Hue[0.6, 0.7, 0.5], Opacity[0.7], Arrowheads[0.], ArrowBox[{{0., 0.012484330888018924`}, {
           0.011333771416589089`, 1.1666638939968836`}}, 0.10192290483023064`], ArrowBox[{{0., 0.012484330888018924`}, {0.5818822250328072, 0.5841484109973273}}, 0.10192290483023064`], ArrowBox[{{0., 0.012484330888018924`}, {1.1541843168018768`,
            0.}}, 0.10192290483023064`], ArrowBox[
           BezierCurveBox[{{0.011333771416589089`, 1.1666638939968836`}, {-0.18684105756379346`, 1.0962815719756591`}, {-0.3663390917212296, 1.1988433261781792`}, {-0.4132538528198191, 1.3309407689787274`}, {-0.14439391571122775`, 1.5944610769823941`}, {-0.013264036975934812`, 1.544906217419994}, {0.08567681402759109, 1.3633871669206583`}, {0.011333771416589089`, 1.1666638939968836`}},
SplineDegree->7], 0.10192290483023064`], ArrowBox[{{0.011333771416589089`, 1.1666638939968836`}, {
           0.5818822250328072, 0.5841484109973273}}, 0.10192290483023064`], ArrowBox[{{0.011333771416589089`, 1.1666638939968836`}, {
           1.1666848480058034`, 1.1548334540590575`}}, 0.10192290483023064`], ArrowBox[{{0.5818822250328072, 0.5841484109973273}, {
           1.1541843168018768`, 0.}}, 0.10192290483023064`], ArrowBox[{{0.5818822250328072, 0.5841484109973273}, {
           1.1666848480058034`, 1.1548334540590575`}}, 0.10192290483023064`], ArrowBox[{{1.1541843168018768`, 0.}, {1.1666848480058034`, 1.1548334540590575`}}, 0.10192290483023064`], ArrowBox[
           BezierCurveBox[{{1.1666848480058034`, 1.1548334540590575`}, {1.096428457927411, 1.3530529650328658`}, {1.1990740340904533`, 1.532488387080747}, {1.3311868766939918`, 1.579333895788655}, {1.5945558474459913`, 1.3103582119248316`}, {1.5449381846459758`, 1.1792613301726174`}, {1.3633806566239384`, 1.0804177719107293`}, {1.1666848480058034`, 1.1548334540590575`}},
SplineDegree->7], 0.10192290483023064`]}, 
{GrayLevel[1], EdgeForm[{GrayLevel[0], Opacity[
          0.7]}], {
           DiskBox[{0., 0.012484330888018924`}, 0.10192290483023064], InsetBox["1", {0., 0.012484330888018924},
BaseStyle->"Graphics"]}, {
           DiskBox[{0.011333771416589089`, 1.1666638939968836`}, 0.10192290483023064], InsetBox["2", {0.011333771416589089, 1.1666638939968836},
BaseStyle->"Graphics"]}, {
           DiskBox[{0.5818822250328072, 0.5841484109973273}, 0.10192290483023064], InsetBox["3", {0.5818822250328072, 0.5841484109973273},
BaseStyle->"Graphics"]}, {
           DiskBox[{1.1541843168018768`, 0.}, 0.10192290483023064], InsetBox["4", {1.1541843168018768, 0.},
BaseStyle->"Graphics"]}, {
           DiskBox[{1.1666848480058034`, 1.1548334540590575`}, 0.10192290483023064], InsetBox["5", {1.1666848480058034, 1.1548334540590575},
BaseStyle->"Graphics"]}}}],
MouseAppearanceTag["NetworkGraphics"]],
AllowKernelInitialization->False]],
DefaultBaseStyle->"NetworkGraphics",
FormatType->TraditionalForm,
FrameTicks->None,
ImageSize->{131.4000000000001, Automatic}]\)]
Out[6]=

Directed weighted graphs:

In[7]:=
ResourceFunction["GraphWeights"][\!\(\*
GraphicsBox[
NamespaceBox["NetworkGraphics",
DynamicModuleBox[{Typeset`graph = HoldComplete[
Graph[{1, 2, 3, 4, 5}, {{{2, 1}, {3, 1}, {4, 1}, {2, 2}, {3, 2}, {5, 2}, {4, 3}, {3, 5}, {5, 4}, {5, 5}}, Null}, {EdgeWeight -> {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, FormatType -> TraditionalForm, GraphLayout -> {"Dimension" -> 2}, ImageSize -> {131.4000000000001, Automatic}, VertexLabels -> {
Placed[Automatic, Center]}, VertexSize -> {0.25}, VertexStyle -> {
GrayLevel[1]}}]]}, 
TagBox[GraphicsGroupBox[{
{Hue[0.6, 0.7, 0.5], Opacity[0.7], Arrowheads[Medium], ArrowBox[{{1.190022842661447, 1.1546770272592854`}, {
           0.03679537042560399, 1.188650904945174}}, 0.1019003158602884], ArrowBox[
           BezierCurveBox[{{1.190022842661447, 1.1546770272592854`}, {
            1.1236411127449886`, 1.3542275241537842`}, {
            1.2297626249047013`, 1.5316293551710543`}, {
            1.3627629520027529`, 1.5758924081464352`}, {
            1.6208422838675802`, 1.3018373192880388`}, {
            1.5686802606326287`, 1.1717318689835783`}, {
            1.385231705147466, 1.0764438189366212`}, {
            1.190022842661447, 1.1546770272592854`}},
SplineDegree->7], 0.1019003158602884], ArrowBox[{{0.5952499460064733, 0.5947791621857713}, {
           0.03679537042560399, 1.188650904945174}}, 0.1019003158602884], ArrowBox[{{0.5952499460064733, 0.5947791621857713}, {
           1.190022842661447, 1.1546770272592854`}}, 0.1019003158602884], ArrowBox[{{0.5952499460064733, 0.5947791621857713}, {
           1.1547078990864827`, 0.}}, 0.1019003158602884], ArrowBox[{{0., 0.034606881037839266`}, {0.03679537042560399,
            1.188650904945174}}, 0.1019003158602884], ArrowBox[{{0., 0.034606881037839266`}, {0.5952499460064733, 0.5947791621857713}}, 0.1019003158602884], ArrowBox[{{1.1547078990864827`, 0.}, {1.190022842661447, 1.1546770272592854`}}, 0.1019003158602884], ArrowBox[{{1.1547078990864827`, 0.}, {0., 0.034606881037839266`}}, 0.1019003158602884], ArrowBox[
           BezierCurveBox[{{1.1547078990864827`, 0.}, {
            1.354275315950396, 0.06633084504065964}, {
            1.531650081628912, -0.03983589877224877}, {
            1.5758792198540996`, -0.17284750803530252`}, {
            1.301758333114153, -0.43085695111420297`}, {
            1.1716661876601935`, -0.3786617544187604}, {
            1.0764249176482454`, -0.19518890771425393`}, {
            1.1547078990864827`, 0.}},
SplineDegree->7], 0.1019003158602884]}, 
{GrayLevel[1], EdgeForm[{GrayLevel[0], Opacity[
          0.7]}], {
           DiskBox[{0.03679537042560399, 1.188650904945174}, 0.1019003158602884], InsetBox["1", {0.03679537042560399, 1.188650904945174},
BaseStyle->"Graphics"]}, {
           DiskBox[{1.190022842661447, 1.1546770272592854`}, 0.1019003158602884], InsetBox["2", {1.190022842661447, 1.1546770272592854},
BaseStyle->"Graphics"]}, {
           DiskBox[{0.5952499460064733, 0.5947791621857713}, 0.1019003158602884], InsetBox["3", {0.5952499460064733, 0.5947791621857713},
BaseStyle->"Graphics"]}, {
           DiskBox[{0., 0.034606881037839266`}, 0.1019003158602884], InsetBox["4", {0., 0.034606881037839266},
BaseStyle->"Graphics"]}, {
           DiskBox[{1.1547078990864827`, 0.}, 0.1019003158602884], InsetBox["5", {1.1547078990864827, 0.},
BaseStyle->"Graphics"]}}}],
MouseAppearanceTag["NetworkGraphics"]],
AllowKernelInitialization->False]],
DefaultBaseStyle->"NetworkGraphics",
FormatType->TraditionalForm,
FrameTicks->None,
ImageSize->{131.4000000000001, Automatic}]\)]
Out[7]=

Weighted Multigraphs:

In[8]:=
ResourceFunction["GraphWeights"][\!\(\*
GraphicsBox[
NamespaceBox["NetworkGraphics",
DynamicModuleBox[{Typeset`graph = HoldComplete[
Graph[{1, 2, 3, 4, 5}, {Null, SparseArray[
         Automatic, {5, 5}, 0, {1, {{0, 3, 6, 8, 9, 10}, {{2}, {3}, {4}, {2}, {3}, {
            5}, {4}, {5}, {5}, {5}}}, {2, 1, 1, 1, 1, 1, 1, 1, 2, 1}}]}, {EdgeWeight -> {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
          FormatType -> TraditionalForm, GraphLayout -> {"Dimension" -> 2}, ImageSize -> {131.4000000000001, Automatic}, VertexLabels -> {
Placed[Automatic, Center]}, VertexSize -> {0.25}, VertexStyle -> {
GrayLevel[1]}}]]}, 
TagBox[GraphicsGroupBox[{
{Hue[0.6, 0.7, 0.5], Opacity[0.7], Arrowheads[0.], ArrowBox[
           BezierCurveBox[{{0., 0.012484330888018924`}, {-0.18412726403280927`, 0.5914378462825931}, {0.011333771416589089`, 1.1666638939968836`}}], 0.10192290483023064`], ArrowBox[
           BezierCurveBox[{{0., 0.012484330888018924`}, {
            0.195461035449399, 0.5877103786023026}, {
            0.011333771416589089`, 1.1666638939968836`}}], 0.10192290483023064`], ArrowBox[{{0., 0.012484330888018924`}, {0.5818822250328072, 0.5841484109973273}}, 0.10192290483023064`], ArrowBox[{{0., 0.012484330888018924`}, {1.1541843168018768`,
            0.}}, 0.10192290483023064`], ArrowBox[
           BezierCurveBox[{{0.011333771416589089`, 1.1666638939968836`}, {-0.18684105756379346`, 1.0962815719756591`}, {-0.3663390917212296, 1.1988433261781792`}, {-0.4132538528198191, 1.3309407689787274`}, {-0.14439391571122775`, 1.5944610769823941`}, {-0.013264036975934812`, 1.544906217419994}, {0.08567681402759109, 1.3633871669206583`}, {0.011333771416589089`, 1.1666638939968836`}},
SplineDegree->7], 0.10192290483023064`], ArrowBox[{{0.011333771416589089`, 1.1666638939968836`}, {
           0.5818822250328072, 0.5841484109973273}}, 0.10192290483023064`], ArrowBox[{{0.011333771416589089`, 1.1666638939968836`}, {
           1.1666848480058034`, 1.1548334540590575`}}, 0.10192290483023064`], ArrowBox[{{0.5818822250328072, 0.5841484109973273}, {
           1.1541843168018768`, 0.}}, 0.10192290483023064`], ArrowBox[{{0.5818822250328072, 0.5841484109973273}, {
           1.1666848480058034`, 1.1548334540590575`}}, 0.10192290483023064`], ArrowBox[
           BezierCurveBox[{{1.1541843168018768`, 0.}, {
            0.9705329063463092, 0.5794723237208922}, {
            1.1666848480058034`, 1.1548334540590575`}}], 0.10192290483023064`], ArrowBox[
           BezierCurveBox[{{1.1541843168018768`, 0.}, {
            1.3503362584613736`, 0.5753611303381693}, {
            1.1666848480058034`, 1.1548334540590575`}}], 0.10192290483023064`], ArrowBox[
           BezierCurveBox[{{1.1666848480058034`, 1.1548334540590575`}, {1.096428457927411, 1.3530529650328658`}, {1.1990740340904533`, 1.532488387080747}, {1.3311868766939918`, 1.579333895788655}, {1.5945558474459913`, 1.3103582119248316`}, {1.5449381846459758`, 1.1792613301726174`}, {1.3633806566239384`, 1.0804177719107293`}, {1.1666848480058034`, 1.1548334540590575`}},
SplineDegree->7], 0.10192290483023064`]}, 
{GrayLevel[1], EdgeForm[{GrayLevel[0], Opacity[
          0.7]}], {
           DiskBox[{0., 0.012484330888018924`}, 0.10192290483023064], InsetBox["1", {0., 0.012484330888018924},
BaseStyle->"Graphics"]}, {
           DiskBox[{0.011333771416589089`, 1.1666638939968836`}, 0.10192290483023064], InsetBox["2", {0.011333771416589089, 1.1666638939968836},
BaseStyle->"Graphics"]}, {
           DiskBox[{0.5818822250328072, 0.5841484109973273}, 0.10192290483023064], InsetBox["3", {0.5818822250328072, 0.5841484109973273},
BaseStyle->"Graphics"]}, {
           DiskBox[{1.1541843168018768`, 0.}, 0.10192290483023064], InsetBox["4", {1.1541843168018768, 0.},
BaseStyle->"Graphics"]}, {
           DiskBox[{1.1666848480058034`, 1.1548334540590575`}, 0.10192290483023064], InsetBox["5", {1.1666848480058034, 1.1548334540590575},
BaseStyle->"Graphics"]}}}],
MouseAppearanceTag["NetworkGraphics"]],
AllowKernelInitialization->False]],
DefaultBaseStyle->"NetworkGraphics",
FormatType->TraditionalForm,
FrameTicks->None,
ImageSize->{131.4000000000001, Automatic}]\)]
Out[8]=

Mixed weighted graphs:

In[9]:=
ResourceFunction["GraphWeights"][\!\(\*
GraphicsBox[
NamespaceBox["NetworkGraphics",
DynamicModuleBox[{Typeset`graph = HoldComplete[
Graph[{2, 1, 3, 4, 5}, {{{1, 2}, {4, 2}, {3, 1}, {3, 5}, {5, 5}}, {{3, 2}, {1, 1}, {5, 1}, {5, 4}}}, {EdgeWeight -> {1, 1, 1, 1, 1, 1, 1, 1, 1}, FormatType -> TraditionalForm, GraphLayout -> {"Dimension" -> 2}, ImageSize -> {189.5999999999999, Automatic}, VertexLabels -> {
Placed[Automatic, Center]}, VertexSize -> {0.2}, VertexStyle -> {
GrayLevel[1]}}]]}, 
TagBox[GraphicsGroupBox[{
{Hue[0.6, 0.7, 0.5], Opacity[0.7], Arrowheads[Medium], 
{Arrowheads[0.], ArrowBox[
            BezierCurveBox[{{1.7750956636961295`, 0.13226206424409698`}, {1.966185455332654, 0.22007793074812088`}, {2.154132967190708, 0.1340028601544194}, {2.2126817457067816`, 0.0066437403350446955`}, {
             1.968508088505095, -0.27987041008990765`}, {
             1.8334766867831747`, -0.2422562449934525}, {
             1.718689729212581, -0.07033434597232444}, {
             1.7750956636961295`, 0.13226206424409698`}},
SplineDegree->7], 0.07142150267772777]}, 
{Arrowheads[0.], ArrowBox[{{1.7750956636961295`, 0.13226206424409698`}, {
            0.943188822949299, 0.}}, 0.07142150267772777]}, ArrowBox[{{1.7750956636961295`, 0.13226206424409698`}, {
           0.9446927018534159, 0.979176716752343}}, 0.07142150267772777], 
{Arrowheads[0.], ArrowBox[{{0.9446927018534159, 0.979176716752343}, {
            1.7739785995788884`, 0.8464762174518206}}, 0.07142150267772777]}, ArrowBox[{{1.7739785995788884`, 0.8464762174518206}, {
           1.7750956636961295`, 0.13226206424409698`}}, 0.07142150267772777], ArrowBox[{{1.7739785995788884`, 0.8464762174518206}, {
           0.943188822949299, 0.}}, 0.07142150267772777], 
{Arrowheads[0.], ArrowBox[{{0., 0.4896215582219036}, {0.943188822949299, 0.}}, 0.07142150267772777]}, ArrowBox[{{0., 0.4896215582219036}, {0.9446927018534159, 0.979176716752343}}, 0.07142150267772777], ArrowBox[
           BezierCurveBox[{{0.943188822949299, 0.}, {
            1.1211614620882508`, -0.12504457378621806`}, {
            1.1394455650204758`, -0.32804828408198256`}, {
            1.0602355701475654`, -0.44122719423721557`}, {
            0.6928479934285716, -0.38172090202006825`}, {
            0.6534140914385866, -0.2493250698895323}, {
            0.7348438308049134, -0.06247215350711399}, {
            0.943188822949299, 0.}},
SplineDegree->7], 0.07142150267772777]}, 
{GrayLevel[1], EdgeForm[{GrayLevel[0], Opacity[
          0.7]}], {
           DiskBox[{1.7750956636961295`, 0.13226206424409698`}, 0.07142150267772777], InsetBox["2", {1.7750956636961295, 0.13226206424409698},
BaseStyle->"Graphics"]}, {
           DiskBox[{0.9446927018534159, 0.979176716752343}, 0.07142150267772777], InsetBox["1", {0.9446927018534159, 0.979176716752343},
BaseStyle->"Graphics"]}, {
           DiskBox[{1.7739785995788884`, 0.8464762174518206}, 0.07142150267772777], InsetBox["3", {1.7739785995788884, 0.8464762174518206},
BaseStyle->"Graphics"]}, {
           DiskBox[{0., 0.4896215582219036}, 0.07142150267772777], InsetBox["4", {0., 0.4896215582219036},
BaseStyle->"Graphics"]}, {
           DiskBox[{0.943188822949299, 0.}, 0.07142150267772777], InsetBox["5", {0.943188822949299, 0.},
BaseStyle->"Graphics"]}}}],
MouseAppearanceTag["NetworkGraphics"]],
AllowKernelInitialization->False]],
DefaultBaseStyle->"NetworkGraphics",
FormatType->TraditionalForm,
FrameTicks->None,
ImageSize->{189.5999999999999, Automatic}]\)]
Out[9]=

Applications (1) 

We can identify if a graph has negatively weighted edges:

In[10]:=
g = Graph[{1 \[UndirectedEdge] 3, 4 \[UndirectedEdge] 3, 1 \[UndirectedEdge] 2}, EdgeWeight -> {1 \[UndirectedEdge] 2 -> -2}];
In[11]:=
ResourceFunction["GraphWeights"][g]
Out[11]=
In[12]:=
VectorQ[%, Positive]
Out[12]=

Possible Issues (1) 

If the graph is not weighted it returns weights of value 1:

In[13]:=
ResourceFunction["GraphWeights"]@
 Graph[{1 \[UndirectedEdge] 3, 4 \[UndirectedEdge] 3, 1 \[UndirectedEdge] 2}]
Out[13]=

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 1.0.0 – 15 November 2024

Related Resources

License Information