How to get shortest path in python when using Apache-AGE Graph Database ?
Tags: apacheage
Published: 2023-03-28 09:32:26

To get the shortest path between two nodes using Apache-AGE Graph Database in Python, you can follow these steps:

- Install the following requirements in ubuntu
```bash
sudo apt-get update
&& sudo apt-get install python3-dev libpq-dev
&& pip install --no-binary :all: psycopg2
```

- Install the Apache-AGE Python package: Before you can use Apache-AGE in Python, you need to install the Apache-AGE Python package. You can install it using pip by running the following command:
```bash
pip install apache-age-dijkstra
```

- Now RUN postgreSQL server where you are using Apache-AGE graph database. Make sure you have created a database. Save the database name, username, password. 

Now in python code you can use the package as follow:
### Import
```py
from age_dijkstra import Age_Dijkstra
```
### Making connection to postgresql (when using [this docker reepository](https://github.com/Munmud/apache_age))
```py
con = Age_Dijkstra()
con.connect(
    host="localhost",       # default is "172.17.0.2" 
    port="5430",            # default is "5432"
    dbname="postgresDB",    # default is "postgres"
    user="postgresUser",    # default is "postgres"
    password="postgresPW",  # default is "agens"
    printMessage = True     # default is False
)
```

### Get all edges
```py
edges = con.get_all_edge()
```
- structure : 
`
{
    v1 : start_vertex, 
    v2 : end_vertex,
    e : edge_object
}
`

### Get all vertices
```py
nodes = []
for x in con.get_all_vertices():
    nodes.append(x['property_name'])
```

### Create adjacent matrices using edges
```py
init_graph = {}
for node in nodes:
    init_graph[node] = {}
for edge in edges :
    v1 = edge['v1']['vertices_property_name']
    v2 = edge['v2']['vertices_property_name']
    dist = int(edge['e']['edge_property_name'])
    init_graph
    init_graph[v1][v2] = dist
```

### Initialized Graph
```py
from age_dijkstra import  Graph
graph = Graph(nodes, init_graph)
```

### Use dijkstra Algorithm
```py
previous_nodes, shortest_path = Graph.dijkstra_algorithm(graph=graph, start_node="vertices_property_name")
```

### Print shortest Path
```py
Graph.print_shortest_path(previous_nodes, shortest_path, start_node="vertices_property_name", target_node="vertices_property_name")
```

### Create Vertices
```py
con.set_vertices(
    graph_name = "graph_name", 
    label="label_name", 
    property={"key1" : "val1",}
    )
```

### Create Edge
```py
con.set_edge( 
    graph_name = "graph_name", 
    label1="label_name1", 
    prop1={"key1" : "val1",}, 
    label2="label_name2", 
    prop2={"key1" : "val1",}, 
    edge_label = "Relation_name", 
    edge_prop = {"relation_property_name":"relation_property_value"}
)
```

### For more information about [Apache AGE](https://age.apache.org/)
* Apache Incubator Age: https://age.apache.org/
* Github: https://github.com/apache/incubator-age
* Documentation: https://age.incubator.apache.org/docs/
* apache-age-dijkstra GitHub: https://github.com/Munmud/apache-age-dijkstra
* apache-age-python GitHub: https://github.com/rhizome-ai/apache-age-python

### License
[Apache-2.0 License](https://www.apache.org/licenses/LICENSE-2.0)