Function Repository Resource:

CommonNeighbors

Source Notebook

Return vertices that are adjacent to two vertices

Contributed by: Alejandra Ortiz Duran

ResourceFunction["CommonNeighbors"][g,u,v]

returns a list of vertices adjacent to vertices u and v in the graph g.

Details

The common neighbors of a pair of vertices in a graph are the vertices that are adjacent to both vertices.
ResourceFunction["CommonNeighbors"] returns a list of common neighbors.
ResourceFunction["CommonNeighbors"] works with undirected graphs, directed graphs, weighted graphs, multigraphs, and mixed graphs.

Examples

Basic Examples (1) 

Find the common neighbors in a graph for a pair of vertices:

In[1]:=
ResourceFunction["CommonNeighbors"][\!\(\*
GraphicsBox[
NamespaceBox["NetworkGraphics",
DynamicModuleBox[{Typeset`graph = HoldComplete[
Graph[{1, 2, 3, 4, 5, 6}, {Null, {{1, 2}, {1, 3}, {3, 6}, {4, 6}, {5, 1}, {5, 4}, {
         6, 1}}}, {PerformanceGoal -> "Quality", VertexShapeFunction -> {"Name"}}]], Typeset`boxes, Typeset`boxes$s2d = GraphicsGroupBox[{{
Directive[
Opacity[0.7], 
Hue[0.6, 0.7, 0.5]], 
LineBox[{{
DynamicLocation["VertexID$1", Automatic, Center], 
DynamicLocation["VertexID$2", Automatic, Center]}, {
DynamicLocation["VertexID$1", Automatic, Center], 
DynamicLocation["VertexID$3", Automatic, Center]}, {
DynamicLocation["VertexID$1", Automatic, Center], 
DynamicLocation["VertexID$5", Automatic, Center]}, {
DynamicLocation["VertexID$1", Automatic, Center], 
DynamicLocation["VertexID$6", Automatic, Center]}, {
DynamicLocation["VertexID$3", Automatic, Center], 
DynamicLocation["VertexID$6", Automatic, Center]}, {
DynamicLocation["VertexID$4", Automatic, Center], 
DynamicLocation["VertexID$5", Automatic, Center]}, {
DynamicLocation["VertexID$4", Automatic, Center], 
DynamicLocation["VertexID$6", Automatic, Center]}}]}, {
Directive[
Hue[0.6, 0.2, 0.8], 
EdgeForm[
Directive[
GrayLevel[0], 
Opacity[0.7]]]], 
TagBox[
InsetBox[
BoxData[
FormBox[
PaneBox["1", Alignment -> Center, ImageMargins -> 2], TraditionalForm]], {1.0601905298969303`, 0.9732626818622435}, BaseStyle -> "Graphics"], "DynamicName", BoxID -> "VertexID$1"], 
TagBox[
InsetBox[
BoxData[
FormBox[
PaneBox["2", Alignment -> Center, ImageMargins -> 2], TraditionalForm]], {0., 1.3420272727963332`}, BaseStyle -> "Graphics"], "DynamicName", BoxID -> "VertexID$2"], 
TagBox[
InsetBox[
BoxData[
FormBox[
PaneBox["3", Alignment -> Center, ImageMargins -> 2], TraditionalForm]], {1.15642560628993, 0.}, BaseStyle -> "Graphics"], "DynamicName", BoxID -> "VertexID$3"], 
TagBox[
InsetBox[
BoxData[
FormBox[
PaneBox["4", Alignment -> Center, ImageMargins -> 2], TraditionalForm]], {2.6667097632387313`, 1.1027576704891433`}, BaseStyle -> "Graphics"], "DynamicName", BoxID -> "VertexID$4"], 
TagBox[
InsetBox[
BoxData[
FormBox[
PaneBox["5", Alignment -> Center, ImageMargins -> 2], TraditionalForm]], {1.9299251373459498`, 1.6457440911129178`}, BaseStyle -> "Graphics"], "DynamicName", BoxID -> "VertexID$5"], 
TagBox[
InsetBox[
BoxData[
FormBox[
PaneBox["6", Alignment -> Center, ImageMargins -> 2], TraditionalForm]], {1.8980903409406675`, 0.4556885949465811}, BaseStyle -> "Graphics"], "DynamicName", BoxID -> "VertexID$6"]}}], $CellContext`flag}, 
TagBox[
DynamicBox[GraphComputation`NetworkGraphicsBox[
        3, Typeset`graph, Typeset`boxes, $CellContext`flag], {CachedValue :> Typeset`boxes, SingleEvaluation -> True, SynchronousUpdating -> False, TrackedSymbols :> {$CellContext`flag}},
ImageSizeCache->{{0., 89.}, {-35.29423267546841, 28.680951425468393`}}],
MouseAppearanceTag["NetworkGraphics"]],
AllowKernelInitialization->False,
UnsavedVariables:>{$CellContext`flag}]],
DefaultBaseStyle->{"NetworkGraphics", FrontEnd`GraphicsHighlightColor -> Hue[0.8, 1., 0.6]},
FrameTicks->None,
GridLinesStyle->Directive[
GrayLevel[0.5, 0.4]]]\), 1, 4]
Out[1]=

