日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

bellman ford 算法 判断是否存在负环

發(fā)布時間:2024/8/23 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 bellman ford 算法 判断是否存在负环 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Flyer

  • 目錄視圖
  • 摘要視圖
  • 訂閱
微信小程序實戰(zhàn)項目——點餐系統(tǒng)????????程序員11月書訊,評論得書啦????????Get IT技能知識庫,50個領域一鍵直達 關閉 ?

bellman ford 算法

?11148人閱讀?評論(0)?收藏?舉報 ?分類: ACM(11)?

[cpp]?view plaincopy
  • <span?style="font-family:?Arial,?Helvetica,?sans-serif;"><span?style="border-bottom-width:1px;?border-bottom-style:dashed;?border-bottom-color:rgb(201,201,201)"><a?href="http://www.wutianqi.com/?p=1890"?target="_blank"?style="background-color:transparent;?border-width:0px?0px?1px;?border-bottom-style:dashed;?border-bottom-color:rgb(201,201,201);?margin:0px;?padding:0px;?vertical-align:baseline;?color:rgb(0,102,204);?text-decoration:none;?outline:none">Dijkstra算法</a></span></span><a?href="http://wutianqi-blog.b0.upaiyun.com/2011/01/2.png"?style="font-family:?Arial,?Helvetica,?sans-serif;?background-color:?transparent;?border-width:?0px?0px?1px;?border-bottom-style:?dashed;?border-bottom-color:?rgb(201,?201,?201);?margin:?0px;?padding:?0px;?vertical-align:?baseline;?color:?rgb(0,?102,?204);?text-decoration:?none;?outline:?none;"></a><a?href="http://wutianqi-blog.b0.upaiyun.com/2011/01/4.png"?style="font-family:?Arial,?Helvetica,?sans-serif;?background-color:?transparent;?border-width:?0px?0px?1px;?border-bottom-style:?dashed;?border-bottom-color:?rgb(201,?201,?201);?margin:?0px;?padding:?0px;?vertical-align:?baseline;?color:?rgb(0,?102,?204);?text-decoration:?none;?outline:?none;"></a><a?href="http://wutianqi-blog.b0.upaiyun.com/2011/01/bellman_ford.jpg"?style="font-family:?Arial,?Helvetica,?sans-serif;?background-color:?transparent;?border-width:?0px?0px?1px;?border-bottom-style:?dashed;?border-bottom-color:?rgb(201,?201,?201);?margin:?0px;?padding:?0px;?vertical-align:?baseline;?color:?rgb(0,?102,?204);?text-decoration:?none;?outline:?none;"></a><span?style="font-family:?Arial,?Helvetica,?sans-serif;">是處理單源最短路徑的有效算法,但它局限于邊的權值非負的情況,若圖中出現(xiàn)權值為負的邊,Dijkstra算法就會失效,求出的最短路徑就可能是錯的。這時候,就需要使用其他的算法來求解最短路徑,</span><a?href="http://www.wutianqi.com/?p=1912"?target="_blank"?style="font-family:?Arial,?Helvetica,?sans-serif;?background-color:?transparent;?border-width:?0px?0px?1px;?border-bottom-style:?dashed;?border-bottom-color:?rgb(201,?201,?201);?margin:?0px;?padding:?0px;?vertical-align:?baseline;?color:?rgb(0,?102,?204);?text-decoration:?none;?outline:?none;">Bellman-Ford算法</a><span?style="font-family:?Arial,?Helvetica,?sans-serif;">就是其中最常用的一個。該算法由美國數(shù)學家理查德?貝爾曼(Richard?Bellman,?動態(tài)規(guī)劃的提出者)和小萊斯特?福特(Lester?Ford)發(fā)明。Bellman-Ford算法的流程如下:</span>??
  • 給定圖G(V, E)(其中V、E分別為圖G的頂點集與邊集),源點s,

    • 數(shù)組Distant[i]記錄從源點s到頂點i的路徑長度,初始化數(shù)組Distant[n]為, Distant[s]為0;
    • ?
      以下操作循環(huán)執(zhí)行至多n-1次,n為頂點數(shù):
      對于每一條邊e(u, v),如果Distant[u] + w(u, v) < Distant[v],則另Distant[v] = Distant[u]+w(u, v)。w(u, v)為邊e(u,v)的權值;
      若上述操作沒有對Distant進行更新,說明最短路徑已經(jīng)查找完畢,或者部分點不可達,跳出循環(huán)。否則執(zhí)行下次循環(huán);
    • 為了檢測圖中是否存在負環(huán)路,即權值之和小于0的環(huán)路。對于每一條邊e(u, v),如果存在Distant[u] + w(u, v) < Distant[v]的邊,則圖中存在負環(huán)路,即是說改圖無法求出單源最短路徑。否則數(shù)組Distant[n]中記錄的就是源點s到各頂點的最短路徑長度。

    可知,Bellman-Ford算法尋找單源最短路徑的時間復雜度為O(V*E).

    首先介紹一下松弛計算。如下圖:


    ?

    松弛計算之前,點B的值是8,但是點A的值加上邊上的權重2,得到5,比點B的值(8)小,所以,點B的值減小為5。這個過程的意義是,找到了一條通向B點更短的路線,且該路線是先經(jīng)過點A,然后通過權重為2的邊,到達點B。
    當然,如果出現(xiàn)一下情況


    ?

    則不會修改點B的值,因為3+4>6。
    ?
    Bellman-Ford算法可以大致分為三個部分
    第一,初始化所有點。每一個點保存一個值,表示從原點到達這個點的距離,將原點的值設為0,其它的點的值設為無窮大(表示不可達)。
    第二,進行循環(huán),循環(huán)下標為從1到n-1(n等于圖中點的個數(shù))。在循環(huán)內部,遍歷所有的邊,進行松弛計算。
    第三,遍歷途中所有的邊(edge(u,v)),判斷是否存在這樣情況:
    d(v) > d (u) + w(u,v)
    則返回false,表示途中存在從源點可達的權為負的回路。
    ?
    之所以需要第三部分的原因,是因為,如果存在從源點可達的權為負的回路。則 應為無法收斂而導致不能求出最短路徑。
    考慮如下的圖:
    ?

    經(jīng)過第一次遍歷后,點B的值變?yōu)?,點C的值變?yōu)?,這時,注意權重為-10的邊,這條邊的存在,導致點A的值變?yōu)?#xff0d;2。(8+ -10=-2)
    ?
    ?

    第二次遍歷后,點B的值變?yōu)?,點C變?yōu)?,點A變?yōu)?#xff0d;4。正是因為有一條負邊在回路中,導致每次遍歷后,各個點的值不斷變小。
    ?
    在回過來看一下bellman-ford算法的第三部分,遍歷所有邊,檢查是否存在d(v) > d (u) + w(u,v)。因為第二部分循環(huán)的次數(shù)是定長的,所以如果存在無法收斂的情況,則肯定能夠在第三部分中檢查出來。比如
    ?

    此時,點A的值為-2,點B的值為5,邊AB的權重為5,5 > -2 + 5. 檢查出來這條邊沒有收斂。
    ?
    所以,Bellman-Ford算法可以解決圖中有權為負數(shù)的邊的單源最短路徑問。

    個人感覺算法導論講解很不錯,把這一章貼出來和大家分享:

    24.1 The Bellman-Ford algorithm

    The?Bellman-Ford algorithm?solves the single-source shortest-paths problem in the general case in which edge weights may be negative. Given a weighted, directed graph?G?= (V,?E) with source?s?and weight function?w?:?E?→?R, the Bellman-Ford algorithm returns a boolean value indicating whether or not there is a negative-weight cycle that is reachable from the source. If there is such a cycle, the algorithm indicates that no solution exists. If there is no such cycle, the algorithm produces the shortest paths and their weights.

    The algorithm uses relaxation, progressively decreasing an estimate?d[v] on the weight of a shortest path from the source?s?to each vertex?v?∈?V?until it achieves the actual shortest-path weight?δ(s,?v). The algorithm returns TRUE if and only if the graph contains no negative-weight cycles that are reachable from the source.

    BELLMAN-FORD(G, w, s) 1 INITIALIZE-SINGLE-SOURCE(G, s) 2 for i1 to |V[G]| - 1 3 do for each edge (u, v) ∈ E[G] 4 do RELAX(u, v, w) 5 for each edge (u, v) ∈ E[G] 6 do if d[v] > d[u] + w(u, v) 7 then return FALSE 8 return TRUE

    Figure 24.4?shows the execution of the Bellman-Ford algorithm on a graph with 5 vertices. After initializing the?d?and π values of all vertices in line 1, the algorithm makes |V| – 1 passes over the edges of the graph. Each pass is one iteration of the?for?loop of lines 2-4 and consists of relaxing each edge of the graph once. Figures 24.4(b)-(e) show the state of the algorithm after each of the four passes over the edges. After making |V|- 1 passes, lines 5-8 check for a negative-weight cycle and return the appropriate boolean value. (We’ll see a little later why this check works.)

    (單擊圖片可以放大)

    Figure 24.4: The execution of the Bellman-Ford algorithm. The source is vertex?s. The?d?values are shown within the vertices, and shaded edges indicate predecessor values: if edge (u, v) is shaded, then π[v] =?u. In this particular example, each pass relaxes the edges in the order (t, x), (t, y), (t, z), (x, t), (y, x), (y, z), (z, x), (z, s), (s, t), (s, y). (a) The situation just before the first pass over the edges. (b)-(e) The situation after each successive pass over the edges. The?d?and π values in part (e) are the final values. The Bellman-Ford algorithm returns TRUE in this example.

    The Bellman-Ford algorithm runs in time?O(V E), since the initialization in line 1 takes Θ(V) time, each of the |V| – 1 passes over the edges in lines 2-4 takes Θ(E) time, and the?for?loop of lines 5-7 takes?O(E) time.

    以下是Bellman-Ford代碼:

    [cpp]?view plaincopy
  • #include?<iostream>??
  • using?namespace?std;??
  • const?int?maxnum?=?100;??
  • const?int?maxint?=?99999;??
  • ??
  • //?邊,??
  • typedef?struct?Edge{??
  • ????int?u,?v;????//?起點,重點??
  • ????int?weight;??//?邊的權值??
  • }Edge;??
  • ??
  • Edge?edge[maxnum];?????//?保存邊的值??
  • int??dist[maxnum];?????//?結點到源點最小距離??
  • ??
  • int?nodenum,?edgenum,?source;????//?結點數(shù),邊數(shù),源點??
  • ??
  • //?初始化圖??
  • void?init()??
  • {??
  • ????//?輸入結點數(shù),邊數(shù),源點??
  • ????cin?>>?nodenum?>>?edgenum?>>?source;??
  • ????for(int?i=1;?i<=nodenum;?++i)??
  • ????????dist[i]?=?maxint;??
  • ????dist[source]?=?0;??
  • ????for(int?i=1;?i<=edgenum;?++i)??
  • ????{??
  • ????????cin?>>?edge[i].u?>>?edge[i].v?>>?edge[i].weight;??
  • ????????if(edge[i].u?==?source)??????????//注意這里設置初始情況??
  • ????????????dist[edge[i].v]?=?edge[i].weight;??
  • ????}??
  • }??
  • ??
  • //?松弛計算??
  • void?relax(int?u,?int?v,?int?weight)??
  • {??
  • ????if(dist[v]?>?dist[u]?+?weight)??
  • ????????dist[v]?=?dist[u]?+?weight;??
  • }??
  • ??
  • bool?Bellman_Ford()??
  • {??
  • ????for(int?i=1;?i<=nodenum-1;?++i)??
  • ????????for(int?j=1;?j<=edgenum;?++j)??
  • ????????????relax(edge[j].u,?edge[j].v,?edge[j].weight);??
  • ????bool?flag?=?1;??
  • ????//?判斷是否有負環(huán)路??
  • ????for(int?i=1;?i<=edgenum;?++i)??
  • ????????if(dist[edge[i].v]?>?dist[edge[i].u]?+?edge[i].weight)??
  • ????????{??
  • ????????????flag?=?0;??
  • ????????????break;??
  • ????????}??
  • ????return?flag;??
  • }??
  • int?main()??
  • {??
  • ????//freopen("input3.txt",?"r",?stdin);??
  • ????init();??
  • ????if(Bellman_Ford())??
  • ????????for(int?i?=?1?;i?<=?nodenum;?i++)??
  • ????????????cout?<<?dist[i]?<<?endl;??
  • ????return?0;??
  • }??


  • 補充:


    考慮:為什么要循環(huán)V-1次?
    答:因為最短路徑肯定是個簡單路徑,不可能包含回路的,
    如果包含回路,且回路的權值和為正的,那么去掉這個回路,可以得到更短的路徑
    如果回路的權值是負的,那么肯定沒有解了

    圖有n個點,又不能有回路
    所以最短路徑最多n-1邊

    又因為每次循環(huán),至少relax一邊
    所以最多n-1次就行了

    3
    0

    我的同類文章

    ACM(11)
    • ?Uva 10891 sum 游戲 (及其變型) ;動態(tài)規(guī)劃2013-05-31閱讀440
    • ?二分圖匹配算法總結2013-05-25閱讀839
    • ?求最大權二分匹配的KM算法2013-05-25閱讀353
    • ?ACM進階指南2013-05-25閱讀629
    • ?匈牙利算法 求解 完美的牛欄2013-05-24閱讀1106
    • ?歐拉回路2013-05-28閱讀347
    • ?二分圖帶權匹配 KM算法與費用流模型建立2013-05-25閱讀650
    • ?著名的北郵ACM推薦50題2013-05-25閱讀1702
    • ?HDOJ---2036 過山車[匈牙利算法]2013-05-24閱讀601
    • ?hdu 1466計算直線的交點數(shù)2013-05-03閱讀579
    更多文章

    參考知識庫

    算法與數(shù)據(jù)結構知識庫

    10401關注|2281收錄

    猜你在找
    《C語言/C++學習指南》加密解密篇(安全相關算法)
    C語言系列之 遞歸算法示例與?Windows?趣味小項目
    C語言系列之 字符串相關算法
    C語言系列之 字符串壓縮算法與結構體初探
    模板匹配的字符識別(OCR)算法原理
    圖最短路徑Bellman-Ford算法
    BellmanFord算法實現(xiàn)解決存在負邊情況下單源最短路徑問題
    最短路模板Dijkstra & Dijkstra算法+堆優(yōu)化 & bellman_ford & 單源最短路SPFA
    最短路徑算法Bellman-Ford貝爾曼
    poj Wormholes 最短路 spfa Bellman-Ford 算法 鄰接表實現(xiàn)
    查看評論
    ??暫無評論

    * 以上用戶言論只代表其個人觀點,不代表CSDN網(wǎng)站的觀點或立場
    核心技術類目
    全部主題?Hadoop?AWS?移動游戲?Java?Android?iOS?Swift?智能硬件?Docker?OpenStack?VPN?Spark?ERP?IE10Eclipse?CRM?JavaScript?數(shù)據(jù)庫?Ubuntu?NFC?WAP?jQuery?BI?HTML5?Spring?Apache?.NET?API?HTML?SDK?IISFedora?XML?LBS?Unity?Splashtop?UML?components?Windows Mobile?Rails?QEMU?KDE?Cassandra?CloudStack?FTCcoremail?OPhone?CouchBase?云計算?iOS6?Rackspace?Web App?SpringSide?Maemo?Compuware?大數(shù)據(jù)?aptech?PerlTornado?Ruby?Hibernate?ThinkPHP?HBase?Pure?Solr?Angular?Cloud Foundry?Redis?Scala?Django?Bootstrap
    • 個人資料
    • ?
      Flyer_ ?
      • 訪問:75445次
      • 積分:1071
      • 等級:?
      • 排名:千里之外
      • 原創(chuàng):24篇
      • 轉載:47篇
      • 譯文:0篇
      • 評論:10條
    • 文章搜索
    • 文章分類
    • unix環(huán)境高級編程(18)
    • unix網(wǎng)絡編程(4)
    • tcp/ip(4)
    • unix/linux(10)
    • ACM(12)
    • 筆試(13)
    • 面試(8)
    • PAT(4)
    • 30天自制操作系統(tǒng)(1)
    • 前端(4)
    • HTML&CSS(3)
    • ML(7)
    • 文章存檔
      • 2016年10月(1)
      • 2016年08月(1)
      • 2016年07月(6)
      • 2015年07月(3)
      • 2015年03月(1) 展開
    • 閱讀排行
    • bellman ford 算法(11142)
    • 網(wǎng)絡爬蟲c實現(xiàn)(9636)
    • 2014阿里巴巴9月14北京校園招聘筆試及參考答案(3983)
    • 兩道操作系統(tǒng)題目---多道程序(3511)
    • TCP/IP網(wǎng)絡編程之四書五經(jīng)(2704)
    • 2012九月十月騰訊,網(wǎng)易游戲,百度最新校園招聘筆試題(2619)
    • Linux進程地址空間與虛擬內存(2337)
    • PAT 1010. 一元多項式求導 (25)(2186)
    • PAT 1009. 說反話 (20)(2011)
    • 騰訊后臺開發(fā)面試題(1764)
    • 評論排行
    • 2014阿里巴巴9月14北京校園招聘筆試及參考答案(3)
    • 網(wǎng)絡爬蟲c實現(xiàn)(2)
    • 進程與線程的一個簡單解釋(2)
    • 兩道操作系統(tǒng)題目---多道程序(1)
    • LINUX/UNIX 文件狀態(tài)標志的 與或非 操作(1)
    • 編程之美_單鏈表面試題_結合3.4_3.6(1)
    • 2014找工作總結-機會往往留給有準備的人(1)
    • 著名的北郵ACM推薦50題(0)
    • ACM進階指南(0)
    • 匈牙利算法 求解 完美的牛欄(0)
    • 推薦文章
      • * RxJava詳解,由淺入深
      • * 倍升工作效率的小策略
      • * Android熱修復框架AndFix原理解析及使用
      • * “區(qū)塊鏈”究竟是什么鬼
      • * 架構設計:系統(tǒng)存儲-MySQL主從方案業(yè)務連接透明化(中)
    • 最新評論
    • 兩道操作系統(tǒng)題目---多道程序

      Daringoo: 第二題,騰訊給的標準答案是A。在csdn上有自動的測試,我看到了顯示的答案。但是不知怎么得來的。

    • ssd8ex1

      Tiger_Humour: 樓主你好,請問18到25行代碼里的;這一段代碼是如何確定uri.indexOf('/',8)+1的。...

    • 網(wǎng)絡爬蟲c實現(xiàn)

      Echo_Wei1991: 正在寫這個的大作業(yè),謝謝啦~太贊

    • 進程與線程的一個簡單解釋

      qwe8642511: 很好,但是為什么那個鎖上在了門外面。。。。。。出不來了~

    • 2014阿里巴巴9月14北京校園招聘筆試及參考答案

      weidahou227: @xiaor186:大哥你能好好算算在評論嗎,字母有重復的

    • 2014阿里巴巴9月14北京校園招聘筆試及參考答案

      xiaor186: 哎,哥啊,看來這參考答案也真的是只能是參考下而已。第一個的答案用腳趾數(shù)都不可能是D吧,應該是A

    • 編程之美_單鏈表面試題_結合3.4_3.6

      歲月小龍: 沒有實現(xiàn)代碼啊

    • 網(wǎng)絡爬蟲c實現(xiàn)

      歲月小龍: xialai慢慢看

    • 進程與線程的一個簡單解釋

      歲月小龍: 這個圖文并茂,真是太好了

    • 2014找工作總結-機會往往留給有準備的人

      Deebug: 謝謝博主的建議。+1

    • 鏈接
    • Hackbuteer1
      結構之法 算法之道
    公司簡介|招賢納士|廣告服務|聯(lián)系方式|版權聲明|法律顧問|問題報告|合作伙伴|論壇反饋
    網(wǎng)站客服雜志客服微博客服webmaster@csdn.net400-600-2320|北京創(chuàng)新樂知信息技術有限公司 版權所有|江蘇知之為計算機有限公司|江蘇樂知網(wǎng)絡技術有限公司

    總結

    以上是生活随笔為你收集整理的bellman ford 算法 判断是否存在负环的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: 自拍偷拍一区 | 免费看日批| 一级二级毛片 | 精品三级av| 特黄色一级片 | 97操操 | 黄色香港三级三级三级 | 亚洲精品乱码久久 | jizz一区| 大香伊人 | 国产又色又爽无遮挡免费 | 久久9热| 欧美日一本 | 国产淫视 | 欧美成人手机视频 | 一区二区日韩在线观看 | 欧美性猛交xx| 精品国产99久久久久久宅男i | 黑人巨大猛烈捣出白浆 | www成人在线观看 | 亚欧精品在线观看 | 黄色专区| 麻豆乱码国产一区二区三区 | 日韩激情一区二区 | 亚洲不卡一区二区三区 | 精品人妻互换一区二区三区 | 免费久久一级欧美特大黄 | 91亚洲一区二区三区 | 一级片在线观看视频 | 亚洲综合插| 午夜三级在线 | 91精品小视频 | 久久精品久久久精品美女 | 色中文字幕在线观看 | 综合av在线 | 人成免费在线视频 | 日本九九热 | 亚洲精品伊人 | 欧美性生交片4 | 放荡的美妇在线播放 | 国产乱淫av公 | 亚洲色图27p| 久在线观看视频 | 国产又黄又粗又爽 | 亚洲视频在线观看一区二区三区 | 岛国大片在线观看 | www免费黄色 | 人人妻人人澡人人爽 | 亚洲av无码一区二区三区性色 | 韩国伦理在线 | 日韩中文字幕综合 | 中国少妇av | 一区二区三区偷拍 | 午夜激情视频在线观看 | 午夜毛片在线 | 美女网站黄频 | 三上悠亚一区二区 | 国产精品成人av久久 | www.五月天婷婷 | 日韩性生活视频 | 中文在线а√在线 | 变态另类一区二区 | 欧美午夜精品久久久久久孕妇 | av观看网址| 亚洲欧美国产高清va在线播放 | 伊人久久狼人 | 色哟哟官网 | 亚洲欧美成人一区二区 | 亚洲第七页 | 国产午夜精品一区 | 国产乱人乱偷精品视频 | 国产一区二区在线电影 | 91精品国产手机 | 久久亚洲AV无码精品 | 欧美日韩小说 | 亚洲一区二区小说 | 日本亚洲色大成网站www久久 | 国产又黄又爽 | 成人视屏在线 | 免费一级黄色片 | 中文字幕一区二区三区四区欧美 | 国产精品久久久久999 | 亚洲911精品成人18网站 | 亚洲视频观看 | 黄色精品网站 | 一级黄色a级片 | 亚洲av无码一区二区三区人妖 | 欢乐谷在线观看免费播放高清 | 色婷婷成人网 | 国产性猛交╳xxx乱大交一区 | 清冷学长被爆c躁到高潮失禁 | 青草青在线视频 | 色视频2| 波多野结衣av片 | 免费看黄色的网站 | 午夜免费播放观看在线视频 | 性av网| 中国极品少妇videossexhd 就要干就要操 | 欧美网站在线观看 |