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

huangapple go评论86阅读模式
英文:

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

问题

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

提供的作业代码:

import edu.princeton.cs.algs4.Graph;

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

        // 统计在G中有多少边连接一个白色节点(不在rednodes中的节点)和一个红色节点(在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 }));                                        // 应该输出 3
        System.out.println(RedWhite.count(G, new int[] { 3, 4, 2 }));                                  // 应该输出 1
        System.out.println(RedWhite.count(G, new int[] { 3, 4, 2, 11, 12 }));                          // 应该输出 4
        System.out.println(RedWhite.count(G, new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 })); // 应该输出 0
        System.out.println(RedWhite.count(G, new int[] {  }));                                         // 应该输出 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++;
            }
        }

        // 统计在G中有多少边连接一个白色节点(不在rednodes中的节点)和一个红色节点(在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 }));                                        // 应该输出 3
        System.out.println(RedWhite.count(G, new int[] { 3, 4, 2 }));                                  // 应该输出 1
        System.out.println(RedWhite.count(G, new int[] { 3, 4, 2, 11, 12 }));                          // 应该输出 4
        System.out.println(RedWhite.count(G, new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 })); // 应该输出 0
        System.out.println(RedWhite.count(G, new int[] {  }));                                         // 应该输出 0
    }
}
英文:

I would like to count how many edges there are that goes between a white and a red node. The red nodes are the ones in the new int[] in the main method.

Provided code for the assignment:

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;
}    
}

and

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
}
}

I am having trouble traversing the graph and I don't know how to use rednodes[] to compare to the graph. When running my attempted code below I get errors in the marked and count methods.

Any help is appreciated.

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
}
}

答案1

得分: 1

  • 代码问题:

由于marked未初始化,你将会得到一个错误,因为count是一个静态方法,不需要创建RedWhite的新实例。

你可以做的是从count方法的定义中移除static关键字。同时,不要将marked声明为static

在你的主方法中,进行以下操作:RedWhite rw = new RedWhite(G, s),然后调用rw.count(G, new int[]{})

  • 解决方案:

现在来看解决方案,我不确定你为什么要使用深度优先搜索(DFS)。你已经定义了图,你只需要查看每个红色节点的邻接列表并计数。

为了更有效率,对红色节点进行排序,然后对排序后的红色节点进行二分搜索,如果邻接节点不在排序后的红色节点中,则它是一个白色节点。然后增加计数。对所有红色节点执行此操作,你将得到最终的计数。

代码如下:

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;
}
英文:
  • Code issues:

You will get an error because marked is not initialized because count is a static method and it don't need to create a new instance of RedWhite.

What you can do is remove static from count method definition. Also don't declare marked as static.

And in your main method, do : RedWhite rw = new RedWhite(G, s) and rw.count(G, new int[]{}).

  • Solution:

Now coming to the solution, I m not sure why you are going for DFS. You have the graph defined, all you need is to look at the adjacency list of each red node and count.

To be more efficient, sort the red nodes and do a binary search for each adjacent node in the sorted rednodes, if it is not found it is a white node. Then increment the count. Do this for all rednodes you will get the final count.

Code is :

    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) &lt; 0 ) {
count++;
}
}
}
return count;
}

答案2

得分: -1

抱歉,我只能提供英文翻译。以下是您要求的翻译内容:

To be fair I code in C++, not java, but here's a question: Why does your `marked()` function return the value of a `marked` array? Wouldn't it make more sense to use that array directly?
For the time being I can tell you are using a DFS to traverse the graph correctly. I am not sure, however, why you are traversing the graph at all.
I am sorry, but this is going to be one of these "don't do this, do that instead" answers.
If you are looking for a linear solution (for one execution of the count method), you better just create an array which, **for each node** holds info about whether it is white or red. I'd recommend filling it with zeroes, and then going over the `rednodes` array to change appropriate values to `1`.
Next, you declare a result variable equal to zero, which will count the number of edges which connect to nodes of different colours. You iterate over all the edges, and check if the vertices they connect are of varying colours. If so, increment the result variable.
英文:

To be fair I code in C++, not java, but here's a question: Why does your marked() function return the value of a marked array? Wouldn't it make more sense to use that array directly?

For the time being I can tell you are using a DFS to traverse the graph correctly. I am not sure, however, why you are traversing the graph at all.

I am sorry, but this is going to be one of these "don't do this, do that instead" answers.

If you are looking for a linear solution (for one execution of the count method), you better just create an array which, for each node holds info about whether it is white or red. I'd recommend filling it with zeroes, and then going over the rednodes array to change appropriate values to 1.

Next, you declare a result variable equal to zero, which will count the number of edges which connect to nodes of different colours. You iterate over all the edges, and check if the vertices they connect are of varying colours. If so, increment the result variable.

huangapple
  • 本文由 发表于 2020年10月7日 22:41:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/64246492.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定