- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我不认为这是有效的。我正在尝试为此创建一个更快的实现(最好是二进制搜索或使用集合),但现在这就是我所在的位置。我不确定它是否相关,但我创建了一个计数变量只是为了查看该方法被调用了多少次。来到了577次。
这是“子集总和”算法,我必须显示添加到目标总和的所有子集,在本例中为 3165。没有特别说明这个问题是算法,但我意识到这确实是同一个概念。
我的问题是我怎么知道这个程序的效率如何,方法调用是指标吗?
public class SubsetSumAlgorithm {
static int count = 0;
public static void main(String[] args) {
int[] array = {26, 39, 104, 195, 403, 504, 793, 995, 1156, 1673};
System.out.println("COLLECTIONS IN ARRAY THAT ADD TO 3165: ");
findCollections(array, 0, 0, 3165, "");
System.out.println("Method called " + count + " times.");//method calls
}
public static void findCollections(int[] array, int index, int currentPosition, int sum, String collection) {
count++; //<---COUNTING THE METHOD CALLS HERE
if (array.length < index || currentPosition > sum) {
return;
}
//if sum is found, add to subset
for (int i = index; i < array.length; i++) {
if (currentPosition + array[i] == sum) {
System.out.println(collection + " " + array[i]);
}
//otherwise, call the method again
else if (currentPosition + array[i] < sum) {//recursive call
findCollections(array, i + 1, currentPosition + array[i], sum, collection + " " + array[i]);
}
}
}
}
这是输出:
COLLECTIONS IN ARRAY THAT ADD TO 3165:
26 195 793 995 1156
195 504 793 1673
Method called 577 times.
最佳答案
My question is how can I know how efficient this program is, and are the method calls an indicator?
推导算法渐近运行时间的唯一方法是亲自动手并手动。话虽如此,尝试在算法运行时跟踪方法调用并不是推导算法运行时间的可靠或合理方法。
要开始尝试弄清楚您的算法运行的速度有多快或有多慢,我们可以从分析每行的运行时间来推导递归开始:
1 public static void findCollections(int[] array, int index, int currentPosition, int sum, String collection) {
2 if(array.length < index || currentPosition > sum)
3 return;
4 for(int i = index; i < array.length; i++) {
5 if(currentPosition + array[i] == sum) {
6 System.out.println(collection + " " + array[i]);
7 }
8 else if(currentPosition + array[i] < sum) {
9 findCollections(array, i + 1, currentPosition + array[i], sum, collection + " " + array[i]);
10 }
11 }
12 }
...
1 -
2 1
3 1
4 n+1
5 n
6 n
7 -
8 n
9 n*T(n-1)
10 -
11 -
12 -
我们已经分析了每一行,我们可以得出递归:
T(n) = n*T(n-1) + 4n + 3
=> T(n) = n*T(n-1) + 4n + Θ(1)
=> T(n) = n*T(n-1) + 4n
由于我们不能使用主定理,并且尝试使用递归树会变得非常困惑并且很快就会对这种特定的递归产生混淆,我们可以使用以下方法解决这种递归替换法。我们将初始猜测设置为 2^n
,因为在我看来它可能是指数级的:
O(2^n)
这意味着
T(n) ≤ c * 2^n
如果这个命题成立,这意味着我们也知道
T(n-1) ≤ c * 2^(n-1)
因此,我们现在可以写出我们的递归并尝试证明我们的猜测:
c * 2^n ≥ n * (c * 2^(n-1)) + 4n
=> c * 2^n ≥ c * n * 2^(n-1) + 4n
这适用于所有 n > 0 | c = 1
,因此 T(n) = O(2^n)
Ω(2^n)
这意味着
T(n) ≥ c * 2^n
如果这个命题成立,这意味着我们也知道
T(n-1) ≥ c * 2^(n-1)
因此,我们现在可以写出我们的递归并尝试证明我们的猜测:
c * 2^n ≤ n * (c * 2^(n-1)) + 4n
=> c * 2^n ≤ c * n * 2^(n-1) + 4n
这适用于所有 n > 0 | c = 5000
,因此 T(n) = Ω(2^n)
Θ(2^n)
由于我们已经证明该算法既是O(2^n)
又是 Ω(2^n)
,因此它根据定义,也是 Θ(2^n)
。所以基本上,您的算法的时间复杂度是 Θ(2^n)
,非常慢,因为它是指数级的。
关于java - 我将如何计算此递归函数的最坏情况时间复杂度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35804306/
多数元素问题: Given an array of size n, find the majority element. The majority element is the element tha
我有一个简单的问题来找到数组 A 中的第一个唯一元素。但是,令我困扰的是使用不同方法的时间复杂度。到目前为止,我已经尝试过这两种方法。 第一种方法: LinkedHashMap> map = new
STL 中valarray::min 和valarray::max 函数的时间复杂度是多少? 此外,什么是查找各种其他 STL 组件的时间/空间复杂性的良好来源? 最佳答案 O(N) 这些函数不会缓存
我目前正在学习复杂性(或效率,不管你怎么调用它),我在我得到的一本书中读到了它。写了一些我觉得很无意义的东西,我需要一个解释。我试过在线查找,但我没有找到他们给出的这个特定示例的答案。 For an
如何分析算法?是什么让快速排序具有 O(n^2) 的最坏情况性能,而合并排序具有 O(n log(n)) 的最坏情况性能? 最佳答案 这是整个学期的主题。最终,我们讨论的是在算法完成之前必须完成的操作
有谁知道最流行的数据库的 SQL LIKE 运算符的复杂度是多少? 最佳答案 让我们分别考虑三个核心案例。此讨论是特定于 MySQL 的,但也可能适用于其他 DBMS,因为索引通常以类似的方式实现。
Go 编程语言中这个循环的计算复杂度是多少? var a []int for i := 0 ; i doublecap { newcap = cap } else {
我需要创建一个查找函数,其中 (X,Y) 对对应于特定的 Z 值。对此的一个主要要求是我需要尽可能接近 O(1) 复杂度。我的计划是使用 unordered_map。 我通常不使用哈希表进行查找,因为
快速提问,主要满足我对该主题的好奇心。 我正在编写一些带有 SQlite 数据库后端的大型 python 程序,并且将来会处理大量记录,因此我需要尽可能优化。 对于一些功能,我正在通过字典中的键进行搜
Go 编程语言中这个循环的计算复杂度是多少? var a []int for i := 0 ; i doublecap { newcap = cap } else {
我有这个方法: public static int what(String str, char start, char end) { int count=0; for(int i=0;
for (i = 0; i i; j--) //some code that yields O(1) } 我认为上面的代码会产生 n*log(n) 但我看到另一个消息来源说它真的是 n^2
我对 InnoDB 中 OFFSET 的复杂性有疑问。我知道这主要适用于线性复杂性,但如果我在字段上有索引?! 示例: CREATE TABLE `person_rand` ( `p_id` int
我嵌套了一些 if/else 语句,但我想减少它们的开销。 在示例中,我正在评估从哪个下拉列表中单击了 li 项目,以及该 li 项目是否是第一个 (currentIndex === 0)。 代码:
这是我的第一个问题,所以我希望我没有违反任何规则。我终于设法为基数排序算法编写代码,但我想知道我是否做错了。让我觉得我的算法看起来复杂度为 O(n^3),但众所周知,基数排序是一个 O(k.n) 算法
几周前我认识了 big-O 并试图掌握它,但是尽管有很多关于计算时间复杂度的 Material ,但我似乎无法找到如何使算法更高效。 我一直在练习 Codility 中的演示挑战: Write a f
在最近的一次考试中,我们得到了一个函数来计算在未排序的 ArrayList 中出现了多少个 double (不是原始 double,而是一个项目出现两次的次数)。 我正确地确定了 Big O 复杂度为
以下循环的大 O 复杂度是多少: for each vertex u ∈ C do for each vertex v ∈ C and v > u do 我在这里做的是想象以下集合 {
我想对条款进行排序,使每个条款都是下一个条款的大 O √n√logn √n log( n^30) n/〖(logn)〗^2 〖16〗^(log√n) 谁能帮忙找到顺序? 最佳答案 claim :16
我正在尝试计算此选择排序实现的大 O 时间复杂度: void selectionsort(int a[], int n) { int i, j, mini
我是一名优秀的程序员,十分优秀!