C Shortest Path Algorithms šŸŽÆ

beginner
10 min

C Shortest Path Algorithms šŸŽÆ

Welcome to your C programming journey into the world of Shortest Path Algorithms! In this lesson, we'll explore various techniques to find the shortest path between nodes in a graph, which is a fundamental concept in computer science with numerous real-world applications.

What is a Shortest Path Algorithm? šŸ“

Shortest Path Algorithms help find the shortest path between nodes in a graph, given some specific criteria. This is crucial for navigation systems, social networks, and more.

Breadth-First Search (BFS) šŸ’”

Let's start with Breadth-First Search (BFS), a simple algorithm to traverse and search trees or graphs. BFS visits all vertices of a graph before backtracking.

c
#include <stdio.h> #include <limits.h> #include <stdbool.h> #define V 9 int minDistance(int dist[], bool sptSet[]) { int min = INT_MAX, minIndex; for (int v = 0; v < V; v++) if (sptSet[v] == false && dist[v] <= min) min = dist[v], minIndex = v; return minIndex; } void printSolution(int dist[], int n) { printf("Vertex Distance from Source\n"); for (int i = 0; i < V; i++) printf("%d \t %d\n", i, dist[i]); } void BFS(int graph[V][V], int src) { int dist[V]; bool sptSet[V]; for (int i = 0; i < V; i++) dist[i] = INT_MAX, sptSet[i] = false; dist[src] = 0; sptSet[src] = true; for (int count = 0; count < V - 1; count++) { int u = minDistance(dist, sptSet); sptSet[u] = true; for (int v = 0; v < V; v++) if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v]) dist[v] = dist[u] + graph[u][v]; } printSolution(dist, V); } int main() { int graph[V][V] = {{0, 4, 0, 0, 0, 0, 0, 8, 0}, {4, 0, 8, 0, 0, 0, 0, 0, 0}, {0, 8, 0, 7, 0, 4, 0, 0, 2}, {0, 0, 7, 0, 9, 14, 0, 0, 0}, {0, 0, 0, 9, 0, 10, 0, 0, 0}, {0, 0, 4, 14, 10, 0, 2, 0, 0}, {0, 0, 0, 0, 0, 2, 0, 1, 6}, {8, 0, 0, 0, 0, 0, 1, 0, 7}, {0, 0, 2, 0, 0, 0, 6, 7, 0}}; BFS(graph, 0); return 0; }

šŸ’” Pro Tip: The given example uses an adjacency matrix to represent the graph, and the source vertex is 0.

Dijkstra's Algorithm šŸ’”

Dijkstra's Algorithm is used to find the shortest path between nodes in a graph with non-negative edge weights.

c
#include <stdio.h> #include <limits.h> #include <stdbool.h> #define V 9 int minDistance(int dist[], bool sptSet[]) { int min = INT_MAX, minIndex; for (int v = 0; v < V; v++) if (sptSet[v] == false && dist[v] <= min) min = dist[v], minIndex = v; return minIndex; } void printSolution(int dist[], int n) { printf("Vertex Distance from Source\n"); for (int i = 0; i < V; i++) printf("%d \t %d\n", i, dist[i]); } void dijkstra(int graph[V][V], int src) { int dist[V]; bool sptSet[V]; for (int i = 0; i < V; i++) { dist[i] = INT_MAX; sptSet[i] = false; } dist[src] = 0; for (int count = 0; count < V - 1; count++) { int u = minDistance(dist, sptSet); sptSet[u] = true; for (int v = 0; v < V; v++) if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v]) dist[v] = dist[u] + graph[u][v]; } printSolution(dist, V); } int main() { int graph[V][V] = {{0, 4, 0, 0, 0, 0, 0, 8, 0}, {4, 0, 8, 0, 0, 0, 0, 0, 0}, {0, 8, 0, 7, 0, 4, 0, 0, 2}, {0, 0, 7, 0, 9, 14, 0, 0, 0}, {0, 0, 0, 9, 0, 10, 0, 0, 0}, {0, 0, 4, 14, 10, 0, 2, 0, 0}, {0, 0, 0, 0, 0, 2, 0, 1, 6}, {8, 0, 0, 0, 0, 0, 1, 0, 7}, {0, 0, 2, 0, 0, 0, 6, 7, 0}}; dijkstra(graph, 0); return 0; }

šŸ’” Pro Tip: The main difference between BFS and Dijkstra's Algorithm is that Dijkstra's Algorithm can handle graphs with negative edge weights, whereas BFS can only handle graphs with non-negative edge weights.

Quiz šŸŽÆ

Quick Quiz
Question 1 of 1

What is the main difference between BFS and Dijkstra's Algorithm?