Pseudocode Conventions

Pseudocode Conventions

Appendix A Pseudocode Conventions A.1 Introduction In this part, the pseudocode conventions for writing an algorithm are presented. The conventions we use follow the modern programming guidelines and are similar to the ones used in [1] and [2]. Every algorithm has a name specified in its heading, and each line of an algorithm is numbered to provide citation. The first part of an algorithm usually starts by its inputs. Blocks within the algorithm are shown by indentations. The pseudocode conventions adopted are described as data structures, control structures, and distributed algorithm structure as follows. A.2 Data Structures Expressions are built using constants, variables, and operators as in any functional programming language and yield a definite value. Statements are composed of ex- pressions and are the main unit of executions. All statements are in the form of numbered lines. Declaring a variable is done as in languages like Pascal and C, where its type precedes its label with possible initialization as follows: set of int neighbors ← ∅ Here we declare a set called neighbors of a vertex in a graph each element of which is an integer. This set is initialized to ∅ (empty) value. The other commonly used variable types in the algorithms are boolean for boolean variables and message types for the possible types of messages. For assignment, we use ← operator, which shows that the value on the right is assigned to the variable in the left. For example, the statement a ← a + 1 increments the value of the integer variable a. Two or more statements in a line are separated by semicolons and comments are shown by symbol at the end of the K. Erciyes, Distributed Graph Algorithms for Computer Networks, 305 Computer Communications and Networks, DOI 10.1007/978-1-4471-5173-9, © Springer-Verlag London 2013 306 A Pseudocode Conventions Table A.1 General algorithm conventions Notation Meaning x ← y assignment = comparison of equality = comparison of inequality true, false logical true and false null nonexistence comment Table A.2 Arithmetic and logical operators Notation Meaning ¬ logical negation ∧ logical and ∨ logical or ⊕ logical exclusive-or x/y x divided by y x · y or xy multiplication line as follows: 1 : a ← 1; c ← a + 2; c is now 3 General algorithmic conventions are outlined in Table A.1. Table A.2 summarizes the arithmetic and logical operators used in the text with their meanings. Sets instead of arrays are frequently used to represent a collection of similar variables. Inclusion of an element u toasetS can be done as follows: S ← S ∪{u} and deletion of an element v from S is performed as follows: S ← S \{v} Table A.3 shows the set operations used in the text with their meanings. A.3 Control Structures In the sequential operation, statements are executed consecutively. Branching to another statement can be done by selection described below. A.3 Control Structures 307 Table A.3 Set operations Notation Meaning |S| cardinality of S ∅ empty set u ∈ Suis a member of S S ∪ R union of S and R S ∩ R intersection of S and R S \ R set subtraction S ⊂ RSis a proper subset of R max / min S maximum/minimum value of the elements of S max / min{...} S maximum/minimum value of a collection of values Algorithm A.1 if–then–else structure 1: if condition then first check 2: statement1 3: if condition2 then second (nested) if 4: statement2 5: end if end of second if 6: else if condition3 then else if of first if 7: statement3 8: else 9: statement4 10: end if end of first if A.3.1 Selection Selection is performed using conditional statements, which are implemented using if–then–else in the usual fashion, and indentation is used to specify the blocks as shown in the example code segment (see Algorithm A.1). In order to select from a number of branches, case–of construct is used. The ex- pression within this construct should return a value that is checked against a number of constant values, and the matching branch is taken as follows: 1. case expression of 2. constant1: statement1 . 3. 4. constantn: statementn 5. end case 308 A Pseudocode Conventions A.3.2 Repetition The main loops in accordance with the usual high-level language syntax are the for, while, and loop constructs. The for–do loop is used when the count of iterations can be evaluated before entering the loop as follows: 1. for i ← 1 to n do . 2. 3. end for The second form of this construct is the for all loop, which arbitrarily selects an element from the set specified and iterates until all members of the set are processed as shown below, where a set S with three elements and an empty set R are given, and each element of S is copied to R iteratively. 1. S ←{3, 1, 5}; R ← ∅ 2. for all u ∈ S do 3. R ← R ∪{u} 4. end for For the indefinite cases where the loop may not be entered at all, the while–do construct may be used, where the boolean expression is evaluated, and the loop is entered if this value is true as follows: 1. while boolean expression do 2. statement 3. end while A.4 Distributed Algorithm Structure Distributed algorithms have significantly different structures than the sequential al- gorithms as their execution pattern is determined by the type of messages they re- ceive from their neighbors. For this reason, the general distributed algorithm pseu- docode usually includes a similar structure to the algorithm template shown in Al- gorithm A.2. In this algorithm structure, there may be n types of messages, and the type of action depends on the type of message received. For this example, the while–do loop executes as long as the value of the boolean variable flag evaluates to true. Typically, a message received by this node (i) at some point triggers an action that changes the value of the flag variable to true, which then results in this termination of the loop. In another frequently used distributed algorithm structure, the while–do loop is executed forever, and one or more of the actions should provide exit from this endless while loop as shown in Algorithm A.3. The indefinite structure of this loop type makes it suitable for distributed algo- rithms where the type of message, in general, cannot be determined beforehand. References 309 Algorithm A.2 Distributed Algorithm Structure 1 1: int i, j i is this node; j is the sender of the current message 2: while ¬flag do all nodes execute the same code 3: receive msg(j) 4: case msg(j).type of 5: type_1: Action_1 6: ... : ... 7: type_n: Action_n 8: if condition then 9: flag ← true 10: end if 11: end while Algorithm A.3 Distributed Algorithm Structure 2 1: while forever do 2: receive msg(j) 3: case msg(j).type of 4: type_1: Action_1: if condition1 then exit 5: ... : ... 6: type_x: Action_1: if conditionx then exit 7: type_n: Action_n 8: end while References 1. Cormen TH, Leiserson CE, Rivest RL, Stein C (2001) Introduction to algorithms. MIT Press, Cambridge 2. Smed J, Hakonen H (2006) Algorithms and networking for computer games. Wiley, New York. ISBN 0-470-01812-7. Appendix B ASSIST Code B.1 Buffer Pool Management /* assist.c */ #include <stdio.h> #include <fcntl.h> #include <pthread.h> #include <semaphore.h> #include <stdlib.h> #include <sys/types.h> /*************************************************** pool data structure ****************************************************/ #define POOL_SIZE 20 /* size of allocated space */ #define ERR_POOLEMPTY -1 #define ERR_POOLFULL -2 typedef struct buffer *bufptr; typedef struct buffer { int data; bufptr next; int sender; int type; } buf_t; typedef struct pool *poolptr; typedef struct pool{ int state; int pool_size; sem_t poolsem; pthread_mutex_t poolmut; K. Erciyes, Distributed Graph Algorithms for Computer Networks, 311 Computer Communications and Networks, DOI 10.1007/978-1-4471-5173-9, © Springer-Verlag London 2013 312 B ASSIST Code bufptr front; bufptr rear; buf_t bufs[POOL_SIZE]; } pool_t; /*************************************************** initialize a buffer pool ****************************************************/ int init_pool (poolptr pp, int pool_length) { inti; pp->pool_size=pool_length; sema_init(&pp->poolsem,pp->pool_size,USYNC_THREAD,0); mutex_init(&pp->poolmut,USYNC_THREAD,0); pp->front=&(pp->bufs[0]); for (i=0; i<pp->pool_size - 1; i ++) pp->bufs[i].next = &(pp->bufs[i+1]); pp->rear=&(pp->bufs[pp->pool_size-1]); } /*************************************************** get a buffer from a pool ****************************************************/ bufptr get_buf(poolptr pp) { bufptr bp; sema_wait(&pp->poolsem); mutex_lock(&pp->poolmut); bp=pp->front; pp->front=bp->next; mutex_unlock(&pp->poolmut); return(bp); } /*************************************************** put a buffer to a pool ****************************************************/ int put_buf(poolptr pp, bufptr bp) { bp->next=NULL; mutex_lock(&pp->poolmut); pp->rear->next=bp; pp->rear=bp; if (pp->front==NULL) pp->front=bp; mutex_unlock(&pp->poolmut); sema_post(&pp->poolsem); } B.2 Interprocess Communication 313 B.2 Interprocess Communication /*************************************************** fifo data structure ****************************************************/ #define FIFO_SIZE 10 #define N_FIFOS 10 #define ALLOCATED 1 #define ERR_FIFOEMPTY -1 #define ERR_FIFOFULL -2 typedef struct fifo *fifoptr; typedef struct fifo{ int state ; int fifo_size; int read_idx; int write_idx; sem_t fullsem; sem_t emptysem; pthread_mutex_t fifomut; bufptr bufs[FIFO_SIZE]; } fifo_t; /*************************************************** initialize a fifo ****************************************************/ int init_fifo(fifoptr fp) { int

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    19 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us