




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、11/23/2021數(shù)據(jù)結(jié)構(gòu)與程序設(shè)計1Chapter 12nGRAPHSnTopological SortnShortest PathnMinimal Spanning Trees11/23/2021數(shù)據(jù)結(jié)構(gòu)與程序設(shè)計2Topological order(拓撲排序)Let G be a directed graph with no cycles. A topological order for G is a sequential listing of all the vertices in G such that, for all vertices v,w G, if there is an
2、 edge from v to w, then v precedes w in the sequential listing.P580 Figure 12.711/23/2021數(shù)據(jù)結(jié)構(gòu)與程序設(shè)計3Example: Topological order(拓撲排序)nC0 高等數(shù)學(xué),C1 程序設(shè)計語言,C2 離散數(shù)學(xué), C3 數(shù)據(jù)結(jié)構(gòu),C4 算法語言,C5 編譯技術(shù),C6 操作系統(tǒng), C7 概率論,C8 計算機原理11/23/2021數(shù)據(jù)結(jié)構(gòu)與程序設(shè)計412.4.2 Depth-first AlgorithmnStart by finding a vertex that has no succes
3、sors and place it last in the order.nBy recursive, After placed all the successors of a vertex into the topological order, then place the vertex itself in position before any of its successors.11/23/2021數(shù)據(jù)結(jié)構(gòu)與程序設(shè)計511/23/2021數(shù)據(jù)結(jié)構(gòu)與程序設(shè)計6Digraph Class, Topological Sorttypedef int Vertex;template class Di
4、graph public:Digraph( );void read( );void write( );/ methods to do a topological sortvoid depth_sort(List &topological_order);void breadth_sort(List &topological_order);private:int count;List neighborsgraph_size;void recursive_depth_sort(Vertex v, bool visited, List &topological_order);1
5、1/23/2021數(shù)據(jù)結(jié)構(gòu)與程序設(shè)計7Depth-First Algorithm p581template void Digraph :depth_sort(List &topological_order)/* Post: The vertices of the Digraph are placed into List topological_order with adepth-first traversal of those vertices that do not belong to a cycle.Uses: Methods of class List, and function
6、 recursive_depth_sort to perform depth-first traversal. */bool visitedgraph_size;Vertex v;for (v = 0; v count; v+) visitedv = false;topological_order.clear( );for (v = 0; v count; v+)if (!visitedv) / Add v and its successors into topological order.recursive_depth_sort(v, visited, topological_order);
7、11/23/2021數(shù)據(jù)結(jié)構(gòu)與程序設(shè)計8Depth-First Algorithm p581template void Digraph :recursive_depth_sort(Vertex v, bool *visited, List &topological_order)/* Pre: Vertex v of the Digraph does not belong to the partially completed List topological_order.Post: All the successors of v and finally v itself are adde
8、d to topological orderwith a depth-first search.Uses: Methods of class List and the function recursive_depth_sort. */ visitedv = true;int degree = neighborsv.size( );for (int i = 0; i degree; i+) Vertex w; / A (neighboring) successor of vneighborsv.retrieve(i, w);if (!visitedw) / Order the successor
9、s of w.recursive_depth_sort(w, visited, topological_order);topological_order.insert(0, v); / Put v into topological_order.11/23/2021數(shù)據(jù)結(jié)構(gòu)與程序設(shè)計912.4.3 Breadth-First AlgorithmnStart by finding the vertices that should be first in the topological order. That is the vertices which have no predecessor.nAp
10、ply the fact that every vertex must come before its successors.11/23/2021數(shù)據(jù)結(jié)構(gòu)與程序設(shè)計1011/23/2021數(shù)據(jù)結(jié)構(gòu)與程序設(shè)計11Breadth-First Algorithm p582template void Digraph :breadth_sort(List &topological_order)/* Post: The vertices of the Digraph are arranged into the List topological_orderwhich is found with a
11、breadth-first traversal of those vertices that do not belongto a cycle.Uses: Methods of classes Queue and List. */ topological_order.clear( );Vertex v, w;int predecessor_countgraph size;for (v = 0; v count; v+) predecessor_countv = 0;for (v = 0; v count; v+)for (int i = 0; i neighborsv.size( ); i+)
12、neighborsv.retrieve(i, w); / Loop over all edges v-w.predecessor_countw+;11/23/2021數(shù)據(jù)結(jié)構(gòu)與程序設(shè)計12Breadth-First Algorithm p582Queue ready_to_process;for (v = 0; v count; v+)if (predecessor_countv = 0)ready_to_process.append(v);while (!ready_to_process.empty( ) ready_to_process.retrieve(v);topological_or
13、der.insert(topological order.size( ), v);for (int j = 0; j 0node from node V0 to other nodesV1V2V3V4bestdistance table11/23/2021數(shù)據(jù)結(jié)構(gòu)與程序設(shè)計16Example Dijkstra AlgorithmnGreedy AlgorithmnAssume all weight of edge 0node from node V0 to other nodesV15V23V3 V42bestV4distance table11/23/2021數(shù)據(jù)結(jié)構(gòu)與程序設(shè)計17Examp
14、le Dijkstra Algorithmnstep 1: find the shortest path to node 0nnode 4 is selected to set Snode from node V0 to other nodesV15V23V3 V42bestV4distance table11/23/2021數(shù)據(jù)結(jié)構(gòu)與程序設(shè)計18Example Dijkstra Algorithmnstep 2: recalculate the path to all other nodesnfind the shortest path to node 0. Node 2 is select
15、ednode from node V0 to other nodesV155V233V3 6V42bestV4V2distance table11/23/2021數(shù)據(jù)結(jié)構(gòu)與程序設(shè)計19Example Dijkstra Algorithmnstep 3: recalculate the path to all other nodesnfind the shortest path to node 0. node 1 is selectednode from node V0 to other nodesV1554V233V3 65V42bestV4V2V1distance table11/23/20
16、21數(shù)據(jù)結(jié)構(gòu)與程序設(shè)計20Example Dijkstra Algorithmnstep 4: recalculate the path to all other nodesnfind the shortest path to node 0. node 3 is selectednode from node V0 to other nodesV1554V233V3 655V42bestV4V2V1V311/23/2021數(shù)據(jù)結(jié)構(gòu)與程序設(shè)計21A Greedy Algorithm: Shortest Paths p587template class Digraph public: / Add a
17、 constructor and methods for Digraph input and output.void set_distances(Vertex source, Weight distance) const;protected:int count;Weight adjacencygraph_sizegraph_size;11/23/2021數(shù)據(jù)結(jié)構(gòu)與程序設(shè)計22A Greedy Algorithm: Shortest Pathstemplate void Digraph : set_distances(Vertex source,Weight distance) const/*
18、Post: The array distance gives the minimal path weight from vertex source to each vertex of the Digraph. */ Vertex v, w;bool foundgraph_size; / Vertices found in Sfor (v = 0; v count; v+) foundv = false;distancev = adjacencysourcev;foundsource = true; / Initialize with vertex source alone in the set
19、 S.distancesource = 0;11/23/2021數(shù)據(jù)結(jié)構(gòu)與程序設(shè)計23A Greedy Algorithm: Shortest Pathsfor (int i = 0; i count; i+) / Add one vertex v to S on each pass.Weight min = infinity;for (w = 0; w count; w+) if (!foundw)if (distancew min) v = w;min = distancew;foundv = true;for (w = 0; w count; w+) if (!foundw)if (mi
20、n + adjacencyvw distancew)distancew = min + adjacencyvw;11/23/2021數(shù)據(jù)結(jié)構(gòu)與程序設(shè)計2412.6 Minimal Spanning Trees(MST)最小生成樹nA (connected) tree that is build up out of all the vertices and some of the edges of G is called a spanning tree of G.nIf original graph has n vertices, the spanning tree has n vertices
21、 and n-1 edges.nNo circle in this subgraphnDEFINITION A minimal spanning tree of a connected network is a spanning tree such that the sum of the weights of its edges is as small as possible.11/23/2021數(shù)據(jù)結(jié)構(gòu)與程序設(shè)計25Two Spanning Tree11/23/2021數(shù)據(jù)結(jié)構(gòu)與程序設(shè)計26Minimum Spanning Tree (MST)6715102061015Spanning tr
22、ee with minimum weight11/23/2021數(shù)據(jù)結(jié)構(gòu)與程序設(shè)計27Prims Algorithm for Minimal Spanning TreesnStart with a source vertex. nKeep a set X of those vertices whose paths to source in the minimal spanning tree that we are building have been found. nKeep the set Y of edges that link the vertices in X in the tree
23、under construction. nThe vertices in X and edges in Y make up a small tree that grows to become our final spanning tree.11/23/2021數(shù)據(jù)結(jié)構(gòu)與程序設(shè)計28Prims Algorithm for Minimal Spanning TreesnInitially:nsource is the only vertex in X, and Y is empty. nAt each step:nwe add an additional vertex to X: nThis ve
24、rtex is chosen so that an edge back to X has samllest weight. This minimal edge back to X is added to Y .nneighborw, is a vertex in X which is nearest to w.nDistancew, is the value for the nearst distance of w. 11/23/2021數(shù)據(jù)結(jié)構(gòu)與程序設(shè)計29Example of Prims Algorithm11/23/2021數(shù)據(jù)結(jié)構(gòu)與程序設(shè)計30Example of Prims Algo
25、rithm11/23/2021數(shù)據(jù)結(jié)構(gòu)與程序設(shè)計31Example of Prims Algorithm11/23/2021數(shù)據(jù)結(jié)構(gòu)與程序設(shè)計32Example of Prims Algorithm11/23/2021數(shù)據(jù)結(jié)構(gòu)與程序設(shè)計33Implementation of Prims Algorithmtemplate class Network: public Digraph public:Network( );void read( ); / overridden method to enter a Networkvoid make empty(int size = 0);void add
26、 edge(Vertex v, Vertex w, Weight x);void minimal spanning(Vertex source,Network &tree) const;11/23/2021數(shù)據(jù)結(jié)構(gòu)與程序設(shè)計34template void Network : minimal spanning(Vertex source, Network &tree) const/* Post: The Network tree contains a minimal spanning tree for the connected componentof the original Network
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 三年級數(shù)學(xué)下冊教案-5.3 長方形的面積(2)-北師大版
- 五年級下冊數(shù)學(xué)教案-2.1 分數(shù)的意義 ︳西師大版
- 2025年合同占股模板
- 一年級下數(shù)學(xué)教案-綜合練習(xí)-北師大版
- 2025年河北省石家莊市單招職業(yè)傾向性測試題庫附答案
- 2024年浸酸劑項目資金籌措計劃書代可行性研究報告
- 2025年湖南省郴州市單招職業(yè)適應(yīng)性測試題庫審定版
- 2025年度心理咨詢師培訓(xùn)朋輩督導(dǎo)小組保密合作協(xié)議
- 2025年度家禽養(yǎng)殖與食品安全監(jiān)管合作協(xié)議
- 2025年度導(dǎo)演與票務(wù)銷售公司聘用合同
- 中小學(xué)教師教育法律法規(guī)培訓(xùn)PPT頁
- 醫(yī)療器械可用性工程文檔
- 非遺文化介紹推廣課件
- 統(tǒng)編教材四年級下冊語文第二單元教學(xué)解讀及建議1
- 火電機組整套啟動前安全技術(shù)交底卡
- 菲斯特轉(zhuǎn)子秤的
- 藥學(xué)專業(yè)教學(xué)資源庫建設(shè)申報書
- 解讀《泰州市市區(qū)城市排水管理辦法》
- 人教版五年級下冊口算題大全(全冊齊全)
- 林則徐課件完整版
- 旅行社運營實務(wù)電子課件 6.1 初涉旅行社管理
評論
0/150
提交評論