📄 page556.html
字号:
<HTML><HEAD><TITLE>Implementation</TITLE></HEAD><BODY bgcolor="#FFFFFF"> <a href="../index.html" target="_top"><img src="../icons/usins.gif" alt="Logo" align=right></a><b>Data Structures and Algorithms with Object-Oriented Design Patterns in Python</b><br><A NAME="tex2html7553" HREF="page557.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html7551" HREF="page555.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html7545" HREF="page555.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html7555" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION0016331000000000000000">Implementation</A></H3><P>Instead of implementing an algorithm that computes a topological sort,we have chosen to implement a traversal that visits the verticesof a DAG in the order given by the topological sort.The topological order traversal can be usedto implement many other graph algorithms.Furthermore, given such a traversal,it is easy to define a visitor that computes a topological sort.<P>In order to implement the algorithm described in the preceding section,an array of integers of length <IMG WIDTH=18 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline70975" SRC="img2249.gif" > is usedto record the in-degrees of the vertices.As a result, it is not really necessary to removevertices or edges from the graph during the traversal.Instead, the effect of removing a vertexand all the edges emanating from that vertexis simulated by decreasing the apparent in-degreesof all the successors of the removed vertex.<P>In addition, we use a queue to keep track of the verticesthat have not yet been visited,but whose in-degree is zero.Doing so eliminates the need to search the array for zero entries.<P>Program <A HREF="page556.html#progdigraphb"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the <tt>topologicalOrderTraversal</tt>method of the abstract <tt>Digraph</tt> class.This method takes as its argument a <tt>Visitor</tt>.The <tt>visit</tt> method of the visitoris called once for each vertex in the graph.The order in which the vertices are visited is given bya topological sort of those vertices.<P><P><A NAME="50756"> </A><A NAME="progdigraphb"> </A> <IMG WIDTH=575 HEIGHT=409 ALIGN=BOTTOM ALT="program50696" SRC="img2306.gif" ><BR><STRONG>Program:</STRONG> Abstract <tt>Digraph</tt> class <tt>topologicalOrderTraversal</tt> method.<BR><P><P>The algorithm begins by computing the in-degrees of all the vertices.An array of integers of length <IMG WIDTH=11 HEIGHT=12 ALIGN=BOTTOM ALT="tex2html_wrap_inline70551" SRC="img2167.gif" > called <tt>inDegree</tt>is used for this purpose.First, all the array elements are set to zero (lines 2-4).Then, for each edge <IMG WIDTH=79 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline71285" SRC="img2307.gif" >,array element <IMG WIDTH=92 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline71287" SRC="img2308.gif" > is increased by one (lines 5-6).Next, a queue to hold vertices is created.All vertices with in-degree zero are put into this queue (lines 7-10).<P>The main loop of the <tt>topologicalOrderTraversal</tt> methodcomprises lines 11-17.This loop continues as long as the queue is not emptyand the visitor is not finished.In each iteration of the main loop exactly one vertex is dequeuedand visited (lines 12-13).<P>Once a vertex has been visited,the effect of removing that vertex from the graph is simulatedby decreasing by one the in-degrees of all the successors of that vertex.When the in-degree of a vertex becomes zero,that vertex is enqueued (lines 14-17).<P><HR><A NAME="tex2html7553" HREF="page557.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html7551" HREF="page555.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html7545" HREF="page555.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html7555" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <P><ADDRESS><img src="../icons/bruno.gif" alt="Bruno" align=right><a href="../copyright.html">Copyright © 2003</a> by <a href="../signature.html">Bruno R. Preiss, P.Eng.</a> All rights reserved.</ADDRESS></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -