With respect to abstract datatypes (ADTs), are the terms "dictionary" and "associative array" perfect synonyms or are there slight differences between them?
-
What research have you done? Can you express either in ADT form? What kind of "associative array" are you talking about? (In some dynamic languages, ass. arrays can store key-value pairs of arbitrary type, which is not usually how dictionaries are viewed.) – Raphael Sep 15 '14 at 10:25
-
@Raphael So you're saying they're not two words for the same thing then? I've always used the words interchangeably; apparently, so has the Internet. – sgarza62 Sep 15 '14 at 12:55
-
1I'd be very hesitant to try to draw any meaningful distinction between these terms if you expect anybody else to have the same distinction(s) in mind. When in doubt, if it matters and in a way that's appropriate for your intended audience, explain briefly what properties the thing you're talking about has. When consuming communications, listen for the author's clarification; if not present, assume whatever comes to mind and is consistent with the presentation. – Patrick87 Sep 16 '14 at 06:52
-
2Such clarification questions are useful. But people expect the asker to show some investigation of his own. Hence the variations of the question grading between positive and negative, i.e. around 0. But, maybe, the importance of this should be stressed more explicitly in the introductory tour. CC@Patrick87 – babou Sep 16 '14 at 08:14
1 Answers
As far as I can tell, associative array is a newer term, maybe emerging from popular use in dynamic programming languages. In algorithms as an academic field, it seems to denote a generalisation of the dictionary but one usually works with dictionaries (probably because most resource characteristics carry over so one does not need to complicate notation).
Citing from theory-leaning [1, p281] (bold emphasis mine):
The dictionary, symbol table, or simply search problem is a fundamental one in computer science: a set of distinct keys is to be organized so that client queries whether or not a given key is in the set can be efficiently answered. More generally, with distinct keys, we can use binary search trees to implement an associative array, where we associate information with each key and can use the kry to store or retrieve such information.
CLRS [2, p229] make no mention of associative arrays but define dictionaries:
We call a dynamic set that supports [insertion, deletion and membership test] a dictionary.
In the more practically inclined [3, p361 ff], the term symbol table is used as a synonym for dictionary but defined in a slightly different way:
A symbol table is a data structure for key-value pairs that supports two operations: insert (put) a new pair into the table and search for (get) the value associated with a given key.
Later, they write:
[The conventions "no duplicate keys" and "put on an existing key replaces the old value"] define the associative array abstraction, where you can think of a symbol table as being just like an array, where keys are indices and values are array entries.
Summarising this small literature research (which is not without contradictions) and from a theoretical viewpoint, I'd state the following:
A dictionary is a data structure that implements a (dynamic) set with insertion, deletion and membership test.
Mathematically speaking, a dictionary over universe $X$ represents an element of the power set $2^X$ of $X$.
An associative array is a data structure that implements a (dynamic) mapping with insertion/update, deletion and retrieval.
Mathematically speaking, an associative array over key universe $X$ and value universe $Y$ represents an element of the set of functions $X \to Y$.
One can thus view a dictionary over universe $X$ as associative array over $(X,\{0,1\})$; this closely resembles the indicator function of sets.
I think this fits programmers' reality quite well, but terms may be used differently over there.
Important: The usage of the term "array" may suggest $O(1)$ accesses. This can, in general, not be expected to be true; at least not in the worst-case and without amortisation arguments. Implementations using hash tables come closest; see here for a discussion about what to expect w.r.t. performance.
- An Introduction to the Analysis of Algorithms by R. Sedgewick and P. Flajolet (2nd ed, 2013)
- Introduction to Algorithms by T. H. Cormen, C. E. Leiserson, R. L. Rivest and C. Stein (3rd ed, 2009)
- Algorithms by R. Sedgewick and K. Wayne (4th ed, 2011)
-
I hope those who downvote will explain their views in an answer and substantiate it with credible sources. – Raphael Sep 15 '14 at 20:28
-
1I think you're overgeneralizing by stating that “dictionary” and “associative array” as having different meanings out of context. You even cite S&R which doesn't distinguish between the two. A (finite) map, a dictionary, a hash (that hash, not that other hash), an associative array are all the same concept of a data structure with an index (usually, but not systematically, with associated data) unless explicitly defined otherwise. – Gilles 'SO- stop being evil' Sep 15 '14 at 20:57
-
@Gilles While it is true that the books by Sedgewick contradict each other to some extent, I think my "conclusion" from the perspective of theory is valid. In particular, the OP asks wrt ADTs --
find
/contains?
is clearly a different operation fromget
(even though the latter can be used to model the former; pickboolean
as value type). What's more, "both are the same" is not a statement that can explain the rich literature that talks about "dictionaries" without ever bothering with stored values (besides the keys themselves). – Raphael Sep 15 '14 at 21:30 -
Note that my "definition" still admits the point of view "a dictionary is a special kind of associative array"; understanding a set as mapping from the universe to booleans is not uncommon. – Raphael Sep 15 '14 at 21:32
-
find
is so clearly not different fromget
that I don't understand which one you think is which! A text that explains how to organize indexes in data structure (e.g. how to balance a search tree) doesn't need to bother with the part of the data that isn't the index, it's an irrelevant detail. A set is a map with values in the unit type, not boolean values. – Gilles 'SO- stop being evil' Sep 15 '14 at 21:34 -
@Gilles Not if you use map in its mathematical meaning. I certainly don't mean
Map<K,V>
when I write "map". – Raphael Sep 15 '14 at 21:37 -
1I mean map as in a data structure, i.e. a finite map = dictionary = associative array = … It's common terminology among both programmers and computer scientists. – Gilles 'SO- stop being evil' Sep 15 '14 at 21:39
-
@Gilles I hope we can agree that when we are criticising my answer, we'll have to assume my terminology. Otherwise the critique is quite pointless. (FWIW, I have not seen "map" as term for a data structure in algorithms/data structures textbooks. Do you have a reference?) I tried to clarify my "summary". – Raphael Sep 16 '14 at 05:59
-
Re: "Mathematically speaking, a dictionary over universe X represents an element of the power set 2X of X.". Why did you choose this formulation, rather than using the formulation you used for associative array? The "2" in "2X" is keys and values - X and Y. In addition, the quote you have refers to keys - value is implicit in the meaning of key. IMHO, your conclusion is the opposite of the quote. Paraphrased correctly: dictionary is an association between keys and values. Therefore, by your own quotes, dictionary and associative array are interchangeable. – ToolmakerSteve Oct 01 '19 at 11:07