The website content provides an in-depth exploration of the Google Maps shortest path algorithm, detailing the fundamentals of graph theory, the implementation of Dijkstra's algorithm using Python packages NetworkX and Graph-tool, and its practical applications in various fields.
Abstract
The article delves into the core logic behind Google Maps' route planning functionality, which is a practical application of graph theory. It explains the concept of graphs, nodes, and edges, and the significance of finding the shortest path within them. The author emphasizes the use of Dijkstra's algorithm by Google Maps to calculate the most efficient routes and discusses the algorithm's limitations, such as its inability to handle negative edge weights. The article also guides readers through implementing Dijkstra's algorithm using two powerful Python libraries: NetworkX and Graph-tool, providing code examples and illustrating the process with visual aids. The author concludes by acknowledging the simplicity these packages bring to complex network analysis and encourages further exploration and learning in this domain.
Opinions
The author expresses gratitude towards their professors at Freie Universität Berlin and Technische Universität Berlin for their understanding of Dijkstra's algorithm.
The author suggests that the actual implementations of navigation systems are far more complex than the examples provided.
There is an appreciation for the NetworkX and Graph-tool packages for their extensive functionalities and ease of use in handling graph structures and algorithms.
The author encourages readers to follow their work on LinkedIn and Medium and to support them by buying them a coffee, indicating a desire to build a community and share knowledge.
The author invites readers to reach out with questions or issues related to the topic, showing a willingness to assist and engage with the audience.
Google Maps Shortest Path Algorithm: Fundamentals and Implementation
Get to implement the core logic using NetworkX and Graph-Tool
Not all those who ‘use Google Maps’, are Lost! — Author
Google-Maps
Google Mapsis a web mapping platform and consumer application offered by Google. It offers satellite imagery, aerial photography, street maps, 360° interactive panoramic views of streets (Street View), real-time traffic conditions, and route planning for traveling by foot, car, bike, air (in beta), and public transportation¹
One of the biggest reasons why Google Maps is popular is you can find the best and the shortest path from one specific location to another, despite you are driving, biking, walking, or using public transport, which eventually helps you to plan your route seamlessly.
To understand the algorithm behind one of the World’s Most Popular functionality we will have to dig into the fundamentals of Graph.
Graph
A graph is made up of Vertices (also called nodes or points) which are connected by Edges (also called links or lines). There are two common types of Graph :
Mathematically, a Graph is an ordered pair G = (V, E) where :
V is a set of vertices (also called nodes or points)
E ⊆ {{x,y} | x,y ∈ V and x ≠y}, a set of edges (also called links or lines).
Applications of Graphs :
Computer Science
Mathematics
Physics
Chemistry
Biology
and many more…
One of the most common applications of Graph is finding the Shortest Path which is finding a path between two vertices (or nodes) such that the sum of the weights of its constituent edges is minimized.
Weights can be assigned to each edge of the Graph, which is called Weighted Graphs, and are used to represent structures in which pairwise connections have some numerical values. For example, if a graph represents a road network, the weights could represent the length of each road. There may be several weights associated with each edge, including distance, travel time, or monetary cost. Such weighted graphs are commonly used to program GPS’s, and travel-planning search engines that compare flight times and costs.²
Routing on such a graph can be accomplished effectively using any of the numbers of routing algorithms mentioned above. Different weightings such as distance, cost, or accessibility may be associated with each edge, and sometimes with nodes.³
There are a lot of algorithms that gives the shortest path :
Dijkstra’s Algorithm
Bellman-Ford Algorithm
Floyd-Warshall Algorithm
Johnson’s Algorithm
A* (Star) Algorithm
and more…
Dijkstra’s Algorithm
Google Maps uses Dijkstra’s Algorithm to calculate the Shortest Path between the Source and the Destination.⁴
Dijkstra’s algorithm finds all the shortest paths from the source vertex to every other vertex by iteratively “growing” the set of vertices S to which it knows the shortest path. At each step of the algorithm, the next vertex added to S is determined by a priority queue. The queue contains the vertices in V — S prioritized by their distance label, which is the length of the shortest path seen so far for each vertex. The vertex u at the top of the priority queue is then added to S, and each of its out-edges is relaxed: if the distance to u plus the weight of the out-edge (u, v) is less than the distance label for v then the estimated distance for vertex v is reduced. The algorithm then loops back, processing the next vertex at the top of the priority queue. The algorithm finishes when the priority queue is empty.⁵
I would really like to thank My Professors at Freie Universität Berlin and Technische Universität Berlin, for making me understand this algorithm (and other algorithms) in the best possible way.
Dijkstra Animation (From Wikipedia)
Disadvantages of Dijkstra Algorithm
The major disadvantage of the algorithm is the fact that it does a blind search thereby consuming a lot of time and waste of necessary resources.
It cannot handle negative edges. This leads to acyclic graphs and most often cannot obtain the right shortest path.⁶
Some other applications of the Dijkstra Algorithm⁷
Social Networking Applications
Telephone Network
IP Routing
Flighting Agenda
Robotic Path
and many more…
Packages & Implementation
Thankfully, there are existing Python Packages that already have implemented the Dijkstra Algorithms (and others). I came across two extremely powerful Python Based packages which helped me to explore Graphs.
NetworkX
Graph-tool
NetworkX
NetworkX is a Python package for the creation, manipulation, and study of the structure, dynamics, and functions of complex networks.⁸
Installation
To Install the Current Release of the NetworkX with pip.
pip install networkx
To install the Development Release of the NetworkX with pip. (Git should be installed in your system)
git clone https://github.com/networkx/networkx.git
cd networkx
pip install -e .[default]
NetworkX is a very powerful package with various classes and methods, for now, we will,l stick to the required methods to fetch the shortest path using Dijkstra’s Algorithm.
Implementation
Example of a Weighted Edge Graph (By Author)
Looking at the above Example, the best path from Vertex 1 to Vertex 9 would be 1 -> 2 -> 4 ->5 ->7 -> 8 -> 9
Using the networkx.dijkstra_path() method the shortest path can be found by passing the Graph, Source and the Destination vertex numbers.
Graph-tool is an efficient Python module for the manipulation and statistical analysis of graphs (a.k.a. networks). Contrary to most other python modules with similar functionality, the core data structures and algorithms are implemented in C++.
Installation
For Graph-Tool the installation is not as simple as other packages for Windows operating system. Graph-tool is a C++ library wrapped in Python, and it has many C++ dependencies such as Boost, CGAL, and expat, which are not installable via Python-only package management systems such as pip
Open CMD, and type docker images, and a list of existing images will be shown as an output on the console.
Type docker pulls Tiago Peixoto/graph-tool to pull the graph-tool package.
Type docker image again and graph-tool image should be shown on the console.
Steps 2,3 and 4 (From Author)
5. Run docker run -p 8888:8888 -p 6006:6006 -it -u user -w /home/user tiagopeixoto/graph-tool bash on the CMD console. This will forward the necessary ports to the container, so that your native browser can connect to it at http://localhost:8888/
6. Start the Jupyter Notebook server with jupyter notebook --ip 0.0.0.0
7. Copy Paste one of the URLs from the CMD console to the Browser (Highlighted in the below image).
Steps 5,6 and 7 (From Author)
8. Jupyter Server will start with the graph-tool package installed in it.
9. Open a new Jupyter Notebook, and you are good to go.
Implementation
Similar graph as above drew by Graph-tool Package (By Author)
>>>[1, 2, 4, 5, 7, 8, 9]
Both my implementations from NetworkX and Graph-Tool packages gave the path of 1 -> 2 -> 4 -> 5 -> 7 -> 8 -> 9 which is the best path with the least costs from Source 1to Destination 9.
These algorithms form the spine of the Navigation Systems, of course, the actual implementations are way more complex than this.
Conclusion
It is always interesting to know how things work at the back of one of the most used functionalities (Route Planning with Google Maps). Graph is one of the most powerful structures with innumerable strong applications.
Built-in methods and classes of packages like NetworkX and Graph-tool make implementing Graph and their complex functionalities exceedingly effortless. Both the packages are extremely powerful and have functionalities ranging from changing the properties of Node/Edges up to processing Complex Networks.
[6] S Sanan, L Jain, B Kappor, “Shortest Path Algorithm” in International Journal of Application or Innovation in Engineering & Management (IJAIEM), Volume 2, Issue 7, pp 316–217, July 2013
You can become a member of Medium by signing up here and if you would like get notified of my articles you can subscribe here.
If you run into any issues regarding this topic, do not hesitate to comment on the article or DM me on LinkedIn, I will try to revert as fast as possible.