Function Repository Resource:

EdgeMultiplicity

Source Notebook

Count the multiplicity of edges in a graph

Contributed by: Nikolay Murzin

ResourceFunction["EdgeMultiplicity"][graph]

returns edge multiplicities for all the edges in a graph.

ResourceFunction["EdgeMultiplicity"][graph, v]

returns edge multiplicities for edges starting with vertex v.

ResourceFunction["EdgeMultiplicity"][graph,All,w]

returns edge multiplicities for edges ending with vertex w.

ResourceFunction["EdgeMultiplicity"][graph,v,w]

returns edge multiplicities starting with vertex v and ending with vertex w.

Details

ResourceFunction["EdgeMultiplicity"] returns an Association with edges as keys and edge multiplicities as values.
The edge multiplicity of a given end vertex in a multigraph is the number of multiple edges sharing that end vertex.
For an undirected graph, an edge is taken to be both an in-edge and an out-edge.
ResourceFunction["EdgeMultiplicity"] works with undirected graphs, directed graphs, multigraphs and mixed graphs.

Examples

Basic Examples (3) 

Count edges in an undirected multigraph:

In[1]:=
ResourceFunction["EdgeMultiplicity"][\!\(\*
GraphicsBox[
NamespaceBox["NetworkGraphics",
DynamicModuleBox[{Typeset`graph = HoldComplete[
Graph[{1, 2}, {Null, {{1, 1}, {1, 2}, {1, 2}}}, {VertexLabels -> {Automatic}}]]}, 
TagBox[GraphicsGroupBox[{
{Hue[0.6, 0.7, 0.5], Opacity[0.7], Arrowheads[0.], ArrowBox[BezierCurveBox[{{1., 0.}, {1.0884792554677603`, 0.1907835323853024}, {1.2873577330053512`, 0.24717977484589673`}, {1.4145283574798084`, 0.1882226960710236}, {
           1.4145283574798089`, -0.18822269607102265`}, {
           1.2873577330053516`, -0.2471797748458961}, {
           1.0884792554677607`, -0.19078353238530207`}, {1., 0.}},
SplineDegree->7], 0.01273], ArrowBox[BezierCurveBox[{{1., 0.}, {
           0.5000000000000006, -0.16444074718311627`}, {0., 1.2246467991473532`*^-16}}], 0.01273], ArrowBox[BezierCurveBox[{{1., 0.}, {0.5000000000000001, 0.16444074718311646`}, {0., 1.2246467991473532`*^-16}}], 0.01273]}, 
{Hue[0.6, 0.2, 0.8], EdgeForm[{GrayLevel[0], Opacity[ 0.7]}], {DiskBox[{1., 0.}, 0.01273], InsetBox["1", Offset[{2, 2}, {1.01273, 0.01273}], ImageScaled[{0, 0}],
BaseStyle->"Graphics"]}, {
           DiskBox[{0., 1.2246467991473532*^-16}, 0.01273], InsetBox["2", Offset[{2, 2}, {0.01273, 0.012730000000000123}], ImageScaled[{0, 0}],
BaseStyle->"Graphics"]}}}],
MouseAppearanceTag["NetworkGraphics"]],
AllowKernelInitialization->False]],
DefaultBaseStyle->{"NetworkGraphics", FrontEnd`GraphicsHighlightColor -> Hue[0.8, 1., 0.6]},
FormatType->TraditionalForm,
FrameTicks->None]\)]
Out[1]=

Count edges in a directed multigraph:

In[2]:=
ResourceFunction["EdgeMultiplicity"][\!\(\*
GraphicsBox[
NamespaceBox["NetworkGraphics",
DynamicModuleBox[{Typeset`graph = HoldComplete[
Graph[{1, 2, 3}, {{{1, 2}, {1, 1}, {1, 2}, {2, 3}, {3, 3}, {3, 3}, {3, 3}},
          Null}, {FormatType -> TraditionalForm, ImageSize -> {154.00390625, Automatic}, VertexLabels -> {Automatic}}]]}, 
