数据结构与程序设计(王丽苹)32-graphs拓扑排序.ppt
《数据结构与程序设计(王丽苹)32-graphs拓扑排序.ppt》由会员分享,可在线阅读,更多相关《数据结构与程序设计(王丽苹)32-graphs拓扑排序.ppt(37页珍藏版)》请在三一办公上搜索。
1、9/11/2023,数据结构与程序设计,1,数据结构与程序设计(32),王丽苹,9/11/2023,数据结构与程序设计,2,Chapter 12,GRAPHSTopological SortShortest PathMinimal Spanning Trees,9/11/2023,数据结构与程序设计,3,Topological 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 th
2、at,for 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.7,9/11/2023,数据结构与程序设计,4,Example:Topological order(拓扑排序),C0 高等数学,C1 程序设计语言,C2 离散数学,C3 数据结构,C4 算法语言,C5 编译技术,C6 操作系统,C7 概率论,C8 计算机原理,9/11/2023,数据结构与程序设计,5,12.4.2 Depth-first Algorithm,St
3、art by finding a vertex that has no successors and place it last in the order.By 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/11/2023,数据结构与程序设计,6,9/11/2023,数据结构与程序设计,7,Digraph Class,Topologic
4、al Sort,typedef int Vertex;template class Digraph public:Digraph();void read();void write();/methods to do a topological sortvoid depth_sort(List,9/11/2023,数据结构与程序设计,8,Depth-First Algorithm p581,template void Digraph:depth_sort(List,9/11/2023,数据结构与程序设计,9,Depth-First Algorithm p581,template void Digr
5、aph:recursive_depth_sort(Vertex v,bool*visited,List/Put v into topological_order.,9/11/2023,数据结构与程序设计,10,12.4.3 Breadth-First Algorithm,Start by finding the vertices that should be first in the topological order.That is the vertices which have no predecessor.Apply the fact that every vertex must com
6、e before its successors.,9/11/2023,数据结构与程序设计,11,9/11/2023,数据结构与程序设计,12,Breadth-First Algorithm p582,template void Digraph:breadth_sort(List,9/11/2023,数据结构与程序设计,13,Breadth-First Algorithm p582,Queue ready_to_process;for(v=0;v count;v+)if(predecessor_countv=0)ready_to_process.append(v);while(!ready_to
7、_process.empty()ready_to_process.retrieve(v);topological_order.insert(topological order.size(),v);for(int j=0;j neighborsv.size();j+)neighborsv.retrieve(j,w);/Traverse successors of v.predecessor_countw;if(predecessor_countw=0)ready_to_process.append(w);ready_to_process.serve();,9/11/2023,数据结构与程序设计,
8、14,12.5 A Greedy Algorithm:Shortest Paths,The problem of shortest paths:Given a directed graph in which each edge has a nonnegativeweight or cost,find a path of least total weight from a givenvertex,called the source,to every other vertex in the graph.P583 figure 12.8,9/11/2023,数据结构与程序设计,15,Method f
9、or shortest path:Dijkstra Algorithm(Greedy Algorithm),Method:We keep a set S of vertices whose shortest distances from source is known.At each step,we add to S a remaining vertex for which the shortest path from source has been found.We maintain a table distance that gives,for each remaining vertex
10、v,the shortest distance from S to v.Initial:S=source,distance table is the distance from source to v.,9/11/2023,数据结构与程序设计,16,Example Dijkstra Algorithm,Greedy AlgorithmAssume all weight of edge 0,distance table,9/11/2023,数据结构与程序设计,17,Example Dijkstra Algorithm,Greedy AlgorithmAssume all weight of ed
11、ge 0,distance table,9/11/2023,数据结构与程序设计,18,Example Dijkstra Algorithm,step 1:find the shortest path to node 0node 4 is selected to set S,distance table,9/11/2023,数据结构与程序设计,19,Example Dijkstra Algorithm,step 2:recalculate the path to all other nodesfind the shortest path to node 0.Node 2 is selected,
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 程序设计 王丽苹 32 graphs 拓扑 排序

链接地址:https://www.31ppt.com/p-5986020.html