To get neighboring vertices I first tried:
gg = GridGraph[{10, 10, 10, 10}];
VertexList[NeighborhoodGraph[gg, 1, 1]] // AbsoluteTiming
{5.539308, {1, 2, 11, 101, 1001}}
But that is really slow. This is much faster and still uses the new Graph
package:
Union[VertexInComponent[gg, 1, 1], VertexOutComponent[gg, 1, 1]] // AbsoluteTiming
{0.000172, {1, 2, 11, 101, 1001}}'
I believe that this mimics the NeigborhoodGraph
approach above, but is still much faster than using NeighborhoodGraph
:
VertexList[Subgraph[gg, Union[VertexInComponent[gg, 1, 1],
VertexOutComponent[gg, 1, 1]]]] // AbsoluteTiming
{0.000306, {1, 2, 11, 101, 1001}}
The last two approaches are also faster than NeighborhoodVertices
from the GraphUtilities
package:
Needs["GraphUtilities`"]
NeighborhoodVertices[gg, 1, 1] // AbsoluteTiming
{0.017248, {1, 2, 11, 101, 1001}}
I'm working with large graphs that I need to manipulate quickly for several interactive information visualization tools. Any tips on why NeighborhoodGraph is so slow here and how to best use the new Graph package when speed is an issue?
NeighborhoodVertices
. Just replaced it withVertexComponents
. Thanks $\endgroup$ – Cameron Murray Nov 13 '12 at 11:17