繁体   English   中英

如何计算图中红色和白色节点之间的边数?

[英]How to count number of edges between red and white nodes in a graph?

我想计算白色节点和红色节点之间有多少条边。 红色节点是 main 方法中 new int[] 中的节点。

提供的作业代码:

import edu.princeton.cs.algs4.Graph;

public class RedWhite {
    public static int count(Graph G, int[] rednodes) {
        int rw_count = 0;

        // Count how many of the edges in G connect a white node (one that
        // isn't in rednodes) with a red node (one that is in rednodes).

        return rw_count;
    }    
}

import edu.princeton.cs.algs4.Graph;

public class TestRW {
    public static void main(String[] args) {
        Graph G = new Graph(13);
        G.addEdge(0, 1);
        G.addEdge(3, 2);
        G.addEdge(3, 5);
        G.addEdge(4, 3);
        G.addEdge(4, 2);
        G.addEdge(6, 9);
        G.addEdge(8, 7);
        G.addEdge(8, 9);
        G.addEdge(9, 11);
        G.addEdge(10, 12);
        G.addEdge(11, 12);
        G.addEdge(12, 9);

        System.out.println(RedWhite.count(G, new int[] { 3 }));                                        // should print 3
        System.out.println(RedWhite.count(G, new int[] { 3, 4, 2 }));                                  // should print 1
        System.out.println(RedWhite.count(G, new int[] { 3, 4, 2, 11, 12 }));                          // should print 4
        System.out.println(RedWhite.count(G, new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 })); // should print 0
        System.out.println(RedWhite.count(G, new int[] {  }));                                         // should print 0
    }
}

我在遍历图形时遇到问题,我不知道如何使用 rednodes[] 与图形进行比较。 在下面运行我尝试的代码时,我在标记和计数方法中遇到错误。

任何帮助表示赞赏。

import edu.princeton.cs.algs4.Graph;

public class RedWhite {
    private static boolean[] marked;

    public RedWhite(Graph G, int s) {
        marked = new boolean[G.V()];
        dfs(G, s);
    }

    public static int count(Graph G, int[] rednodes) {
        int rw_count = 0;

        for (int w : G.adj(rednodes.length))
        {
            if (marked(w)) {
                rw_count++;
            }
        }

        // Count how many of the edges in G connect a white node (one that
        // isn't in rednodes) with a red node (one that is in rednodes).

        return rw_count;
    }
   
    private void dfs(Graph G, int v) {
        marked[v] = true;
        for (int w : G.adj(v)) {
            if (!marked[w]) {
                dfs(G, w);
            }
        }
    }

    public static boolean marked(int v) {
        return marked[v];
    }

    public static void main(String[] args) {
        Graph G = new Graph(13);
    G.addEdge(0, 1);
    G.addEdge(3, 2);
    G.addEdge(3, 5);
    G.addEdge(4, 3);
    G.addEdge(4, 2);
    G.addEdge(6, 9);
    G.addEdge(8, 7);
    G.addEdge(8, 9);
    G.addEdge(9, 11);
    G.addEdge(10, 12);
    G.addEdge(11, 12);
    G.addEdge(12, 9);

    System.out.println(RedWhite.count(G, new int[] { 3 }));                                        // should print 3
    System.out.println(RedWhite.count(G, new int[] { 3, 4, 2 }));                                  // should print 1
    System.out.println(RedWhite.count(G, new int[] { 3, 4, 2, 11, 12 }));                          // should print 4
    System.out.println(RedWhite.count(G, new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 })); // should print 0
    System.out.println(RedWhite.count(G, new int[] {  }));                                         // should print 0
    }
}
  • 代码问题:

你会得到一个错误,因为marked没有初始化,因为count是一个 static 方法,它不需要创建RedWhite的新实例。

您可以做的是从count方法定义中删除static 也不要声明markedstatic

在您的主要方法中,执行: RedWhite rw = new RedWhite(G, s) and rw.count(G, new int[]{})

  • 解决方案:

现在谈到解决方案,我不确定您为什么要使用 DFS。 您已经定义了图形,您只需要查看每个红色节点的邻接列表并进行计数即可。

为了更高效,对红色节点进行排序,对排序后的红色节点中的每个相邻节点进行二分查找,如果没有找到则为白色节点。 然后增加计数。 对所有 rednode 执行此操作,您将获得最终计数。

代码是:

    private int countEdges(Graph G, int[] rednodes) {
        int count = 0;
        Arrays.sort(rednodes);
        for ( int rednode : rednodes ) {
            for ( int adj : G.adj(rednode) ) {
                if ( Arrays.binarySearch(rednodes, adj) < 0 ) {
                    count++;
                }
            }
            
        }
        return count;
    }

公平地说,我在 C++ 中编码,而不是 java,但这里有一个问题:为什么你的marked() function 返回marked数组的值? 直接使用那个数组不是更有意义吗?

目前我可以告诉您正在使用 DFS 正确遍历图形。 但是,我不确定您为什么要遍历图表。

对不起,但这将是其中一个“不要这样做,而是那样做”的答案。

如果您正在寻找线性解决方案(对于一次执行的计数方法),您最好只创建一个数组,其中每个节点都包含有关它是白色还是红色的信息。 我建议用零填充它,然后遍历rednodes数组以将适当的值更改为1

接下来,您将结果变量声明为零,它将计算连接到不同颜色节点的边数。 您遍历所有边,并检查它们连接的顶点是否具有不同的颜色。 如果是这样,增加结果变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM