home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / programm / 2578 < prev    next >
Encoding:
Text File  |  1992-09-08  |  5.3 KB  |  111 lines

  1. Newsgroups: comp.programming
  2. Path: sparky!uunet!email!news
  3. From: rainer@ruble.fml.tuwien.ac.at (Rainer Staringer)
  4. Subject: Re: How to find vectors with minimum Hamming-distance?
  5. Message-ID: <1992Sep7.071227.11596@email.tuwien.ac.at>
  6. Sender: news@email.tuwien.ac.at
  7. Nntp-Posting-Host: ruble.fml.tuwien.ac.at
  8. Reply-To: rainer@eimoni.tuwien.ac.at
  9. Organization: Technical University of Vienna
  10. References: <9209042121.AA00144@news.cis.ohio-state.edu>
  11. Date: Mon, 7 Sep 1992 07:12:27 GMT
  12. Lines: 97
  13.  
  14. In article <9209042121.AA00144@news.cis.ohio-state.edu>  
  15. Marc.Ringuette@daisy.learning.cs.cmu.edu writes:
  16. > rainer@ruble.fml.tuwien.ac.at (Rainer Staringer) writes,
  17. > > I stumbled over the following problem recently, and I wonder if there
  18. > > is a clever algorithm for it: I have a set of n m-dimensional vectors
  19. > > (vector elements are from a finite set, but not necessarily binary),
  20. > > and I want to find the 2 vectors with the minimum Hamming-distance
  21. > > (i.e. those which have the most elements in common). Is there any
  22. > > way you can find them using less than O(n^2) vector comparisons?
  23. > > I know there is a clever algorithm for the equivalent problem with
  24. > > Euclidean metric, but I could not find any for the Hamming-metric
  25. > > variant.
  26. > Good problem!  Some friends and I tried to come up with something
  27. > better, but couldn't.  We were thinking along the lines of geometric
  28. > algorithms (but couldn't manage to extend a 2-d closest-pair-of-points
  29. > algorithm) or something based on sorting or divide-and-conquer.  I like
  30. > the other poster's n*log(n)*m! answer -- deliciously crazy and good for
  31. > minuscule m.
  32.  
  33. Taking closest-pair and modifying it for the Hamming-metric was my first
  34. idea too, but it doesnt't work. As for m!*n*log(n) -- m is an order of
  35. magnitude smaller than n in my situation, but could still range up to
  36. 10 or 20, so I will not use this method if I can avoid it :-)
  37.  
  38. > If you expect the distribution to be skewed, with a small number of
  39. > very well matched pairs of vectors, then you might adopt a method with
  40. > better average-case behavior at the expense of worst-case running time.
  41. > You could use a hashing technique to see if you might luck out and find
  42. > two duplicate vectors.
  43.  
  44. I don't expect the case with two duplicates to be very common in my special
  45. application, but this might be a useful optimization for the general case.
  46.  
  47. > With an extra log n factor, you can compare the n^2 candidate
  48. > vector-pairs in a best-first fashion, comparing elements in each pair
  49. > only until you have seen more mismatches than the best so far.  This
  50. > means that if b is the number of total mismatches for the best pair,
  51. > p_ij is the number of elements in the largest prefix of vectors i and j
  52. > which has fewer than b mismatches, and P is the sum of all p_ij, then
  53. > the algorithm runs in time O(P log n), where n*b+m <= P <= n^2*m.
  54. > ----
  55. > Other ideas, anyone?  If not, how about a lower bound on the worst case 
  56. > running time?
  57.  
  58. Andy Lowry (lowry@watson.ibm.com) pointed out in email that if a, b, and c
  59. are three of my m-dimensional vectors, then h (a, c) >= |h (a, b) - h (b, c)|.
  60. Nifty proof of this claim follows:
  61.  
  62. > Pf (A. Lowry): For each component i there are three cases:
  63. >   1. a[i] = b[i] = c[i].  In this case, a[i] = c[i].
  64. >   2. a[i] <> b[i] <> c[i].  In this case we may or may not have a[i] =
  65. >      c[i].
  66. >   3. a[i] = b[i] <> c[i] or a[i] <> b[i] = c[i].  In this case, a[i]
  67. >      <> c[i].
  68. > Let n1(a,b,c), n2(a,b,c), and n3(a,b,c) be the number of instances of
  69. > case 1, 2, and 3, respectively, in comparing vectors a, b, and c.  And
  70. > let k=h(a,b) and j=h(b,c).
  71. > The minimum possible value of h(a,c) occurs in situations in which
  72. > case 3 is minimized and all instances of case 2 do indeed have a[i] =
  73. > c[i].  And in such an instance, of course, h(a,c) = n3(a,b,c).  But
  74. > n3(a,b,c) can never be less than |h(a,b)-h(b,c)| = |k - j|.  For
  75. > suppose otherwise, and wlog suppose k >= j.  And let n3a(a,b,c) and
  76. > n3c(a,b,c) count the instances of case 3 in which a[i] = b[i] or b[i]
  77. > = c[i], respectively (so n3(a,b,c) = n3a(a,b,c)+n3c(a,b,c)).
  78. > We have k = n2(a,b,c)+n3c(a,b,c) and j = n2(a,b,c)+n3a(a,b,c).  So
  79. > 0 <= k-j = n3c(a,b,c)-n3a(a,b,c) <= n3(a,b,c), which contradicts our
  80. > assumption that n3(a,b,c) < |k-j|.  This completes the proof of the
  81. > claim.
  82.  
  83. Making use of this fact you can prune away a lot of the n(n-1)/2 comparisons.
  84. I imagine a possible algorithm might look like this: Compare the first vector
  85. x_1 to all others and sort the remaining vectors according to their Hamming
  86. distances from x_1 (because these are integers between 0 and m you could even
  87. use distribution counting and do this in linear time).
  88.  
  89. Now, because you need only compare each vector x_i to those vectors x_j where
  90. |h (x_i, x_1) - h (x_1, x_j)| < b (the min distance found so far), you can
  91. easily find smaller subdivisions of the set of remaining vectors to which the
  92. same algorithm can be applied recursively -- these subdivisions can overlap,
  93. though, so you can't do the sorting in place... Hmm, maybe there is a simpler
  94. way?
  95.  
  96. Anybody care to analyse the running time of this? :-)
  97.  
  98. > [ Marc Ringuette | Cranberry Melon University, Cucumber Science Department  ]
  99. > [ mnr@cs.cmu.edu | 412-268-3728 |      ".surivorter erutangis a ma I"       ]
  100.  
  101. --
  102. Rainer Staringer                   | rainer@fml.tuwien.ac.at
  103. Financial Markets Lab, TU Vienna   | +43 (1) 58801/8138
  104.