page570.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 68 行
HTML
68 行
<HTML><HEAD><TITLE>Floyd's Algorithm</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="tex2html7710" HREF="page571.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html7708" HREF="page569.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html7702" HREF="page569.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html7712" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION0016421000000000000000">Floyd's Algorithm</A></H3><P><em>Floyd's algorithm</em><A NAME=52008> </A> uses thedynamic programming methodto solve the all-pairs shortest-path problem on a dense graph.The method makes efficient use ofan adjacency matrix to solve the problem.Consider an edge-weighted graph <IMG WIDTH=73 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline70549" SRC="img2166.gif" >,where <I>C</I>(<I>v</I>,<I>w</I>) represents the weight on edge (<I>v</I>,<I>w</I>).Suppose the vertices are numbered from 1 to <IMG WIDTH=18 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline70975" SRC="img2249.gif" >.That is, let <IMG WIDTH=145 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline71893" SRC="img2379.gif" >.Furthermore,let <IMG WIDTH=16 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline71895" SRC="img2380.gif" > be the set comprised of the first <I>k</I> vertices in <IMG WIDTH=11 HEIGHT=12 ALIGN=BOTTOM ALT="tex2html_wrap_inline70551" SRC="img2167.gif" >.That is, <IMG WIDTH=142 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline71901" SRC="img2381.gif" >, for <IMG WIDTH=78 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline71903" SRC="img2382.gif" >.<P>Let <IMG WIDTH=57 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline71905" SRC="img2383.gif" > be the shortest path from vertex <I>v</I> to <I>w</I>that passes only through vertices in <IMG WIDTH=16 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline71895" SRC="img2380.gif" >,if such a path exists.That is, the path <IMG WIDTH=57 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline71905" SRC="img2383.gif" > has the form<P> <IMG WIDTH=330 HEIGHT=35 ALIGN=BOTTOM ALT="displaymath71873" SRC="img2384.gif" ><P><P>Let <IMG WIDTH=60 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline71915" SRC="img2385.gif" > be the <em>length</em> of path <IMG WIDTH=57 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline71905" SRC="img2383.gif" >:<P> <IMG WIDTH=391 HEIGHT=48 ALIGN=BOTTOM ALT="displaymath71874" SRC="img2386.gif" ><P><P>Since <IMG WIDTH=46 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline71919" SRC="img2387.gif" >,the <IMG WIDTH=17 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline67015" SRC="img1611.gif" > paths are correspond to the edges of <I>G</I>:<P> <IMG WIDTH=373 HEIGHT=48 ALIGN=BOTTOM ALT="displaymath71875" SRC="img2388.gif" ><P><P>Therefore, the <IMG WIDTH=20 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline71925" SRC="img2389.gif" > path lengths correspond to the weights on the edges of <I>G</I>:<P> <IMG WIDTH=367 HEIGHT=48 ALIGN=BOTTOM ALT="displaymath71876" SRC="img2390.gif" ><P><P>Floyd's algorithm computes the sequence of matrices <IMG WIDTH=112 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline71929" SRC="img2391.gif" >.The distances in <IMG WIDTH=17 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline71931" SRC="img2392.gif" > represent paths with intermediate vertices in <IMG WIDTH=14 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline71933" SRC="img2393.gif" >.Since <IMG WIDTH=129 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline71935" SRC="img2394.gif" >,we can obtain the distances in <IMG WIDTH=33 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline71937" SRC="img2395.gif" > from those in <IMG WIDTH=17 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline71931" SRC="img2392.gif" >by considering only the paths that pass through vertex <IMG WIDTH=27 HEIGHT=15 ALIGN=MIDDLE ALT="tex2html_wrap_inline70713" SRC="img2200.gif" >.Figure <A HREF="page570.html#figgraph14"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> illustrates how this is done.<P><P><A NAME="52227"> </A><A NAME="figgraph14"> </A> <IMG WIDTH=575 HEIGHT=146 ALIGN=BOTTOM ALT="figure52030" SRC="img2396.gif" ><BR><STRONG>Figure:</STRONG> Calculating <IMG WIDTH=33 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline71937" SRC="img2395.gif" > in Floyd's algorithm.<BR><P><P>For every pair of vertices (<I>v</I>,<I>w</I>),we compare the distance <IMG WIDTH=57 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline71959" SRC="img2397.gif" >,(which represents the shortest path from <I>v</I> to <I>w</I>that does not pass through <IMG WIDTH=27 HEIGHT=15 ALIGN=MIDDLE ALT="tex2html_wrap_inline70713" SRC="img2200.gif" >)with the sum <IMG WIDTH=172 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline71967" SRC="img2398.gif" >(which represents the shortest path from <I>v</I> to <I>w</I>that does pass through <IMG WIDTH=27 HEIGHT=15 ALIGN=MIDDLE ALT="tex2html_wrap_inline70713" SRC="img2200.gif" >).Thus, <IMG WIDTH=33 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline71937" SRC="img2395.gif" > is computed as follows:<P> <IMG WIDTH=440 HEIGHT=16 ALIGN=BOTTOM ALT="displaymath71877" SRC="img2399.gif" ><P><HR><A NAME="tex2html7710" HREF="page571.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html7708" HREF="page569.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html7702" HREF="page569.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html7712" 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 + -
显示快捷键?