You can download this code by clicking the button below.
This code is now available for download.
This function uses Dijkstra's algorithm from the graph-tool library to find the shortest path between two nodes in an undirected graph.
Technology Stack : Graph-tool
Code Type : Function
Code Difficulty : Intermediate
def find_shortest_path(graph, source, target):
"""
Find the shortest path between two nodes in an undirected graph using Dijkstra's algorithm.
:param graph: graph-tool graph object
:param source: starting node
:param target: target node
:return: shortest path from source to target
"""
from graph_tool.all import dijkstra
# Run Dijkstra's algorithm to find the shortest path from source to target
path = dijkstra(graph, s=source, t=target, all_pairs=False)
return path