View Single Post
  #29  
Old 11-29-2007, 08:47 AM
Kristi Kristi is offline
Bowie's Deciple
Kristi's Avatar
Join Date: Dec 2003
Location: Boston, MA
Posts: 748
Kristi has a spectacular aura aboutKristi has a spectacular aura about
Send a message via AIM to Kristi Send a message via MSN to Kristi
Quote:
Originally Posted by Tolnaftate2004 View Post
A good point: O(N1*N2 + N!/((N-3)!*3!))

I had originally intended to do something along these lines:
PHP Code:
  for (elmttemp.c[0]) {
    if (
elmt in temp.c[1])
      
temp.intersect.add(elmt);
  } 
becomes
PHP Code:
  while (temp.c[0].size()) {
    
temp.elmt temp.c[0][0];
    if (
temp.elmt in temp.c[1]) {
      
temp.intersect.add(temp.elmt);
      
temp.c[0].remove(elmt);
      
temp.c[1].remove(elmt);
    } else 
temp.c[0].delete(0);
  } 
Still O(N1*N2), but often less.
It just slipped my mind. Silly me.
Also, first thing should be if (arrA.size() > 2 && arrB.size() > 2)...

As for the naive algorithm, It has O(N2^3*N1!/((N1-3)!*3!)) (?), I think.
Deleting elements from the array and etc is actually going to make it take longer to process.

Anyway, hashes would optimize finding the intersect, but graal has no support for it. However, we can dynamically name variables, so we can emulate it well enough for this situation.

code snippet:
PHP Code:
for(etemp.array1)
   
temp.b.("v"@e) = true;

for(
etemp.array2)
   if(
temp.b.("v"@e))
     
temp.intersect.add(e); 
with larger datasets, this gets exponentially faster. with 5000-10000 element arrays, its literally 120x faster then searching through the arrays for intersects.

Also, it would be slightly faster to make an array with the max possible size first, so you're not resizing the array (and thus possibly moving it to a new location in memory constantly.)
PHP Code:
  temp.intersect = new[smallerarray.size()];

  for(
etemp.largerarray)
   
temp.b.("v"@e) = true;

  for(
esmallerarray)
   if(
temp.b.("v"@e)) {
     
temp.intersect[temp.j] = e;
     
temp.j++;
   } 
This does in fact improve performance, although it's not terribly noticeable.

I'll optimize finding the sets from the intersects later. Lets let this soak in first.
__________________

Last edited by Kristi; 11-29-2007 at 09:08 PM..
Reply With Quote