Array list: number: Difference between revisions

From Algowiki
Jump to navigation Jump to search
(Created page with "'''Algorithmic problem:''' Linear sequence: number '''Prerequisites:''' '''Type of algorithm:''' loop '''Auxiliary data:''' # A pointer <math>p</math> of type "pointer...")
 
No edit summary
Line 9: Line 9:
# A pointer <math>p</math> of type "pointer to array list item of type <math>K</math>".
# A pointer <math>p</math> of type "pointer to array list item of type <math>K</math>".
# A counter <math>c \in \mathbb{N}_0</math>.
# A counter <math>c \in \mathbb{N}_0</math>.
Abstract view


Invariant: After  iterations:
== Abstract view ==
The pointer  points to the array list item at position  (or is void if there is no such item).
The value of  is the sum of the values n of all array list items at positions .
Variant:  is moved one step forward so as to point to the next array list item.
Break condition: It is void.


Induction basis
'''Invariant:''' After <math>i \ge 0</math> iterations:


Abstract view: Initialize  so as to point to the first array list item.
# The pointer <math>p</math> points to the array list item at position <math>i+1</math> (or is void if there is no such item).
Implementation:
#The value of  is the sum of the values n of all array list items at positions <math>1,...,i</math>.
Set first.
Set .
Proof: Nothing to show.


Induction step
'''Variant:''' <math>p</math> is moved one step forward so as to point to the next array list item.
'''Break condition:''' It is <math>p=</math> void.


Abstract view: Add the number of elements in the current array and then go forward.
==Induction basis==
Implementation:
If void, terminate the algorithm and return the value of .
Otherwise:
Set .n.
Set .next.
Correctness: Nothing to show.


Complexity
'''Abstract view:''' Initialize <math>p</math> so as to point to the first array list item.


Statement: Linear in the length of the sequence in the worst case (for a fixed value of ).
'''Implementation:'''
Proof: Obvious.


Further information
# Set <math>p:=first</math>.
# Set <math>c:=0</math>.
 
'''Proof:''' Nothing to show.
 
==Induction step==
 
'''Abstract view:''' Add the number of elements in the current array and then go forward.
 
'''Implementation:'''
# If void, terminate the algorithm and return the value of <math>c</math>.
# Otherwise:
## Set <math>c:=c+p</math>.n.
## Set <math>p:=p</math>.next.
 
'''Correctness:''' Nothing to show.
 
==Complexity==
 
'''Statement:''' Linear in the length of the sequence in the worst case (for a fixed value of ).
 
'''Proof:''' Obvious.
 
==Further information==

Revision as of 14:00, 27 January 2015

Algorithmic problem: Linear sequence: number

Prerequisites:

Type of algorithm: loop

Auxiliary data:

  1. A pointer [math]\displaystyle{ p }[/math] of type "pointer to array list item of type [math]\displaystyle{ K }[/math]".
  2. A counter [math]\displaystyle{ c \in \mathbb{N}_0 }[/math].

Abstract view

Invariant: After [math]\displaystyle{ i \ge 0 }[/math] iterations:

  1. The pointer [math]\displaystyle{ p }[/math] points to the array list item at position [math]\displaystyle{ i+1 }[/math] (or is void if there is no such item).
  2. The value of is the sum of the values n of all array list items at positions [math]\displaystyle{ 1,...,i }[/math].

Variant: [math]\displaystyle{ p }[/math] is moved one step forward so as to point to the next array list item. Break condition: It is [math]\displaystyle{ p= }[/math] void.

Induction basis

Abstract view: Initialize [math]\displaystyle{ p }[/math] so as to point to the first array list item.

Implementation:

  1. Set [math]\displaystyle{ p:=first }[/math].
  2. Set [math]\displaystyle{ c:=0 }[/math].

Proof: Nothing to show.

Induction step

Abstract view: Add the number of elements in the current array and then go forward.

Implementation:

  1. If void, terminate the algorithm and return the value of [math]\displaystyle{ c }[/math].
  2. Otherwise:
    1. Set [math]\displaystyle{ c:=c+p }[/math].n.
    2. Set [math]\displaystyle{ p:=p }[/math].next.

Correctness: Nothing to show.

Complexity

Statement: Linear in the length of the sequence in the worst case (for a fixed value of ).

Proof: Obvious.

Further information