计算任意两个定点的最长路径
来源:互联网 发布:淘宝店怎么来推广 编辑:程序博客网 时间:2024/06/08 12:44
原文地址:Longest path between any pair of vertices
已知一个城市连接图,它们之间是通过电缆连接的,这样的话任意两个城市之间都没有环路。我们需要在已知的这个城市图中找到两个城市之间最大长度的电缆。
Input : n = 6 1 2 3 // Cable length from 1 to 2 (or 2 to 1) is 3 2 3 4 2 6 2 6 4 6 6 5 5Output: maximum length of cable = 12
Method 1 (Simple DFS)
方法 1(简单的深度优先搜寻(DFS))
我们可以为这个已知的城市地图建立一个无向图,对于每一个城市都去做DFS并找到最长的电缆。在遍历的过程中,我们寻找可以达到当前城市的最长电缆,如果它的相邻城市不能访问的话,那么就调用DFS,但是如果当前节点所有的相邻城市都访问过了,如max_length以前的值比现在的总长度要小,那么就更新max_length的值。
// C++ program to find the longest cable length// between any two cities.#include<bits/stdc++.h>using namespace std;// visited[] array to make nodes visited// src is starting node for DFS traversal// prev_len is sum of cable length till current node// max_len is pointer which stores the maximum length// of cable value after DFS traversalvoid DFS(vector< pair<int,int> > graph[], int src, int prev_len, int *max_len, vector <bool> &visited){ // Mark the src node visited visited[src] = 1; // curr_len is for length of cable from src // city to its adjacent city int curr_len = 0; // Adjacent is pair type which stores // destination city and cable length pair < int, int > adjacent; // Traverse all adjacent for (int i=0; i<graph[src].size(); i++) { // Adjacent element adjacent = graph[src][i]; // If node or city is not visited if (!visited[adjacent.first]) { // Total length of cable from src city // to its adjacent curr_len = prev_len + adjacent.second; // Call DFS for adjacent city DFS(graph, adjacent.first, curr_len, max_len, visited); } // If total cable length till now greater than // previous length then update it if ((*max_len) < curr_len) *max_len = curr_len; // make curr_len = 0 for next adjacent curr_len = 0; }}// n is number of cities or nodes in graph// cable_lines is total cable_lines among the cities// or edges in graphint longestCable(vector<pair<int,int> > graph[], int n){ // maximum length of cable among the connected // cities int max_len = INT_MIN; // call DFS for each city to find maximum // length of cable for (int i=1; i<=n; i++) { // initialize visited array with 0 vector< bool > visited(n+1, false); // Call DFS for src vertex i DFS(graph, i, 0, &max_len, visited); } return max_len;}// driver program to test the inputint main(){ // n is number of cities int n = 6; vector< pair<int,int> > graph[n+1]; // create undirected graph // first edge graph[1].push_back(make_pair(2, 3)); graph[2].push_back(make_pair(1, 3)); // second edge graph[2].push_back(make_pair(3, 4)); graph[3].push_back(make_pair(2, 4)); // third edge graph[2].push_back(make_pair(6, 2)); graph[6].push_back(make_pair(2, 2)); // fourth edge graph[4].push_back(make_pair(6, 6)); graph[6].push_back(make_pair(4, 6)); // fifth edge graph[5].push_back(make_pair(6, 5)); graph[6].push_back(make_pair(5, 5)); cout << "Maximum length of cable = " << longestCable(graph, n); return 0;}
输出:
Maximum length of cable = 12
时间复杂度:O(V * (V + E))
方法2(更加有效的)
我们可以在 O(V+E)的时间内解决这个问题。下面就是具体的步骤。
1)建立一个距离矩阵dist[],用无限小初始化矩阵的每一个单元。
2)对所有的定点进行拓扑排序。
3)按照拓扑排序的顺序访问每一个定点u。
…..Do following for every adjacent vertex v of u
………..if (dist[v] < dist[u] + weight(u, v))
……………dist[v] = dist[u] + weight(u, v)
4)返回dist[]中的最大值
因为图中没有负的权重,所以以拓扑顺序处理定点总是会产生最长路径矩阵dist[],这样dist[u]就是到顶点‘u’的最长路径。
以上的实现方法可以很容易地应用到这个问题。不同点就是边的权重都是非负的,我们需要找到的是整体的最长路径(而不是从开始节点的最长路径)。最后我们返回dist[]中所有值的最大值。
时间复杂度:O(V + E)
- 计算任意两个定点的最长路径
- 计算两个任意日期之间的工作日
- 计算两个任意长的正整数相加
- 计算两个路径的相对路径
- 求任意两个顶点间的最短路径
- 求图中任意两个顶点间的最短路径
- 求任意两个点之间的最短路径
- 计算两个文件的相对路径(php)
- 计算两个文件的相对路径
- 计算两个文件的相对路径
- php 计算两个文件的相对路径
- php计算两个文件的相对路径
- PHP 计算两个文件的相对路径
- Java计算任意两个日期间隔的工作天数
- Java中计算任意两个日期之间的工作天数
- 计算任意两个日期之间的天数问题
- Java中计算任意两个日期之间的工作天数
- Java计算任意两个日期间隔的工作天数
- 11.Javascript中的数据类型转换
- Const类型变量
- @SuppressWarnings注解
- MYSQL的用户变量(@)和系统变量(@@)
- springMVC----ajax以及Json对象
- 计算任意两个定点的最长路径
- 六款APP加上一支笔 让你成为绘画大师
- github windows 客户端 使用教程
- 返回顶部小火箭
- PagerAdapter滑动页面,onpagechangelistener监听滑动
- 获取文件类型和文件名
- opencv 人脸识别 java版------2
- 创建一个规范的iOS工程(xcode8新特性)
- Swift Objective-c 原生api 二维码条形码的扫描