【问题标题】:How to effectively match on multiple values如何有效匹配多个值
【发布时间】:2014-04-16 03:24:01
【问题描述】:

我正在尝试提出一个匹配 3 个属性的匹配算法,但我想不出一个有效的解决方案。这是我的算法的伪代码

该算法执行以下操作:

  • 检查第一个条件是否匹配。
  • 如果第一个条件匹配,我会尝试根据其他 2 个条件缩小匹配范围 标准。
  • 如果第一个条件不匹配,则尝试 根据第二个条件进行匹配。
  • 如果在 第二个标准,然后我尝试根据 最后一个标准。
  • 等等……

这个算法似乎只是在重复自己,如果我添加另一个值来匹配,那么这个算法会很快变得非常大。

//try to match on criteria 1
if results exist for match on criteria 1 {
    //try to match on criteria 1 & 2
    if results exist for match on criteria 1 & 2 {
        //try to match on criteria 3
        if result exist for match on criteria 1,2,3 {
            return results for match on 1,2,3
        }
        else
            return results for match on 1,2
    }
    else
        return results for match on 1
}
//try to match on criteria 2
else if results exist for match on criteria 2 {
    //try to match on criteria 2 & 3
    if result exist for match on criteria 2,3 {
        return results for match on 2,3
    }
    else
        return results for match on 2
}
//try to match on criteria 3
else if results exist for match on criteria 3 {
    return results for match on 3
}
else {
    no match
}

有没有更好的方法来做到这一点?好像

【问题讨论】:

  • @MitchWheat 如果我合并结果,那么我将无法获得尽可能缩小的结果。我会得到一个包含紧密匹配和窄匹配的大型结果集。该算法试图获得最接近的匹配。
  • 如果列表具有验证条件 X 的值,是否向列表中添加新值会使条件 X 无效?

标签: algorithm search pattern-matching computer-science


【解决方案1】:

如果我理解正确,这里是 Javascript 中的一个实现。

function filter(arr, condition) {
  var retval = [];
  for (i in arr) if (condition(arr[i])) retval.push(arr[i]);
  return retval;
}

function first_match(arr, conditions) {
  if (conditions.length == 0) return("No match");
  var initial_arr = Array.prototype.slice.call(arr);
  var prev_arr;
  var i = 0;
  for (; i < conditions.length && arr.length != 0; i++) {
    prev_arr = Array.prototype.slice.call(arr);
    arr = filter(arr, conditions[i]);
  }
  if (i <= 1 && arr.length == 0) return first_match(initial_arr, Array.prototype.slice.call(conditions, 1));
  else if (i == conditions.length && arr.length != 0) return arr;
  else return prev_arr;
}

示例

var conditions = [function(x) { return x > 3; },
                  function(x) { return x % 2 == 0; },
                  function(x) { return Math.sqrt(x) % 1 == 0; }];

var example1 = [1,2,3,4,5,6,7,8,9,10];
console.log(first_match(example1, conditions)); // [4] - all three hold
var example2 = [1,2,3,5,6,7,8,9,10];
console.log(first_match(example2, conditions)); // [6, 8, 10] - First two hold
var example3 = [5, 7, 9];
console.log(first_match(example3, conditions)); // [5, 7, 9] - First one holds
var example4 = [-2, 0, 2];
console.log(first_match(example4, conditions)); // [0] - 2 & 3 hold
var example5 = [-2, 2];
console.log(first_match(example5, conditions)); // [-2, 2] - Only 2 holds
var example6 = [1];
console.log(first_match(example6, conditions)); // [1] - Last one holds
var example7 = [-1];
console.log(first_match(example7, conditions)); // "No match" - None hold

【讨论】:

  • 大小为 n 的输入列表的时间复杂度是多少?
  • O(n * c^2),其中n 是数组的长度,c 是条件的长度。您不能做得更好,因为在最坏的情况下您必须至少应用过滤器n * c(c-1)/2 次。
猜你喜欢
  • 2019-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-22
  • 2021-11-11
  • 1970-01-01
相关资源
最近更新 更多