Scope (6) 

CommonNeighbors works with undirected graphs:

In[2]:=
ResourceFunction["CommonNeighbors"][\!\(\*
GraphicsBox[
NamespaceBox["NetworkGraphics",
DynamicModuleBox[{Typeset`graph = HoldComplete[
Graph[{1, 2, 3, 4, 5, 6}, {Null, {{1, 2}, {1, 3}, {3, 6}, {4, 6}, {5, 1}, {5, 4}, {
         6, 1}}}, {PerformanceGoal -> "Quality", VertexShapeFunction -> {"Name"}}]], Typeset`boxes, Typeset`boxes$s2d = GraphicsGroupBox[{{
Directive[
Opacity[0.7], 
Hue[0.6, 0.7, 0.5]], 
LineBox[{{
DynamicLocation["VertexID$1", Automatic, Center], 
DynamicLocation["VertexID$2", Automatic, Center]}, {
DynamicLocation["VertexID$1", Automatic, Center], 
DynamicLocation["VertexID$3", Automatic, Center]}, {
DynamicLocation["VertexID$1", Automatic, Center], 
DynamicLocation["VertexID$5", Automatic, Center]}, {
DynamicLocation["VertexID$1", Automatic, Center], 
DynamicLocation["VertexID$6", Automatic, Center]}, {
DynamicLocation["VertexID$3", Automatic, Center], 
DynamicLocation["VertexID$6", Automatic, Center]}, {
DynamicLocation["VertexID$4", Automatic, Center], 
DynamicLocation["VertexID$5", Automatic, Center]}, {
DynamicLocation["VertexID$4", Automatic, Center], 
DynamicLocation["VertexID$6", Automatic, Center]}}]}, {
Directive[
Hue[0.6, 0.2, 0.8], 
EdgeForm[
Directive[
GrayLevel[0], 
Opacity[0.7]]]], 
TagBox[
InsetBox[
BoxData[
FormBox[
PaneBox["1", Alignment -> Center, ImageMargins -> 2], TraditionalForm]], {1.0601905298969303`, 0.9732626818622435}, BaseStyle -> "Graphics"], "DynamicName", BoxID -> "VertexID$1"], 
TagBox[
InsetBox[
BoxData[
FormBox[
PaneBox["2", Alignment -> Center, ImageMargins -> 2], TraditionalForm]], {0., 1.3420272727963332`}, BaseStyle -> "Graphics"], "DynamicName", BoxID -> "VertexID$2"], 
TagBox[
InsetBox[
BoxData[
FormBox[
PaneBox["3", Alignment -> Center, ImageMargins -> 2], TraditionalForm]], {1.15642560628993, 0.}, BaseStyle -> "Graphics"], "DynamicName", BoxID -> "VertexID$3"], 
TagBox[
InsetBox[
BoxData[
FormBox[
PaneBox["4", Alignment -> Center, ImageMargins -> 2], TraditionalForm]], {2.6667097632387313`, 1.1027576704891433`}, BaseStyle -> "Graphics"], "DynamicName", BoxID -> "VertexID$4"], 
TagBox[
InsetBox[
BoxData[
FormBox[
PaneBox["5", Alignment -> Center, ImageMargins -> 2], TraditionalForm]], {1.9299251373459498`, 1.6457440911129178`}, BaseStyle -> "Graphics"], "DynamicName", BoxID -> "VertexID$5"], 
TagBox[
InsetBox[
BoxData[
FormBox[
PaneBox["6", Alignment -> Center, ImageMargins -> 2], TraditionalForm]], {1.8980903409406675`, 0.4556885949465811}, BaseStyle -> "Graphics"], "DynamicName", BoxID -> "VertexID$6"]}}], $CellContext`flag}, 
TagBox[
DynamicBox[GraphComputation`NetworkGraphicsBox[
        3, Typeset`graph, Typeset`boxes, $CellContext`flag], {CachedValue :> Typeset`boxes, SingleEvaluation -> True, SynchronousUpdating -> False, TrackedSymbols :> {$CellContext`flag}},
ImageSizeCache->{{0., 78.99999999999999}, {-32.20851237361942, 25.595231123619435`}}],
MouseAppearanceTag["NetworkGraphics"]],
AllowKernelInitialization->False,
UnsavedVariables:>{$CellContext`flag}]],
DefaultBaseStyle->{"NetworkGraphics", FrontEnd`GraphicsHighlightColor -> Hue[0.8, 1., 0.6]},
FrameTicks->None,
GridLinesStyle->Directive[
GrayLevel[0.5, 0.4]]]\), 5, 6]
Out[2]=

