page568.html

来自「wqeqwvrw rkjqhwrjwq jkhrjqwhrwq jkhrwq」· HTML 代码 · 共 70 行

HTML
70
字号
<HTML>
<HEAD>
<TITLE>Implementation</TITLE>
</HEAD>
<BODY bgcolor="#FFFFFF">
 <img src="cover75.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cover75.gif" alt="Logo" align=right>
<b>Data Structures and Algorithms 
with Object-Oriented Design Patterns in C++</b><br>
<A NAME="tex2html8934" HREF="page569.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page569.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="next_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/next_motif.gif"></A> <A NAME="tex2html8932" HREF="page565.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page565.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="up_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/up_motif.gif"></A> <A NAME="tex2html8926" HREF="page567.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page567.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/previous_motif.gif"></A> <A NAME="tex2html8936" HREF="page9.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page9.html"><IMG WIDTH=65 HEIGHT=24 ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/contents_motif.gif"></A> <A NAME="tex2html8937" HREF="page620.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page620.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="index_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/index_motif.gif"></A> <BR><HR>
<H3><A NAME="SECTION0017413000000000000000">Implementation</A></H3>
<P>
A version of Dijkstra's algorithm is shown in Program&nbsp;<A HREF="page568.html#proggraphalgs2c" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page568.html#proggraphalgs2c"><IMG  ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A>.
The <tt>DijkstrasAlgorithm</tt> function takes two arguments.
The first is a <tt>const</tt> reference to a directed graph instance.
It is assumed that the directed graph is an edge-weighted graph
in which the weights are instances of the
<tt>Int</tt> class defined in Program&nbsp;<A HREF="page116.html#progwrapper2h" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page116.html#progwrapper2h"><IMG  ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A>.
The second argument is a <tt>const</tt> reference to the start node.
<P>
<P><A NAME="52081">&#160;</A><A NAME="proggraphalgs2c">&#160;</A> <IMG WIDTH=575 HEIGHT=907 ALIGN=BOTTOM ALT="program52078" SRC="img2479.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img2479.gif"  ><BR>
<STRONG>Program:</STRONG> Dijkstra's Algorithm<BR>
<P>
<P>
The <tt>DijkstrasAlgorithm</tt> routine returns its result
in the form of a shortest-path graph.
Therefore, the return value is a reference to a <tt>Digraph</tt> instance.
The function allocates the storage, constructs the shortest-path graph
and returns a reference to that graph.
<P>
The main data structures used are called
<tt>table</tt> and <tt>queue</tt> (lines&nbsp;4-5).
The former is an array of  <IMG WIDTH=48 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline72635" SRC="img2480.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img2480.gif"  > <tt>TableEntry</tt> elements.
The latter is a reference to a priority queue.
In this case,
a <tt>BinaryHeap</tt> of length  <IMG WIDTH=16 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline71793" SRC="img2367.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img2367.gif"  > is used.
(See Section&nbsp;<A HREF="page354.html#secpqueuesbinheaps" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page354.html#secpqueuesbinheaps"><IMG  ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A>).
<P>
The algorithm begins by setting the tentative distance for the start
vertex to zero and inserting the start vertex into the priority
queue with priority zero (lines&nbsp;7-8).
<P>
The main loop of the routine comprises lines&nbsp;8-35.
In each iteration of this loop the vertex with the smallest distance
is dequeued (lines&nbsp;10-12).
The vertex is processed only if its table entry
indicates that the shortest path is not already known (line&nbsp;13).
<P>
When a vertex <tt>v0</tt> is processed,
its shortest path is deemed to be <em>known</em> (line&nbsp;15).
Then each vertex <tt>v1</tt> adjacent to vertex is considered (lines&nbsp;16-19).
The distance to <tt>v1</tt> along the path that passes through <tt>v0</tt>
is computed (lines&nbsp;20-23).
If this distance is less than the tentative distance
associated with <tt>v1</tt>,
entries in the table for <tt>v1</tt> are updated,
and the <tt>v1</tt> is given a new priority
and inserted into the priority queue (lines&nbsp;24-29).
<P>
The main loop terminates when all the shortest paths have been found.
The shortest-path graph is then
constructed using the information in the table (lines&nbsp;38-45).
<P>
<HR><A NAME="tex2html8934" HREF="page569.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page569.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="next_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/next_motif.gif"></A> <A NAME="tex2html8932" HREF="page565.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page565.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="up_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/up_motif.gif"></A> <A NAME="tex2html8926" HREF="page567.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page567.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/previous_motif.gif"></A> <A NAME="tex2html8936" HREF="page9.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page9.html"><IMG WIDTH=65 HEIGHT=24 ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/contents_motif.gif"></A> <A NAME="tex2html8937" HREF="page620.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page620.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="index_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/index_motif.gif"></A> <P><ADDRESS>
<img src="bruno.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/bruno.gif" alt="Bruno" align=right>
<a href="javascript:if(confirm('http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html'" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html">Copyright &#169; 1997</a> by <a href="javascript:if(confirm('http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html'" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html">Bruno R. Preiss, P.Eng.</a>  All rights reserved.

</ADDRESS>
</BODY>
</HTML>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?