C語言求解無向圖頂點(diǎn)之間的所有最短路徑
本文實(shí)例為大家分享了C語言求解無向圖頂點(diǎn)之間的所有最短路徑的具體代碼,供大家參考,具體內(nèi)容如下
思路一:
DFS,遇到終點(diǎn)之后進(jìn)行記錄
輔助存儲(chǔ):
std::vector<int> tempPath; std::vector<std::vector<int>> totalPath;
實(shí)現(xiàn):
//查找無向圖的所有最短路徑,直接dfs就可以解決了 //記錄保存這里用 vector<vector<int>> 插入失敗,重新搞一下 OK // 時(shí)間復(fù)雜度 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) { //遇到終點(diǎn),進(jìn)行路徑判定 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); //存儲(chǔ) 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); //存儲(chǔ)當(dāng)前路徑 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; }
時(shí)間分析:
O(V + E)
缺點(diǎn):
可能會(huì)爆棧,我這里算86W點(diǎn)+414W邊直接爆,小的沒問題。
思路二:
BFS,位圖/vector/.. 記錄好每一步的路徑即可
時(shí)間
O(V + E)
額外開銷:
存儲(chǔ)每一步的路徑,如何維護(hù)好,盡量避免循環(huán)查找。
思路三:
1. 先求出起始點(diǎn)start到其余所有點(diǎn)的最短路徑; Dijkstra
2. 然后以終點(diǎn)end為開始,反向進(jìn)行dfs/bfs搜索;
每回退 i 層,判斷值(path-i)與起點(diǎn)到當(dāng)前點(diǎn)最短路徑長(zhǎng)度 temp 的比較;
二者相等,則繼續(xù)(利用子問題的正確性); 若 (path-i) < temp ,則這個(gè)點(diǎn)不在最短路徑上,放棄。
如圖所示:
先求出start到其余所有點(diǎn)的最短路徑;
然后從 end 點(diǎn)開始往回搜索;
end上面一個(gè)點(diǎn),(path - 1 = 3)等于起始點(diǎn)到它的最短路徑長(zhǎng) 3,判斷,是最短路徑上的點(diǎn),繼續(xù);
再往上搜索:
左邊那個(gè)點(diǎn)3,因?yàn)榇藭r(shí)(path - 2)= 2,而那個(gè)點(diǎn)的 temp=3,即 (path - i) < temp ,因此那個(gè)點(diǎn)一定不在 start 到 end 的最短路徑上。
而上面那個(gè)點(diǎn)2,此時(shí) (path - 2)= 2 , 而那個(gè)點(diǎn) temp = 2, 即 (path - i) == temp , 因此它必然在 start 到 end 的最短路徑上。繼續(xù)搜索下去 。
重復(fù)這樣的過程直到搜索完畢,最終得到兩條最短路徑。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:C++實(shí)現(xiàn)機(jī)票預(yù)訂系統(tǒng)
欄 目:C語言
下一篇:C語言實(shí)現(xiàn)病例管理系統(tǒng)
本文標(biāo)題:C語言求解無向圖頂點(diǎn)之間的所有最短路徑
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/481.html
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)數(shù)怎么表達(dá)
- 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ù)求階乘


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)
- 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ù)求
隨機(jī)閱讀
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置