- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试学习算法和 java。运行 320x320 的网格,100 次试验的运行速度比非递归 Quick-Union 实现快约 5 倍。但是,在大约 400x400(160,000 个站点)的网格之上,我遇到堆栈溢出错误。
我知道 java 没有针对尾递归进行优化(更不用说非尾递归了)。但是,我认为有时可以选择递归算法而不是非递归版本,因为它可以运行得更快并且同样安全。
请记住,我只是在学习这些东西,我的代码可能不是最佳的。但是,我将其包括在内是为了更好地理解我的问题。
评估何时可以在 Java 应用程序中安全地使用递归算法(假设它比非递归算法运行得更快)的过程是什么?
(注意:2x 比率只是之前的当前运行时间除以之前的运行时间)
|-----------|-----------|------------|-------------|-------------|
| N | Recursive | Recursive | Quick-Union | Quick-Union |
| (sites) | time | 2x Ratio | time | 2x Ratio |
|===========|===========|============|=============|=============|
| 196 | 35 | | 42 | |
| 400 | 25 | 0.71 | 44 | 1.05 |
| 784 | 45 | 1.80 | 46 | 1.05 |
| 1600 | 107 | 2.38 | 86 | 1.87 |
| 3136 | 48 | 0.45 | 113 | 1.31 |
| 6400 | 75 | 1.56 | 303 | 2.68 |
| 12769 | 183 | 2.44 | 858 | 2.83 |
| 25600 | 479 | 2.62 | 2682 | 3.13 |
| 51076 | 1253 | 2.62 | 8521 | 3.18 |
| 102400 | 4730 | 3.77 | 27256 | 3.20 |
|-----------|-----------|------------|-------------|-------------|
public class PercolateRecur implements Percolation {
// the site has been opened for percolation but is not connected
private final int OPEN = 0;
// the site is not open for percolation (default state)
private final int BLOCKED = -1;
// the matrix that will be percolated. Values default to `BLOCKED = -1`
// two sites that are connected together share the same value.
private int[][] matrix;
// the size of the sides of the matrix (1 to n)
private int size;
// whether water can flow from top to bottom of the matrix
private boolean percolated;
public PercolateRecur(int N) {
percolated = false;
size = N;
initMatrix();
}
/**
* initializes the matrix to default values
*/
private void initMatrix() {
matrix = new int[size+1][size+1];
// open up the top of the matrix
for (int x = 1; x < size+1; x++)
matrix[x][0] = x;
// set all values in matrix to closed
for (int x = 1; x < size+1; x++)
for (int y = 1; y < size+1; y++)
matrix[x][y] = BLOCKED;
}
/**
* indicates site (x,y) is a valid coordinate
* @param x x-portion of x/y coordinate
* @param y y-portion of x/y coordinate
* @return boolean
*/
private boolean isValid(int x, int y) {
return x > 0 && x < size+1 && y > 0 && y < size+1;
}
/**
* returns value of site above (x,y)
* @param x x-portion of x/y coordinate
* @param y y-portion of x/y coordinate
* @return int value
*/
private int above(int x, int y) {
if (y <= 0)
return BLOCKED;
else
return matrix[x][y-1];
}
/**
* returns value of site below (x,y)
* @param x x-portion of x/y coordinate
* @param y y-portion of x/y coordinate
* @return int value
*/
private int below(int x, int y) {
if (y >= size)
return BLOCKED;
else
return matrix[x][y+1];
}
/**
* returns value of site left of (x,y)
* @param x x-portion of x/y coordinate
* @param y y-portion of x/y coordinate
* @return int value
*/
private int left(int x, int y) {
if (x <= 0)
return BLOCKED;
return matrix[x-1][y];
}
/**
* returns value of site right of (x,y)
* @param x x-portion of x/y coordinate
* @param y y-portion of x/y coordinate
* @return int value
*/
private int right(int x, int y) {
if (x >= size)
return BLOCKED;
else
return matrix[x+1][y];
}
/**
* connects (x,y) to open adjacent sites
* @param x x-portion of x/y coordinate
* @param y y-portion of x/y coordinate
*/
private void connect(int x, int y) {
if (isFull(x,y))
return;
if (above(x,y) > OPEN)
matrix[x][y] = above(x, y);
else if (below(x, y) > OPEN)
matrix[x][y] = below(x, y);
else if (left(x, y) > OPEN)
matrix[x][y] = left(x, y);
else if (right(x, y) > OPEN)
matrix[x][y] = right(x, y);
else if (matrix[x][y] == BLOCKED)
matrix[x][y] = OPEN;
}
/**
* recursively connects open sites in same group as (x,y)
* @param x x-portion of x/y coordinate
* @param y y-portion of x/y coordinate
*/
private void expand(int x, int y) {
if (!isFull(x, y))
return;
if (above(x,y) == OPEN)
openWith(x,y-1, matrix[x][y]);
if (below(x,y) == OPEN)
openWith(x,y+1, matrix[x][y]);
if (left(x,y) == OPEN)
openWith(x-1,y, matrix[x][y]);
if (right(x,y) == OPEN)
openWith(x+1,y, matrix[x][y]);
}
/**
* opens a site (x,y) on the matrix
* @param x x-portion of x/y coordinate
* @param y y-portion of x/y coordinate
*/
public void open(int x, int y) {
if (percolated || !isValid(x, y))
return;
connect(x, y);
expand(x, y);
}
/**
* opens a site with given value
* @param x x-portion of x/y coordinate
* @param y y-portion of x/y coordinate
* @param val value of point
*/
private void openWith(int x, int y, int val) {
matrix[x][y] = val;
open(x, y);
}
/**
* Returns whether site (x,y) is open
* @param x x-portion of x/y coordinate
* @param y y-portion of x/y coordinate
* @return true if not blocked
*/
public boolean isOpen(int x, int y) {
return matrix[x][y] > BLOCKED;
}
/**
* Returns whether site (x,y) is full (connected to the top)
* @param x x-portion of x/y coordinate
* @param y y-portion of x/y coordinate
* @return true if is full
*/
public boolean isFull(int x, int y) {
return matrix[x][y] > OPEN;
}
/**
* indicates whether site is blocked (not open)
* @param x x-portion of x/y coordinate
* @param y y-portion of x/y coordinate
* @return true if blocked
*/
public boolean isBlocked(int x, int y) {
return matrix[x][y] == BLOCKED;
}
/**
* indicates whether water can flow from top to bottom of matrix
* @return true if matrix is percolated
*/
public boolean percolates() {
for (int x = 1; x <= size; x++)
if (matrix[x][size] > OPEN)
percolated = true;
return percolated;
}
/**
* prints the matrix to the command line
*/
public void print() {
for (int y = 1; y < size+1; y++) {
System.out.println();
for (int x = 1; x < size+1; x++) {
if (matrix[x][y] == BLOCKED)
System.out.print("XX ");
else if (matrix[x][y] < 10)
System.out.print(matrix[x][y] + " ");
else
System.out.print(matrix[x][y] + " ");
}
}
System.out.println();
}
}
最佳答案
在 Java 中实现递归算法的问题之一是 Java 平台不执行标准的“尾调用消除”优化。这意味着深度递归需要深度堆栈。由于 Java 线程堆栈不会“增长”,这意味着您很容易受到堆栈溢出的影响。
有两种解决方法:
增加线程堆栈大小,方法是在命令行上使用 -Xss
选项,或者在 Thread
中显式提供(更大的)堆栈大小> 构造函数。
在您的 Java 代码中显式实现尾调用消除……至少可以说这很丑陋。
在您的情况下,第一个解决方法将为您提供“真正递归”的衡量标准。第二个……好吧,该算法将不再是真正的递归算法,但您可以通过这种方式使递归算法在 Java 中适用于“深层”问题。
注意:您始终可以将 Java 中的递归算法转换为使用“模拟堆栈”数据结构来保存递归状态的等效迭代算法。两个版本的算法复杂度应该相同。也许您也应该尝试这种方法,并将“模拟堆栈”变体作为第三对列包含在您的评估中。
关于java - 如何评估 Java 中递归算法的效用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14917925/
在本教程中,您将借助示例了解 JavaScript 中的递归。 递归是一个调用自身的过程。调用自身的函数称为递归函数。 递归函数的语法是: function recurse() {
我的类(class) MyClass 中有这段代码: public new MyClass this[int index] { get {
我目前有一个非常大的网站,大小约为 5GB,包含 60,000 个文件。当前主机在帮助我将站点转移到新主机方面并没有做太多事情,我想的是在我的新主机上制作一个简单的脚本以 FTP 到旧主机并下载整个
以下是我对 AP 计算机科学问题的改编。书上说应该打印00100123我认为它应该打印 0010012但下面的代码实际上打印了 3132123 这是怎么回事?而且它似乎没有任何停止条件?! publi
fun fact(x: Int): Int{ tailrec fun factTail(y: Int, z: Int): Int{ if (y == 0) return z
我正在尝试用c语言递归地创建线性链表,但继续坚持下去,代码无法正常工作,并出现错误“链接器工具错误 LNK2019”。可悲的是我不明白发生了什么事。这是我的代码。 感谢您提前提供的大力帮助。 #inc
我正在练习递归。从概念上讲,我理解这应该如何工作(见下文),但我的代码不起作用。 请告诉我我做错了什么。并请解释您的代码的每个步骤及其工作原理。清晰的解释比只给我有效的代码要好十倍。 /* b
我有一个 ajax 调用,我想在完成解析并将结果动画化到页面中后调用它。这就是我陷入困境的地方。 我能记忆起这个功能,但它似乎没有考虑到动画的延迟。即控制台不断以疯狂的速度输出值。 我认为 setIn
有人愿意用通俗易懂的语言逐步解释这个程序(取自书籍教程)以帮助我理解递归吗? var reverseArray = function(x,indx,str) { return indx == 0 ?
目标是找出数组中整数的任意组合是否等于数组中的最大整数。 function ArrayAdditionI(arr) { arr.sort(function(a,b){ return a -
我在尝试获取 SQL 查询所需的所有数据时遇到一些重大问题。我对查询还很陌生,所以我会尽力尽可能地描述这一点。 我正在尝试使用 Wordpress 插件 NextGen Gallery 进行交叉查询。
虽然网上有很多关于递归的信息,但我还没有找到任何可以应用于我的问题的信息。我对编程还是很陌生,所以如果我的问题很微不足道,请原谅。 感谢您的帮助:) 这就是我想要的结果: listVariations
我一整天都在为以下问题而苦苦挣扎。我一开始就有问题。我不知道如何使用递归来解决这个特定问题。我将非常感谢您的帮助,因为我的期末考试还有几天。干杯 假设有一个包含“n”个元素的整数数组“a”。编写递归函
我有这个问题我想创建一个递归函数来计算所有可能的数字 (k>0),加上数字 1 或 2。数字 2 的示例我有两个可能性。 2 = 1+1 和 2 = 2 ,对于数字 3 两个 poss。 3 = 1+
目录 递归的基础 递归的底层实现(不是重点) 递归的应用场景 编程中 两种解决问题的思维 自下而上(Bottom-Up) 自上而下(Top-
0. 学习目标 递归函数是直接调用自己或通过一系列语句间接调用自己的函数。递归在程序设计有着举足轻重的作用,在很多情况下,借助递归可以优雅的解决问题。本节主要介绍递归的基本概念以及如何构建递归程序。
我有一个问题一直困扰着我,希望有人能提供帮助。我认为它可能必须通过递归和/或排列来解决,但我不是一个足够好的 (PHP) 程序员。 $map[] = array("0", "1", "2", "3")
我有数据 library(dplyr, warn.conflicts = FALSE) mtcars %>% as_tibble() %>% select(mpg, qsec) %>% h
在 q 中,over 的常见插图运算符(operator) /是 implementation of fibonacci sequence 10 {x,sum -2#x}/ 1 1 这确实打印了前 1
我试图理解以下代码片段中的递归调用。 static long fib(int n) { return n <= 1 ? n : fib(n-1) + fib(n-2); } 哪个函数调用首先被
我是一名优秀的程序员,十分优秀!