# Smallest set vertices to visit all nodes of the given Graph

## Introduction

Finding the smallest set of vertices to visit all nodes in a graph could be a crucial issue in graph hypothesis. It has practical applications in different areas, counting network optimization, directing algorithms, and task planning. In this article, we are going investigate three diverse approaches to illuminate this problem: Depth-First Search (DFS), Breadth-First Search (BFS), and Depth-First Traversal with Backtracking. We are going give point by point clarifications, code usage within the C language, and algorithmic steps for each approach. Also, we'll illustrate the utilization of these approaches with a test graph to guarantee that all three strategies yield the same output.

## Approach 1: Depth-First Search (DFS)

### Algorithm

• Step 1 − Make a boolean array visited[] to check gone to vertices.

• Step 2 − Initialize an empty stack and push the beginning vertex onto the stack.

• Step 3 − Whereas the stack isn't empty, do the taking after −

• Step 4 −  Pop a vertex v from the stack. If v isn't gone to, check it as gone by and handle it. Thrust all unvisited adjoining vertices of v onto the stack. Rehash step 3 until the stack gets to be purge.

• Step 5 − Return the gone by vertices as the littlest set of vertices.

### Example

#include <stdio.h>
#include <stdbool.h>

#define MAX_VERTICES 100

void dfs(int graph[MAX_VERTICES][MAX_VERTICES], int V, int start, bool visited[]) {
visited[start] = true;
printf("%d ", start);

for (int i = 0; i < V; i++) {
if (graph[start][i] == 1 && !visited[i]) {
dfs(graph, V, i, visited);
}
}
}

void smallestSetOfVertices(int graph[MAX_VERTICES][MAX_VERTICES], int V) {
bool visited[MAX_VERTICES] = { false };

for (int i = 0; i < V; i++) {
if (!visited[i]) {
dfs(graph, V, i, visited);
}
}
}

int main() {
int V = 5;
int graph[MAX_VERTICES][MAX_VERTICES] = {
{0, 1, 1, 0, 0},
{1, 0, 1, 0, 0},
{1, 1, 0, 0, 0},
{0, 0, 0, 0, 1},
{0, 0, 0, 1, 0}
};

printf("Smallest set of vertices: ");
smallestSetOfVertices(graph, V);

return 0;
}


### Output

Smallest set of vertices: 0 1 2 3 4


## Approach 2: Breadth-First Search (BFS)

### Algorithm

• Step 1 −  Make a boolean array visited[] to check gone by vertices.

• Step 2 − Creation of an empty stack and enqueue the starting vertex.

• Step 3 − Whereas the line isn't empty, do the taking after − Dequeue a vertex v from the line.

• Step 4 − In case v isn't gone to, check it as gone to and prepare it. Enqueue all unvisited adjoining vertices of v onto the line.

• Step 5 − Rehash step 3 until the stack gets to be empty.

• Step 6 − Invoke the defined function smallestSetOfVertices() and print the result.

### Example

#include <stdio.h>
#include <stdbool.h>

#define MAX_VERTICES 100

void bfs(int graph[MAX_VERTICES][MAX_VERTICES], int V, int start, bool visited[]) {
int queue[MAX_VERTICES];
int front = 0, rear = 0;
queue[rear++] = start;
visited[start] = true;

while (front != rear) {
int v = queue[front++];
printf("%d ", v);

for (int i = 0; i < V; i++) {
if (graph[v][i] == 1 && !visited[i]) {
visited[i] = true;
queue[rear++] = i;
}
}
}
}

void smallestSetOfVertices(int graph[MAX_VERTICES][MAX_VERTICES], int V) {
bool visited[MAX_VERTICES] = { false };

for (int i = 0; i < V; i++) {
if (!visited[i]) {
bfs(graph, V, i, visited);
}
}
}

int main() {
int V = 5;
int graph[MAX_VERTICES][MAX_VERTICES] = {
{0, 1, 1, 0, 0},
{1, 0, 1, 1, 0},
{1, 1, 0, 0, 1},
{0, 1, 0, 0, 1},
{0, 0, 1, 1, 0}
};

printf("Smallest set of vertices: ");
smallestSetOfVertices(graph, V);

return 0;
}


### Output

Smallest set of vertices: 0 1 2 3 4


## Approach 3: Depth-First Traversal with Backtracking

### Algorithm

• Step 1 − Make a boolean array visited[] to check gone by vertices. Initialize a purge stack and thrust the beginning vertex onto the stack.

• Step 2 − Whereas the stack isn't purge, do the taking after −  Peek the best vertex v from the stack.

• Step 3 − If v isn't gone by, check it as gone to and prepare it. Discover an unvisited adjoining vertex u of v.

• Step 4 − If such a vertex u exists, thrust u onto the stack and break. If there are no unvisited adjoining vertices, pop v from the stack.

• Step 5 − Rehash steps 3-4 until the stack gets to be empty.

• Step 6 − Finally, display littlest set of vertices.

### Example

#include <stdio.h>
#include <stdbool.h>

#define MAX_VERTICES 100

void dfs(int graph[MAX_VERTICES][MAX_VERTICES], int V, int start, bool visited[]) {
int stack[MAX_VERTICES];
int top = -1;
stack[++top] = start;

while (top != -1) {
int v = stack[top];

if (!visited[v]) {
visited[v] = true;
printf("%d ", v);
}

int u;
for (u = 0; u < V; u++) {
if (graph[v][u] == 1 && !visited[u]) {
stack[++top] = u;
break;
}
}

if (u == V)
top--;
}
}

void smallestSetOfVertices(int graph[MAX_VERTICES][MAX_VERTICES], int V) {
bool visited[MAX_VERTICES] = { false };

for (int i = 0; i < V; i++) {
if (!visited[i]) {
dfs(graph, V, i, visited);
}
}
}

int main() {
int V = 5;
int graph[MAX_VERTICES][MAX_VERTICES] = {
{0, 1, 1, 0, 0},
{1, 0, 1, 0, 0},
{1, 1, 0, 0, 0},
{0, 0, 0, 0, 1},
{0, 0, 0, 1, 0}
};

printf("Smallest set of vertices: ");
smallestSetOfVertices(graph, V);

return 0;
}


### Output

Smallest set of vertices: 0 1 2 3 4


## Conclusion

We have investigated three successful approaches to discover the littlest set of vertices required to visit all nodes in a given graph. These strategies offer diverse techniques to navigate the graph and recognize the vital vertices. By leveraging these approaches, we will optimize network ways, minimize asset utilization, and make strides errand planning proficiency. The given code cases and algorithmic steps offer a strong establishment for executing these approaches within the C language. Whether you're working on organize optimization, assignment planning, or any other graph-related issue, these approaches will serve as valuable tools in your problem-solving toolkit.

Updated on: 25-Aug-2023

26 Views