Graph algorithms are crucial in solving a wide range of real-world problems, including pathfinding, network routing, and AI-driven decision-making. Among the most advanced and widely used algorithms are:
In this article, we will explore these two powerful algorithms, understand how they work, and see their practical applications.
The Floyd-Warshall Algorithm is a dynamic programming algorithm used to compute the shortest distances between all pairs of nodes in a weighted graph. Unlike Dijkstra or Bellman-Ford, which focus on single-source shortest paths, Floyd-Warshall efficiently finds shortest paths between every pair of nodes.
dist[i][j]
represents the shortest path from node i
to node j
.k
), checking if including k
in the path reduces the shortest distance between nodes i
and j
.dist[i][k] + dist[k][j] < dist[i][j]
, update dist[i][j]
.dist[i][j] = weight(i, j)
, if there is an edge between i
and j
.dist[i][j] = ∞
(infinity) if i
and j
are not directly connected.dist[i][i] = 0
for all i
(distance to itself is zero).k
as an intermediate node):dist[i][j]
by checking if i → k → j
is a shorter path.V
iterations (where V
is the number of vertices), dist[i][j]
contains the shortest path between every pair (i, j)
.#include <iostream>
#include <vector>
#include <climits>
using namespace std;
#define INF INT_MAX
void floydWarshall(vector<vector<int>>& graph, int V) {
vector<vector<int>> dist = graph;
for (int k = 0; k < V; k++) {
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (dist[i][k] != INF && dist[k][j] != INF && dist[i][k] + dist[k][j] < dist[i][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}
cout << "Shortest distances between every pair of nodes:\n";
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (dist[i][j] == INF) cout << "INF ";
else cout << dist[i][j] << " ";
}
cout << endl;
}
}
int main() {
vector<vector<int>> graph = {
{0, 3, INF, INF, INF},
{INF, 0, 1, INF, INF},
{INF, INF, 0, 7, INF},
{INF, INF, INF, 0, 2},
{INF, INF, INF, INF, 0}
};
floydWarshall(graph, 5);
return 0;
}
✅ Network routing protocols (e.g., shortest paths in the internet).
✅ Social network analysis (finding shortest connections between people).
✅ Robotics (path planning for multiple locations).
✅ Traffic navigation systems (computing all-pairs shortest routes).
A* (A-Star) is an informed search algorithm that finds the shortest path between a start node and a goal node using a heuristic function. It is widely used in game AI, robotics, and GPS navigation systems.
A* maintains a priority queue and explores paths using:
n
.n
to the goal.A* prioritizes nodes with the smallest f(n)
, making it more efficient than Dijkstra’s Algorithm for specific goal-oriented searches.
f(n)
.g(n)
and f(n)
for its neighbors.#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <unordered_map>
using namespace std;
struct Node {
int x, y, f, g, h;
Node* parent;
};
struct Compare {
bool operator()(const Node* a, const Node* b) {
return a->f > b->f;
}
};
int heuristic(Node* a, Node* b) {
return abs(a->x - b->x) + abs(a->y - b->y); // Manhattan distance
}
void aStar(Node* start, Node* goal) {
priority_queue<Node*, vector<Node*>, Compare> openSet;
openSet.push(start);
while (!openSet.empty()) {
Node* current = openSet.top();
openSet.pop();
if (current == goal) {
cout << "Path found!\n";
return;
}
// Explore neighbors (For simplicity, this part is omitted)
}
}
int main() {
Node start = {0, 0, 0, 0, 0, nullptr};
Node goal = {5, 5, 0, 0, 0, nullptr};
aStar(&start, &goal);
return 0;
}
✅ Game AI (e.g., pathfinding in video games).
✅ Robotics (autonomous navigation).
✅ GPS navigation systems (efficient route calculation).