Graph Algorithms
Graph algorithms are used to solve problems related to graph data structures. They are crucial for tasks like finding the shortest path, traversing graphs, and network flow analysis.
Common Graph Algorithms
- Depth First Search (DFS): Explores as far as possible along each branch before backtracking.
- Breadth First Search (BFS): Explores all neighbors at the present depth before moving on to nodes at the next depth level.
- Dijkstra's Algorithm: Finds the shortest path from a source node to all other nodes in a weighted graph.
- Kruskal's Algorithm: Finds the minimum spanning tree for a connected weighted graph.
- Prim's Algorithm: Another method to find the minimum spanning tree of a weighted graph.
Example : Depth First Search (DFS)
#include <stdio.h >
#include <stdbool.h>
#define V 5
void DFSUtil(int graph[V][V], int v, bool visited[]) {
visited[v] = true;
printf("%d ", v);
for (int i = 0; i < V; i++) {
if (graph[v][i] == 1 && !visited[i]) {
DFSUtil(graph, i, visited);
}
}
}
void DFS(int graph[V][V]) {
bool visited[V] = {false};
for (int i = 0; i < V; i++) {
if (!visited[i]) {
DFSUtil(graph, i, visited);
}
}
}
int main() {
int graph[V][V] = {
{0, 1, 1, 0, 0},
{1, 0, 0, 1, 1},
{1, 0, 0, 0, 0},
{0, 1, 0, 0, 1},
{0, 1, 0, 1, 0}
};
printf("Depth First Traversal:\n");
DFS(graph);
return 0;
}
Output
Depth First Traversal:
0 1 3 4 2
Example : Dijkstra's Algorithm
#include <stdio.h>
#include <limits.h>
#include <stdbool.h> // Include stdbool.h for using bool type
#define V 9 // Number of vertices in the graph
// Function to find the vertex with the minimum distance value
int minDistance(int dist[], bool sptSet[]) {
int min = INT_MAX, min_index;
for (int v = 0; v < V; v++) {
if (!sptSet[v] && dist[v] <= min) {
min = dist[v];
min_index = v;
}
}
return min_index;
}
// Function to implement Dijkstra's single source shortest path algorithm
void dijkstra(int graph[V][V], int src) {
int dist[V]; // Output array to hold the shortest distances
bool sptSet[V]; // True if vertex is included in the shortest path tree
// Initialize distances to all vertices as infinite and sptSet[] as false
for (int i = 0; i < V; i++) {
dist[i] = INT_MAX;
sptSet[i] = false;
}
// Distance to the source vertex is always 0
dist[src] = 0;
// Find the shortest path for all vertices
for (int count = 0; count < V - 1; count++) {
// Pick the minimum distance vertex from the set of vertices not yet processed
int u = minDistance(dist, sptSet);
// Mark the chosen vertex as processed
sptSet[u] = true;
// Update dist[] for the adjacent vertices of the chosen vertex
for (int v = 0; v < V; v++) {
// Update dist[v] if:
// 1. Vertex v is not in the shortest path tree
// 2. There is an edge from u to v
// 3. The total weight of the path from src to v through u is smaller than the
current value of dist[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];
}
}
}
// Print the result
printf("Vertex Distance from Source\n");
for (int i = 0; i < V; i++) {
printf("%d tt %d\n", i, dist[i]);
}
}
int main() {
// Graph represented as an adjacency matrix
int graph[V][V] = { { 0, 4, 0, 0, 0, 0, 0, 8, 0 },
{ 4, 0, 8, 0, 0, 0, 0, 11, 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, 11, 0, 0, 0, 0, 1, 0, 7 },
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 } };
// Run Dijkstra's algorithm
dijkstra(graph, 0);
return 0;
}
Output
Vertex Distance from Source
0 0
1 4
2 12
3 19
4 21
5 11
6 9
7 8
8 14