⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 page563.html

📁 wqeqwvrw rkjqhwrjwq jkhrjqwhrwq jkhrwq
💻 HTML
字号:
<HTML>
<HEAD>
<TITLE>Testing for Cycles in a Directed Graph</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="tex2html8868" HREF="page564.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page564.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="tex2html8866" HREF="page560.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page560.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="tex2html8862" HREF="page562.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page562.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="tex2html8870" 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="tex2html8871" 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="SECTION0017343000000000000000">Testing for Cycles in a Directed Graph</A></H3>
<P>
The final application of graph traversal
that we consider in this section
is to test a directed graph for cycles.
An easy way to do this is to attempt a topological-order traversal
using the algorithm given in Section&nbsp;<A HREF="page557.html#secgraphstoposort" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page557.html#secgraphstoposort"><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>.
This algorithm only visits all the vertices of a directed graph
if that graph contains no cycles.
<P>
To see why this is so,
consider the directed cyclic graph  <IMG WIDTH=24 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline72171" SRC="img2436.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img2436.gif"  > shown in Figure&nbsp;<A HREF="page563.html#figgraph11" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page563.html#figgraph11"><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 topological traversal algorithm begins
by computing the <em>in-degrees</em> of the vertices.
(The number shown below each vertex in Figure&nbsp;<A HREF="page563.html#figgraph11" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page563.html#figgraph11"><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>
is the in-degree of that vertex).
<P>
<P><A NAME="51406">&#160;</A><A NAME="figgraph11">&#160;</A> <IMG WIDTH=575 HEIGHT=95 ALIGN=BOTTOM ALT="figure51247" SRC="img2437.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img2437.gif"  ><BR>
<STRONG>Figure:</STRONG> A Directed Cyclic Graph<BR>
<P>
<P>
At each step of the traversal,
a vertex with in-degree of zero is visited.
After a vertex is visited,
the vertex and all the edges emanating from that vertex
are removed from the graph.
Notice that if we remove vertex <I>a</I> and edge (<I>a</I>,<I>b</I>) from  <IMG WIDTH=24 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline72171" SRC="img2436.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img2436.gif"  >,
all the remaining vertices have in-degrees of one.
The presence of the cycle prevents
the topological-order traversal from completing.
<P>
Therefore, the a simple way to test whether a directed graph is cyclic
is to attempt a topological traversal of its vertices.
If all the vertices are not visited,
the graph must be cyclic.
<P>
Program&nbsp;<A HREF="page563.html#proggraph6c" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page563.html#proggraph6c"><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> gives the implementation of the <tt>IsCyclic</tt>
member function of the <tt>Digraph</tt> class.
This Boolean-valued accessor returns <tt>true</tt> if the graph is cyclic.
The implementation simply makes use of a <tt>CountingVisitor</tt>
to count the number of vertices visited during a
<tt>TopologicalOrderTraversal</tt> of the graph.
<P>
<P><A NAME="51466">&#160;</A><A NAME="proggraph6c">&#160;</A> <IMG WIDTH=575 HEIGHT=124 ALIGN=BOTTOM ALT="program51416" SRC="img2438.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img2438.gif"  ><BR>
<STRONG>Program:</STRONG> <tt>Digraph</tt> Class <tt>IsCyclic</tt> Member Function Definition<BR>
<P>
<P>
The worst-case running time of the <tt>IsCyclic</tt> routine
is determined by the time taken by the <tt>TopologicalOrderTraversal</tt>.
Since  <IMG WIDTH=120 HEIGHT=26 ALIGN=MIDDLE ALT="tex2html_wrap_inline61332" SRC="img715.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img715.gif"  >,
the running time of <tt>IsCyclic</tt> is
 <IMG WIDTH=50 HEIGHT=25 ALIGN=MIDDLE ALT="tex2html_wrap_inline71729" SRC="img2358.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img2358.gif"  > when adjacency matrices are used to represent the graph
and  <IMG WIDTH=82 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline71915" SRC="img2391.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img2391.gif"  > when adjacency lists are used.
<P>
<HR><A NAME="tex2html8868" HREF="page564.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page564.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="tex2html8866" HREF="page560.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page560.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="tex2html8862" HREF="page562.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page562.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="tex2html8870" 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="tex2html8871" 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -