<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.0 20120330//EN" "JATS-archivearticle1.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink">
  <front>
    <journal-meta />
    <article-meta>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Jörg Desel</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Julia Fleischer</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Moritz Sommer</string-name>
          <xref ref-type="aff" rid="aff1">1</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>FernUniversität in Hagen</institution>
          ,
          <country country="DE">Germany</country>
        </aff>
        <aff id="aff1">
          <label>1</label>
          <institution>Luitpold-Gymnasium München</institution>
          ,
          <country country="DE">Germany</country>
        </aff>
      </contrib-group>
      <abstract>
        <p>A small high-level Petri net describing the behavior of a simple algorithm with one loop is presented and discussed. The net can run into a deadlock if and only if the algorithm terminates. The behavior of the net is surprisingly complex, so that we provide a tool to visualize possible runs. We ofer the reader a puzzle, ask for a termination proof - and of course provide a solution.</p>
      </abstract>
      <kwd-group>
        <kwd>eol&gt;high-level Petri net</kwd>
        <kwd>termination proof</kwd>
      </kwd-group>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>1. Introduction</title>
      <p>Very often, Petri nets and other formal modeling languages are taught independently of application
domains such as software engineering. In addition, the methods used in formal techniques difer
significantly from methods in other fields, although both are based on mathematics. Due to the usual
study structure in separate modules, taught by diferent professors, students find it dificult to recognize
connections and integrate their knowledge.</p>
      <p>Since Petri nets have a particularly wide range of uses, it makes sense to use them as a comprehensive
bridge in studies. This traditionally happens in some places, for example in the old textbook [1] of
Rüdiger Valk and Eike Jessen, which is both a book on Petri nets and on applications in computing
systems.</p>
      <p>The primary modeling aspect of Petri nets is concurrency. However, many concepts developed
for Petri nets are likewise applicable to sequential systems, which can be viewed as a special case of
concurrent systems.</p>
      <p>This short article presents an example – actually a puzzle or a challenging exercise for students
– which integrates three areas of knowledge: programming practice, programming theory and Petri
net modeling. The question to be answered is to find a descending measure for a loop, proving that a
given algorithm terminates. The algorithm is presented as a Petri net, so that the termination proof is
at the same time a termination proof for the Petri net behavior. Since the behavior of the algorithm
under consideration turns out to be surprisingly complex, we provide as a web-based educational
resource a Python program. This program can be modified by the user and contributes to understanding
the behavior of the algorithm. This understanding is essential for finding the required termination
argument.</p>
      <p>We found the algorithm under consideration by chance. The question in this article meets the two
most important beauty criteria: The algorithm itself is very simple and immediately understandable,
whereas the question to the students is surprisingly dificult but solvable. Let us end this introduction
by providing the algorithm in natural language:</p>
      <p>Given two nonzero integers, repeat the following until one of the numbers is zero:
If the numbers have diferent signs, add the second number to the first; otherwise, subtract the first
from the second.</p>
      <p>
        So, starting e.g. with the pair (
        <xref ref-type="bibr" rid="ref5">5, 7</xref>
        ), we obtain the sequence
      </p>
      <p>
        (
        <xref ref-type="bibr" rid="ref5">5, 7</xref>
        ), (
        <xref ref-type="bibr" rid="ref2 ref5">5, 2</xref>
        ), (
        <xref ref-type="bibr" rid="ref5">5, − 3</xref>
        ), (
        <xref ref-type="bibr" rid="ref2">2, − 3</xref>
        ), (− 1, − 3), (− 1, − 2), (− 1, − 1), (− 1, 0)
which terminates with the second number being zero, whereas the pair (
        <xref ref-type="bibr" rid="ref6">− 8, 6</xref>
        ) leads to the sequence
(
        <xref ref-type="bibr" rid="ref6">− 8, 6</xref>
        ), (
        <xref ref-type="bibr" rid="ref6">− 2, 6</xref>
        ), (
        <xref ref-type="bibr" rid="ref4 ref6">4, 6</xref>
        ), (
        <xref ref-type="bibr" rid="ref2 ref4">4, 2</xref>
        ), (
        <xref ref-type="bibr" rid="ref4">4, − 2</xref>
        ), (
        <xref ref-type="bibr" rid="ref2">2, − 2</xref>
        ), (0, − 2).
      </p>
    </sec>
    <sec id="sec-2">
      <title>2. The Algorithm</title>
      <p>Instead of starting with a textual representation of the algorithm, we formalize it by means of the
following high-level Petri net (for the ocurrence rule of high-level Petri nets see [2] or [3]). Notice that
the initial values of the two tokens  and  in the two places represent the initial values of the two
integers.</p>
      <p>This Petri net corresponds to the following algorithm without INPUT and OUTPUT operations,
when 0 is the input value for the variable  and 0 is the input value for the variable .</p>
      <p>INPUT ,  ∈ Z
WHILE  ·  ̸= 0</p>
      <p>IF  ·  &lt; 0 THEN</p>
      <p>←  + 
ELSE</p>
      <p>←  −</p>
      <p>OUTPUT ,</p>
      <p>Actually, repeated calculation of the product  ·  is not the most eficient way to evaluate the
conditions after WHILE and IF. Instead, a real implementation would rather check  ̸= 0 and  ̸= 0
instead of  ·  ̸= 0 and  &gt; 0 xor  &gt; 0 instead of  ·  &lt; 0.</p>
      <p>This example departs from common applications of Petri nets. It deals with integers, a scenario
somewhat atypical in the Petri net world, since token numbers on places (token counts) can never
be negative. Consequently, these integers are not represented as markings of places but rather as
high-level tokens.</p>
      <p>Let us start with some observations:
• There is an obvious invariant stating that, for each reachable marking, both places carry one token
representing an integer each. Therefore, for any reachable marking , we can write () = 
instead of () = {}, and the same applies to the place .
• A transition is only enabled at a marking  satisfying () ̸= 0 and () ̸= 0. Since each
transition occurrence changes only one of the two tokens, we have () ̸= 0 or () ̸= 0 after
any transition occurrence. Consequently, the marking  satisfying () = () = 0 can only
be reached if this is the initial marking.
• Taking the transition guards into account, each reachable marking enables at most one of the two
transitions. It enables no transition if and only if one of the two tokens represents the value 0.</p>
      <p>Therefore, the behavior is deterministic for each initial marking.</p>
      <p>We are interested in termination of the algorithm: Does it terminate for arbitrary input values 0
and 0, or only for some and, if so, for which ones? In terms of Petri nets, we address the question of
whether the Petri net can reach a deadlock and, if so, from which initial markings a deadlock can be
reached. Given that the behavior of the net is neither concurrent nor nondeterministic, such a deadlock
would inevitably be eventually reached – if it is reachable at all.</p>
      <p>This question is naturally a task for you, the reader, or for your students.</p>
    </sec>
    <sec id="sec-3">
      <title>3. Finding a solution</title>
      <p>How would you approach finding an answer? Naturally, you would first test the algorithm with
diferent input values 0 and 0 and find that during the process the numbers sometimes become
positive, sometimes negative, and also vary in their magnitude . . . and that, ultimately, regardless of
the initial values of the variables, one of the two - and thus the product - inevitably turns to zero. The
remaining number is then always the greatest common divisor of the initial values, or its negative value,
if this concept is generalized from natural numbers to integers.</p>
      <p>To prove termination of the algorithm, it is very helpful to find a descending measure, that is, a
function Z × Z → N that allocates a natural number to each assignment of the variables  and . This
number should decrease by at least one with every loop iteration (see e.g. [4] for this and more general
termination criteria). In Petri net language, the function, applied to () and (), decreases with
every transition occurrence. Once a suitable descending measure  is found,  (0, 0) indicates for
initial values  and  how many times the transitions can fire at most. That, is, after a finite number of
loop iterations there can eventually be no more iterations of the loop, or in Petri net terminology, a
deadlock is reached. In our example, this happens precisely when () = 0 or () = 0.</p>
      <p>Before we present a solution, a bit more about the genesis of this puzzle. It was planned as a regular
exercise for our students and meant to be quite harmless, in the expectation that the algorithm either
does not terminate or that a suitable decreasing measure is easy to find. But neither is the case! Since
after many experiments it seemed unlikely that the algorithm would not terminate for any initial
marking, we concentrated on the search for a descending measure, proving termination generally.</p>
      <p>When looking for a descending measure, it helps to look at the algorithm in more detail. For this
purpose, it has proven useful to note the respective values of  and  in a coordinate system, with the
-axis representing  and the -axis representing . Then arrows represent the changes caused by a
loop execution, i.e., if the values  and  are changed to ′ and ′, then there is an arrow from the point
(, ) to the point (′, ′). That is exactly what we have done for several example values in Figure 2.
Yellow (horizontal) arrows represent occurrences of transition  and blue (vertical) arrows represent
occurrences of transition .</p>
      <p>And because it’s so easy to make a mistake, we implemented the algorithm and the visualization
