page571.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 51 行
HTML
51 行
<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="tex2html7721" HREF="page572.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html7719" HREF="page569.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html7713" HREF="page570.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html7723" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION0016422000000000000000">Implementation</A></H3><P>An implementation of Floyd's algorithm is shown in Program <A HREF="page567.html#progalgorithmsc"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.The <tt>FloydsAlgorithm</tt> method takes as its argument a directed graph.The directed graph is assumed to be an edge-weighted graphin which the weights are <tt>int</tt>s.<P><P><A NAME="52175"> </A><A NAME="progalgorithmsd"> </A> <IMG WIDTH=575 HEIGHT=527 ALIGN=BOTTOM ALT="program52172" SRC="img2400.gif" ><BR><STRONG>Program:</STRONG> Floyd's algorithm.<BR><P><P>The <tt>FloydsAlgorithm</tt> method returns its resultin the form of an edge-weighted directed graph.Therefore, the return value is a <tt>Digraph</tt>.<P>The principal data structure use by the algorithmis a <IMG WIDTH=57 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline71089" SRC="img2274.gif" > matrix of integers called <tt>distance</tt>.All the elements of the matrix are initially set to <IMG WIDTH=14 HEIGHT=7 ALIGN=BOTTOM ALT="tex2html_wrap_inline68397" SRC="img1875.gif" > (lines 6-8).Next, an edge iterator is used to visit all the edges in the input graphin order to transfer the weightsfrom the graph to the <tt>distance</tt> matrix (lines 9-10).<P>The main work of the algorithm is done in three, nested loops (lines 11-18).The outer loop computes the sequence of distance matrices <IMG WIDTH=112 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline71981" SRC="img2401.gif" >.The inner two loops consider all possible pairs of vertices.Notice that as <IMG WIDTH=33 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline71937" SRC="img2395.gif" > is computed, its entries overwrite those of <IMG WIDTH=17 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline71931" SRC="img2392.gif" >.<P>Finally, the values in the <tt>distance</tt> matrix are transferedto the result graph (lines 19-25).The result graph contains the same set of vertices as the input graph.For each finite entry in the <tt>distance</tt> matrix,a weighted edge is added to the result graph.<P><HR><A NAME="tex2html7721" HREF="page572.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html7719" HREF="page569.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html7713" HREF="page570.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html7723" 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 + -
显示快捷键?