Function Repository Resource:

WeightedSimpleGraph

Source Notebook

SimpleGraph with edge and vertex weights

Contributed by: Nikolay Murzin

ResourceFunction["WeightedSimpleGraph"][g]

gives the underlying simple graph from the graph g with edge weights corresponding to edge multiplicities.

ResourceFunction["WeightedSimpleGraph"][g,f]

also applies function f to vertices of the graph g and assigns vertex weights based on produced tally.

Details and Options

ResourceFunction["WeightedSimpleGraph"][g] removes all self-loops and multiple edges between the same vertices.
Self-loops can also be weighted by including SelfLoopsTrue option.
ResourceFunction["WeightedSimpleGraph"] works with undirected graphs, directed graphs, multigraphs, and mixed graphs.

Examples

Basic Examples (3) 

Remove multi-edges from a graph and assign edge weights with their multiplicities:

In[1]:=
ResourceFunction["WeightedSimpleGraph"][\!\(\*
GraphicsBox[
NamespaceBox["NetworkGraphics",
DynamicModuleBox[{Typeset`graph = HoldComplete[
Graph[{1, 2, 3}, {{{1, 2}, {1, 2}, {2, 3}, {2, 3}, {2, 3}}, Null}]]}, TagBox[GraphicsGroupBox[GraphicsComplexBox[CompressedData["
1:eJw90W1IU2EUAOCXkOYUYmiUWujEcJAZwpYwEM+tIA3Cpv2JZbZWaT+M1neL
fkSyAvtSDDIK0aQW9LVLEMOwd5YxR8IMf6QGNY2luS3Xtmowbbndc+4Ll8uB
wznnOafYfLzhyArG2PblL/Wnl9Xes0tvHaiSojAoGldrSx+JGDOB8haaFx5e
Vb4GYcSoyQk9AGV/IjP/rwss5wbcnw7awdDJyovODoPlWUngftkT2H9PrB9O
uqHjsttcUvscfs9OQbjzA/h0VzweowM2m/0vbRu9YOk4/L5aL4J1XOMVD32E
EeOtn8E3Iui1qTcOmQ3OQltAhJbm1PsuxzVrWvM0dbNyvtW+sqtpy5xcT7FD
GW5b90PuV363K7CKzcMlnMfxmevOz8yDCefdunZoUP82ACb0lOZuy/b2BKEC
vSH0+9zpmJNfJeXzOvSrpHp8H/orpH48in6HNA/fhH5BmpeTv1vycPJP1Ke9
nPwUk5/yyU/1yE/9yK/GecjPcF7yM/SQfwy9lj9NF7odv+R9mE8XFX8rjMj7
Ksvp9b+7GZH3eUxb2R5PRuR9JxsTiuqTUZjAe9Q8vfjK4I/CUbzX15jnVHBv
DFx4T+9o6sWA4b116X0kIC75YUN25fVcewKcmD+4p23ni4JFMGG9jP6M+NCN
RRjDfndGPWcUbAlMOM9k6EBB1YklEHDevmu+L+unl2P03Fb22Vp2/wMVeskf
xn2QX437Ir8a90l+AfdNfifeg/y1eC/y9+I9ye/De5N/DmPyP8Z88huwHvld
2I/8NA/5aV7yk4f85P0PWXcXng==
"], {
{Hue[0.6, 0.7, 0.5], Opacity[0.7], Arrowheads[Medium], ArrowBox[
            BezierCurveBox[{1, {0.4999999999999994, 0.16444074718311658`}, 2}], 0.02261146496815286], ArrowBox[
            BezierCurveBox[{1, {
              0.4999999999999998, -0.1644407471831159}, 2}], 0.02261146496815286], ArrowBox[
            BezierCurveBox[{2, {1.499999999999998, 0.16444074718311621`}, 3}], 0.02261146496815286], ArrowBox[{2, 3}, 0.02261146496815286], ArrowBox[
            BezierCurveBox[{2, {
              1.4999999999999984`, -0.1644407471831161}, 3}], 0.02261146496815286]}, 
{Hue[0.6, 0.2, 0.8], EdgeForm[{GrayLevel[0], Opacity[0.7]}], DiskBox[1, 0.02261146496815286], DiskBox[2, 0.02261146496815286], DiskBox[3, 0.02261146496815286]}}]],
MouseAppearanceTag["NetworkGraphics"]],
AllowKernelInitialization->False]],
DefaultBaseStyle->{"NetworkGraphics", FrontEnd`GraphicsHighlightColor -> Hue[0.8, 1., 0.6]},
FormatType->TraditionalForm,
FrameTicks->None,
ImageSize->{246.73828125, Automatic}]\), EdgeLabels -> "EdgeWeight"]
Out[1]=

Work with mixed graphs:

In[2]:=
ResourceFunction[
 "WeightedSimpleGraph"][{1 \[UndirectedEdge] 2, 2 \[UndirectedEdge] 1,
   2 \[DirectedEdge] 3, 2 \[DirectedEdge] 3, 3 -> 1}, EdgeLabels -> "EdgeWeight"]
Out[2]=

Merge each vertex using a function and assign vertex weight based on the number of original vertices producing the same result:

In[3]:=
ResourceFunction["WeightedSimpleGraph"][\!\(\*
GraphicsBox[
NamespaceBox["NetworkGraphics",
DynamicModuleBox[{Typeset`graph = HoldComplete[
Graph[{1, 12, 22, 3}, {{{1, 2}, {1, 3}, {2, 4}, {1, 4}}, Null}, {VertexLabels -> {
Placed[Automatic, Below]}, VertexLabelStyle -> {Bold}, VertexSize -> {0.1}, VertexStyle -> {22 -> RGBColor[1, 0, 0], 12 -> RGBColor[1, 0, 0]}}]]}, 
TagBox[GraphicsGroupBox[{
{Hue[0.6, 0.7, 0.5], Opacity[0.7], Arrowheads[Medium], ArrowBox[{{0., 2.}, {-1., 1.}}, 0.05], ArrowBox[{{0., 2.}, {0., 1.}}, 0.05], ArrowBox[BezierCurveBox[CompressedData["
1:eJxTTMoPSmViYGCQAWIQjQYcZvKamy4+tMJ+knvYs1eW/+3l1q8JvnV4lz2f
6dGoQ8H/7Kv2zBQ9O/mofdEJn9g237/2Gp+mT/A4ds5+D+Pbt2bqf+x9ajLO
rq+5Yv/2wVKeKw9+2T9JV5LqFLtlz55WvjO26qf9+7s24hESD+w5JiZ+ufz5
u72sjXnrpNTH9p8SU/aa+X6zr/ZjuP750zP7QzfqRdravthHTA4Vtb/5wj61
S8VPrf6z/bmWUJ+Qtlf293uFbN9mf7J/pMRQY/zrtb3+Y8+Ht3w+2vfnhi65
Y/LWPqTrlNV7xQ/211JCj/qbvbOfz6Y5OeTaW/udHAz3e/69s3ea4y8tEfja
3iwy9OPk3vf2E69y8GlueWHvFxb6J+Xxe/ueTSHZ7f+f2kOC5YO9MRg8hvPn
bey+bp//AM4/x989pXH5bXtGKF8856BYzsFrcHnJil2m9jqX4PwV6W8efT1/
Gs6fYvdyst3Lw3D+g5fX/mzw2Ann/9M608nHugLOh0UYALTwvFk=
"]], 0.05], ArrowBox[{{-1., 1.}, {1., 0.}}, 0.05]}, 
{Hue[0.6, 0.2, 0.8], EdgeForm[{GrayLevel[0], Opacity[
          0.7]}], {DiskBox[{0., 2.}, 0.05], InsetBox[
StyleBox["1",
StripOnInput->False,
FontWeight->Bold], Offset[{0, -2}, {0., 1.95}], ImageScaled[{0.5, 1}],
            
BaseStyle->"Graphics"]}, {
{RGBColor[1, 0, 0], DiskBox[{-1., 1.}, 0.05]}, InsetBox[
StyleBox["12",
StripOnInput->False,
FontWeight->Bold], Offset[{0, -2}, {-1., 0.95}], ImageScaled[{0.5, 1}],
BaseStyle->"Graphics"]}, {
{RGBColor[1, 0, 0], DiskBox[{0., 1.}, 0.05]}, InsetBox[
StyleBox["22",
StripOnInput->False,
FontWeight->Bold], Offset[{0, -2}, {0., 0.95}], ImageScaled[{0.5, 1}],
            
BaseStyle->"Graphics"]}, {DiskBox[{1., 0.}, 0.05], InsetBox[
StyleBox["3",
StripOnInput->False,
FontWeight->Bold], Offset[{0, -2}, {1., -0.05}], ImageScaled[{0.5, 1}],
BaseStyle->"Graphics"]}}}],
MouseAppearanceTag["NetworkGraphics"]],
AllowKernelInitialization->False]],
DefaultBaseStyle->{"NetworkGraphics", FrontEnd`GraphicsHighlightColor -> Hue[0.8, 1., 0.6]},
FormatType->TraditionalForm,
FrameTicks->None]\), Mod[#, 10] &, VertexLabels -> "VertexWeight"]
Out[3]=

Options (1) 

SelfLoops (1) 

Self-loops can also be weighted:

In[4]:=
ResourceFunction["WeightedSimpleGraph"][\!\(\*
GraphicsBox[
NamespaceBox["NetworkGraphics",
DynamicModuleBox[{Typeset`graph = HoldComplete[
Graph[{1, 2}, {{{1, 2}, {1, 2}}, {{1, 1}, {1, 1}, {1, 1}}}]]}, 
TagBox[GraphicsGroupBox[GraphicsComplexBox[CompressedData["
1:eJxdlHtMk1cYxgsSusbNMcWxaQZICZgyBk4WhmE8OGEy5gUkM1ovYYiSLBB0
sGpQa4ApUceUwdgYu1FBEcW0sD8IgqdFSis4KisUBgx0TqAt0FIubTDCcOd8
f9iTfPlycm7v732f512XkrnrsCuPx0tb+p7/6bCC5zT4+z03BlytiLQez64s
LzVBqvx2c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xQffG8Oey6nqqAgFiXk9/Y3A
HaOQiy9Njt9RkNQjz8cIrIkN3mfNCrLx/6HHGJtn6QN1ikNdqGb7fVOe1J8V
6Zb6E71vdLQf1qIOKNl7O8sVia2LGkSzeLYU8YJ9JK3wZfEuyJ6+9KZdCV/G
03PEUlUguA2rhvI68cOJH078cOKHEz+c+OHEDyd+cPwONuf4G9h+P8afzO4z
Mv4H7L1Exs/FE8v4uXg5fo6nl/FzvP8BT1djSw==
"], {
{Hue[0.6, 0.7, 0.5], Opacity[0.7], Arrowheads[Medium], 
{Arrowheads[0.], ArrowBox[BezierCurveBox[{1, 6, 9, 11, 17, 19, 22, 1},
SplineDegree->7], 0.01273]}, 
{Arrowheads[0.], ArrowBox[BezierCurveBox[{1, 29, 32, 34, 40, 42, 45, 1},
SplineDegree->7], 0.01273]}, 
{Arrowheads[0.], ArrowBox[BezierCurveBox[{1, 52, 55, 57, 63, 65, 68, 1},
SplineDegree->7], 0.01273]}, ArrowBox[
            BezierCurveBox[{1, {
              0.5000000000000006, -0.16444074718311627`}, 2}], 0.01273], ArrowBox[
            BezierCurveBox[{1, {0.5000000000000001, 0.16444074718311646`}, 2}], 0.01273]}, 
{Hue[0.6, 0.2, 0.8], EdgeForm[{GrayLevel[0], Opacity[0.7]}], DiskBox[1, 0.01273], DiskBox[2, 0.01273]}}]],
MouseAppearanceTag["NetworkGraphics"]],
AllowKernelInitialization->False]],
DefaultBaseStyle->{"NetworkGraphics", FrontEnd`GraphicsHighlightColor -> Hue[0.8, 1., 0.6]},
FormatType->TraditionalForm,
FrameTicks->None,
ImageSize->{183.34375, Automatic}]\), SelfLoops -> True, EdgeLabels -> "EdgeWeight"]
Out[4]=

Applications (1) 

Produce weighted evolution graph from its full evolution graph for the MultiwaySystem:

In[5]:=
ResourceFunction["WeightedSimpleGraph"][
 ResourceFunction["MultiwaySystem"][{"A" -> "ABA", "AA" -> "B"}, "AABAA", 2, "EvolutionGraphFullStructure"], EdgeLabels -> "EdgeWeight", AspectRatio -> 1/3]
Out[5]=
In[6]:=
ResourceFunction["MultiwaySystem"][{"A" -> "ABA", "AA" -> "B"}, "AABAA", 2, "EvolutionGraphWeightedStructure", EdgeLabels -> "EdgeWeight"]
Out[6]=

Properties and Relations (1) 

Edge weights assigned by WeightedSimpleGraph can also be computed with EdgeMultiplicity:

In[7]:=
ResourceFunction["EdgeMultiplicity"][\!\(\*
GraphicsBox[
NamespaceBox["NetworkGraphics",
DynamicModuleBox[{Typeset`graph = HoldComplete[
Graph[{1, 2}, {{{1, 2}, {1, 2}}, {{1, 1}, {1, 1}, {1, 1}}}]]}, 
TagBox[GraphicsGroupBox[GraphicsComplexBox[CompressedData["
1:eJxdlHtMk1cYxgsSusbNMcWxaQZICZgyBk4WhmE8OGEy5gUkM1ovYYiSLBB0
sGpQa4ApUceUwdgYu1FBEcW0sD8IgqdFSis4KisUBgx0TqAt0FIubTDCcOd8
f9iTfPlycm7v732f512XkrnrsCuPx0tb+p7/6bCC5zT4+z03BlytiLQez64s
LzVBqvx2cnBNG+rPr/7qis4IwW2lUujSju2C5fytMCJ9szaqI6sT/uUhKql5
DKfG14n/ruiC9GNVt6p3aT4SPrDg3Y0HO03Rj9yMSLjcb7CUGrDGoIlcPGpE
QNOBma8tffi1UzIv8zKh/NlI0cKrA4gZH+GNTZng+rlSRx4N4jXPVYGaOTPW
ZwlcCxOHIJ3tD5KsnYDr6YUbIQnDOLdp29xfSZNoS3OU+OiHkbThx1b3Ygt+
0+Zvt3UP40JalPTfw1bkixoLSj4dRsLdgJwfHFbUVV5/HC8ewqgsea7w5BTy
MnYH5kwMotrraJzYOIWbE/OhEd4D+HPdHz1ffGCDx/WeUMViHxot74gOZNsQ
G7KtbVe1ARntQff1F22YvVqdOhbaDR/Ru/FDeTas14n1l+q7sFq6IXdgrw3z
0mPxn3zXiV61UH56pQ0itTx4v087HOrgm4/504gx1Ovrwu/Cvriv5tA30+gJ
fp8/NKLGQUXbmVPCGbgP/Vzme0uDHEtp1jLVDITpjoWqHC1cY8vOZyfPIkfy
isyxRYvWmo4z4U9nMR3aKNn3sgbhHiWyJxfmYLhTy6972IpmUWRJocCOZn/+
M3uvCg9Tlm2yHbNjvKAzt9y/CVLPk2EtxA4/dUFVXGY9qrv036stdujNLRl7
+64w3TjA6YVbl9N1wp33pucJd/8ovZ80sfcb6PuEi6+Lxke4+Cdo/MSF8X1J
+cgJxv8W5Sdcflxofsgcy999mj/C5Tea5pf0sfz70fwTL1afKVof4svqJ6T1
I5msvtO0vqSJ1X8zrT/pZvpYQfVBal7UDzG9qC+SxPQnp/ojhUyfeVSfZA/T
bwXVL7nI9K2l+ia5TP9uVP/Ei/kjmPqDbGX+caP+IZXMXxXUX8Sb+e9t6j+i
Z/7cTf1Jcpl/86h/SQDzdy71N9nB/J9N/U9+Z/3Bg/YHwvWPfNo/iHDVh8t1
v4zDQysOXDkhI/BSNUe0mBF9vFHT+9k1UjtAwk78Y0J0rdD8U9ANIiorNq/g
mZCcp0kRxt0ibh8JrPlrjdCGnbt3TywnkmvuxQffG8Oey6nqqAgFiXk9/Y3A
HaOQiy9Njt9RkNQjz8cIrIkN3mfNCrLx/6HHGJtn6QN1ikNdqGb7fVOe1J8V
6Zb6E71vdLQf1qIOKNl7O8sVia2LGkSzeLYU8YJ9JK3wZfEuyJ6+9KZdCV/G
03PEUlUguA2rhvI68cOJH078cOKHEz+c+OHEDyd+cPwONuf4G9h+P8afzO4z
Mv4H7L1Exs/FE8v4uXg5fo6nl/FzvP8BT1djSw==
"], {
{Hue[0.6, 0.7, 0.5], Opacity[0.7], Arrowheads[Medium], 
{Arrowheads[0.], ArrowBox[BezierCurveBox[{1, 6, 9, 11, 17, 19, 22, 1},
SplineDegree->7], 0.01273]}, 
{Arrowheads[0.], ArrowBox[BezierCurveBox[{1, 29, 32, 34, 40, 42, 45, 1},
SplineDegree->7], 0.01273]}, 
{Arrowheads[0.], ArrowBox[BezierCurveBox[{1, 52, 55, 57, 63, 65, 68, 1},
SplineDegree->7], 0.01273]}, ArrowBox[
            BezierCurveBox[{1, {
              0.5000000000000006, -0.16444074718311627`}, 2}], 0.01273], ArrowBox[
            BezierCurveBox[{1, {0.5000000000000001, 0.16444074718311646`}, 2}], 0.01273]}, 
{Hue[0.6, 0.2, 0.8], EdgeForm[{GrayLevel[0], Opacity[0.7]}], DiskBox[1, 0.01273], DiskBox[2, 0.01273]}}]],
MouseAppearanceTag["NetworkGraphics"]],
AllowKernelInitialization->False]],
DefaultBaseStyle->{"NetworkGraphics", FrontEnd`GraphicsHighlightColor -> Hue[0.8, 1., 0.6]},
FormatType->TraditionalForm,
FrameTicks->None,
ImageSize->{183.34375, Automatic}]\)]
Out[7]=

Version History

  • 1.1.0 – 03 February 2023
  • 1.0.0 – 24 October 2022

Related Resources

License Information