Quantcast
Viewing all articles
Browse latest Browse all 3

Answer by kglr for How to find mean and median number of links of neighboring nodes

Update: We can use AdjacencyList to define a function to apply an arbitrary function to an arbitrary property value of neighbors:

ClearAll[neighborPropertyMap]neighborPropertyMap[foo_: Identity, property_: VertexDegree][g_] :=  ReplaceAll[foo[{}] -> {}] @* foo @*   Map[(property /. Identity -> (#2 &))[g, #] &] /@ AdjacencyList[g]

Examples:

simplegraph = SimpleGraph[graph, Options @ graph]

List of neighbors' degrees:

neighborPropertyMap[] @ simplegraph
{{4, 2}, {2, 4, 4, 2}, {4, 4, 3}, {}, {4, 3, 2, 3}, {4, 3, 2, 3}, {2, 4}, {}, {4, 4}, {3, 4, 4}}

Mean of neighbor degrees:

neighborPropertyMap[Mean] @ simplegraph{3, 3, 11/3, {}, 3, 3, 3, {}, 4, 11/3}

Variance of neighbor degrees:

neighborPropertyMap[Variance] @ simplegraph{2, 4/3, 1/3, {}, 2/3, 2/3, 2, {}, 0, 1/3}

Median:

neighborPropertyMap[Median] @ simplegraph{3, 3, 4, {}, 3, 3, 3, {}, 4, 4}

MinMax:

neighborPropertyMap[MinMax] @ simplegraph{{2, 4}, {2, 4}, {3, 4}, {}, {2, 4}, {2, 4}, {2, 4}, {}, {4, 4}, {3,   4}}

List of neighbors:

neighborPropertyMap[Identity, Identity] @ simplegraph{{"A2", "B2"}, {"A1", "A5", "B1", "B4"}, {"A5", "B1", "B5"}, {}, {"A2", "A3", "B4", "B5"}, {"A2", "A3", "B2", "B5"}, {"A1", "B1"}, {}, {"A2", "A5"}, {"A3", "A5", "B1"}}

VertexWeight's of neighbors:

SeedRandom[77]simplegraph2 = SetProperty[simplegraph, VertexWeight -> {v_ :> RandomInteger[{1, 100}]}];neighborPropertyMap[Identity, PropertyValue[{#, #2}, VertexWeight] &]@simplegraph2
{{8, 88}, {1, 52, 63, 79}, {52, 63, 77}, {}, {8, 88, 79, 77},   {8, 88, 88, 77}, {1, 63}, {}, {8, 52}, {88, 52, 63}}
neighborPropertyMap[N@*Mean, PropertyValue[{#, #2}, VertexWeight] &]@simplegraph2
 {48., 48.75, 64., {}, 63., 65.25, 32., {}, 30., 67.6667}

VertexStyle's of neighbors:

SeedRandom[123]simplegraph3 = SetProperty[simplegraph, VertexStyle -> {v_ :> RandomColor[]}];neighborPropertyMap[Identity, PropertyValue[{#, #2}, VertexStyle] &]@simplegraph3

Image may be NSFW.
Clik here to view.
enter image description here

neighborPropertyMap[Lighter@*Lighter, PropertyValue[{#, #2}, VertexStyle] &]@simplegraph3

Image may be NSFW.
Clik here to view.
enter image description here

neighborPropertyMap[Blend, PropertyValue[{#, #2}, VertexStyle] &]@simplegraph3

Image may be NSFW.
Clik here to view.
enter image description here

Original answer:

Remove the self-loops from graph:

simplegraph = SimpleGraph[graph, Options @ graph]

Image may be NSFW.
Clik here to view.
enter image description here

Get an Association associating each vertex with its neighbors:

neighborsList =  AssociationThread[VertexList[simplegraph],   AdjacencyList[simplegraph]]
<|"A1" -> {"A2", "B2"}, "A2" -> {"A1", "A5", "B1", "B4"}, "A3" -> {"A5", "B1", "B5"}, "A4" -> {}, "A5" -> {"A2", "A3", "B4", "B5"}, "B1" -> {"A2", "A3", "B2", "B5"}, "B2" -> {"A1", "B1"}, "B3" -> {}, "B4" -> {"A2", "A5"}, "B5" -> {"A3", "A5", "B1"}|>

Map VertexDegree on neighborsList to get an Association linking each vertex to the list of vertex degrees of its neighbors:

neighborDegrees = Map[VertexDegree[simplegraph, #] &] /@ neighborsList
<|"A1" -> {4, 2}, "A2" -> {2, 4, 4, 2}, "A3" -> {4, 4, 3}, "A4" -> {},"A5" -> {4, 3, 2, 3}, "B1" -> {4, 3, 2, 3}, "B2" -> {2, 4}, "B3" -> {}, "B4" -> {4, 4}, "B5" -> {3, 4, 4}|>

Alternatively, map Length @* neighborsList on neighborsList (number of neighbors of each neighbor):

Map[Length @* neighborsList] /@ neighborsList == neighborDegrees
True

Now map any function you like to the association values:

foo /@ neighborDegrees
<|"A1" -> foo[{4, 2}], "A2" -> foo[{2, 4, 4, 2}], "A3" -> foo[{4, 4, 3}], "A4" -> foo[{}], "A5" -> foo[{4, 3, 2, 3}], "B1" -> foo[{4, 3, 2, 3}], "B2" -> foo[{2, 4}], "B3" -> foo[{}], "B4" -> foo[{4, 4}], "B5" -> foo[{3, 4, 4}]|>
If[# === {}, {}, Median@#] & /@ neighborDegrees
<|"A1" -> 3, "A2" -> 3, "A3" -> 4, "A4" -> {}, "A5" -> 3, "B1" -> 3, "B2" -> 3, "B3" -> {}, "B4" -> 4, "B5" -> 4|>
Values @ %
 {3, 3, 4, {}, 3, 3, 3, {}, 4, 4}
If[# === {}, {}, Mean@#] & /@ neighborDegrees
<|"A1" -> 3, "A2" -> 3, "A3" -> 11/3, "A4" -> {}, "A5" -> 3, "B1" -> 3, "B2" -> 3, "B3" -> {}, "B4" -> 4, "B5" -> 11/3|>
Values @ %
{3, 3, 11/3, {}, 3, 3, 3, {}, 4, 11/3}

Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>