page559.html

来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 70 行

HTML
70
字号
<HTML><HEAD><TITLE>Connectedness of an Undirected Graph</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="tex2html7586" HREF="page560.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html7584" HREF="page558.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html7578" HREF="page558.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html7588" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION0016341000000000000000">Connectedness of an Undirected Graph</A></H3><P><BLOCKQUOTE> <b>Definition (Connectedness of an Undirected Graph)</b><A NAME="defngraphsconnected">&#160;</A>An undirected graph  <IMG WIDTH=73 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline70549" SRC="img2166.gif"  > is <em>connected</em><A NAME=50716>&#160;</A><A NAME=50717>&#160;</A>if there is a path in <I>G</I> between every pair of vertices in  <IMG WIDTH=11 HEIGHT=12 ALIGN=BOTTOM ALT="tex2html_wrap_inline70551" SRC="img2167.gif"  >.</BLOCKQUOTE><P>Consider the undirected graph shown in Figure&nbsp;<A HREF="page559.html#figgraph9"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.It is tempting to interpret this figure as a picture of two graphs.However, the figure actually representsthe undirected graph  <IMG WIDTH=80 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline71299" SRC="img2309.gif"  >, given by<P> <IMG WIDTH=500 HEIGHT=42 ALIGN=BOTTOM ALT="eqnarray50720" SRC="img2310.gif"  ><P>Clearly, the graph  <IMG WIDTH=19 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline71301" SRC="img2311.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=19 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline71301" SRC="img2311.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=50723>&#160;</A><A NAME=50724>&#160;</A>.<P><P><A NAME="50894">&#160;</A><A NAME="figgraph9">&#160;</A> <IMG WIDTH=575 HEIGHT=137 ALIGN=BOTTOM ALT="figure50725" SRC="img2312.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 vertexwill only visit all the other vertices of the graphif 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&nbsp;<A HREF="page559.html#proggraphf"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> shows how this can be implemented.The <tt>getIsConnected</tt> method of the abstract <tt>Graph</tt> classreturns <tt>True</tt> if the graph is connected.<P><P><A NAME="50966">&#160;</A><A NAME="proggraphf">&#160;</A> <IMG WIDTH=575 HEIGHT=447 ALIGN=BOTTOM ALT="program50901" SRC="img2313.gif"  ><BR><STRONG>Program:</STRONG> Abstract <tt>Graph</tt> class <tt>getIsConnected</tt> method.<BR><P><P>The property is implemented using athe <tt>depthFirstTraversal</tt> method and a visitorthat simply counts the number of vertices it visits.The <tt>visit</tt> method adds one the <tt>_count</tt> instance attributeof the counter each time it is called.<P>The worst-case running time of the <tt>getIsConnected</tt> methodis determined by the time taken by the <tt>depthFirstTraversal</tt>.Clearly in this case  <IMG WIDTH=108 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline60707" SRC="img668.gif"  >.Therefore, the running time of <tt>getIsConnected</tt> is <IMG WIDTH=50 HEIGHT=28 ALIGN=MIDDLE ALT="tex2html_wrap_inline70923" SRC="img2242.gif"  > when adjacency matrices are used to represent the graphand  <IMG WIDTH=81 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline71119" SRC="img2282.gif"  > when adjacency lists are used.<P><HR><A NAME="tex2html7586" HREF="page560.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html7584" HREF="page558.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html7578" HREF="page558.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html7588" 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 &#169; 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 + -
显示快捷键?