## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

# Getting immediate and distant neighbors

The neighbors function lists the neighbors of a given node (excluding itself):

```> neighbors(g, 1)
[1]  2  6 12
> neighbors(bg, "u1")
[1] 12 13 14
> # for a bipartite graph, refer to nodes by node name and
> # get results also as node names
> V(bg)\$name[neighbors(bg,"g1")]
[1] "u2"  "u9"  "u10" ```

The neighborhood function gets the list of neighbors lying at most a specified distance from a given node or a set of nodes. The node in question is always included in the list as it is of distance 0 from itself:

```> #immediate neighbors of node 1
> neighborhood(dg, 1, 1)
[[1]]
[1]  1  2  6 12

> neighborhood(dg, 2, 1)
[[1]]
[1]  1  2  6 12  7 11 15 ```

## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required