# Count $k$-hop neighborhood for every vertex

For a node $v$ of a directed unweighted graph $G$, I define the $k$-hop neighborhood of $v$ as the set of vertices that are reachable from $v$ in $k$ hops or fewer (that is following a path with $k$ edges or fewer). I would like to compute the cardinality of this $k$-hop neighborhood for very node $v$ in $G$.

One option is to simply raise the adjacency matrix of $G$ to the power $k$ using Boolean arithmetic and then sum the rows. This is however rather slow and seems wasteful (consider for example the complete graph or a very sparse graph). Another approach is to naively search from each node but this is at least $O(n d^k)$ time for a $d$-regular graph, for example. This naive method is also wasteful as it doesn't take advantage of any previous computations it has done. The sizes of the neighborhood of two adjacent nodes are surely not independent.

Is there a fast, perhaps randomized, way of solving this problem?

• just to make clear what matrix mult. gives: you can take the $k$-th power of the adjacency matrix and count the number of nonzero entries in each row. this gives $O(n^{\omega}\log k)$ using the best matrix multiplication algorithm, and $O(nm\log k)$ using sparse matrix multiplication if you have a sparse graph. Sep 13 '12 at 19:02
• Can you use a random walk approach? For $2$-hop I can imagine you could add the degree of the all the neighbours of $v$ to get an upper bound. Then perform repeated random walks of length $2$ starting at $v$ and count the number of times you land at the same node. Then use this as the proportion of the upper bound to get your estimate. Assuming all the nodes hold their own degree, the time is roughly proportional to the number of nodes times the number of random walks per node. Maybe the idea can be extended to $k$-hop? Sep 14 '12 at 8:02
• @Sasho Nikolov, Am I right in thinking that for $k=2$ the sparse matrix multiplication complexity is always very pessimistic? If the max degree is less than $\sqrt{n}$, say, then you are always better off doing the naive breadth first search as this takes essentially $O(n^2)$ time. However, as the OP says, this seems not to take into account the redundancies in the calculations you are doing. Sep 16 '12 at 15:23
• @Raphael how do you use BFS to solve the problem in $O(n^2)$ time when the max degree is $O(\sqrt{n})$? Starting a BFS from each node gives $O(nm + n^2)$. You are right that this bound is better than what I wrote above for sparse matrix mult. Sep 17 '12 at 19:40
• Lipoff, your problem is of course reducible to all pairs shortest paths on unweighted undirected graphs. the best bounds i know in that case are indeed matrix multiplication and the trivial BFS from each node. Sep 17 '12 at 19:44

## 1 Answer

There is this quasi-linear time approximation algorithm based on HyperLogLog: https://papers-gamma.link/paper/187