数据结构与程序设计王丽苹32graphs拓扑排序.ppt
《数据结构与程序设计王丽苹32graphs拓扑排序.ppt》由会员分享,可在线阅读,更多相关《数据结构与程序设计王丽苹32graphs拓扑排序.ppt(37页珍藏版)》请在第壹文秘上搜索。
1、9/7/2023数据结构与程序设计1数据结构与程序设计数据结构与程序设计(32)9/7/2023数据结构与程序设计2Chapter 12nGRAPHSnTopological SortnShortest PathnMinimal Spanning Trees9/7/2023数据结构与程序设计3Topological 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
2、all vertices v,w G,if there is an edge from v to w,then v precedes w in the sequential listing.P580 Figure 12.79/7/2023数据结构与程序设计4Example:Topological order(拓扑排序)nC0 高等数学,C1 程序设计语言,C2 离散数学,C3 数据结构,C4 算法语言,C5 编译技术,C6 操作系统,C7 概率论,C8 计算机原理9/7/2023数据结构与程序设计512.4.2 Depth-first AlgorithmnStart by finding a
3、vertex that has no successors 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.9/7/2023数据结构与程序设计69/7/2023数据结构与程序设计7Digraph Class,Topological Sorttypedef int Vertex
4、;template class Digraph 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);9/7/
5、2023数据结构与程序设计8Depth-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 recursive_dept
6、h_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);9/7/2023数据结构与程序设计9Depth-First Algor
7、ithm 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 added to topological orderwith a depth-first search.U
8、ses: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 successors of w.recursive_depth_sort(w,visited,topological_order);topological_or
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 程序设计 王丽苹 32 graphs 拓扑 排序
![提示](https://www.1wenmi.com/images/bang_tan.gif)