【问题标题】:Using DFS (depth first), find the density of blocks which density is over %X使用 DFS(深度优先),找到密度超过 %X 的块的密度
【发布时间】:2018-06-11 21:46:44
【问题描述】:

在我的工作(一些矩阵计算)中,我遇到了如下问题:

matrix (NxN):   
          xxxx------------------
          xxxx------------------
          ----xxx-x-------------
          ----xx--x-------------
          ---------x--x---------
          ---------xxxx---------
                   xxxx---------
            ......................

我有一个 2D adj 矩阵,它有块对角线的图案(如上所示)。每个块可以是密集的(100%)或稀疏的(0.01-5%)。使用这些 adj 矩阵并使用图搜索 (DFS),我如何找到块大小(begin_row、being_col、end_row、end_col)以及它们对应的密度(mod(E)/mod(V))?

我确信有一种简单的方法可以找到块和密度。我正在寻找任何想法或伪代码,非常感谢您抽出宝贵时间。

【问题讨论】:

  • 一个。 [dfs] 标签与深度优先搜索无关。请更改它。湾。 “block daigonals”对我来说不是很清楚。你能澄清一下吗?一如既往,榜样胜于言语。 C。可以连接块吗? d。密度如何发挥作用?
  • 您好,c0der,我正在尝试使用图形对矩阵进行分区。所以我打算使用 dfs 来遍历图形并找到块对角线。块对角线就像通过对角线元素连接的岛屿。这是它的图片:google.com/…
  • 是的,可以连接块。现在可以将每个块视为具有 V 和 E 的子图。这些子图也可以具有密度。
  • 现在,我正在考虑的任务是:1. 绘制该矩阵的块。 2. 求块或子图的密度
  • 你有什么想法可以帮忙吗?我没有找到任何好的方法来实现它。

标签: depth-first-search adjacency-matrix


【解决方案1】:

您可以横穿对角线来映射每个块的开始和结束位置。
横向可以通过dfs或简单循环来完成。
然后关键是检测块结束。如isCorner 方法所示,使用“完整”块非常简单。 必须修改此方法以解决漏洞。

    //test data
    public static int[][] intArray = {
        {1,  1,  0,  0,  0,  0,  0,  0,  0},
        {1,  1,  0,  0,  0,  0,  0,  0,  0},
        {0,  0,  1,  1,  1,  1,  0,  0,  0},
        {0,  0,  1,  1,  1,  1,  0,  0,  0},
        {0,  0,  1,  1,  1,  1,  0,  0,  0},
        {0,  0,  1,  1,  1,  1,  0,  0,  0},
        {0,  0,  0,  0,  0,  0,  1,  1,  1},
        {0,  0,  0,  0,  0,  0,  1,  1,  1},
        {0,  0,  0,  0,  0,  0,  1,  1,  1}
    };

    public static void main(String[] args) {

        mapBlock();
    }

    //traverse diagonal to find block start and block end
    private static void mapBlock() {
        //todo check that matrix is nxn

        int[] origin = {0,0}; //dfs start point
        //traverse diagonal
        for(int rowIndex =0; rowIndex < intArray.length ; rowIndex ++) {
            if(isCorner(rowIndex, rowIndex)) { //diagonal row and col index are equal
                int[] target = {rowIndex, rowIndex}; //dfs target
                block(origin, target);
                origin = new int[]{rowIndex+1, rowIndex+1};
            }
        }
    }

    //is intArray[row Index][column Index] a corner 
    private static boolean isCorner(int rowIndex, int colIndex) {

        //corner must be on diagonal
        if(rowIndex != colIndex ) {
            return false;
        }

        //if last row and col it is a corner
        if(((rowIndex+1) == intArray.length) && ((colIndex+1) == intArray.length))  {
            return true;
        }

        //if left and bottom neighbors are empty - it is a corner
        //todo if you blocks have holes this criteria needs to change
        if((intArray[rowIndex+1][colIndex] == 0) && (intArray[rowIndex][colIndex+1] == 0) ) {
            return true;
        }

        return false;
    }

    private static void block(int[] origin, int[] target) {

        System.out.println("block from " +Arrays.toString(origin)+
                " to "+Arrays.toString(target));
        //todo store sub array 
    }

输出:

从 [0, 0] 到 [1, 1] 的块
从 [2, 2] 到 [5, 5] 的块
从 [6, 6] 到 [8, 8] 的块

(测试一下online

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-16
    • 2017-03-04
    • 2010-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多