I am trying to figure out, how one can use numerical approach, and count what’s not in an imputed array?
I am using their library, and reading their book. https://algs4.cs.princeton.edu/41graph/
But, I would like to better understand, maybe some one knows better.
I would like to count how many edges there are that goes between a white and a black node. The black nodes are the ones in the new int in the main method.
I am having trouble traversing the graph and I don’t know how to use blacknodes to compare to the graph. When running my attempted code below I get errors in the marked and count methods.
Exception in thread “main” java.lang.NullPointerException: Cannot load from byte/boolean array because “BlackWhite.marked” is null
import edu.princeton.cs.algs4.Graph;
public class BlackWhite {
private static boolean[] marked;
public BlackWhite(Graph G, int s) {
marked = new boolean[G.V()];
dfs(G, s);
}
public static int count(Graph G, int[] blacknodes) {
int rw_count = 0;
for (int w : G.adj(blacknodes.length))
{
if (marked(w)) {
rw_count++;
}
}
// Count how many of the edges in G connect a white node (one that
// isn't in blacknodes) with a red node (one that is in blacknodes).
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(3);
G.addEdge(1, 2);
G.addEdge(4, 1);
G.addEdge(1, 8);
System.out.println(BlackWhite.count(G, new int[] { 1 })); // should print 3
}
}