cancel
Showing results for 
Search instead for 
Did you mean: 

How to _import_ an adjacency matrix

wallputer
Node Link

This is an absolute newbie question. I am familiar with the adjacency matrix representation of a graph aka excel spreadsheet (can be made into a CSV file) with rows (nodes) and columns (weight of connection between the appropriate indexed nodes). All I want to do is to import this into some Neo4J facility and visualize it using the original GraphViz representations [circular, spring, radial, etc.] I am confused by all the different platforms/frameworks associated with N4J. Please provide me with commented stepwise instructions. I tried searching for "adjacency matrix" in these blogs, N4J site, as well as global Google search associating the term with Neo4J but all to no avail-- discussions center around exporting the data as an adjacency matrix ..Your smart realtime search as I type shows me 'similar' queries except they deal with "export", I want to import. Thank you in advance and I apologize if I have broken any blogging conventions, etc. This is the first time this fossil has blogged in 5 years...

13 REPLIES 13

glilienfield
Ninja
Ninja

As I understand it from a quick read of adjacency matrix representations, it is a description of the relationships among a fixed collection of vertices (nodes in neo4j). The matrix has a non-zero entry at (i, j) when node(i) has an outgoing relationships to node(j). This can be represented as a collection of nodes in neo4j and directed relations between each two nodes that have non-zero entries in the matrix. Values other when 1 represent weights, which can be represented as relationship properties.

Easiest approach is to manually create the N nodes and then create the graph by importing a csv file that identifies the relationships between the nodes. The easiest format would be spreadsheet with two columns, where each row indicates the originating node and terminating node for each relationship. I believe you could create a cypher import script that reads a spreadsheet with the adjacently matrix defined in the rows/columns, but that will be a lot more complex.

I don't have any familiarity with VizGraph. Maybe others can provide feedback here. In terms of native visualization tools, you can use Neo4j Desktop or Bloom. Desktop is probably fine to start with.

As I understand it, you can multiple the adjacently matrix with itself N times to find out which pair of nodes has a path of length N between them, and the number represent number of paths. This type of information can easily be derived using cypher and pattern matching.

You can get started with importing here:

if you are really new, maybe start with reading about cypher:

and taking some of the fundamental graphAcademy courses:

https://graphacademy.neo4j.com/?ref=old&_gl=11t5n66a_gaNTQ5NjY1MDA4LjE2MTI1ODkwMDU._ga_DL38Q8KGQC*MTY0OTEwMDkzNS40MzMuMS4xNjQ5MTA1NzMwLjA.&_ga=2.85547038.647411931.1649009807-549665008.1612589005

info on visualizations:

