page389.html

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

HTML
71
字号
<HTML><HEAD><TITLE>Array and Bit-Vector Sets</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="tex2html5667" HREF="page390.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html5665" HREF="page386.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html5659" HREF="page388.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html5669" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H1><A NAME="SECTION0012200000000000000000">Array and Bit-Vector Sets</A></H1><P>In this section we consider finite sets over a finite universe.Specifically, the universe we consider is  <IMG WIDTH=115 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline66639" SRC="img1549.gif"  >,the set of integers in the range from zero to <I>N</I>-1,for some fixed and relatively small value of <I>N</I>.<P>Let  <IMG WIDTH=149 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline66651" SRC="img1553.gif"  > be the universe.Every set which we wish to represent is a subset of <I>U</I>.The set of all subsets of <I>U</I> is called the <em>power set</em><A NAME=27679>&#160;</A>of <I>U</I> and is written  <IMG WIDTH=17 HEIGHT=14 ALIGN=BOTTOM ALT="tex2html_wrap_inline66659" SRC="img1554.gif"  >.Thus, the sets which we wish to represent are the <em>elements</em> of  <IMG WIDTH=17 HEIGHT=14 ALIGN=BOTTOM ALT="tex2html_wrap_inline66659" SRC="img1554.gif"  >.The number of elements in the set <I>U</I>, written |<I>U</I>|, is <I>N</I>.Similarly,  <IMG WIDTH=115 HEIGHT=30 ALIGN=MIDDLE ALT="tex2html_wrap_inline66669" SRC="img1555.gif"  >.This observation should be obvious:For each element of the universal set <I>U</I> there are only two possibilities:Either it is, or it is not,a member of the given set.<P>This suggests a relatively straightforwardrepresentation of the elements of  <IMG WIDTH=17 HEIGHT=14 ALIGN=BOTTOM ALT="tex2html_wrap_inline66659" SRC="img1554.gif"  >--an array of <tt>bool</tt> values, one for each element of the universal set.By using array subscripts in <I>U</I>,we can represent the set implicitly.That is, <I>i</I> is a member of the set if the  <IMG WIDTH=17 HEIGHT=14 ALIGN=BOTTOM ALT="tex2html_wrap_inline57847" SRC="img77.gif"  > array elementis true.<P>Program&nbsp;<A HREF="page389.html#progsetAsArraya"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> introduces the class <tt>SetAsArray</tt>.The <tt>SetAsArray</tt> class extends the abstract <tt>Set</tt> classdefined in Program&nbsp;<A HREF="page388.html#progseta"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.This class uses an array of length  <IMG WIDTH=145 HEIGHT=11 ALIGN=BOTTOM ALT="tex2html_wrap_inline66681" SRC="img1556.gif"  >to represent the elements of  <IMG WIDTH=17 HEIGHT=14 ALIGN=BOTTOM ALT="tex2html_wrap_inline66659" SRC="img1554.gif"  > where  <IMG WIDTH=149 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline66651" SRC="img1553.gif"  >.<P><P><A NAME="28205">&#160;</A><A NAME="progsetAsArraya">&#160;</A> <IMG WIDTH=575 HEIGHT=180 ALIGN=BOTTOM ALT="program27690" SRC="img1557.gif"  ><BR><STRONG>Program:</STRONG> <tt>SetAsArray</tt> class <tt>__init__</tt> method.<BR><P><P>Program&nbsp;<A HREF="page389.html#progsetAsArraya"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the <tt>__init__</tt> methodfor the <tt>SetAsArray</tt> class.In addition to <tt>self</tt>,the <tt>__init__</tt> method takes a single argument  <IMG WIDTH=8 HEIGHT=7 ALIGN=BOTTOM ALT="tex2html_wrap_inline66687" SRC="img1558.gif"  >,which defines the universe and, consequently,the size of the array of <tt>bool</tt> values.The <tt>__init__</tt> method creates the empty set by initializingall the elements of the <tt>bool</tt> array to <tt>False</tt>.Clearly, the running time of the <tt>__init__</tt> method is <I>O</I>(<I>N</I>),where  <IMG WIDTH=44 HEIGHT=11 ALIGN=BOTTOM ALT="tex2html_wrap_inline66691" SRC="img1559.gif"  >.<P><BR> <HR><UL> <LI> <A NAME="tex2html5670" HREF="page390.html#SECTION0012201000000000000000">Basic Operations</A><LI> <A NAME="tex2html5671" HREF="page391.html#SECTION0012202000000000000000">Union, Intersection, and Difference</A><LI> <A NAME="tex2html5672" HREF="page392.html#SECTION0012203000000000000000">Comparing Sets</A><LI> <A NAME="tex2html5673" HREF="page393.html#SECTION0012210000000000000000">Bit-Vector Sets</A></UL><HR><A NAME="tex2html5667" HREF="page390.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html5665" HREF="page386.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html5659" HREF="page388.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html5669" 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 + -
显示快捷键?