# How does the fadjlist field of simplegraphs in LightGraphs package work?

**URL:** https://discourse.julialang.org/t/how-does-the-fadjlist-field-of-simplegraphs-in-lightgraphs-package-work/19972
**Category:** General Usage
**Created:** [January 23, 2019, 10:55am UTC](https://discourse.julialang.org/t/how-does-the-fadjlist-field-of-simplegraphs-in-lightgraphs-package-work/19972 "2019-01-23T10:55:44Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)
#### Post date: [January 23, 2019, 10:55am UTC](https://discourse.julialang.org/t/how-does-the-fadjlist-field-of-simplegraphs-in-lightgraphs-package-work/19972/1 "2019-01-23T10:55:44Z")

</div>

I think this is really strange. ;(

```julia
_/ |\ __'_|_|_|\__'_| | Official http://julialang.org/ release
|__/ | x86_64-pc-linux-gnu

julia> using LightGraphs

julia> g=LightGraphs.Grid([2,2])
{4, 4} undirected simple Int64 graph

julia> g.ne
4

julia> g.fadjlist
4-element Array{Array{Int64,1},1}:
 [2, 3]
 [1, 4]
 [1, 4]
 [2, 3]

```

What exactly is this .fadjlist ? Is this forward adjacency list ?

---

<div class="post-metadata">

### Author: ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)
#### Post date: [January 23, 2019, 1:15pm UTC](https://discourse.julialang.org/t/how-does-the-fadjlist-field-of-simplegraphs-in-lightgraphs-package-work/19972/2 "2019-01-23T13:15:39Z")

</div>

I figured it out. `.fadjlist` is a vector of vectors. Let’s call it vector `v`. Then, `v[i]` is the indices of the vertices which are adjacent to the vertex `i`. Apparently, each vertex in the graph has an index associated with it by default. Good to know.

---

<div class="post-metadata">

### Author: ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)
#### Post date: [January 24, 2019, 4:40am UTC](https://discourse.julialang.org/t/how-does-the-fadjlist-field-of-simplegraphs-in-lightgraphs-package-work/19972/3 "2019-01-24T04:40:40Z")

</div>

This is true, but you should never use `.fadjlist` if you want any sort of guarantee that your code will continue to work. To get the forward adjacency list of a `SimpleGraph`, please use the `fadj` accessor.

Similarly, do not access `.ne` directly. `ne()` is the accessor. In general, that is, you should not directly access fields of graph structs in LightGraphs. (The current exception is for structs returned from the shortest paths algorithms, and even that should change in time.)

---

<div class="post-metadata">

### Author: ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)
#### Post date: [January 24, 2019, 9:28am UTC](https://discourse.julialang.org/t/how-does-the-fadjlist-field-of-simplegraphs-in-lightgraphs-package-work/19972/4 "2019-01-24T09:28:06Z")

</div>

@anon94023334, I understand this restriction control. But I do not quite understand how to use the `fadj` accessor.

```julia
julia> g=LightGraphs.Grid([3,3])
{9, 12} undirected simple Int64 graph

julia> g.ne
12

julia> g.fadj
ERROR: type SimpleGraph has no field fadj
Stacktrace:
 [1] macro expansion at /home/devel/.julia/v0.6/Atom/src/repl.jl:118 [inlined]
 [2] anonymous at ./<missing>:?

```

Plus, `fieldnames(g)` would only give me two choices. one is `.ne`, and the other one is `.fadjlist`. Could you let me know?

---

<div class="post-metadata">

### Author: ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)
#### Post date: [January 24, 2019, 12:55pm UTC](https://discourse.julialang.org/t/how-does-the-fadjlist-field-of-simplegraphs-in-lightgraphs-package-work/19972/5 "2019-01-24T12:55:38Z")

</div>

`fadj` and `ne` are functions that take the graph as an argument, so:

```julia
fadj(g)
ne(g)

```

---

<div class="post-metadata">

### Author: ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)
#### Post date: [January 24, 2019, 1:47pm UTC](https://discourse.julialang.org/t/how-does-the-fadjlist-field-of-simplegraphs-in-lightgraphs-package-work/19972/6 "2019-01-24T13:47:35Z")

</div>

@anon94023334, not quite. Did I miss something?

```julia
julia> using LightGraphs

julia> g=LightGraphs.Grid([3,3])
{9, 12} undirected simple Int64 graph

julia> fadj(g)
ERROR: UndefVarError: fadj not defined
Stacktrace:
 [1] macro expansion at /home/devel/.julia/v0.6/Atom/src/repl.jl:118 [inlined]
 [2] anonymous at ./<missing>:?

julia> ne(g)
12

julia> fadjlist(g)
ERROR: UndefVarError: fadjlist not defined
Stacktrace:
 [1] macro expansion at /home/devel/.julia/v0.6/Atom/src/repl.jl:118 [inlined]
 [2] anonymous at ./<missing>:? 

```

---

<div class="post-metadata">

### Author: ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)
#### Post date: [January 24, 2019, 3:43pm UTC](https://discourse.julialang.org/t/how-does-the-fadjlist-field-of-simplegraphs-in-lightgraphs-package-work/19972/7 "2019-01-24T15:43:00Z")

</div>

Yes. Because getting forward adjacencies is not part of the API contract and is not valid for other graph types (e.g., `SimpleWeightedGraph`s), it is not exported. Try `LightGraphs.SimpleGraphs.fadj()`. But be aware that use of `fadj` is a code smell that can indicate you’re taking the wrong approach to a given problem. Its primary use is to provide efficient / fast support for core functions like `neighbors`.

---

<div class="post-metadata">

### Author: ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)
#### Post date: [January 24, 2019, 3:55pm UTC](https://discourse.julialang.org/t/how-does-the-fadjlist-field-of-simplegraphs-in-lightgraphs-package-work/19972/8 "2019-01-24T15:55:14Z")

</div>

@anon94023334, I see. Thank you so much!

---

<div class="post-metadata">

### Author: ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)
#### Post date: [January 24, 2019, 4:16pm UTC](https://discourse.julialang.org/t/how-does-the-fadjlist-field-of-simplegraphs-in-lightgraphs-package-work/19972/9 "2019-01-24T16:16:22Z")

</div>

My pleasure. Feel free to join us on slack in the #graphs channel if you have any other questions 🙂

---

<div class="post-metadata">

### Author: ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)
#### Post date: [January 24, 2019, 5:03pm UTC](https://discourse.julialang.org/t/how-does-the-fadjlist-field-of-simplegraphs-in-lightgraphs-package-work/19972/10 "2019-01-24T17:03:00Z")

</div>

@anon94023334, could you kindly let me know how to join? Is it a open channel which everyone can join?

---

<div class="post-metadata">

### Author: ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)
#### Post date: [January 24, 2019, 5:05pm UTC](https://discourse.julialang.org/t/how-does-the-fadjlist-field-of-simplegraphs-in-lightgraphs-package-work/19972/11 "2019-01-24T17:05:34Z")

</div>

go to [https://slackinvite.julialang.org](https://slackinvite.julialang.org) to get an invitation, then to [https://julialang.slack.com](https://julialang.slack.com) to log in. We hang out in #graphs but there are dozens of other great channels.
