Why does VertexComponent not return all vertices at distance <= 3?
Something seems broken. As a workaround you might do something like:
gg[1] := NeighborhoodGraph[g, 50, 1]
gg[k_] := NeighborhoodGraph[g, gg[k - 1], 1]
HighlightGraph[g, gg[3], GraphHighlightStyle -> "Thick"]
Here's an alternate implementation of VertexComponent
using AdjacencyMatrix
:
vertexComponent[g_, v_, n___] := Module[{vec = initialVector[g, v, n]},
ivertexComponent[g, v, vec, n] /; vec =!= $Failed
] /; ArgumentCountQ[vertexComponent, Length @ Hold[g, v, n], 2, 3]
initialVector[g_, v_, n___] := Module[{vl = VertexList[g]},
Which[
MemberQ[vl, v],
Boole[Thread[vl == v]],
ListQ[v] && ContainsAll[vl, v],
Boole[MemberQ[v, #]& /@ vl],
True,
Message[vertexComponent::inv, HoldForm[OutputForm @ vertexComponent[g,v,n]], v, "vertex"];
$Failed
]
]
ivertexComponent[g_, v_, vec_, n___] := Module[{p, vl = VertexList[g], a},
p = Replace[{n},
{
{i_Integer?NonNegative} :> i,
{} -> Length[vl],
{x_} :> (Message[vertexComponent::inv, HoldForm[OutputForm @ vertexComponent[g,v,n]], x, "parameter"]; $Failed)
}
];
(
a = AdjacencyMatrix[g] + IdentityMatrix[Length[vl], SparseArray];
Pick[vl, Unitize @ MatrixPower[a, p, vec], 1]
) /; p =!= $Failed
]
For your example:
vertexComponent[g, 50, 3]
{17, 26, 27, 28, 29, 30, 36, 37, 38, 39, 40, 41, 42, 47, 48, 49, 50, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64, 69, 70, 71, 72, 73, 74, 75, 82, 83, 84}
Comparison:
Complement[vertexComponent[g, 50, 3], VertexComponent[g, 50, 3]]
{42, 69, 75}