DEV Community

Moontasir Mahmood
Moontasir Mahmood

Posted on • Edited on

How to get shortest path in python when using Apache-AGE Graph Database ?

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
sudo apt-get update
&& sudo apt-get install python3-dev libpq-dev
&& pip install --no-binary :all: psycopg2
Enter fullscreen mode Exit fullscreen mode
  • 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:
pip install apache-age-dijkstra
Enter fullscreen mode Exit fullscreen mode
  • 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

from age_dijkstra import Age_Dijkstra
Enter fullscreen mode Exit fullscreen mode

Making connection to postgresql (when using this docker reepository)

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
)
Enter fullscreen mode Exit fullscreen mode

Get all edges

edges = con.get_all_edge()
Enter fullscreen mode Exit fullscreen mode
  • structure : { v1 : start_vertex, v2 : end_vertex, e : edge_object }

Get all vertices

nodes = []
for x in con.get_all_vertices():
    nodes.append(x['property_name'])
Enter fullscreen mode Exit fullscreen mode

Create adjacent matrices using edges

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
Enter fullscreen mode Exit fullscreen mode

Initialized Graph

from age_dijkstra import  Graph
graph = Graph(nodes, init_graph)
Enter fullscreen mode Exit fullscreen mode

Use dijkstra Algorithm

previous_nodes, shortest_path = Graph.dijkstra_algorithm(graph=graph, start_node="vertices_property_name")
Enter fullscreen mode Exit fullscreen mode

Print shortest Path

Graph.print_shortest_path(previous_nodes, shortest_path, start_node="vertices_property_name", target_node="vertices_property_name")
Enter fullscreen mode Exit fullscreen mode

Create Vertices

con.set_vertices(
    graph_name = "graph_name", 
    label="label_name", 
    property={"key1" : "val1",}
    )
Enter fullscreen mode Exit fullscreen mode

Create Edge

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"}
)
Enter fullscreen mode Exit fullscreen mode

For more information about Apache AGE

License

Apache-2.0 License

Top comments (0)