数据结构与程序设计王丽苹32graphs拓扑排序.ppt

上传人:王** 文档编号:468110 上传时间:2023-09-08 格式:PPT 页数:37 大小:1.06MB
下载 相关 举报
数据结构与程序设计王丽苹32graphs拓扑排序.ppt_第1页
第1页 / 共37页
数据结构与程序设计王丽苹32graphs拓扑排序.ppt_第2页
第2页 / 共37页
数据结构与程序设计王丽苹32graphs拓扑排序.ppt_第3页
第3页 / 共37页
数据结构与程序设计王丽苹32graphs拓扑排序.ppt_第4页
第4页 / 共37页
数据结构与程序设计王丽苹32graphs拓扑排序.ppt_第5页
第5页 / 共37页
数据结构与程序设计王丽苹32graphs拓扑排序.ppt_第6页
第6页 / 共37页
数据结构与程序设计王丽苹32graphs拓扑排序.ppt_第7页
第7页 / 共37页
数据结构与程序设计王丽苹32graphs拓扑排序.ppt_第8页
第8页 / 共37页
数据结构与程序设计王丽苹32graphs拓扑排序.ppt_第9页
第9页 / 共37页
数据结构与程序设计王丽苹32graphs拓扑排序.ppt_第10页
第10页 / 共37页
亲,该文档总共37页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《数据结构与程序设计王丽苹32graphs拓扑排序.ppt》由会员分享,可在线阅读,更多相关《数据结构与程序设计王丽苹32graphs拓扑排序.ppt(37页珍藏版)》请在优知文库上搜索。

1、9/8/2023数据结构与程序设计1数据结构与程序设计数据结构与程序设计(32)9/8/2023数据结构与程序设计2Chapter 12nGRAPHSnTopological SortnShortest PathnMinimal Spanning Trees9/8/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/8/2023数据结构与程序设计4Example:Topological order(拓扑排序)nC0 高等数学,C1 程序设计语言,C2 离散数学,C3 数据结构,C4 算法语言,C5 编译技术,C6 操作系统,C7 概率论,C8 计算机原理9/8/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/8/2023数据结构与程序设计69/8/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/8/

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/8/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

9、der.insert(0,v);/Put v into topological_order.9/8/2023数据结构与程序设计1012.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.nApply the fact that every vertex must come before its successors.9/8/2023数据结构与程序

10、设计119/8/2023数据结构与程序设计12Breadth-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 breadth-first traversal of those vertices that do not belongto a cycle.Uses:Methods of cla

11、sses 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+)neighborsv.retrieve(i,w);/Loop over all edges v-w.predecessor_countw+;9/8/2023数据结构与程序设计13Breadth-First Algorithm p5

12、82Queue 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_order.insert(topological order.size(),v);for(int j=0;j 0node from node V0 to other nodesV1V2V3V4bestdistance table9/8/2023数据结构与程序设计17

13、Example Dijkstra AlgorithmnGreedy AlgorithmnAssume all weight of edge 0node from node V0 to other nodesV15V23V3 V42bestV4distance table9/8/2023数据结构与程序设计18Example Dijkstra Algorithmnstep 1:find the shortest path to node 0nnode 4 is selected to set Snode from node V0 to other nodesV15V23V3 V42bestV4di

14、stance table9/8/2023数据结构与程序设计19Example Dijkstra Algorithmnstep 2:recalculate the path to all other nodesnfind the shortest path to node 0.Node 2 is selectednode from node V0 to other nodesV155V233V3 6V42bestV4V2distance table9/8/2023数据结构与程序设计20Example Dijkstra Algorithmnstep 3:recalculate the path t

15、o all other nodesnfind the shortest path to node 0.node 1 is selectednode from node V0 to other nodesV1554V233V3 65V42bestV4V2V1distance table9/8/2023数据结构与程序设计21Example Dijkstra Algorithmnstep 4:recalculate the path to all other nodesnfind the shortest path to node 0.node 3 is selectednode from node

16、 V0 to other nodesV1554V233V3 655V42bestV4V2V1V39/8/2023数据结构与程序设计22A Greedy Algorithm:Shortest Paths p587template class Digraph public:/Add a constructor and methods for Digraph input and output.void set_distances(Vertex source,Weight distance)const;protected:int count;Weight adjacencygraph_sizegraph_size;9/8/2023数据结构与程序设计23A Greedy Algorithm:Shortest Pathstemplate void Digraph:set_distances(Vertex source,Weight distance)const/*Post:The array distance gives the minimal path weight from vertex so

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > IT计算机 > 数据结构与算法

copyright@ 2008-2023 yzwku网站版权所有

经营许可证编号:宁ICP备2022001189号-2

本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!