Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Print the lexicographically smallest BFS of the graph starting from 1 in C Program.
We will be given a connected graph with N vertices and M edges. We need to print the lexicographically smallest BFS traversal of the graph starting from vertex 1.
Lexicographically smallest means visiting vertices in the smallest numerical order possible at each level. Instead of using a regular queue for BFS, we use a priority queue (min-heap) to always visit the smallest numbered unvisited vertex first.
Syntax
void lexicographicalBFS(int graph[][MAX], int n, int start);
Algorithm
The approach uses a priority queue to ensure we always visit the smallest numbered vertex next −
- Initialize a visited array and a priority queue
- Mark starting vertex as visited and add to queue
- While queue is not empty:
- Extract minimum vertex from queue
- Print the vertex
- Add all unvisited adjacent vertices to queue
Example: Using Adjacency Matrix
Here's a complete C implementation using adjacency matrix representation −
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 100
// Simple priority queue implementation for integers
typedef struct {
int data[MAX];
int size;
} PriorityQueue;
void initQueue(PriorityQueue *pq) {
pq->size = 0;
}
void insert(PriorityQueue *pq, int value) {
int i = pq->size;
pq->data[i] = value;
pq->size++;
// Heapify up
while (i > 0 && pq->data[i] < pq->data[(i-1)/2]) {
int temp = pq->data[i];
pq->data[i] = pq->data[(i-1)/2];
pq->data[(i-1)/2] = temp;
i = (i-1)/2;
}
}
int extractMin(PriorityQueue *pq) {
if (pq->size == 0) return -1;
int min = pq->data[0];
pq->data[0] = pq->data[pq->size - 1];
pq->size--;
// Heapify down
int i = 0;
while (2*i + 1 < pq->size) {
int left = 2*i + 1;
int right = 2*i + 2;
int smallest = i;
if (left < pq->size && pq->data[left] < pq->data[smallest])
smallest = left;
if (right < pq->size && pq->data[right] < pq->data[smallest])
smallest = right;
if (smallest != i) {
int temp = pq->data[i];
pq->data[i] = pq->data[smallest];
pq->data[smallest] = temp;
i = smallest;
} else {
break;
}
}
return min;
}
bool isEmpty(PriorityQueue *pq) {
return pq->size == 0;
}
void lexicographicalBFS(int graph[][MAX], int n) {
bool visited[MAX] = {false};
PriorityQueue pq;
initQueue(&pq);
visited[1] = true;
insert(&pq, 1);
printf("Lexicographically smallest BFS: ");
while (!isEmpty(&pq)) {
int current = extractMin(&pq);
printf("%d ", current);
for (int i = 1; i <= n; i++) {
if (graph[current][i] == 1 && !visited[i]) {
visited[i] = true;
insert(&pq, i);
}
}
}
printf("<br>");
}
int main() {
int n = 5;
int graph[MAX][MAX] = {0};
// Adding edges
graph[1][4] = graph[4][1] = 1;
graph[3][4] = graph[4][3] = 1;
graph[5][4] = graph[4][5] = 1;
graph[3][2] = graph[2][3] = 1;
graph[1][5] = graph[5][1] = 1;
lexicographicalBFS(graph, n);
return 0;
}
Lexicographically smallest BFS: 1 4 5 2 3
How It Works
Key Points
- Priority queue ensures vertices are visited in ascending numerical order
- Time complexity: O(V log V + E) where V is vertices and E is edges
- Space complexity: O(V) for visited array and priority queue
- Works for any connected graph starting from vertex 1
Conclusion
Lexicographically smallest BFS uses a priority queue instead of a regular queue to ensure vertices are always visited in the smallest numerical order. This guarantees a deterministic traversal order for graph algorithms.
