#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Posts
    1
    Rep Power
    0

    Basic Algorithm help please ?


    Hey guys,

    I'm having some problems writing out basic algorithms and was hoping some of you could (please) give me a helping hand ?

    We have been assigned this question :

    "Write algorithms which return true or false for the following tasks:
    (i) Given as input an object x and a set S, return true if x ∈ S, and false if x ∉S
    (ii) Given as input a set S and another set T, return true if S ⊆ T, and false if S ⊆ T
    You can assume a command getNextElement(S) which returns the next element of a set S (i.e. the first element of S that has not been given to you before). So you can have a line in your algorithm which says, for example, y ← getNextElement(S). So if you had a set S = {1,2,3}, and you had the following lines in your algorithm
    y ← getNextElement(S)
    z ← getNextElement(S)
    then y would have the value 1, and z would have the value 2. If S has no more elements, then getNextElement(S) returns the special value NULL."

    Now, for part one, I am thinking an answer could be :

    Input : Object ‘X’ and set ‘S’.
    • If x ∈ ‘S’
    • Return a value of “true”.
    • If x ∉ ‘S’
    • Return a value of “false”.

    I have a feeling I'm missing out on some basic stuff, but my lecturer has decided to just blaze on ahead with giving us examples, rather than explaining. Which is fair enough, its probably the best way to learn, its just that the course is continuous assessment and in order to qualify for my doctorate, I need a first class honors ! Hence I need to buck up, and quickly !

    I have absolutely no idea how to tackle the second problem either.

    Can anyone please help me out ?

    Thanks a lot.
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,143
    Rep Power
    9398
    Originally Posted by Reapersoul
    Input : Object ‘X’ and set ‘S’.
    • If x ∈ ‘S’
    • Return a value of “true”.
    • If x ∉ ‘S’
    • Return a value of “false”.
    That's... not an algorithm. An algorithm is a step-by-step process you can follow to do something: what you've given doesn't describe how to do a ∈ or ∉.

    If I gave you a bag of marbles and asked you if there was a blue and green striped marble in the bag, how would you go about finding the answer?


    For part two, imagine I gave you two bags of marbles and asked if the second bag had an identical copy of all the marbles in the first bag. Hint: you'll be using the answer to part (i).

    Comments on this post

    • jwdonahue agrees
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    May 2004
    Posts
    3,417
    Rep Power
    887
    Originally Posted by Reapersoul
    the course is continuous assessment and in order to qualify for my doctorate, I need a first class honors ! Hence I need to buck up, and quickly !
    Please tell me your not a graduate student. If you are working on a doctorate in information or computer science, this should already be old hat for you.

    Look up Donald Knuth. Also try googling algorithm. The Wikipedia has some coverage as well.

    Algorithm for getting to work on time:

    1. Set alarm to 8am.
    2. Sleep until alarm goes off.
    3. Kill the bloody alarm!
    4. Have a quicky (if she's in the mood).
    5. If quicky, get dressed and run out the door, else have breakfast first.
    6. Drive car to office, taking short-cuts and avoid traffic jams. Don't speed! Cops take forever to write tickets.
    7. Don't chat up the cute secretary on the way in the door. Race to your office, fire up your computer and broadcast a "good morning" message to co-workers so everyone knows your their on time.
    I no longer wish to be associated with this site.

IMN logo majestic logo threadwatch logo seochat tools logo