【问题标题】:Pick all subgraphs by a specific pattern from a graph从图中按特定模式选择所有子图
【发布时间】:2013-04-18 10:17:30
【问题描述】:

所以我很期待从图表中选择一个三角形/正方形/../六边形。

这是什么意思:

input from keybord: a-b b-c c-a

output m-n-o, x-y-z, s-t-u
(其中每个子图都尊重顶点的关系模式)

如何解决这个问题:它必须是原始版本,没有优化或其他东西,但没有回溯/递归。

解决方案:将顶点转置为矩阵并在 for 循环中进行组合。

我遇到的问题:例如,如果我希望我的图表接受最多 octogns,我需要为 in for's 制作 8 吗?!

【问题讨论】:

    标签: graph subgraph


    【解决方案1】:

    自己的解决方案不是最好的,但足以完成休闲。用组合学 jar 完成。有帮助的洞

        @SuppressWarnings("unchecked")
        public static void drawMatrix(Graph g, int tempMatrixSize){
            System.out.println(" ");
            System.out.println("Matrix:");
            ArrayList<Node> nodesSet = new ArrayList<>();
            nodesSet = (ArrayList<Node>) g.getNodes().clone();
    
            int size = nodesSet.size();
    
            int[][] matrix = new int[size][size];
    
            System.out.print("  ");
            for (Node node : nodesSet){
                System.out.print(" " + node.getName()+ " ");
            }
            System.out.println();
    
    
            for (int i=0; i<size; i++) {
                System.out.print(nodesSet.get(i).getName()+ " ");
                for (int j=0; j<size; j++){
                    if (i == j) {
                        System.out.print(" 1 ");
                        matrix[i][j] = 1;
    
                    }
                    else{
                        if (nodesSet.get(i).isFriend(nodesSet.get(j))) {
                            System.out.print(" X ");
                            matrix[i][j] = 1;
                        }
                        else {
                            System.out.print(" 0 ");
                            matrix[i][j] = 0;
                        }
                    }
                }
                System.out.println();
            }
            System.out.println();
    
            // temp matrix
            int[][] tempMatrix = new int[tempMatrixSize][tempMatrixSize];
    
            // Find combinations
            ArrayList<Integer> al= new ArrayList<>();
            for (int i = 0; i<size; i++ ){
                al.add(i);
            }
    
            ICombinatoricsVector<Integer> initialVector = Factory.createVector(al);
            Generator<Integer> gen = Factory.createSimpleCombinationGenerator(initialVector, tempMatrixSize);
            int index = 0;
            for (ICombinatoricsVector<Integer> combination : gen) {
                boolean isConnected = true;
                System.out.println(combination);
                List<Integer> comb = combination.getVector();
                for(int i=0; i<tempMatrixSize; i++){
                    for(int j=0; j<tempMatrixSize; j++){
                        tempMatrix[i][j] = matrix[comb.get(i)][comb.get(j)];
    //                  System.out.print(tempMatrix[i][j]+ " ");
                    }
                    System.out.println();
    
    
                }
                // main matrix coordinations
                System.out.println("main matrix used coords: ");
                for(int i=0; i<tempMatrixSize; i++){
                    for(int j=0; j<tempMatrixSize; j++){
                        tempMatrix[i][j] = matrix[comb.get(i)][comb.get(j)];
                        System.out.print("["+comb.get(i)+","+comb.get(j)+"] ");
                    }
                    System.out.println();
                }
                System.out.println();
    
                for(int i=0; i<tempMatrixSize; i++){
                    for(int j=0; j<tempMatrixSize; j++){
                        if (tempMatrix[i][j] == 0){
                            isConnected = false;
                        }
                    }
                }
                if (isConnected) {
                    System.out.println("Is connected by >" + tempMatrixSize);
                    for (int i=0; i<tempMatrixSize; i++) {
                        System.out.println(" >" +nodesSet.get(comb.get(i)).getName());
                    }           
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-12
      • 2019-05-10
      • 1970-01-01
      • 1970-01-01
      • 2014-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多