图中距离最远节点的最小值
这里的目标是从给定的起点确定到整个图的终点的跳跃次数最少的路径。可以使用多种方法计算此距离,包括专门为图遍历设计的那些方法(如广度优先搜索)和最短路径发现方法(如 Dijkstra 算法)。
使用的方法
广度优先搜索
Dijkstra 算法
广度优先搜索方法
使用广度优先搜索算法遍历所有图顶点。在继续下一阶段之前,先访问源节点的所有邻居。在无权图中,BFS 确定最短路径。通过对每个节点应用 BFS 并测量每个节点与最远节点之间的最大距离,我们可以获得最小的图距离。
算法
初始化最大距离数组和 BFS 遍历队列。
将初始节点入队,距离为 0。
出队第一个节点。
如果与出队节点相邻的节点尚未访问,则将该节点入队,将其标记为已访问,并将其距离更新为当前节点的距离加一。
遍历图后,找到距离数组中的最大值。
最远节点的最小距离就是最大距离。
示例
#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;
}
输出
Minimum value of distance of the farthest node using BFS: 2
Dijkstra 算法方法
在加权网络中查找源节点和所有其他节点之间的最短路径是图遍历技术(如 Dijkstra 算法)的常见任务。它重复选择最接近的节点并更新其邻居的距离。Dijkstra 方法通过使用优先级队列有效地选择距离最小的下一个节点来确保最佳距离。我们可以通过从每个图节点执行 Dijkstra 方法并取最大距离来获得最远节点的最小距离。
算法
Dijkstra 方法需要一个最大值距离数组和优先级队列。
将起始节点及其距离 0 放入优先级队列。
出队优先级队列中最接近的节点。
如果出队节点尚未访问,则将其标记为已访问,如果其邻居的距离更小,则调整其邻居的距离。
遍历图后,找到距离数组中的最大值。
最远节点的最小距离就是最大距离。
示例
#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;
}
输出
Minimum value of distance of the farthest node using Dijkstra's algorithm: 3
结论
总之,网络中最远节点的最小距离提供了对其范围和覆盖范围的洞察。这个最小值有助于我们理解图的结构、连通性和具有较大距离的重要节点或位置。我们讨论了多种查找最远节点最小距离的方法。广度优先搜索 (BFS) 通过遍历图来查找每个节点到其他每个节点的最大距离。使用 Dijkstra 方法,我们可以通过确定单个源节点的最大距离来获得到最远节点的最小距离。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP