Difference between revisions of "PEGWiki:Notational conventions"
From PEGWiki
(→Technical notation) |
(\mathbb{N}_0, \mathbb{N}_1) |
||
Line 43: | Line 43: | ||
* A ''list'' is written like a set but with square brackets, <math>[\,]</math>. It is an ordered collection of elements of the same type. A list is like a set in that its size is not fixed but elements can be added and removed. It is like a tuple in that two lists are considered equal if and only if they are of the same size and all pairs of corresponding elements are equal. Hence, <math>[1,2,3] \neq [1,3,2]</math>, as their elements are in different orders. | * A ''list'' is written like a set but with square brackets, <math>[\,]</math>. It is an ordered collection of elements of the same type. A list is like a set in that its size is not fixed but elements can be added and removed. It is like a tuple in that two lists are considered equal if and only if they are of the same size and all pairs of corresponding elements are equal. Hence, <math>[1,2,3] \neq [1,3,2]</math>, as their elements are in different orders. | ||
* Two lists written with a plus sign between them represents the concatenation of the two lists: hence, <math>[1,2] + [3,4] = [1,2,3,4]</math>. Notice that concatenation is noncommutative. | * Two lists written with a plus sign between them represents the concatenation of the two lists: hence, <math>[1,2] + [3,4] = [1,2,3,4]</math>. Notice that concatenation is noncommutative. | ||
+ | * <math>\mathbb{N}_1</math> represents the positive integers. <math>\mathbb{N}_0</math> represents the non-negative integers. Unless the intended meaning is absolutely clear from context, please use a subscript 0 or 1 instead of simply writing <math>\mathbb{N}</math>. | ||
===Graph theory=== | ===Graph theory=== |
Revision as of 19:02, 28 June 2011
This page documents some conventions to be followed when writing articles on PEGWiki. These are not binding policy on PEGWiki, but following them should minimize confusion and promote consistency across articles. You won't get in trouble for not following them, but what you write is likely to be slightly modified to conform with these conventions if it doesn't initially. If you see arcane or unfamiliar notation anywhere in an article, but especially in a pseudocode block, you may find its meaning here. Changes to these conventions should be discussed on the talk page.
Contents
Pseudocode conventions
Both text-mode and math-mode
- Structure should be indicated by indentation, as in Python. Adding braces
{}
as in C or words such asbegin
andend
as in Pascal would introduce unnecessary clutter into pseudocode blocks. Do not use tabs for indentation. Five spaces is a good guideline. (In LaTeX pseudocode, this means five full-width spaces (a full-width space is "\
").) - The
for
,if
/else
, andwhile
statements should not be followed by words like "do" or "then"; pseudocode is intended for humans, not machines. - Similarly, statements should not end in semicolons.
- Indices into arrays should be placed in square brackets,
[]
. The Pascal convention of separating indices by commas is preferred to the C convention of having a separate set of brackets for each index (simply by virtue of being nicer and more intuitive), but the latter may be occasionally appropriate to reinforce the interpretation of the array's structure. Use your common sense. - Parameter lists should be enclosed in parentheses,
()
, and parameters should be separated by commas. - A function header should contain with the word
function
, the function's name, and the function's parameter list, in that order. The body of a function should be indented. - If something is not a real command, e.g.
input
orprint
, it should not be written like a function (its parameter list should not have parentheses). - Object-oriented code should be used wherever appropriate, and nowhere else. The members of an object should be indicated by the dot operator
.
. For example, it makes sense to represent a data structure as an object (which encapsulates the structure's data). However, please do not wrap an entire algorithm in an object as would be done in Java; that is pointless and only leads to clutter. - The mod operator should never return a negative result; the number-theoretic convention of always yielding a non-negative integer should be followed.
Text-mode only
- Variable assignment should occur through the use of a single equals sign,
=
, although the Pascal notation of:=
is also acceptable. - Equality testing should also occur through the use of a single equals sign,
=
, but is distinguished from variable assignment by context. For example, if it is preceded byif
, it is an equality test, and if it stands alone, it is an assignment. The C notation of==
should be avoided. - The symbols
<=
,>=
, and<>
are equally acceptable as their single-symbol Unicode counterparts≤
,≥
, and≠
, but in most other cases a Unicode symbol should be used whenever both necessary and possible. For example,oo
for∞
is unacceptable. - Variables (and named constants) from pseudocode blocks which are referenced in the article text should be in italics; all other items, such as functions and literal constants, should be
monospaced
. The exception is literal numerical constants; it is acceptable not to format them specially, since they cause no confusion.
Math-mode only
- The LaTeX
\sqrt
command is almost always preferable to typing out . - The vinculum (horizontal bar) to indicate division (produced by the
\frac
command) is usually best, but if the numerator or denominator is complex, a single slash is preferable when it improves readability or overall aesthetic appeal. - Exponentiation should always occur in the form rather than in the form ^. When the exponent is so complex that superscripting makes the expression look ugly, use a temporary variable for the exponent. The exception is when the base is : you can replace with in that case.
- Actual words, such as "if" and "and", should not be italicized. Use the
\mathrm
function to de-italicize them. - Equality testing should occur through the symbol.
- Variable assignment should occur through the symbol (LaTeX code:
\gets
)
Technical notation
- Items enclosed in braces and separated by commas form a set or multiset. For example, is a set containing the elements 1, 2, and 3. Normally they form a set; they form a multiset if stated otherwise. The difference is that a set contains no duplicate elements. Sets and multisets should be typed: that is, a set or multiset should not contain elements of different types. However, a set/multiset may itself contain sets/multisets, or any other kind of item.
- A set/multiset that contains no elements is denoted ∅ (LaTeX: ).
- A set/multiset that contains all elements of a given type is denoted , the universal set. The type can generally be inferred from context.
- Items enclosed in parentheses and separated by commas form a tuple. For example, is an ordered pair (a tuple with two elements). Tuples, unlike sets and multisets, may contain elements of different types, and, again unlike sets and multisets, they have a fixed size.
- Equality of tuples is denoted by an equals sign, . Two tuples are equal if and only if they are equal in size and all corresponding pairs of elements are equal; hence, .
- The symbol ∈ means "is a member of", and ∉ means "is not a member of ".
- The union of two sets or multisets and is denoted , their intersection , and their difference .
- The size or cardinality of a set or multiset is denoted .
- The symbols , , , and denote subset, proper subset, superset, and proper superset, respectively. The symbols and are ambiguous and should be avoided. These relations are defined on multisets similarly to how they are defined on sets.
- The Cartesian product of two sets or multisets and is denoted .
- The complement of a set is denoted ; the complement is not defined on multisets.
- The sum of two multisets and is denoted ; this is not defined on ordinary sets.
- A list is written like a set but with square brackets, . It is an ordered collection of elements of the same type. A list is like a set in that its size is not fixed but elements can be added and removed. It is like a tuple in that two lists are considered equal if and only if they are of the same size and all pairs of corresponding elements are equal. Hence, , as their elements are in different orders.
- Two lists written with a plus sign between them represents the concatenation of the two lists: hence, . Notice that concatenation is noncommutative.
- represents the positive integers. represents the non-negative integers. Unless the intended meaning is absolutely clear from context, please use a subscript 0 or 1 instead of simply writing .
Graph theory
- Vertices in graphs should be given either non-negative integers or letters as labels; letters however must be italicized except when in code blocks. Subscripts on letters are okay: we might for example label the vertices of a bipartite graph as , in which edges exist only between s and s. Subscripts on numbers, however, are not.
- An edge in a directed graph is an ordered pair of vertices. For example, if a graph has an edge from to , then this can be described as . An edge in an undirected graph is an unordered pair, or a set containing two vertices: for example, . (If self-loops are allowed, then it's actually a multiset.)
- A graph is an ordered pair of two sets: and . The former is the set of vertices, and the latter the set of edges. We can write to indicate that the graph has vertex set and edge set . Also, given some graph , is the vertex set of , and is the edge set of . An example of a graph is then , for which and . This graph has vertices , , and , an edge from to , an edge from to , and an edge from to . Note: we can write to indicate that there exists an edge from to . However, the notation is incorrect.
- The number of vertices in graph is denoted (the size of the vertex set), and the number of edges (the size of the edge set). When discussing asymptotic performance, however, we generally write just and .
- The weight of an edge from vertex u to vertex v is regarded as the value of a cost function which should be denoted by a sensible name such as
wt
, hence we havewt(u,v)
and so on. A weighted graph is then regarded as the tuple .