# Minimum Value of Distance of Farthest Node in a Graph

The goal here is to determine the path with the fewest hops from a given starting point to the endpoint of the whole graph. This distance may be computed using a variety of methods, including those specifically designed for graph traversal (like Breadth−First Search) and shortest path discovery (like Dijkstra's algorithm).

## Methods Used

• Dijkstra's algorithm

All graph vertices are traversed using the breadth−first search algorithm. A source node's neighbours are all visited before moving on to the next stage. In an unweighted graph, BFS determines the shortest path. By applying BFS to each node and measuring the maximum distance between each node and the furthest node, we may get the least graph distance.

### Algorithm

• Initialise a maximum distance array and a BFS traversal queue.

• Enqueue the initial node with a distance of 0.

• Dequeue the first node.

• Enqueue the node, mark it as visited, and update its distance from the current node plus one if a node adjacent to the dequeued node hasn't been visited.

• Find the distance array's maximum after traversing the graph.

• The farthest node's minimal distance is the greatest distance.

### Example

#include <iostream>
#include <vector>
#include <queue>
#include <limits>
#include<bits/stdc++.h>

// Function to perform Breadth-First Search (BFS)
int bfs(const std::vector<std::vector<int>>& graph, int start) {
int numVertices = graph.size();
std::vector<bool> visited(numVertices, false);
std::vector<int> distance(numVertices, std::numeric_limits<int>::max());

std::queue<int> q;
q.push(start);
visited[start] = true;
distance[start] = 0;

while (!q.empty()) {
int currNode = q.front();
q.pop();

for (int neighbor : graph[currNode]) {
if (!visited[neighbor]) {
visited[neighbor] = true;
distance[neighbor] = distance[currNode] + 1;
q.push(neighbor);
}
}
}

int maxDistance = *std::max_element(distance.begin(), distance.end());
return maxDistance;
}

// Function to find the minimum value of the distance of the farthest node using BFS approach
int findMinimumDistanceBFS(const std::vector<std::vector<int>>& graph) {
int numVertices = graph.size();
int minDistance = std::numeric_limits<int>::max();

for (int i = 0; i < numVertices; ++i) {
int maxDistance = bfs(graph, i);
minDistance = std::min(minDistance, maxDistance);
}

return minDistance;
}

int main() {
// Example usage
int numVertices = 6;
std::vector<std::vector<int>> graph(numVertices);
std::vector<std::vector<std::pair<int, int>>> weightedGraph(numVertices);

// Add edges to the graph
graph[0] = {1, 2};
graph[1] = {0, 2, 3};
graph[2] = {0, 1, 4};
graph[3] = {1};
graph[4] = {2, 5};
graph[5] = {4};

// Add weighted edges to the weighted graph
weightedGraph[0] = {{1, 1}, {2, 2}};
weightedGraph[1] = {{0, 1}, {2, 1}, {3, 1}};
weightedGraph[2] = {{0, 2}, {1, 1}, {4, 1}};
weightedGraph[3] = {{1, 1}};
weightedGraph[4] = {{2, 1}, {5, 2}};
weightedGraph[5] = {{4, 2}};

int minDistanceBFS = findMinimumDistanceBFS(graph);
std::cout << "Minimum value of distance of the farthest node using BFS: " << minDistanceBFS << std::endl;

return 0;
}


### Output

Minimum value of distance of the farthest node using BFS: 2


## Dijkstra’s algorithm method

Finding the shortest routes between a source node and every other node in a weighted network is a typical task for graph traversal techniques like Dijkstra's algorithm. It selects the closest node and updates its neighbours' distances repeatedly. Dijkstra's technique ensures optimal distances by effectively selecting the next node with the least distance using a priority queue. We can get the farthest node's minimal distance by executing Dijkstra's method from each graph node and taking the highest distance.

### Algorithm

• Dijkstra's approach requires a maximum−valued distance array and priority queue.

• Priority queue the beginning node with distance 0.

• Dequeue the closest priority queue node.

• If the dequeued node hasn't been visited, mark it visited and adjust its neighbours' distances if they're smaller.

• Find the distance array's maximum after traversing the graph.

• The farthest node's minimal distance is the greatest distance.

### Example

#include <iostream>
#include <vector>
#include <queue>
#include <limits>
#include<bits/stdc++.h>

// Function to perform Dijkstra's algorithm
int dijkstra(const std::vector<std::vector<std::pair<int, int>>>& graph, int start) {
int numVertices = graph.size();
std::vector<int> distance(numVertices, std::numeric_limits<int>::max());
std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>, std::greater<std::pair<int, int>>> pq;

distance[start] = 0;
pq.push({0, start});

while (!pq.empty()) {
int currNode = pq.top().second;
int currDist = pq.top().first;
pq.pop();

if (currDist > distance[currNode])
continue;

for (auto neighbor : graph[currNode]) {
int neighborNode = neighbor.first;
int neighborDist = neighbor.second;

if (distance[currNode] + neighborDist < distance[neighborNode]) {
distance[neighborNode] = distance[currNode] + neighborDist;
pq.push({distance[neighborNode], neighborNode});
}
}
}

int maxDistance = *std::max_element(distance.begin(), distance.end());
return maxDistance;
}

// Function to find the minimum value of the distance of the farthest node using Dijkstra's algorithm
int findMinimumDistanceDijkstra(const std::vector<std::vector<std::pair<int, int>>>& graph) {
int numVertices = graph.size();
int minDistance = std::numeric_limits<int>::max();

for (int i = 0; i < numVertices; ++i) {
int maxDistance = dijkstra(graph, i);
minDistance = std::min(minDistance, maxDistance);
}

return minDistance;
}

int main() {
// Example usage
int numVertices = 6;
std::vector<std::vector<int>> graph(numVertices);
std::vector<std::vector<std::pair<int, int>>> weightedGraph(numVertices);

// Add edges to the graph
graph[0] = {1, 2};
graph[1] = {0, 2, 3};
graph[2] = {0, 1, 4};
graph[3] = {1};
graph[4] = {2, 5};
graph[5] = {4};

// Add weighted edges to the weighted graph
weightedGraph[0] = {{1, 1}, {2, 2}};
weightedGraph[1] = {{0, 1}, {2, 1}, {3, 1}};
weightedGraph[2] = {{0, 2}, {1, 1}, {4, 1}};
weightedGraph[3] = {{1, 1}};
weightedGraph[4] = {{2, 1}, {5, 2}};
weightedGraph[5] = {{4, 2}};

int minDistanceDijkstra = findMinimumDistanceDijkstra(weightedGraph);
std::cout << "Minimum value of distance of the farthest node using Dijkstra's algorithm: " << minDistanceDijkstra << std::endl;

return 0;
}


### Output

Minimum value of distance of the farthest node using Dijkstra's algorithm: 3


## Conclusion

In conclusion, the minimum distance of the furthest node in a network gives insight into its reach and range. This minimal number helps us comprehend the graph's structure, connectedness, and essential nodes or places with substantial distances.We discussed many ways to find the furthest node's minimal distance. Breadth−First Search (BFS) finds the greatest distance from each node to each other node by traversing the graph.Using Dijkstra's method, we may get the least distance to the furthest node by determining the greatest distance from a single source node.

Updated on: 14-Jul-2023

162 Views