page577.html

来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 48 行

HTML
48
字号
<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="tex2html7786" HREF="page578.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html7784" HREF="page576.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html7780" HREF="page576.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html7788" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION0016511000000000000000">Implementation</A></H3><P>An implementation of Prim's algorithm is shown in Program&nbsp;<A HREF="page577.html#progalgorithmse"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.This implementation is almost identicalto the version of <em>Dijkstra's</em> algorithmgiven in Program&nbsp;<A HREF="page567.html#progalgorithmsc"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.In fact, there are only four differences between the two algorithms.These are found on lines 3, 18-20, 24, and&nbsp;26.<P><P><A NAME="53587">&#160;</A><A NAME="progalgorithmse">&#160;</A> <IMG WIDTH=575 HEIGHT=603 ALIGN=BOTTOM ALT="program53584" SRC="img2440.gif"  ><BR><STRONG>Program:</STRONG> Prim's algorithm.<BR><P><P>The <tt>PrimsAlgorithm</tt> method takes two arguments.The first is an undirected graph instance.We assume that the graph is edge-weightedand that the weights are <tt>int</tt>s.The second argument is the number of the start vertex,  <IMG WIDTH=13 HEIGHT=14 ALIGN=MIDDLE ALT="tex2html_wrap_inline71415" SRC="img2332.gif"  >.<P>The <tt>PrimsAlgorithm</tt> method returns a minimum-cost spanning treerepresented as an undirected graph.Therefore, the return value is a <tt>Graph</tt>.<P>The running time of Prim's algorithm is asymptoticallythe same as Dijkstra's algorithm.That is, the worst-case running time is<P> <IMG WIDTH=315 HEIGHT=16 ALIGN=BOTTOM ALT="displaymath71843" SRC="img2374.gif"  ><P>when adjacency lists are used, and<P> <IMG WIDTH=319 HEIGHT=19 ALIGN=BOTTOM ALT="displaymath71844" SRC="img2375.gif"  ><P>when adjacency matrices are used to represent the input graph.<P><HR><A NAME="tex2html7786" HREF="page578.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html7784" HREF="page576.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html7780" HREF="page576.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html7788" 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 &#169; 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 + -
显示快捷键?