# Creating graphs from data

**URL:** <https://discourse.julialang.org/t/creating-graphs-from-data/27199>\
**Category:** Statistics\
**Created:** [August 6, 2019, 3:07am UTC](https://discourse.julialang.org/t/creating-graphs-from-data/27199 "2019-08-06T03:07:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)\
**Post date:** [August 6, 2019, 3:07am UTC](https://discourse.julialang.org/t/creating-graphs-from-data/27199/1 "2019-08-06T03:07:10Z")

</div>

A common procedure in data science and mathematics is building graphs/networks from a large collection of data points. I am interested in the constructing the adjacency matrix for the k-nearest neighbor graph of a collection of data points. Is there a library for this ?

I couldn’t find this on Graphs.jl or on NearestNeighbors.jl.

Any help would be much appreciated.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [August 6, 2019, 10:41am UTC](https://discourse.julialang.org/t/creating-graphs-from-data/27199/2 "2019-08-06T10:41:40Z")

</div>

Please check LightGraphs.jl, it is the de facto standard package for graphs in Julia. Then, combine NearestNeighbors.jl with LightGraphs.jl in the way you wish to achieve the solution.

---

<div class="post-metadata">

**Author:** ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)\
**Post date:** [August 6, 2019, 11:36am UTC](https://discourse.julialang.org/t/creating-graphs-from-data/27199/3 "2019-08-06T11:36:22Z")

</div>

Thank you very much for the quick reply. I went over the introduction and descriptions on [https://juliagraphs.github.io/LightGraphs.jl/latest/index.html](https://juliagraphs.github.io/LightGraphs.jl/latest/index.html), but did not find any method that takes an input a data file, or a 2d matrix of data points. Could you direct me to the correct place ?

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [August 6, 2019, 11:43am UTC](https://discourse.julialang.org/t/creating-graphs-from-data/27199/4 "2019-08-06T11:43:17Z")

</div>

You won’t find methods that take as input data files. This is outside the scope of any graph package. What matrix are you referring to? The coordinates of the points? The adjacency matrix?

Please take the time to formulate your problem. After that, take the time to read through the docs of the above mentioned packages. If you have more specific questions, preferably with code, please ask, and we can try help.

---

<div class="post-metadata">

**Author:** ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)\
**Post date:** [August 6, 2019, 12:46pm UTC](https://discourse.julialang.org/t/creating-graphs-from-data/27199/5 "2019-08-06T12:46:30Z")

</div>

Okay, here is what I am looking for,

> Input : A NXd Float64 array, containing N data points x\_1, …, x\_N in R^d, a number k\<\<N
> 
> Create a graph with N vertices corresponding to each point, and and edge from i to j if x\_j is among the k closest points to x\_i.
> 
> Output : The adjacency matrix of this graph.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [August 6, 2019, 1:39pm UTC](https://discourse.julialang.org/t/creating-graphs-from-data/27199/6 "2019-08-06T13:39:30Z")

</div>

Did you actually read the documentation for NearestNeighbors.jl? It seems to do exactly the calculation that you need.

As Julio suggested, just take the information that it provides and build the graph using Lightgraphs.jl if you need some kind of graph algorithm on the resulting graph.

---

<div class="post-metadata">

**Author:** ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)\
**Post date:** [August 7, 2019, 4:54pm UTC](https://discourse.julialang.org/t/creating-graphs-from-data/27199/7 "2019-08-07T16:54:52Z")

</div>

> [@dpsanders](#):
>
> NearestNeighbors.jl

The function  
`NNTree(data, metric; leafsize, reorder)`  
does not do what I am looking for. It wouldn’t work for example, for huge data sets.  
Did you actually read the documentation for NearestNeighbors.jl?

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [August 7, 2019, 7:50pm UTC](https://discourse.julialang.org/t/creating-graphs-from-data/27199/8 "2019-08-07T19:50:01Z")

</div>

> [@iamsuddhasattwa](#):
>
> NearestNeighbors.jl

Did you see the section just below that called “k Nearest Neighbor (kNN) searches”? It’s in the README of NearestNeighbors.jl. It sounds like exactly what you are asking about? cc @iamsuddhasattwa
