page546.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 56 行
HTML
56 行
<HTML><HEAD><TITLE>Space Comparison</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="tex2html7439" HREF="page547.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html7437" HREF="page545.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html7431" HREF="page545.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html7441" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION0016281000000000000000">Space Comparison</A></H3><P>Consider the representation of a directed graph <IMG WIDTH=73 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline70549" SRC="img2166.gif" >.In addition to the <IMG WIDTH=18 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline70975" SRC="img2249.gif" > <tt>GraphVertex</tt> class instancesand the <IMG WIDTH=17 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline70987" SRC="img2251.gif" > <tt>GraphEdge</tt> class instances contained by the graph,there is the storage required by the adjacency matrix.In this case, the matrix is a <IMG WIDTH=57 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline71089" SRC="img2274.gif" > matrix of <tt>Edge</tt>s.Therefore, the amount of storage required by an adjacency matriximplementation is<P><A NAME="eqnspaceam"> </A> <IMG WIDTH=530 HEIGHT=42 ALIGN=BOTTOM ALT="equation49840" SRC="img2275.gif" ><P><P>On the other hand,consider the amount of storage requiredwhen we represent the same graph using adjacency lists.In addition to the vertices and the edges themselves,there are <IMG WIDTH=18 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline70975" SRC="img2249.gif" > linked lists.If we use the <tt>LinkedList</tt> class defined in Section <A HREF="page97.html#secfdslinklist"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>,each such list has a <tt>head</tt> and <tt>tail</tt> instance attribute.Altogether there are <IMG WIDTH=17 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline70987" SRC="img2251.gif" > linked lists elementseach of which refers to the linked list itself,to the next element of the listand contains an <tt>Edge</tt>.Therefore, the total space required is<P><A NAME="eqnspaceal"> </A> <IMG WIDTH=543 HEIGHT=135 ALIGN=BOTTOM ALT="equation49854" SRC="img2276.gif" ><P><P>Notice that the space for the vertices and edges themselvescancels out when we compare Equation <A HREF="page546.html#eqnspaceam"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> with Equation <A HREF="page546.html#eqnspaceal"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.If we assume that all object ids require the same amount of space,we can conclude that adjacency lists use less space thanadjacency matrices when<P> <IMG WIDTH=306 HEIGHT=37 ALIGN=BOTTOM ALT="displaymath71081" SRC="img2277.gif" ><P>For example, given a 10 node graph,the adjacency lists version uses less spacewhen there are fewer than 30 edges.As a rough rule of thumb,we can say that adjacency lists use less spacewhen the average degree of a node, <IMG WIDTH=75 HEIGHT=30 ALIGN=MIDDLE ALT="tex2html_wrap_inline71095" SRC="img2278.gif" >,satisfies <IMG WIDTH=66 HEIGHT=33 ALIGN=MIDDLE ALT="tex2html_wrap_inline71097" SRC="img2279.gif" >.<P><HR><A NAME="tex2html7439" HREF="page547.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html7437" HREF="page545.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html7431" HREF="page545.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html7441" 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 + =
减小字号Ctrl + -
显示快捷键?