Thanks a million for your prompt and very useful response.
Indeed, adjacency matrix was the classic way of representing a network using rows and columns, thereby mapping very easily and naturally to an Excel spreadsheet. This is what I used in the 1990s and early 2000s, working with a graphical visualization package from Bell Labs (GraphViz) as the front-end to display resulting graphs.
In the simplest case (and that's how I would like to start my exploration of Neo4J), all I want to do is to simply import the symmetric (undirected) adjacency matrix, defaulting to row number for node name, i.e., nodes are numbered and labeled sequentially starting from 1, and use the "strength" value in the connecting column (say normalized to values from 0 to 1) to show the connection, with the thickness of the line in the visualization indicating the "strength." Is there any way you can share a one-liner/small script for the import? I found all kinds of complicated "relationship" examples relating to movie stars and movies and such, but nothing for an abstract graph, e.g., a completely connected and undirected graph of say 10 nodes, i.e., adjacency matrix is all ones! Thanks in advance....

glilienfield
Ninja
Ninja

I think you will find neo4j very interesting for your application.

To keep is simple, I provided one simple script to create the initial set of nodes. These will need to be created before you import the relationships. Just change the number representing the number of nodes to the number you have and run it (4 below).

with 4 as noOfNodes
unwind range(1,noOfNodes) as index
create(n:Node{id: index})

The following script will read the spreadsheet and relate the nodes using the 'strengths' in the spreadsheet. Neo4j requires every relationship to be directed. The direction will be from the node represented by the row to the node represented by the column. You can query for a direction or no direction. You can also add relationships in both directions if that is what your model requires.

load csv from "file:///Book1.csv" as line
with line skip 1
with line, toInteger(line[0]) as startNode, size(line)-1 as noOfRelationships
match(n:Node{id: startNode})
unwind range(1, noOfRelationships) as index
match(m:Node{id: index})
create(n)-[r:REL]->(m)
set r.strength = toFloat(line[index])

this is the result for the sample spreadsheet;

You can run the following script to delete everything if you want to start over:

match(n) detach delete n

Download the spreadsheet and move it to the databases 'import' directory. Add more rows and columns if you want. You will need to change the 'node creation' script with the new node number. The relationship import script adapts to the number of columns and rows. See the screenshot below to see where to access the import folder.

Book1.txt (55 Bytes)

let me or anyone else know if you have any questions.

Many thanks for the detailed instructions, exactly what I needed to kick the tires--actually, this seemingly simple application, coupled with the powerful and dynamic visualization features that I have seen in the online demos, makes the N4J suite a wonderful 'Network Army' knife. Thanks again....

glilienfield
Ninja
Ninja

Also, look into Bloom for your visualization. I have not used it yet, but it looks powerful. Neo4j Desktop doesn't let you dynamically set the relationship widths based a relationship property. You can change the widths manually.

glilienfield
Ninja
Ninja

You are welcome. Wait for the excitement you will have when the lightbulb goes off after you have grasped cypher. It’s intuitive and you can do so much with a few lines. It is a very declarative language. You kinda tell it what you want, not how to get what you want. It is much better than sql.

snowflake
Node Link

Hi,I am new to Neo4j and still trying to understand it.I have an adjacency matrix of size 600*600 which i want to import to neo4j and build the graph and then i need to perform node2vec on it.I tried with the above code to import adjacency matrix but it shows 'no changes,no records'.Any help on this matter would be appreciated.

Thanks

Can you post the cypher code you used and the csv file? 

Hi,

with 11 as noOfNodes
unwind range(1,noOfNodes) as index
create(n:Node{id: index})
load csv from "file:///book4.csv" as line
with line skip 1
with line, toInteger(line[0]) as startNode, size(line)-1 as noOfRelationships
match(n:Node{id: startNode})
unwind range(1, noOfRelationships) as index
match(m:Node{id: index})
create(n)-[r:REL]->(m)
set r.strength = toFloat(line[index])
Sorry cant share the excel here,but i tried with a small adjacency matrix 11*11 which is like this
0 0 0 0.402892 0 0.202454 0 0 0.134067 0 0.196873
0 0 0 0.394285 0 0.287661 0 0 0.186636 0 0.249018
0 0 0 0.245561 0 0 0.218978 0 0 0 0
0.402892 0.394285 0.245561 0 0.304217 0.266478 0.270659 0.366501 0.241004 0.403725 0.211862
0 0 0 0.304217 0 0.169691 0.197938 0 0 0 0.160712
0.202454 0.287661 0 0.266478 0.169691 0 0.320572 0.208562 0 0.214771 0
0 0 0.218978 0.270659 0.197938 0.320572 0 0 0.240021 0.163761 0.282782
0 0 0 0.366501 0 0.208562 0 0 0.167866 0 0.203796
0.134067 0.186636 0 0.241004 0 0 0.240021 0.167866 0 0.176603 0
0 0 0 0.403725 0 0.214771 0.163761 0 0.176603 0 0.233268
0.196873 0.249018 0 0.211862 0.160712 0 0.282782 0.203796 0 0.233268 0

Ok. Look at the original excel I uploaded. It has a header row and a header column. Each is a sequential list of integers starting a lt one. The column numbers are used to match the nodes before creating the relationship. Try fixing your spreadsheet and try the import again. Let me know if it doesn’t work. 

Hi, i tried with the headers too. still it shows (no changes,no records).Thanks in advance for your help

  0 1 2 3 4 5 6 7 8 9 10 11
0 0 0 0 0.402892 0 0.202454 0 0 0.134067 0 0.196873 0
1 0 0 0 0.394285 0 0.287661 0 0 0.186636 0 0.249018 0
2 0 0 0 0.245561 0 0 0.218978 0 0 0 0 0
3 0.402892 0.394285 0.245561 0 0.304217 0.266478 0.270659 0.366501 0.241004 0.403725 0.211862 0.285246
4 0 0 0 0.304217 0 0.169691 0.197938 0 0 0 0.160712 0
5 0.202454 0.287661 0 0.266478 0.169691 0 0.320572 0.208562 0 0.214771 0 0
6 0 0 0.218978 0.270659 0.197938 0.320572 0 0 0.240021 0.163761 0.282782 0.207581
7 0 0 0 0.366501 0 0.208562 0 0 0.167866 0 0.203796 0
8 0.134067 0.186636 0 0.241004 0 0 0.240021 0.167866 0 0.176603 0 0
9 0 0 0 0.403725 0 0.214771 0.163761 0 0.176603 0 0.233268 0
10 0.196873 0.249018 0 0.211862 0.160712 0 0.282782 0.203796 0 0.233268 0 0
11 0 0 0 0.285246 0 0 0.207581 0 0 0 0 0

Hi,

I got it now,I  made mistake in entering the correct number of nodes.It would be really helpful if you could describe how to use nodetovec algorithm on a very huge number of nodes

Thanks once again for your help

I have not used the nodetovec algorithm. Maybe you can find some useful guidance in the GDS documentation, though I did not see nodetovec included. From my quick glance on what it is, it would be an algorithm you would need to develop using the Neo4j API so you can traverse your graph. In my opinion it is not something you can achieve using cypher.

Maybe the original poster @wallputer of this message can provide some insight.  

Do you have starting point?  

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.