0

Let networklocs be a set of elements of the form (n, t, l) where n is a node in the network, t is a clock tick and l is the location of n at time t. How can I get (in a concise way in pseudo-code) the element of networklocs where node and time is given?

I know I can write a function like

    getElement(ni,t)
    for all (nj,t',l') in networklocs
      if nj=ni and t'= t then return (nj,t',l')

But is there a more concise way to access an element of the set networklocs in the pseudo-code?

Note that I would like to keep networklocs as a set, so solutions with maps or arrays do not fit.

Ben Bost
  • 139
  • 2
  • 7
  • 2
    In pseudocode you write whatever you want, provided people understand you. That's why we call it pseudocode. `networklocs[node=ni,time=t]` is as good as anything else. – n. m. could be an AI Jun 06 '15 at 19:07

1 Answers1

0

Note that returning ni and t is useless because they're already known. In notation and practice, you'd want

Let M be a map: <N, T> -> L

The operation you want is just a map lookup:

l <- M <n, t>

Although the map is the most likely notation, a predicate logic expression can also be used:

Let get(n,t) be x = <n, t, l> | x \in networklocs

The loop you provided as an example is neither correct nor pseudocode. It's a concrete implementation of a map, and it doesn't say what to do when the key is not found.

Gene
  • 46,253
  • 4
  • 58
  • 96
  • :Thanks, but for some reasons I would like to keep networklocs as a set for the pseudocode. I know that with a map is easier to write but here I want to know how can I write if with a set! – Ben Bost Jun 06 '15 at 19:19
  • @BenBost There's no reason you can't define the data as a set and then define the map, too, when it's needed. Pseudocode that does a linear lookup in a list isn't pseudoocode. It's implementation. I'll add one more suggestion in the answer. – Gene Jun 06 '15 at 21:31
  • Thanks for your proposition, but I am sorry I am not interested in the implementation, I assure you I knew I could use a map when I sent the question and as I said the function I proposed was not the way that i thought the pseudo-code should be written, hence I asked the question. And, your proposition is not clear. You say although the map is the most likely notation we can use this logic expression : "Let get(n,t) be x? you use get with two keys as you use it on a map, here I only wanted to use a set. – Ben Bost Jun 06 '15 at 23:01
  • @BenBost Sorry I can't meet your requirements. I've written quite a few published papers on algorithms and read hundreds. I'm telling you the way it would be done in the computer science literature. If you're out for something else, I wish you luck. – Gene Jun 07 '15 at 01:09