[英]BitSet out of memory Java
I am using BitSet to keep track whether nodes in a Graph have been visited using DFS method. 我正在使用BitSet来跟踪是否已使用DFS方法访问了Graph中的节点。 For this purpose I have created a BitSet[] array.
为此,我创建了一个BitSet []数组。 The BitSets themselves can be between 100.000-500.000 entries.
BitSet本身可以在100.000-500.000个条目之间。
This is the code I am using. 这是我正在使用的代码。
public void tc() {
// if (scc_cache_valid)
// return;
marked2 = new BitSet[node_count];
for (int v = 0; v < node_count; v++) {
//if (marked2[v] == null)
marked2[v] = new BitSet(edge_count);
System.out.println("aaa" + marked2[v].size());
for (int w : children.get(v)) {
if (!marked2[v].get(w))
dfs(v, w);
}
}
}
public void tc(int v) {
// if (scc_cache_valid && marked2[v] != null)
// return;
// marked2 = new BitSet[node_count];
// for (int v = 0; v < node_count; v++) {
if (marked2[v] == null)
marked2[v] = new BitSet(node_count);
System.out.println(marked2[v].size());
for (int w : children.get(v)) {
if (!marked2[v].get(w))
dfs(v, w);
}
// }
}
public boolean reachable(int v, int w) {
return marked2[v].get(w);
}
private void dfs(int v, int w) {
marked2[v].set(w, true);
System.out.println(marked2[v].length());
for (int z : children.get(w)) {
if (!marked2[v].get(z))
dfs(v, z);
}
}
Unfortunately I am running out of heap. 不幸的是我快要用光了。 Is there a better (more memory efficient) solution to this problem ?
是否有更好的(更有效的内存使用)解决方案?
Thank You. 谢谢。
I think your DFS algorithm is incorrect. 我认为您的DFS算法不正确。
HashSet<Node>
. HashSet<Node>
。 In either case, the space requirement is O(N)
rather than O(N^2)
. 无论哪种情况,空间需求都是
O(N)
而不是O(N^2)
。
A pseudo-code algorithm for the DAG / graph case is: DAG /图情况的伪代码算法为:
dfs(root) = dfs0(root, new Set());
dfs0(node, visited) =
if !visited.contains(node):
visited.add(node)
// do stuff for node
foreach n in node.children:
dfs0(n, visited)
Note: there is only one Set object used in the traversal. 注意:遍历中仅使用一个Set对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.