# Adjacency Matrix from linestring using GeoPandas

I got my adjacency matrix wrong way round. I got the intersection of edges. I need to have information per node instead. as per https://math.stackexchange.com/questions/1890620/finding-path-lengths-by-the-power-of-adjacency-matrix-of-an-undirected-graph

I want to use adjacency matrix to the power of x to assess a number of paths.

``````from shapely.geometry import *
import geopandas as gpd
import pandas as pd
import os
import descartes # package allows to plot
import matplotlib.pyplot as plt
import fiona

df = Crewe['geometry']

#Crewe.plot(alpha=1)

#Adjecency Matrix that takes value of 1 when there is intersection and 0.0001 if there is not.

line_gdf = Crewe.iloc[0:2500]
ma = line_gdf.geometry.apply(lambda g: line_gdf.intersects(g))
lll = ma < 1  # Where values are low
ma[lll] = 0  # All low values set to x
ma2 = ma
ma2

#Generating Adjecency Matrix to the power of x
from numpy.linalg import matrix_power
ma3 = matrix_power(ma2, 2) #instead of 3 we can take any number
ma3
``````

My matrix looks like that now: I want 0 to represent a node. Now I have 0 as an edge and I know it interacts with edge 4.

``````0   1   2   3   4   5   6   7   8   9   ... 2340    2341    2342    2343    2344    2345    2346    2347    2348    2349
0   1.0 0.0 0.0 0.0 1.0 0.0 1.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
1   0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
2   0.0 0.0 1.0 0.0 0.0 1.0 0.0 1.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
3   0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
4   1.0 0.0 0.0 0.0 1.0 0.0 1.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2345    0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 1.0 1.0 0.0 0.0 0.0 0.0
2346    0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0 1.0 1.0 0.0 0.0
2347    0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0 1.0 1.0 1.0 0.0
2348    0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 1.0 0.0
2349    0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0
``````

``````mpower3=ma2**3