7

Now I'm a programmer who's recently discovered how bad he is when it comes to mathematics and decided to focus a bit on it from that point forward, so I apologize if my question insults your intelligence.

In mathematics, is there the concept of strings that is used in programming? i.e. a permutation of characters.

As an example, say I wanted to translate the following into mathematical notation:

let s be a string of n number of characters.

Reason being I would want to use that representation in find other things about string s, such as its length: len(s).

How do you formally represent such a thing in mathematics?


Talking more practically, so to speak, let's say I wanted to mathematically explain such a function:

fitness(s,n) = 1 / |n - len(s)|

Or written in more "programming-friendly" sort of way:

fitness(s,n) = 1 / abs(n - len(s))

I used this function to explain how a fitness function for a given GA works; the question was about finding strings with 5 characters, and I needed the solutions to be sorted in ascending order according to their fitness score, given by the above function.

So my question is, how do you represent the above pseudo-code in mathematical notation?

Andreas Grech
  • 105,982
  • 98
  • 297
  • 360
  • Two quick comments. First, if s is a string of n characters, then isn't len(s) by definition n? I am having trouble understanding the denominator, because it seems like n = len(s). Second, I looked at our sister site, Mathematics, and did a query on length with http://math.stackexchange.com/search?page=2&tab=relevance&q=length . In graph theory (which seems to be related to your question), they just use a letter for length, as in http://math.stackexchange.com/q/9293/5220. – rajah9 Apr 22 '11 at 13:41
  • No, `n` is not `len(s)`. `n` is the target for my 'optimal' solutions and `len(s)` is just the number of characters for the given chromosome. – Andreas Grech Apr 22 '11 at 13:51
  • Yes they use `n` but I don't need to represent *any* length; I wanted to know a way on how to represent a string's length specifically and the notation involved. – Andreas Grech Apr 23 '11 at 06:24

2 Answers2

9

You can use the notation of language theory, which is used to discuss things like regular languages, context free grammars, compiler theory, etc. A quick overview:

  • A set of characters is known as an alphabet. You could write: "Let A be the ASCII alphabet, a set containing the 128 ASCII characters."

  • A string is a sequence of characters. ε is the empty string.

  • A set of strings is formally known as a language. A common statement is, "Let sL be a string in language L."

  • Concatenating alphabets produces sets of strings (languages). A represents all 1-character strings, AA, also written A2, is the set of all two character strings. A0 is the set of all zero-length strings and is precisely A0 = {ε}. (It contains exactly one string, the empty string.)

  • A* is special notation and represents the set of all strings over the alphabet A, of any length. That is, A* = A0A1A2A3 ... . You may recognize this notation from regular expressions.

  • For length use absolute value bars. The length of a string s is |s|.

So for your statement:

let s be a string of n number of characters.

You could write:

Let A be a set of characters and sAn be a string of n characters. The length of s is |s| = n.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • So, if I understand you correctly, when the absolute value bars are used in the context of a string, they represent the length of the string? – Andreas Grech Apr 22 '11 at 13:59
  • 1
    +1 for steering OP toward language theory and lovely notation. – rajah9 Apr 22 '11 at 15:43
  • Aha, I think this is answer I was searching for...brilliant stuff for me to research more about. Thanks a lot John and I hope I didn't insult your intelligence or anyone else's `:)` – Andreas Grech Apr 23 '11 at 06:21
0

Mathematically, you have explained fitness(s, n) just fine as long as len(s) is well-defined.

In CS texts, a string s over a set S is defined as a finite ordered list of elements of S and its length is often written as |s| - but this is only notation, and doesn't change the (mathematical) meaning behind your definition of fitness, which is pretty clear just how you've written it.

Flash
  • 15,945
  • 13
  • 70
  • 98