Directed graphs:

In[3]:=
ResourceFunction["CommonNeighbors"][\!\(\*
GraphicsBox[
NamespaceBox["NetworkGraphics",
DynamicModuleBox[{Typeset`graph = HoldComplete[
Graph[{1, 2, 3, 4, 5, 6}, {{{1, 3}, {2, 1}, {3, 6}, {4, 6}, {1, 5}, {5, 4}, {6, 1}},
          Null}, {EdgeStyle -> {
Arrowheads[0.08]}, PerformanceGoal -> "Quality", VertexShapeFunction -> {"Name"}}]], Typeset`boxes, Typeset`boxes$s2d = GraphicsGroupBox[{{
Arrowheads[0.03036125800966914], 
Directive[
Opacity[0.7], 
Hue[0.6, 0.7, 0.5]], 
Arrowheads[0.08], 
ArrowBox[{{
DynamicLocation["VertexID$1", Automatic, Center], 
DynamicLocation["VertexID$3", Automatic, Center]}, {
DynamicLocation["VertexID$1", Automatic, Center], 
DynamicLocation["VertexID$5", Automatic, Center]}, {
DynamicLocation["VertexID$2", Automatic, Center], 
DynamicLocation["VertexID$1", Automatic, Center]}, {
DynamicLocation["VertexID$3", Automatic, Center], 
DynamicLocation["VertexID$6", Automatic, Center]}, {
DynamicLocation["VertexID$4", Automatic, Center], 
DynamicLocation["VertexID$6", Automatic, Center]}, {
DynamicLocation["VertexID$5", Automatic, Center], 
DynamicLocation["VertexID$4", Automatic, Center]}, {
DynamicLocation["VertexID$6", Automatic, Center], 
DynamicLocation["VertexID$1", Automatic, Center]}}]}, {
Directive[
Hue[0.6, 0.2, 0.8], 
EdgeForm[
Directive[
GrayLevel[0], 
Opacity[0.7]]]], 
TagBox[
InsetBox[
BoxData[
FormBox[
PaneBox["1", Alignment -> Center, ImageMargins -> 2], TraditionalForm]], {1.0601905248735664`, 0.97326270088256}, BaseStyle -> "Graphics"], "DynamicName",
           BoxID -> "VertexID$1"], 
TagBox[
InsetBox[
BoxData[
FormBox[
PaneBox["2", Alignment -> Center, ImageMargins -> 2], TraditionalForm]], {0., 1.3420273330078685`}, BaseStyle -> "Graphics"], "DynamicName", BoxID -> "VertexID$2"], 
TagBox[
InsetBox[
BoxData[
FormBox[
PaneBox["3", Alignment -> Center, ImageMargins -> 2], TraditionalForm]], {1.1564254908996356`, 0.}, BaseStyle -> "Graphics"], "DynamicName", BoxID -> "VertexID$3"], 
TagBox[
InsetBox[
BoxData[
FormBox[
PaneBox["4", Alignment -> Center, ImageMargins -> 2], TraditionalForm]], {2.6667096733545956`, 1.102757716684402}, BaseStyle -> "Graphics"], "DynamicName", BoxID -> "VertexID$4"], 
TagBox[
InsetBox[
BoxData[
FormBox[
PaneBox["5", Alignment -> Center, ImageMargins -> 2], TraditionalForm]], {1.929925029507699, 1.6457441331995881`}, BaseStyle -> "Graphics"], "DynamicName", BoxID -> "VertexID$5"], 
TagBox[
InsetBox[
BoxData[
FormBox[
PaneBox["6", Alignment -> Center, ImageMargins -> 2], TraditionalForm]], {1.8980902941795241`, 0.45568856301324073`}, BaseStyle -> "Graphics"], "DynamicName", BoxID -> "VertexID$6"]}}], $CellContext`flag}, 
TagBox[
DynamicBox[GraphComputation`NetworkGraphicsBox[
        3, Typeset`graph, Typeset`boxes, $CellContext`flag], {CachedValue :> Typeset`boxes, SingleEvaluation -> True, SynchronousUpdating -> False, TrackedSymbols :> {$CellContext`flag}},
ImageSizeCache->{{-8.881784197001252*^-16, 99.}, {-38.379954601648336`, 31.766673351648322`}}],
MouseAppearanceTag["NetworkGraphics"]],
AllowKernelInitialization->False,
UnsavedVariables:>{$CellContext`flag}]],
DefaultBaseStyle->{"NetworkGraphics", FrontEnd`GraphicsHighlightColor -> Hue[0.8, 1., 0.6]},
FrameTicks->None,
GridLinesStyle->Directive[
GrayLevel[0.5, 0.4]]]\), 1, 4]
Out[3]=

Weighted graphs:

In[4]:=
ResourceFunction["CommonNeighbors"][\!\(\*
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}]\), 3, 4]
Out[4]=

Multigraphs:

In[5]:=
ResourceFunction["CommonNeighbors"][\!\(\*
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}}]}, {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}]\), 3, 4]
Out[5]=

Mixed graphs:

In[6]:=
ResourceFunction["CommonNeighbors"][\!\(\*
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}}}, {FormatType -> TraditionalForm, GraphLayout -> {"Dimension" -> 2}, 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}]\), 2, 3]
Out[6]=

Find common neighbors in a large graphs:

In[7]:=
g = GridGraph[{10, 10, 10, 10}];
In[8]:=
ResourceFunction["CommonNeighbors"][g, 98, 100] // Timing
Out[8]=

Applications (3) 

Find common neighbors in a family network:

In[9]:=
g = ExampleData[{"NetworkGraph", "FamilyGathering"}];
In[10]:=
HighlightGraph[g, {"Nancy", "Linda"}]
Out[10]=

We can identify immediate family members of Nancy and Linda:

In[11]:=
ResourceFunction["CommonNeighbors"][g, "Nancy", "Linda"]
Out[11]=

And highlight them:

In[12]:=
HighlightGraph[g, {{"Nancy", "Linda"}, %}]
Out[12]=

Possible Issues (1) 

If there are no common neighbors an empty list is returned:

In[13]:=
g = HighlightGraph[
  ExampleData[{"NetworkGraph", "FamilyGathering"}], {"John", "Rudy"}]
Out[13]=
In[14]:=
ResourceFunction["CommonNeighbors"][g, "John", "Rudy"]
Out[14]=

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 1.0.0 – 28 October 2024

Related Resources

License Information