home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2000 May
/
Chip_2000-05_cd1.bin
/
zkuste
/
Perl
/
ActivePerl-5.6.0.613.msi
/
䆊䌷䈹䈙䏵-䞅䞆䞀㡆䞃䄦䠥
/
_ef5badafe9192e74daf62dd8bc3927e1
< prev
next >
Wrap
Text File
|
2000-03-23
|
10KB
|
239 lines
<HTML>
<HEAD>
<TITLE>Algorithm - the theory behind it</TITLE>
<LINK REL="stylesheet" HREF="../../../Active.css" TYPE="text/css">
<LINK REV="made" HREF="mailto:">
</HEAD>
<BODY>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
<TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
<STRONG><P CLASS=block> Algorithm - the theory behind it</P></STRONG>
</TD></TR>
</TABLE>
<A NAME="__index__"></A>
<!-- INDEX BEGIN -->
<UL>
<LI><A HREF="#name">NAME</A></LI><LI><A HREF="#supportedplatforms">SUPPORTED PLATFORMS</A></LI>
<LI><A HREF="#description">DESCRIPTION</A></LI>
<UL>
<LI><A HREF="#semirings"><STRONG>Semi-Rings</STRONG></A></LI>
<LI><A HREF="#operator '*'"><STRONG>Operator '*'</STRONG></A></LI>
<LI><A HREF="#kleene's algorithm"><STRONG>Kleene's Algorithm</STRONG></A></LI>
<LI><A HREF="#kleene's algorithm and semirings"><STRONG>Kleene's Algorithm and Semi-Rings</STRONG></A></LI>
</UL>
<LI><A HREF="#see also">SEE ALSO</A></LI>
<LI><A HREF="#author">AUTHOR</A></LI>
<LI><A HREF="#copyright">COPYRIGHT</A></LI>
</UL>
<!-- INDEX END -->
<HR>
<P>
<H1><A NAME="name">NAME</A></H1>
<P>Kleene's Algorithm - the theory behind it</P>
<P>brief introduction</P>
<P>
<HR>
<H1><A NAME="supportedplatforms">SUPPORTED PLATFORMS</A></H1>
<UL>
<LI>Linux</LI>
<LI>Solaris</LI>
<LI>Windows</LI>
</UL>
<HR>
<H1><A NAME="description">DESCRIPTION</A></H1>
<P>
<H2><A NAME="semirings"><STRONG>Semi-Rings</STRONG></A></H2>
<P>A Semi-Ring (S, +, ., 0, 1) is characterized by the following properties:</P>
<OL>
<LI><STRONG><A NAME="item_%29">)</A></STRONG><BR>
a) <CODE>(S, +, 0) is a Semi-Group with neutral element 0</CODE>
<P>b) <CODE>(S, ., 1) is a Semi-Group with neutral element 1</CODE></P>
<P>c) <CODE>0 . a = a . 0 = 0 for all a in S</CODE></P>
<P></P>
<LI><STRONG>)</STRONG><BR>
<CODE>"+"</CODE> is commutative and <STRONG>idempotent</STRONG>, i.e., <CODE>a + a = a</CODE>
<P></P>
<LI><STRONG>)</STRONG><BR>
Distributivity holds, i.e.,
<P>a) <CODE>a . ( b + c ) = a . b + a . c for all a,b,c in S</CODE></P>
<P>b) <CODE>( a + b ) . c = a . c + b . c for all a,b,c in S</CODE></P>
<P></P>
<LI><STRONG>)</STRONG><BR>
<CODE>SUM_{i=0}^{+infinity} ( a[i] )</CODE>
<P>exists, is well-defined and unique</P>
<P><CODE>for all a[i] in S</CODE></P>
<P>and associativity, commutativity and idempotency hold</P>
<P></P>
<LI><STRONG>)</STRONG><BR>
Distributivity for infinite series also holds, i.e.,
<PRE>
( SUM_{i=0}^{+infty} a[i] ) . ( SUM_{j=0}^{+infty} b[j] )
= SUM_{i=0}^{+infty} ( SUM_{j=0}^{+infty} ( a[i] . b[j] ) )</PRE>
<P></P></OL>
<P>EXAMPLES:</P>
<UL>
<LI>
<CODE>S1 = ({0,1}, |, &, 0, 1)</CODE>
<P>Boolean Algebra</P>
<P>See also <A HREF="../../../Math/MatrixBool(3).html">the Math::MatrixBool(3) manpage</A></P>
<P></P>
<LI>
<CODE>S2 = (pos. reals with 0 and +infty, min, +, +infty, 0)</CODE>
<P>Positive real numbers including zero and plus infinity</P>
<P>See also <A HREF="../../../Math/MatrixReal(3).html">the Math::MatrixReal(3) manpage</A></P>
<P></P>
<LI>
<CODE>S3 = (Pot(Sigma*), union, concat, {}, {''})</CODE>
<P>Formal languages over Sigma (= alphabet)</P>
<P>See also <A HREF="../../../DFA/Kleene(3).html">the DFA::Kleene(3) manpage</A></P>
<P></P></UL>
<P>
<H2><A NAME="operator '*'"><STRONG>Operator '*'</STRONG></A></H2>
<P>(reflexive and transitive closure)</P>
<P>Define an operator called ``*'' as follows:</P>
<PRE>
a in S ==> a* := SUM_{i=0}^{+infty} a^i</PRE>
<P>where</P>
<PRE>
a^0 = 1, a^(i+1) = a . a^i</PRE>
<P>Then, also</P>
<PRE>
a* = 1 + a . a*, 0* = 1* = 1</PRE>
<P>hold.</P>
<P>
<H2><A NAME="kleene's algorithm"><STRONG>Kleene's Algorithm</STRONG></A></H2>
<P>In its general form, Kleene's algorithm goes as follows:</P>
<PRE>
for i := 1 to n do
for j := 1 to n do
begin
C^0[i,j] := m(v[i],v[j]);
if (i = j) then C^0[i,j] := C^0[i,j] + 1
end
for k := 1 to n do
for i := 1 to n do
for j := 1 to n do
C^k[i,j] := C^k-1[i,j] +
C^k-1[i,k] . ( C^k-1[k,k] )* . C^k-1[k,j]
for i := 1 to n do
for j := 1 to n do
c(v[i],v[j]) := C^n[i,j]</PRE>
<P>
<H2><A NAME="kleene's algorithm and semirings"><STRONG>Kleene's Algorithm and Semi-Rings</STRONG></A></H2>
<P>Kleene's algorithm can be applied to any Semi-Ring having the properties
listed previously (above). (!)</P>
<P>EXAMPLES:</P>
<UL>
<LI>
<CODE>S1 = ({0,1}, |, &, 0, 1)</CODE>
<P><CODE>G(V,E)</CODE> be a graph with set of vortices V and set of edges E:</P>
<P><A HREF="../../../lib/Pod/perlfunc.html#item_m"><CODE>m(v[i],v[j]) := ( (v[i],v[j]) in E ) ? 1 : 0</CODE></A></P>
<P>Kleene's algorithm then calculates</P>
<P><CODE>c^{n}_{i,j} = ( path from v[i] to v[j] exists ) ? 1 : 0</CODE></P>
<P>using</P>
<P><CODE>C^k[i,j] = C^k-1[i,j] | C^k-1[i,k] & C^k-1[k,j]</CODE></P>
<P>(remember <CODE> 0* = 1* = 1 </CODE>)</P>
<P></P>
<LI>
<CODE>S2 = (pos. reals with 0 and +infty, min, +, +infty, 0)</CODE>
<P><CODE>G(V,E)</CODE> be a graph with set of vortices V and set of edges E, with
costs <A HREF="../../../lib/Pod/perlfunc.html#item_m"><CODE>m(v[i],v[j])</CODE></A> associated with each edge <CODE>(v[i],v[j])</CODE> in E:</P>
<P><A HREF="../../../lib/Pod/perlfunc.html#item_m"><CODE>m(v[i],v[j]) := costs of (v[i],v[j])</CODE></A></P>
<P><CODE>for all (v[i],v[j]) in E</CODE></P>
<P>Set <A HREF="../../../lib/Pod/perlfunc.html#item_m"><CODE>m(v[i],v[j]) := +infinity</CODE></A> if an edge (v[i],v[j]) is not in E.</P>
<P><CODE> ==> a* = 0 for all a in S2</CODE></P>
<P><CODE> ==> C^k[i,j] = min( C^k-1[i,j] ,</CODE></P>
<P><CODE> C^k-1[i,k] + C^k-1[k,j] )</CODE></P>
<P>Kleene's algorithm then calculates the costs of the ``shortest'' path
from any <CODE>v[i]</CODE> to any other <CODE>v[j]</CODE>:</P>
<P><CODE>C^n[i,j] = costs of "shortest" path from v[i] to v[j]</CODE></P>
<P></P>
<LI>
<CODE>S3 = (Pot(Sigma*), union, concat, {}, {''})</CODE>
<P><CODE>M in DFA(Sigma)</CODE> be a Deterministic Finite Automaton with a set of
states <CODE>Q</CODE>, a subset <CODE>F</CODE> of <CODE>Q</CODE> of accepting states and a transition
function <CODE>delta : Q x Sigma --> Q</CODE>.</P>
<P>Define</P>
<P><A HREF="../../../lib/Pod/perlfunc.html#item_m"><CODE>m(v[i],v[j]) :=</CODE></A></P>
<P><CODE> { a in Sigma | delta( q[i] , a ) = q[j] }</CODE></P>
<P>and</P>
<P><A HREF="../../../lib/Pod/perlfunc.html#item_m"><CODE>C^0[i,j] := m(v[i],v[j]);</CODE></A></P>
<P><CODE>if (i = j) then C^0[i,j] := C^0[i,j] union {''}</CODE></P>
<P>(<CODE>{''}</CODE> is the set containing the empty string, whereas <CODE>{}</CODE> is the
empty set!)</P>
<P>Then Kleene's algorithm calculates the language accepted by Deterministic
Finite Automaton M using</P>
<P><CODE>C^k[i,j] = C^k-1[i,j] union</CODE></P>
<P><CODE> C^k-1[i,k] concat ( C^k-1[k,k] )* concat C^k-1[k,j]</CODE></P>
<P>and</P>
<P><CODE>L(M) = UNION_{ q[j] in F } C^n[1,j]</CODE></P>
<P>(state <CODE>q[1]</CODE> is assumed to be the ``start'' state)</P>
<P>finally being the language recognized by Deterministic Finite Automaton M.</P>
<P></P></UL>
<P>Note that instead of using Kleene's algorithm, you can also use the ``*''
operator on the associated matrix:</P>
<P>Define <A HREF="../../../lib/Pod/perlfunc.html#item_m"><CODE>A[i,j] := m(v[i],v[j])</CODE></A></P>
<P><CODE> ==> A*[i,j] = c(v[i],v[j])</CODE></P>
<P>Proof:</P>
<P><CODE>A* = SUM_{i=0}^{+infty} A^i</CODE></P>
<P>where <CODE>A^0 = E_{n}</CODE></P>
<P>(matrix with one's in its main diagonal and zero's elsewhere)</P>
<P>and <CODE>A^(i+1) = A . A^i</CODE></P>
<P>Induction over k yields:</P>
<P><CODE>A^k[i,j] = c_{k}(v[i],v[j])</CODE></P>
<DL>
<DT><STRONG><A NAME="item_k_%3D_0%3A"><CODE>k = 0:</CODE></A></STRONG><BR>
<DD>
<CODE>c_{0}(v[i],v[j]) = d_{i,j}</CODE>
<P>with <CODE>d_{i,j} := (i = j) ? 1 : 0</CODE></P>
<P>and <CODE>A^0 = E_{n} = [d_{i,j}]</CODE></P>
<P></P>
<DT><STRONG><A NAME="item_k"><CODE>k-1 -> k:</CODE></A></STRONG><BR>
<DD>
<CODE>c_{k}(v[i],v[j])</CODE>
<P><A HREF="../../../lib/Pod/perlfunc.html#item_m"><CODE>= SUM_{l=1}^{n} m(v[i],v[l]) . c_{k-1}(v[l],v[j])</CODE></A></P>
<P><CODE>= SUM_{l=1}^{n} ( a[i,l] . a[l,j] )</CODE></P>
<P><CODE>= [a^{k}_{i,j}] = A^1 . A^(k-1) = A^k</CODE></P>
<P></P></DL>
<P>qed</P>
<P>In other words, the complexity of calculating the closure and doing
matrix multiplications is of the same order <CODE>O( n^3 )</CODE> in Semi-Rings!</P>
<P>
<HR>
<H1><A NAME="see also">SEE ALSO</A></H1>
<P>Math::MatrixBool(3), Math::MatrixReal(3), DFA::Kleene(3).</P>
<P>(All contained in the distribution of the ``Set::IntegerFast'' module)</P>
<P>Dijkstra's algorithm for shortest paths.</P>
<P>
<HR>
<H1><A NAME="author">AUTHOR</A></H1>
<P>This document is based on lecture notes and has been put into
POD format by Steffen Beyer <<A HREF="mailto:sb@sdm.de">sb@sdm.de</A>>.</P>
<P>
<HR>
<H1><A NAME="copyright">COPYRIGHT</A></H1>
<P>Copyright (c) 1997 by Steffen Beyer. All rights reserved.</P>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
<TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
<STRONG><P CLASS=block> Algorithm - the theory behind it</P></STRONG>
</TD></TR>
</TABLE>
</BODY>
</HTML>