User:Realazthat/Notes/Algorithms/Set-intersection: Difference between revisions

From Rosetta Code
Content added Content deleted
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Set Difference Not Empty ==
== Set Difference Not Empty ==


Complexity: O(1) if |A| > |B|, O(|A|) otherwise; assuming O(1) for membership testing.

<pre>
<pre>
#Set Difference Not Empty
#Set Difference Not Empty
Line 15: Line 15:
</pre>
</pre>


Complexity: O(1) if |A| > |B|, O(|B|) otherwise.
<pre>
<pre>
#Set Difference Not Empty
#Set Difference Not Empty
Line 40: Line 41:
D = |A| < |B| ? B : A
D = |A| < |B| ? B : A
for ( c in B )
for ( c in C )
if ( c in D )
if ( c in D )
return true
return true

Latest revision as of 20:13, 21 January 2011

Set Difference Not Empty

Complexity: O(1) if |A| > |B|, O(|A|) otherwise; assuming O(1) for membership testing.

#Set Difference Not Empty
DNE(A,B):
  if |A| > |B|
    return true
  
  for ( a in A )
    if ( a not in B )
      return true
  
  return false

Complexity: O(1) if |A| > |B|, O(|B|) otherwise.

#Set Difference Not Empty
DNE(A,B):
  if |A| > |B|
    return true
  
  for ( b in B )
    remove b from A
    remove b from B
    if |A| > |B|
      return true
  
  return false


Set Intersection Not Empty

assuming set membership testing is constant time.

#Set Intersection Not Empty
INE(A,B):
  C = |A| < |B| ? A : B
  D = |A| < |B| ? B : A
  
  for ( c in C )
    if ( c in D )
       return true
  
  return false