page555.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 73 行
HTML
73 行
<HTML><HEAD><TITLE>Topological Sort</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="tex2html7540" HREF="page556.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html7538" HREF="page548.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html7532" HREF="page554.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html7542" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION0016330000000000000000">Topological Sort</A></H2><A NAME="secgraphstoposort"> </A><P>A topological sort is an ordering of the vertices of a<em>directed acyclic graph</em>given by the following definition:<P><BLOCKQUOTE> <b>Definition (Topological Sort)</b>Consider a directed acyclic graph <IMG WIDTH=73 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline70549" SRC="img2166.gif" >.A <em>topological sort</em><A NAME=50447> </A><A NAME=50448> </A>of the vertices of <I>G</I>is a sequence <IMG WIDTH=143 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline71251" SRC="img2298.gif" >in which each element of <IMG WIDTH=11 HEIGHT=12 ALIGN=BOTTOM ALT="tex2html_wrap_inline70551" SRC="img2167.gif" > appears exactly once.For every pair of distinct vertices <IMG WIDTH=12 HEIGHT=14 ALIGN=MIDDLE ALT="tex2html_wrap_inline70715" SRC="img2201.gif" > and <IMG WIDTH=13 HEIGHT=15 ALIGN=MIDDLE ALT="tex2html_wrap_inline71257" SRC="img2299.gif" > in the sequence <I>S</I>,if <IMG WIDTH=50 HEIGHT=16 ALIGN=MIDDLE ALT="tex2html_wrap_inline70863" SRC="img2232.gif" > is an edge in <I>G</I>,i.e., <IMG WIDTH=74 HEIGHT=25 ALIGN=MIDDLE ALT="tex2html_wrap_inline71265" SRC="img2300.gif" >,then <I>i</I><I><</I><I>j</I>.</BLOCKQUOTE><P>Informally, a topological sort is a list of the vertices of a DAGin which all the successors of any given vertexappear in the sequence after that vertex.Consider the directed acyclic graph <IMG WIDTH=19 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline71269" SRC="img2301.gif" > shown in Figure <A HREF="page555.html#figgraph8"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.The sequence <IMG WIDTH=172 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline71271" SRC="img2302.gif" > is a topological sortof the vertices of <IMG WIDTH=19 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline71269" SRC="img2301.gif" >.To see that this is so,consider the set of vertices:<P> <IMG WIDTH=415 HEIGHT=39 ALIGN=BOTTOM ALT="displaymath71245" SRC="img2303.gif" ><P>The vertices in each edge are in alphabetical order,and so is the sequence <I>S</I>.<P><P><A NAME="50679"> </A><A NAME="figgraph8"> </A> <IMG WIDTH=575 HEIGHT=240 ALIGN=BOTTOM ALT="figure50454" SRC="img2304.gif" ><BR><STRONG>Figure:</STRONG> A directed acyclic graph.<BR><P><P>It should also be evident from Figure <A HREF="page555.html#figgraph8"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> thata topological sort is not unique.For example, the following are also valid topological sortsof the graph <IMG WIDTH=19 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline71269" SRC="img2301.gif" >:<P> <IMG WIDTH=500 HEIGHT=94 ALIGN=BOTTOM ALT="eqnarray50683" SRC="img2305.gif" ><P><P>One way to find a topological sortis to consider the <em>in-degrees</em><A NAME=50686> </A> of the vertices.(The number above a vertex in Figure <A HREF="page555.html#figgraph8"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> is the in-degree of that vertex).Clearly the first vertex in a topological sort must have in-degree zero andevery DAG must contain at least one vertex with in-degree zero.A simple algorithm to create the sort goes like this:<P>Repeat the following steps until the graph is empty:<OL><LI> Select a vertex that has in-degree zero.<LI> Add the vertex to the sort.<LI> Delete the vertex and all the edges emanating from it from the graph.</OL><BR> <HR><UL> <LI> <A NAME="tex2html7543" HREF="page556.html#SECTION0016331000000000000000">Implementation</A><LI> <A NAME="tex2html7544" HREF="page557.html#SECTION0016332000000000000000">Running Time Analysis</A></UL><HR><A NAME="tex2html7540" HREF="page556.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html7538" HREF="page548.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html7532" HREF="page554.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html7542" 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 + =
减小字号Ctrl + -
显示快捷键?