📄 page552.html
字号:
<HTML><HEAD><TITLE>Breadth-First Traversal</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="tex2html7507" HREF="page553.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html7505" HREF="page548.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html7499" HREF="page551.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html7509" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION0016320000000000000000">Breadth-First Traversal</A></H2><P>The <em>breadth-first traversal</em><A NAME=50229> </A><A NAME=50230> </A>of a graph is likethe breadth-first traversal of a tree discussed in Section <A HREF="page258.html#sectreestraversals"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.The breadth-first traversal of a tree visits the nodesin the order of their depth in the tree.Breadth-first tree traversal first visits all the nodes at depth zero(i.e., the root),then all the nodes at depth one, and so on.<P>Since a graph has no root,when we do a breadth-first traversal,we must specify the vertex at which to start the traversal.Furthermore, we can define the depth of a given vertex to be the lengthof the shortest path from the starting vertex to the given vertex.Thus, breadth-first traversal first visits the starting vertex,then all the vertices adjacent to the starting vertex,and then all the vertices adjacent to those, and so on.<P>Section <A HREF="page156.html#secqueuesapps"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> presents a non-recursivebreadth-first traversal algorithm for <I>N</I>-ary treesthat uses a queue to keep track vertices that need to be visited.The breadth-first graph traversal algorithm is very similar.<P>First, the starting vertex is enqueued.Then, the following steps are repeated until the queue is empty:<OL><LI> Remove the vertex at the head of the queue and call it <tt>v</tt>.<LI> Visit <tt>v</tt>.<LI> Follow each edge emanating from <tt>v</tt> to find the adjacent vertex and call it <tt>to</tt>. If <tt>to</tt> has not already been put into the queue, enqueue it.</OL>Notice that a vertex can be put into the queue at most once.Therefore, the algorithm must somehow keep track of the verticesthat have been enqueued.<P><P><A NAME="50415"> </A><A NAME="figgraph7"> </A> <IMG WIDTH=575 HEIGHT=272 ALIGN=BOTTOM ALT="figure50240" SRC="img2293.gif" ><BR><STRONG>Figure:</STRONG> Breadth-first traversal.<BR><P><P>Figure <A HREF="page552.html#figgraph7"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> illustrates the breadth-first traversalof the directed graph <IMG WIDTH=17 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline70643" SRC="img2182.gif" > starting from vertex <I>a</I>.The algorithm begins by inserting the starting vertex, <I>a</I>,into the empty queue.Next, the head of the queue (vertex <I>a</I>) is dequeued and visited,and the vertices adjacent to it (vertices <I>b</I> and <I>c</I>) are enqueued.When, <I>b</I> is dequeued and visitedwe find that there is only adjacent vertex, <I>c</I>,and that vertex is already in the queue.Next vertex <I>c</I> is dequeued and visited.Vertex <I>c</I> is adjacent to <I>a</I> and <I>d</I>.Since <I>a</I> has already been enqueued (and subsequently dequeued)only vertex <I>d</I> is put into the queue.Finally, vertex <I>d</I> is dequeued an visited.Therefore, the breadth-first traversal of <IMG WIDTH=17 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline70643" SRC="img2182.gif" > starting from <I>a</I>visits the vertices in the sequence<P> <IMG WIDTH=277 HEIGHT=14 ALIGN=BOTTOM ALT="displaymath71195" SRC="img2294.gif" ><P><BR> <HR><UL> <LI> <A NAME="tex2html7510" HREF="page553.html#SECTION0016321000000000000000">Implementation</A><LI> <A NAME="tex2html7511" HREF="page554.html#SECTION0016322000000000000000">Running Time Analysis</A></UL><HR><A NAME="tex2html7507" HREF="page553.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html7505" HREF="page548.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html7499" HREF="page551.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html7509" 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -