Why are only 3 paragraphs appended? - d3.js

I am a beginner to d3. I read that when one binds a data set of $n$ entities to an element, calls enter, and then performs operations, those operations will be performed $n$ times.
However, here, my paragraph is only appended 3 times even though the size of my data set is 4:
http://jsfiddle.net/johnhoffman/tYr5U/
d3.select("body").data([1, 2, 3, 4]).enter().append("p").text("g");
Output:
g
g
g
Why just 3 times?

Here's the code I suspect you want to use.
d3.select("body").selectAll("p").data([1,2,3,4]).enter().append("p").text("g");
The join should be done with the "p" elements, not the "body" element.
As to why it has three in your example:
The data has four elements, being bound to the single "body" element. By default, the first element, 1, is bound to the existing body (defined in HTML). The remaining 3 elements are bound to non-existing "body" elements. Since "enter()" is only called for non-existing elements, the append operation gets called three times on the root of the DOM.
To demonstrate this, try:
d3.select("body").data([1,2,3,4]).enter().append("p").text(function(d) {return d;});
And you will see the number in the data being appended, instead of g.
Confusing, but the Circles Tutorial helped me understand this.

Related

How to apply Disjoint Set Union in the following algorithm?

(1 point) Suppose you have a set of N people who all do not know each other, and you have to process
a sequence of queries of the following kinds:
Type 1: MakeFriends(x,y) - x and y become friends with each other
Type 2: AreFriends(x,y) - output 1 if x and y are friends, and output 0 otherwise
In particular, for every pair of people, we need to maintain a state indicating whether they are friends
or not, and update this state based on queries of type 1, and report the state for queries of type 2.
Since you have learned about the disjoint sets data structures in your algorithms course, you decide to
store the set of N people as N singleton sets to begin with. For each query of type 1, you perform a
union operation, and for each query of type 2, you perform a find operation.
What can you say about this algorithm?
A. It may not always produce an accurate answer.
B. It works with O(lg∗n), O(lg∗n) amortized response time to both queries.
C. It works with O(n), O(n) worst-case response time to both queries.
D. The correctness of the approach depends on how DSU is implemented.
If we get the following sequence of queries: MakeFriends(1,2), MakeFriends(2,3), AreFriends(1,3), the answer to the last query with our approach (irrespective of how DSU is implemented)
will be 1, while the correct answer is 0.
Is the above explanation correct
Let's imagine two sets S1 and S2, where S1 contains elements 1, 2, 3, and S2 contains elements 4, 5, 6. These two sets don't have any elements in common, so they're disjoint.
Each set can be viewed as a tree that has a single root. Let's say the root of S1 is 1 and the root of S2 is 4.
Now if UNION(2, 5) is called, it will essentially merge set S1 with set S2. In other words, all the elements will now be part of a single set, and the root can be either 1 or 4 depending on the implementation. If FIND(1,6) is called afterwards, it will return true, as 1 and 6 now belongs to the same set, even though there weren't any calls like UNION(1,6) that explicitly asked to join 1 and 6.
If we get the following sequence of queries: MakeFriends(1,2), MakeFriends(2,3), AreFriends(1,3), the answer to the last query with our approach (irrespective of how DSU is implemented) will be 1, while the correct answer is 0.
Coming back to this statement, MakeFriends(1,2) will create a set that includes the elements 1, 2. Calling MakeFriends(2, 3) will include 3 in the same set. As a result, AreFriends(1,3) will return true, even though there weren't any explicit calls that joined 1 and 3, similar to the example mentioned above. So this statement is correct.
In the multiple choice questions, A. It may not always produce an accurate answer is correct.

Parse multiple array similarity query

I am working on an algorithm that will compare 2 objects, object 1 and object 2. Each object has attributes which are 5 different arrays, array A, B, C, D, and E.
In order for the two objects to be a match, at least one item from Object 1 A must be in Object 2 A AND Object 1 B must be in Object 2 B, etc through object E must be similar. With a higher number of matches in each array A-E, the higher of a score The match will produce.
Am I going to have to pull Object 1 and object 2 then do an n^2 complexity search on each array to determine which ones exist in both arrays? Then I would go about serving a score by how many matches there were in each array, then add them up and the total would give me the score.
I feel like there has to be a better option for this, especially for Parse.com
Maybe I am going about this problem all wrong, can someone PLEASE help me with this problem. I would provide some code for this one, but I have not started the code yet because I cannot wrap my head around the best way to design it. The two object database are in place already though.
Thanks!
As I said, I may be thinking of this problem in the wrong way. If I am unclear about anything that I am trying to do, let me know and I will update accordingly.
Simplest solution:
Copy all elements from some array object1 to hash table (unordered map), and thereafter iterate array in the 2nd object, and lookup presence in the map. Thus, time complexity is O(N).
Smart solution:
Keep elements in all objects not in the "naive arrays", but in the arrays, structured as hash tables with double hashing algorithm. If so, all arrays in an objects1, 2, already will be pre-indexed, and what is you needed - iterate array, contains less number of elements, and match elements vs longest pre-indexed array.