mentioned above. So you don’t have to repeat this efort. We are happy to provide the tool, which is
accessible via https://kalliope1907.github.io/jupyter/notebooks/index.html?path=visualisierung_algorithmus.
ipynb. The tool allows to play with the algorithm. It visualizes the live of both variables for arbitrary
initial values. Since it can be freely modified by the user – provided she has some practice in Python
programming – not only the initial values but also the algorithm itself can be changed. And finally, the
tool draws surpringly nice pictures, see the screenshot in Figure 3 for an example.</p>
      <p>Before we developed this tool we gave the task to 600 students at the FernUniversität in Hagen in the
"Software Engineering" module as an assignment and actually received three responses. The solution
we’re about to present is actually a combination of two proposals from students – our previous solution
turned out to be less elegant.</p>
    </sec>
    <sec id="sec-4">
      <title>4. Problem Statement</title>
      <p>
        We are looking for a mapping  : Z × Z → N such that
•  (, ) decreases for any loop execution, i.e.,
• If  (, ) = 0 then the loop terminates, i.e.,
 ·  &lt; 0 =⇒  ( + , ) &lt;  (, )
 ·  &gt; 0 =⇒  (,  − ) &lt;  (, )
 (, ) = 0 =⇒  ·  = 0
(
        <xref ref-type="bibr" rid="ref1">1</xref>
        )
(
        <xref ref-type="bibr" rid="ref2">2</xref>
        )
(
        <xref ref-type="bibr" rid="ref3">3</xref>
        )
Figure 3: Screenshot of the tool
      </p>
    </sec>
    <sec id="sec-5">
      <title>5. Our Solution</title>
      <p>The formal definition of our descending measure turns out to be somewhat tedious. It is better understood
when you consider the following eight areas of the coordinate system.</p>
      <p>We define a mapping  : Z ×</p>
      <p>Z → N and distinguish ten (!) cases:
A more compact version of the definition might be nicer, although less understandable:
|| ·  ·  &gt; || ·  · 
|| ·  ·  &lt; || ·  · 
|| ·  ·  = || ·  ·  ̸= 0
|| ·  ·  = || ·  ·  = 0
As one of our students found out, 2 · max(||, ||) can be equivalently replaced by | + | + | − |.</p>
      <p>This definition clearly satisfies the second requirement:  (, ) = 0 implies  ·  = 0. In fact, since 
and  are integers, we have  (, ) ≥ 1 for all other cases.</p>
      <p>Why is this function truly a descending measure, i.e., why does its value always decrease? We shall
now prove that with each move from values  and  to values ′ and ′ we get  (, ) &gt;  (′, ′). For
this proof, we restrict our considerations to the special case  &gt; 0 and  &gt; 0, i.e., to the first quadrant
of the coordinate system. It is easy to see that by rotation symmetry the same arguments apply to the
other three cases. Note that for  = 0 or  = 0 nothing has to be shown, because in both cases  ·  = 0
and therefore the loop stops.</p>
      <p>Since we assume  &gt; 0 and  &gt; 0 we obtain  ·  &gt; 0. Therefore ′ =  and ′ =  − . We thus
have to prove  (,  − ) &lt;  (, ).</p>
      <p>We distinguish five cases (see the five downward-pointing blue arrows in the coordination system of
Figure 2):
Case 1:  &gt; 2 · .</p>
      <p>Then  −  &gt; . We get  (,  − ) = 2( − ) − 1 &lt; 2 − 1 =  (, ) since  &gt; 0.
Case 2:  = 2 · .</p>
      <p>Then  −  = . We get  (,  − ) = 1 &lt; 2 − 1 =  (, ) since  = 2 ≥ 2.</p>
      <p>Case 3: 2 ·  &gt;  &gt; .</p>
      <p>Then  −  &lt; . We get  (,  − ) = 2 &lt; 2 − 1 =  (, ) since  &lt; .</p>
      <p>Case 4:  = .</p>
      <p>Then  −  = 0. We get  (,  − ) = 0 &lt; 1 =  (, ) since ,  &gt; 0.</p>
      <p>Case 5:  &lt; .</p>
      <p>Then −  &lt;  −  &lt; 0. We get  (,  − ) = 2 − 1 &lt; 2 =  (, ).</p>
      <p>So in all of these five cases the inequality holds, and it holds in each case for a diferent reason. This
