有没有更好的方法来分析输入/输出数组之间的路径作为示例?

时间:2014-03-04 15:59:26

标签: java algorithm recursion

下图所示的示例是我目前正在进行的信号路径分析的简化版本。对于输入数组中的每个索引( iArray ),输出( oArray )完全在同一索引处。正如您在结果中看到的那样,递归调用将具有重复的分析路径,这些路径并不重要(如结果中所示)。问题是,是否有更好的(即更快)的分析解决方案?

代码:

import java.util.*;
import java.lang.*;
import java.io.*;

class Main {

    private static String[] iArray = new String[]{
        "a","a","a","b","b","b","b","b","c","c","c","c",
          "d","d","d","d","e","e","e","e","f","f","g","g","g","g",
            "h","h","k","l","l","m","n","n","n","n","n","o","o",
              "s","s","s","s","s","s"};

    private static String[] oArray = new String[]{
        "b","c","d","e","f","g","h","i","d","f","h","l","j","a","n",
          "s","a","f","g","i","s","b","b","n","a","c","i","j","e","s",
            "b","b","o","d","s","a","b","b","s","a","j","l","o","n","k"};

    public static void recPathAnalysis(String signal, int level, String path) {
        if (level < 10) {
            for (int i = 0; i < iArray.length; i++) {
                if (signal.equals(iArray[i])) {
                    if (!path.contains(oArray[i])) {
                        System.out.println("normal >> " + path + " " + oArray[i]);              
                        recPathAnalysis(oArray[i], level + 1 ,  path + " " + oArray[i]);
                    } else {
                        System.out.println("loop   >> " + path + " " + oArray[i]);              
                    }
                }
            }
        }
}

    public static void main (String[] args) {
        recPathAnalysis("a", 0, "a");
    }
}

结果:

    normal >> a b            **not needed**
    normal >> a b e          **not needed**
    loop   >> a b e a
    normal >> a b e f        **not needed**
    normal >> a b e f s      **not needed** 
    loop   >> a b e f s a
    normal >> a b e f s j
    normal >> a b e f s l    **not needed**
    loop   >> a b e f s l s
    loop   >> a b e f s l b
    normal >> a b e f s o    **not needed**
    loop   >> a b e f s o b
    loop   >> a b e f s o s
    normal >> a b e f s n    **not needed**
    normal >> a b e f s n o
    ....

2 个答案:

答案 0 :(得分:1)

可以在此处使用的数据结构中进行的一项小改进是,可以使用public static void recPathAnalysis(String signal, int level, String path)代替public static void recPathAnalysis(String signal, int level, HashSet<Character> path),而不是if (!path.contains(oArray[i]))。由于使用{{1}},复杂度为O(n),但如果与HashSet相同,则为O(1)。可以实现轻微的性能提升。

答案 1 :(得分:0)

我不确定我理解你的算法是什么,但我的一般建议是在你进行递归调用时不打印。

相反,你可以尝试解决这个问题:

    if (level < 10) {
        for (int i = 0; i < iArray.length; i++) {
            if (signal.equals(iArray[i])) {
                if (!path.contains(oArray[i])) {            
                    recPathAnalysis(oArray[i], level + 1 ,  path + " " + oArray[i]);
                } else {
                    System.out.println("loop   >> " + path + " " + oArray[i]);              
                }
            } else { // stop condition
                System.out.println("normal >> " + path + " " + oArray[i - 1]);
            }
        }
    } else { // stop condition
      System.out.println("normal >> " + path + " " + oArray[i - 1]);
    }

对其他停止条件也这样做。