Geneal B+ Tree split logic

I just want to know if you would split a leaf node after the insert or before the insert. lets say our capacity in the leaf is 4 elements and we already have 3 elements in there. would you add the 4th element and immediately split after the insert so we have now two nodes holding 2 elements each. Or would you just add the 4th element so that the leaf is full. Now if you add the 5th element (which would cause an overflow) we do the split and add the element which would result in 2 leaf nodes one holding 2 and one holding 3 elements.
EDIT: Since I have seed both approaches out there in the www. I would like to know the reason when to choose solution 1 or 2. Or if one of them even is incorrect for some reason.
https://www.cs.usfca.edu/~galles/visualization/BPlusTree.html
This visualization is very useful to understand B+ tree logic.

How can I find the cells adjacent to a cell in a matrix in OCaml?

For example, I am given a list [3;5;6;2;10;4;9;1;3] that maps out to a matrix like this:
3 5 6
2 10 4
9 1 3
I have already coded a function "find_size" that will find the size of the matrix given a list (for this example, it would give me the int 3)
as well a function "find_position" that will tell me where in the matrix 10 is (so here, it would give me the tuple (1, 1) because it is in the second row and second column)
I think the best way to approach this would be to iterate over the list using a fold function that applies a given function to each element and keeps track of the results. Therefore, my goal is to create a function that will tell me if a certain element in the matrix is adjacent to 10 in OCaml (if it is, I would tell the fold function add that element to the answer list).
The final answer for this would be [5;2;4;1] because those elements are adjacent to 10.
You can fairly easily translate your size and position values into a list of the adjacent positions as tuples. You can translate the size and a tuple into a single list index. With a sorted list of indices, you can fold over the list and extract the elements at the given indices.
This would probably be a lot easier if you used an array of numbers, for what it's worth.
Without seeing some code you've tried, and hearing how it did or didn't work for you, it's difficult to say more than this.

Algorithm for finding circular mappings of maximal size

I have nine sets of 500 objects each. Although the sets are independent, I assume that the sets share a core of common objects. However, one and the same object may have a different name (index) depending on the set. But I can measure the pairwise distance between two objects.
Based on the pairwise distances, I already computed optimal mappings between objects of two sets for all pairs of sets. So, for every pair of sets, I can say the correspondence between any two objects.
Now I want to detect closed mapping circles, e.g. { 5 (set 1) -> 13 (set 2) -> 24 (set 3) -> 5 (set 1) }, i.e. object 5 of set 1 maps to object 13 of set 2, which maps to 24 in set 3, which then maps back to object 5 of set 1. I need this form of a circular mapping to argue that the objects are essentially the same.
Of course, in an ideal world, I could identify a majority of circles that span all nine sets. However, common objects between 3-9 sets are also interesting. Thus, I want an exhaustive listing.
Do you know an algorithm to perform this task, or how this problem is termed in combinatorial mathematics!?
As a heuristic approach, I would start by determining circles within all combinations of 3 sets and then combine these results for larger combinations of sets.
If I follow your description correctly, it seems you'll like to find correspondences between the sets. Will this algorithm work for you.
1. Intialize a hashmap H
2. Initialize key frequency map U = {}
3. for each set i
4. for each element e in set i
5. H.insert {e.key, {i, ...}}
6. if U.contain(e.key)
7. c = U.get(e.key)
8. U.update(e.key, c + 1)
9. else
10. U.insert(e.key, 1)
11. endif
12. endfor
13. endfor
Line 5 will insert an element into the map H. Elements with the same key are stored in a linked list. You can find the longest chain by finding the the key with the largest frequency in U. Then by doing H.get(key), you'll get back the list. By linking the last element to the first, you'll obtain the cycle you seek.
I hope this helps.

Resources