page576.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 63 行
HTML
63 行
<HTML><HEAD><TITLE>Prim'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="tex2html7776" HREF="page577.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html7774" HREF="page573.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html7768" HREF="page575.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html7778" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION0016510000000000000000">Prim's Algorithm</A></H2><P><em>Prim's algorithm</em><A NAME=52874> </A>finds a minimum-cost spanning treeof an edge-weighted, connected, undirected graph <IMG WIDTH=73 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline70549" SRC="img2166.gif" >.The algorithm constructs the minimum-cost spanning tree of a graphby selecting edges from the graph one-by-oneand adding those edges to the spanning tree.<P>Prim's algorithm is essentiallya minor variation of <em>Dijkstra's algorithm</em>(see Section <A HREF="page565.html#secgraphsdijkstra"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>).To construct the spanning tree,the algorithm constructs a sequence of spanning trees <IMG WIDTH=116 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline72161" SRC="img2426.gif" >,each of which is a subgraph of <I>G</I>.The algorithm begins with a tree that containsone selected vertex, say <IMG WIDTH=45 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline71411" SRC="img2330.gif" >.That is, <IMG WIDTH=101 HEIGHT=27 ALIGN=MIDDLE ALT="tex2html_wrap_inline72167" SRC="img2427.gif" >.<P>Given <IMG WIDTH=87 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline72169" SRC="img2428.gif" >,we obtain the next tree in the sequence as follows.Consider the set of edges given by<P> <IMG WIDTH=345 HEIGHT=36 ALIGN=BOTTOM ALT="displaymath72155" SRC="img2429.gif" ><P>The set <IMG WIDTH=17 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline72171" SRC="img2430.gif" > contains all the edges <IMG WIDTH=42 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline72173" SRC="img2431.gif" >such that exactly one of <I>v</I> or <I>w</I> is in <IMG WIDTH=14 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline71933" SRC="img2393.gif" > (but not both).Select the edge <IMG WIDTH=79 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline72181" SRC="img2432.gif" > with the smallest edge weight,<P> <IMG WIDTH=352 HEIGHT=27 ALIGN=BOTTOM ALT="displaymath72156" SRC="img2433.gif" ><P>Then <IMG WIDTH=135 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline72183" SRC="img2434.gif" >,where <IMG WIDTH=109 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline72185" SRC="img2435.gif" >and <IMG WIDTH=139 HEIGHT=27 ALIGN=MIDDLE ALT="tex2html_wrap_inline72187" SRC="img2436.gif" >.After <IMG WIDTH=46 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline72059" SRC="img2415.gif" > such steps we get <IMG WIDTH=42 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline72191" SRC="img2437.gif" >which is the minimum-cost spanning tree of <I>G</I>.<P>Figure <A HREF="page576.html#figgraph17"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> illustrates how Prim's algorithm determinesthe minimum-cost spanning tree of the graph <IMG WIDTH=25 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline72147" SRC="img2423.gif" > shown in Figure <A HREF="page575.html#figgraph16"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.The circled vertices are the elements of <IMG WIDTH=14 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline71933" SRC="img2393.gif" >,the solid edges represent the elements of <IMG WIDTH=12 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline72199" SRC="img2438.gif" >and the dashed edges represent the elements of <IMG WIDTH=17 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline72171" SRC="img2430.gif" >.<P><P><A NAME="53577"> </A><A NAME="figgraph17"> </A> <IMG WIDTH=575 HEIGHT=384 ALIGN=BOTTOM ALT="figure52890" SRC="img2439.gif" ><BR><STRONG>Figure:</STRONG> Operation of Prim's algorithm.<BR><P><BR> <HR><UL> <LI> <A NAME="tex2html7779" HREF="page577.html#SECTION0016511000000000000000">Implementation</A></UL><HR><A NAME="tex2html7776" HREF="page577.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html7774" HREF="page573.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html7768" HREF="page575.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html7778" 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 + -
显示快捷键?