completes the proof (we would in fact have 20 diferent cases if we would consider all four quadrants
explicitly).</p>
    </sec>
    <sec id="sec-6">
      <title>6. Conclusion</title>
      <p>We presented an algorithm and its Petri net representation and proved that this algorithm terminates
for all initial values, or, equivalently, that the Petri net terminates for every initial marking.</p>
      <p>This exercise links program analysis and Petri net analysis and demonstrates how Petri nets can be
used as a (programming) language independent means for the representation of algorithms. In addition,
a connection to programming practice is made, as it can be helpful for students to create or use and
modify an illustrative tool to understand the behavior of the algorithm.</p>
      <p>As Petri nets are particularly useful for modeling the concurrent behavior of systems, they represent
distributed algorithm in a natural way. Therefore, Petri net analysis techniques can be applied to analyse
termination and other behavioral properties of distributed algorithm, as shown in many examples in
[5], for another example see [6].</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          [1]
          <string-name>
            <given-names>E.</given-names>
            <surname>Jessen</surname>
          </string-name>
          ,
          <string-name>
            <given-names>R.</given-names>
            <surname>Valk</surname>
          </string-name>
          , Rechensysteme - Grundlagen der Modellbildung, Studienreihe Informatik, Springer,
          <year>1987</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          [2]
          <string-name>
            <given-names>W.</given-names>
            <surname>Reisig</surname>
          </string-name>
          ,
          <string-name>
            <surname>Understanding Petri</surname>
          </string-name>
          Nets - Modeling
          <string-name>
            <surname>Techniques</surname>
          </string-name>
          ,
          <source>Analysis Methods, Case Studies</source>
          , Springer,
          <year>2013</year>
          . URL: https://doi.org/10.1007/978-3-
          <fpage>642</fpage>
          -33278-4. doi:
          <volume>10</volume>
          .1007/ 978-3-
          <fpage>642</fpage>
          -33278-4.
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          [3]
          <string-name>
            <given-names>K.</given-names>
            <surname>Jensen</surname>
          </string-name>
          ,
          <string-name>
            <surname>Coloured Petri</surname>
          </string-name>
          Nets - Basic
          <string-name>
            <surname>Concepts</surname>
          </string-name>
          ,
          <source>Analysis Methods and Practical Use - Volume</source>
          <volume>1</volume>
          ,
          <string-name>
            <surname>Second</surname>
            <given-names>Edition</given-names>
          </string-name>
          ,
          <source>Monographs in Theoretical Computer Science. An EATCS Series</source>
          , Springer,
          <year>1996</year>
          . URL: https://doi.org/10.1007/978-3-
          <fpage>662</fpage>
          -03241-1. doi:
          <volume>10</volume>
          .1007/978-3-
          <fpage>662</fpage>
          -03241-1.
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          [4]
          <string-name>
            <given-names>H. R.</given-names>
            <surname>Jr</surname>
          </string-name>
          .,
          <article-title>Theory of recursive functions and efective computability (Reprint from</article-title>
          <year>1967</year>
          ), MIT Press,
          <year>1987</year>
          . URL: http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&amp;tid=
          <fpage>3182</fpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          [5]
          <string-name>
            <given-names>W.</given-names>
            <surname>Reisig</surname>
          </string-name>
          ,
          <article-title>Elements of distributed algorithms: modeling and analysis with Petri nets</article-title>
          , Springer,
          <year>1998</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          [6]
          <string-name>
            <given-names>J.</given-names>
            <surname>Desel</surname>
          </string-name>
          , E. Kindler,
          <string-name>
            <given-names>T.</given-names>
            <surname>Vesper</surname>
          </string-name>
          ,
          <string-name>
            <given-names>R.</given-names>
            <surname>Walter</surname>
          </string-name>
          ,
          <article-title>A simplified proof for a self-stabilizing protocol: A game of cards, Inf</article-title>
          . Process. Lett.
          <volume>54</volume>
          (
          <year>1995</year>
          )
          <fpage>327</fpage>
          -
          <lpage>328</lpage>
          . URL: https://doi.org/10.1016/
          <fpage>0020</fpage>
          -
          <lpage>0190</lpage>
          (
          <issue>95</issue>
          )
          <fpage>00065</fpage>
          -
          <lpage>K</lpage>
          . doi:
          <volume>10</volume>
          .1016/
          <fpage>0020</fpage>
          -
          <lpage>0190</lpage>
          (
          <issue>95</issue>
          )
          <fpage>00065</fpage>
          -
          <lpage>K</lpage>
          .
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>