TagBox[GraphicsGroupBox[{
{Hue[0.6, 0.7, 0.5], Opacity[0.7], Arrowheads[Medium], ArrowBox[BezierCurveBox[{{0., 3.6739403974420594`*^-16}, {-0.08847925546776053, -0.19078353238530196`}, {-0.2873577330053514, -0.24717977484589607`}, {-0.41452835747980865`, -0.18822269607102282`}, {-0.41452835747980865`, 0.18822269607102343`}, {-0.2873577330053514, 0.24717977484589673`}, {-0.08847925546776059, 0.19078353238530268`}, {0., 3.6739403974420594`*^-16}},
SplineDegree->7], 0.02261146496815286], ArrowBox[BezierCurveBox[{{0., 3.6739403974420594`*^-16}, {
           0.4999999999999994, 0.16444074718311658`}, {1., 2.4492935982947064`*^-16}}], 0.02261146496815286], ArrowBox[BezierCurveBox[{{0., 3.6739403974420594`*^-16}, {
           0.4999999999999998, -0.1644407471831159}, {1., 2.4492935982947064`*^-16}}], 0.02261146496815286], ArrowBox[{{1., 2.4492935982947064`*^-16}, {2., 0.}}, 0.02261146496815286], ArrowBox[BezierCurveBox[{{2., 0.}, {1.7787469647563265`, 0.2755044346046771}, {1.7950435816880512`, 0.3896210085285382}, {1.8420295732133882`, 0.45127864158862974`}, {2.060692288545585, 0.47426101906473683`}, {2.1194708661655373`, 0.4237196901628646}, {2.1591375327522213`, 0.31548509435616207`}, {2., 0.}},
SplineDegree->7], 0.02261146496815286], ArrowBox[BezierCurveBox[{{2., 0.}, {2.2971224322527335`, 0.19124293279387058`}, {2.4089104033200903`, 0.1631071603292102}, {2.4653188959256838`, 0.10993358534918202`}, {
           2.4653188959256838`, -0.10993358534918195`}, {
           2.4089104033200903`, -0.16310716032921005`}, {
           2.297122432252733, -0.19124293279387042`}, {2., 0.}},
SplineDegree->7], 0.02261146496815286], ArrowBox[BezierCurveBox[{{2., 0.}, {
           2.1591375327522218`, -0.31548509435616195`}, {
           2.1194708661655377`, -0.4237196901628646}, {
           2.0606922885455856`, -0.4742610190647367}, {
           1.842029573213389, -0.45127864158862985`}, {
           1.795043581688052, -0.38962100852853854`}, {
           1.778746964756327, -0.2755044346046774}, {2., 0.}},
SplineDegree->7], 0.02261146496815286]}, 
{Hue[0.6, 0.2, 0.8], EdgeForm[{GrayLevel[0], Opacity[
          0.7]}], {
           DiskBox[{0., 3.6739403974420594*^-16}, 0.02261146496815286], InsetBox["1", Offset[{2, 2}, {0.02261146496815286, 0.02261146496815323}], ImageScaled[{0, 0}],
BaseStyle->"Graphics"]}, {
           DiskBox[{1., 2.4492935982947064*^-16}, 0.02261146496815286], InsetBox["2", Offset[{2, 2}, {1.0226114649681528, 0.022611464968153108}], ImageScaled[{0, 0}],
BaseStyle->"Graphics"]}, {DiskBox[{2., 0.}, 0.02261146496815286], InsetBox["3", Offset[{2, 2}, {2.022611464968153, 0.02261146496815286}], ImageScaled[{0, 0}],
BaseStyle->"Graphics"]}}}],
MouseAppearanceTag["NetworkGraphics"]],
AllowKernelInitialization->False]],
DefaultBaseStyle->{"NetworkGraphics", FrontEnd`GraphicsHighlightColor -> Hue[0.8, 1., 0.6]},
FormatType->TraditionalForm,
FrameTicks->None,
ImageSize->{154.00390625, Automatic}]\)]
Out[2]=

Count edges in a mixed multigraph:

In[3]:=
ResourceFunction["EdgeMultiplicity"][
 Graph[{1 -> 2, 1 \[UndirectedEdge] 2, 2 <-> 2, 2 <-> 2, 2 -> 3, 2 -> 3}]]
Out[3]=

Scope (4) 

Count edges starting with a given vertex:

In[4]:=
ResourceFunction[
 "EdgeMultiplicity"][{1 -> 2, 1 -> 2, 2 -> 3, 4 \[UndirectedEdge] 1},
  1]
Out[4]=

Count edges ending with a given vertex:

In[5]:=
ResourceFunction[
 "EdgeMultiplicity"][{1 -> 3, 1 -> 3, 2 -> 3, 3 \[UndirectedEdge] 1}, All, 3]
Out[5]=

Count edges starting and ending with a given vertex:

In[6]:=
ResourceFunction[
 "EdgeMultiplicity"][{1 -> 3, 1 -> 3, 2 -> 3, 3 \[UndirectedEdge] 1}, 1, 3]
Out[6]=

Use patterns:

In[7]:=
ResourceFunction[
 "EdgeMultiplicity"][{1 -> 3, 1 -> 3, 2 -> 3, 3 \[UndirectedEdge] 1}, 1 | 2]
Out[7]=

Version History

  • 1.0.0 – 15 February 2022

Related Resources

License Information