📄 page561.html
字号:
<HTML>
<HEAD>
<TITLE>Connectedness of an Undirected 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="tex2html8846" HREF="page562.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page562.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="tex2html8844" 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="tex2html8838" HREF="page560.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page560.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="tex2html8848" 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="tex2html8849" 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="SECTION0017341000000000000000">Connectedness of an Undirected Graph</A></H3>
<P>
<BLOCKQUOTE> <b>Definition (Connectedness of an Undirected Graph)</b>
<A NAME="defngraphsconnected"> </A>
An undirected graph <IMG WIDTH=72 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline71355" SRC="img2282.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img2282.gif" > is <em>connected</em><A NAME=50829> </A><A NAME=50830> </A>
if there is a path in <I>G</I> between every pair of vertices in <IMG WIDTH=10 HEIGHT=12 ALIGN=BOTTOM ALT="tex2html_wrap_inline71357" SRC="img2283.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img2283.gif" >.
</BLOCKQUOTE>
<P>
Consider the undirected graph shown in Figure <A HREF="page561.html#figgraph9" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page561.html#figgraph9"><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>.
It is tempting to interpret this figure as a picture of two graphs.
However, the figure actually represents
the undirected graph <IMG WIDTH=79 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline72095" SRC="img2418.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img2418.gif" >, given by
<P> <IMG WIDTH=500 HEIGHT=40 ALIGN=BOTTOM ALT="eqnarray50833" SRC="img2419.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img2419.gif" ><P>
Clearly, the graph <IMG WIDTH=18 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline72097" SRC="img2420.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img2420.gif" > is not connected.
For example, there is no path between vertices <I>a</I> and <I>d</I>.
In fact, the graph <IMG WIDTH=18 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline72097" SRC="img2420.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img2420.gif" > consists of two, unconnected parts,
each of which is a connected sub-graph.
The connected sub-graphs of a graph are called <em>connected components</em><A NAME=50836> </A><A NAME=50837> </A>.
<P>
<P><A NAME="51007"> </A><A NAME="figgraph9"> </A> <IMG WIDTH=575 HEIGHT=134 ALIGN=BOTTOM ALT="figure50838" SRC="img2421.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img2421.gif" ><BR>
<STRONG>Figure:</STRONG> An Unconnected, Undirected Graph with Two (Connected) Components<BR>
<P>
<P>
A traversal of an undirected graph (either depth-first or breadth-first)
starting from any vertex
will only visit all the other vertices of the graph
if that graph is connected.
Therefore,
there is a very simply way to test whether an undirected graph is connected:
Count the number of vertices visited during a traversal of the graph.
Only if all the vertices are visited is the graph connected.
<P>
Program <A HREF="page561.html#proggraph4c" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page561.html#proggraph4c"><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> shows how this can be implemented.
The <tt>IsConnected</tt> member function of the <tt>Graph</tt> class
is a Boolean-valued accessor that returns <tt>true</tt> if the graph is connected.
The routine is implemented using a <tt>CountingVisitor</tt>
and the <tt>DepthFirstTraversal</tt> routine.
<P>
<P><A NAME="51070"> </A><A NAME="proggraph4c"> </A> <IMG WIDTH=575 HEIGHT=351 ALIGN=BOTTOM ALT="program51016" SRC="img2422.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img2422.gif" ><BR>
<STRONG>Program:</STRONG> <tt>Graph</tt> Class <tt>IsConnected</tt> Member Function Definition<BR>
<P>
<P>
A <tt>CountingVisitor</tt> is a visitor
that simply counts the number of vertices it visits.
It has a single member variable, <tt>count</tt>,
which is initialized to zero in the constructor.
The <tt>Visit</tt> routine adds one the <tt>count</tt> each time it is called
and the <tt>Count</tt> accessor returns the value of the <tt>count</tt>.
<P>
The worst-case running time of the <tt>IsConnected</tt> routine
is determined by the time taken by the <tt>DepthFirstTraversal</tt>.
Clearly in this case <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" >.
Therefore, the running time of <tt>IsConnected</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="tex2html8846" HREF="page562.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page562.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="tex2html8844" 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="tex2html8838" HREF="page560.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page560.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="tex2html8848" 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="tex2html8849" 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 © 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 + -