# Set in an algorithm

• Dec 17th 2011, 09:32 AM
suzanne
Set in an algorithm
Hello

In part of an algorithm that I am working on, there are two sets, they both have the same elements, except that one has one element more than the other. For example:

A={a,b,c}
B={a,b}

I have written two versions:

H= A - B
array1[h] = f (where h isMemberOf H)

and (h is meant to be a variable)

{h} = A - B
array1[h] = f

Is any of these notations correct? What do you advise?

• Dec 17th 2011, 10:51 AM
emakarov
Re: Set in an algorithm
My guess is that this is intended as a comment, not as an actual program code. In this case I would add some text, like "A - B is a singleton because ... Let {h} = A - B".
• Dec 17th 2011, 01:40 PM
suzanne
Re: Set in an algorithm
Quote:

Originally Posted by emakarov
My guess is that this is intended as a comment, not as an actual program code. In this case I would add some text, like "A - B is a singleton because ... Let {h} = A - B".

Thanks for your reply. This is meant to be as actual program code. Is it wrong to have sets in algorithms then?
• Dec 17th 2011, 09:57 PM
emakarov
Re: Set in an algorithm
If it's an actual programming language (could you say which one?), then this depends on the language. If it's pseudocode, then there is not much difference. The first variant: "array1[h] = f (where h isMemberOf H)" does not look like a deterministic instruction because the computer may not know that there is a single element in H. The second variant resembles a pattern matching in functional languages. In general, the left-hand side of an assignment must be a variable, nor a construction like {h}. However, in functional languages you may say something like "let {h} = A in B". This means: "if A has the form {h}, then evaluate B with h assigned". The program may fail if the pattern in the left-hand side does not match the value in the right-hand side.
• Dec 18th 2011, 10:04 AM
suzanne
Re: Set in an algorithm

My apologies for the mistake made in my comment.

I meant to say this algorithm is meant to used by an actual programmer to develop code.

To increase efficiency, it would be best if we can use sets. What I am not sure of, is how to properly write an algorithm that includes sets and set operations.
• Dec 18th 2011, 10:15 AM
ILikeSerena
Re: Set in an algorithm
Hi suzanne! :)

I recommend using mathematical language if it is about mathematics and leave it up to the programmer how to implement it.

As it is, it is not clear to me what you mean by: array1[h] = f (where h isMemberOf H)

What is array1 supposed to be?
Is it a set? Or an array as its name seems to imply?
What does it mean that you assign it a value of f?
Is that intended to represent "false"?
If so, then for clarity, you should name it "false" instead of "f".
• Dec 18th 2011, 10:36 AM
emakarov
Re: Set in an algorithm
Quote:

Originally Posted by ILikeSerena
I recommend using mathematical language if it is about mathematics and leave it up to the programmer how to implement it.

I agree.

Quote:

Originally Posted by suzanne
I meant to say this algorithm is meant to used by an actual programmer to develop code.

If this algorithm is intended to be read by a person, not a computer, then there is not much difference. You could write

array1[h] = f where h ∊ A - B

or

let {h} = A - B
array1[h] = f

The only thing that should be clear is why there is a unique h ∊ A - B at this point in the algorithm. A person who reads this should not have doubts about which elements of A - B to use and why this set is not empty. This may take a comment or an explanation.