C語言求解無向圖頂點之間的所有最短路徑
本文實例為大家分享了C語言求解無向圖頂點之間的所有最短路徑的具體代碼,供大家參考,具體內(nèi)容如下
思路一:
DFS,遇到終點之后進行記錄
輔助存儲:
std::vector<int> tempPath; std::vector<std::vector<int>> totalPath;
實現(xiàn):
//查找無向圖的所有最短路徑,直接dfs就可以解決了
//記錄保存這里用 vector<vector<int>> 插入失敗,重新搞一下 OK
// 時間復雜度 O(N + E)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <set>
#define MAX 10
#define INF 999999
int graph[MAX + 1][MAX + 1];
int N, M; //node, edge
int nodeBook[MAX + 1];
int minPath = INF;
std::vector<int> pathNodeVec;
std::vector<std::vector<int>> allShortVec;
int startNode, endNode;
void dfs(int i, int step)
{
if (i == endNode) { //遇到終點,進行路徑判定
if (step < minPath) {
std::cout << "step < minpath.., size = " << allShortVec.size() << std::endl;
minPath = step;
pathNodeVec.push_back(i);
for (auto &elem : pathNodeVec)
std::cout << elem << "\t";
std::cout << std::endl;
std::vector<int> tempVec = pathNodeVec;
allShortVec.clear(); //清空
allShortVec.push_back(tempVec); //存儲
pathNodeVec.pop_back();
} else if (step == minPath) {
std::cout << "step == minpath.., size = " << allShortVec.size() << std::endl;
pathNodeVec.push_back(i);
for (auto &elem : pathNodeVec)
std::cout << elem << "\t";
std::cout << std::endl;
std::vector<int> tempVec = pathNodeVec;
allShortVec.push_back(tempVec); //存儲當前路徑
pathNodeVec.pop_back();
} else { ;}
return;
}
nodeBook[i] = 1;
pathNodeVec.push_back(i);
for (int x = 1; x <= N; x++) { //嘗試所有可能性
if (x == i)
continue;
if (nodeBook[x] == 1)
continue;
if (graph[i][x] == INF)
continue;
dfs(x, step + 1);
}
nodeBook[i] = 0;
pathNodeVec.pop_back();
return ;
}
int main(void)
{
std::cin >> N >> M;
for (int x = 1; x <= N; x++)
nodeBook[x] = 0; //表示還沒有訪問
for (int x = 1; x <= N; ++x)
for (int y = 1; y <= N; ++y) {
if (x == y)
graph[x][y] = 0;
else
graph[x][y] = INF;
}
for (int i = 1; i <= M; ++i) {
int tempX, tempY, tempWeight;
std::cin >> tempX >> tempY >> tempWeight;
graph[tempX][tempY] = tempWeight;
}
std::cout << "please input start node & end node :" << std::endl;
std::cin >> startNode >> endNode;
pathNodeVec.clear();
allShortVec.clear();
dfs(startNode, 0);
std::cout << "all shortest path: \t";
std::cout << "size = " << allShortVec.size() << std::endl;
for (std::vector<std::vector<int>>::const_iterator it = allShortVec.begin(); it != allShortVec.end(); it++) {
for (std::vector<int>::const_iterator it2 = (*it).begin(); it2 != (*it).end(); it2++)
std::cout << (*it2) << "\t";
std::cout << std::endl;
}
getchar();
return 0;
}
時間分析:
O(V + E)
缺點:
可能會爆棧,我這里算86W點+414W邊直接爆,小的沒問題。
思路二:
BFS,位圖/vector/.. 記錄好每一步的路徑即可
時間
O(V + E)
額外開銷:
存儲每一步的路徑,如何維護好,盡量避免循環(huán)查找。
思路三:
1. 先求出起始點start到其余所有點的最短路徑; Dijkstra
2. 然后以終點end為開始,反向進行dfs/bfs搜索;
每回退 i 層,判斷值(path-i)與起點到當前點最短路徑長度 temp 的比較;
二者相等,則繼續(xù)(利用子問題的正確性); 若 (path-i) < temp ,則這個點不在最短路徑上,放棄。
如圖所示:
先求出start到其余所有點的最短路徑;
然后從 end 點開始往回搜索;
end上面一個點,(path - 1 = 3)等于起始點到它的最短路徑長 3,判斷,是最短路徑上的點,繼續(xù);
再往上搜索:
左邊那個點3,因為此時(path - 2)= 2,而那個點的 temp=3,即 (path - i) < temp ,因此那個點一定不在 start 到 end 的最短路徑上。
而上面那個點2,此時 (path - 2)= 2 , 而那個點 temp = 2, 即 (path - i) == temp , 因此它必然在 start 到 end 的最短路徑上。繼續(xù)搜索下去 。
重復這樣的過程直到搜索完畢,最終得到兩條最短路徑。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持我們。
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對數(shù)怎么表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10C#中split用法實例總結(jié)
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10SublimeText編譯C開發(fā)環(huán)境設置


