## Sunday, July 10, 2016

### Sparse Similarity

The similarity of two documents (each with distinct words) is defined to be the
size of the intersection divided by the size of the union. For example, if the documents consist of
integers, the similarity of {1, 5, 3} and { 1, 7, 2, 3} is 0. 4, because the intersection has size
2 and the union has size 5.
We have a long list of documents (with distinct values and each with an associated ID) where the
similarity is believed to be "sparse:'That is, any two arbitrarily selected documents are very likely to
have similarity O. Design an algorithm that returns a list of pairs of document IDs and the associated

similarity.

Print only the pairs with similarity greater than 0. Empty documents should not be printed at all. For
simplicity, you may assume each document is represented as an array of distinct integers.

public class Document {
private ArrayList<Integer> words;
private int docId;
}

https://github.com/careercup/CtCI-6th-Edition/blob/master/Java/Ch%2017.%20Hard/Q17_26_Sparse_Similarity/QuestionB.java
public static HashMap<DocPair, Double> computeSimilarities(HashMap<Integer, Document> documents) {
HashMapList<Integer, Integer> wordToDocs = groupWords(documents);
HashMap<DocPair, Double> similarities = computeIntersections(wordToDocs);
return similarities;
}

/* Create hash table from each word to where it appears. */
public static HashMapList<Integer, Integer> groupWords(HashMap<Integer, Document> documents) {
HashMapList<Integer, Integer> wordToDocs = new HashMapList<Integer, Integer>();

for (Document doc : documents.values()) {
ArrayList<Integer> words = doc.getWords();
for (int word : words) {
wordToDocs.put(word, doc.getId());
}
}

return wordToDocs;
}

/* Compute intersections of documents. Iterate through each list of
* documents and then each pair within that list, incrementing the
* intersection of each page. */
public static HashMap<DocPair, Double> computeIntersections(HashMapList<Integer, Integer> wordToDocs) {
HashMap<DocPair, Double> similarities = new HashMap<DocPair, Double>();
Set<Integer> words = wordToDocs.keySet();
for (int word : words) {
ArrayList<Integer> docs = wordToDocs.get(word);
Collections.sort(docs);
for (int i = 0; i < docs.size(); i++) {
for (int j = i + 1; j < docs.size(); j++) {
increment(similarities, docs.get(i), docs.get(j));
}
}
}

return similarities;
}

/* Increment the intersection size of each document pair. */
public static void increment(HashMap<DocPair, Double> similarities, int doc1, int doc2) {
DocPair pair = new DocPair(doc1, doc2);
if (!similarities.containsKey(pair)) {
similarities.put(pair, 1.0);
} else {
similarities.put(pair, similarities.get(pair) + 1);
}
}

/* Adjust the intersection value to become the similarity. */
public static void adjustToSimilarities(HashMap<Integer, Document> documents, HashMap<DocPair, Double> similarities) {
for (Entry<DocPair, Double> entry : similarities.entrySet()) {
DocPair pair = entry.getKey();
Double intersection = entry.getValue();
Document doc1 = documents.get(pair.doc1);
Document doc2 = documents.get(pair.doc2);
double union = (double) doc1.size() + doc2.size() - intersection;
entry.setValue(intersection / union);
}
}

public static HashMap<DocPair, Double> computeSimilarities(HashMap<Integer, Document> documents) {
ArrayList<Document> docs = new ArrayList<Document>();
for (Document doc : documents.values()) {
}
return computeSimilarities(docs);
}

public static HashMap<DocPair, Double> computeSimilarities(ArrayList<Document> documents) {
HashMap<DocPair, Double> similarities = new HashMap<DocPair, Double>();
for (int i = 0; i < documents.size(); i++) {
for (int j = i + 1; j < documents.size(); j++) {
Document doc1 = documents.get(i);
Document doc2 = documents.get(j);
double sim = computeSimilarity(doc1, doc2);
if (sim > 0) {
DocPair pair = new DocPair(doc1.getId(), doc2.getId());
similarities.put(pair, sim);
}
}
}
return similarities;
}

public static double computeSimilarity(Document doc1, Document doc2) {
int intersection = 0;
HashSet<Integer> set1 = new HashSet<Integer>();

for (int word : doc2.getWords()) {
if (set1.contains(word)) {
intersection++;
}
}

double union = doc1.size() + doc2.size() - intersection;

return intersection / union;
}
https://github.com/careercup/CtCI-6th-Edition/blob/master/Java/Ch%2017.%20Hard/Q17_26_Sparse_Similarity/QuestionC.java
public static class Element implements Comparable<Element> {
public int word;
public int document;
public Element(int w, int d) {
word = w;
document = d;
}
public int compareTo(Element e) {
if (word == e.word) {
return document - e.document;
}
return word - e.word;
}
}

public static HashMap<DocPair, Double> computeSimilarities(HashMap<Integer, Document> documents) {
ArrayList<Element> elements = sortWords(documents);
HashMap<DocPair, Double> similarities = computeIntersections(elements);
return similarities;
}

/* Throw all words into one list, sorting by the word then the document. */
public static ArrayList<Element> sortWords(HashMap<Integer, Document> docs) {
ArrayList<Element> elements = new ArrayList<Element>();
for (Document doc : docs.values()) {
ArrayList<Integer> words = doc.getWords();
for (int word : words) {
}
}
Collections.sort(elements);
return elements;
}

/* Increment the intersection size of each document pair. */
public static void increment(HashMap<DocPair, Double> similarities, int doc1, int doc2) {
DocPair pair = new DocPair(doc1, doc2);
if (!similarities.containsKey(pair)) {
similarities.put(pair, 1.0);
} else {
similarities.put(pair, similarities.get(pair) + 1);
}
}

/* Adjust the intersection value to become the similarity. */
public static HashMap<DocPair, Double> computeIntersections(ArrayList<Element> elements) {
HashMap<DocPair, Double> similarities = new HashMap<DocPair, Double>();
for (int i = 0; i < elements.size(); i++) {
Element left = elements.get(i);
for (int j = i + 1; j < elements.size(); j++) {
Element right = elements.get(j);
if (left.word != right.word) {
break;
}
increment(similarities, left.document, right.document);
}
}
return similarities;
}

/* Adjust the intersection value to become the similarity. */
public static void adjustToSimilarities(HashMap<Integer, Document> documents, HashMap<DocPair, Double> similarities) {
for (Entry<DocPair, Double> entry : similarities.entrySet()) {
DocPair pair = entry.getKey();
Double intersection = entry.getValue();
Document doc1 = documents.get(pair.doc1);
Document doc2 = documents.get(pair.doc2);
double union = (double) doc1.size() + doc2.size() - intersection;
entry.setValue(intersection / union);
}
}