用大哦证明小哦

时间:2016-01-01 16:58:12

标签: algorithm big-o

如果我已经知道 public boolean isConnected(){ Log.v(TAG, "mainisconnected"); ConnectivityManager conectivtyManager = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE); if (conectivtyManager.getActiveNetworkInfo() != null && conectivtyManager.getActiveNetworkInfo().isAvailable() && conectivtyManager.getActiveNetworkInfo().isConnected()) { Log.v(TAG, "mainisconnected true"); return true; } else { Log.v(TAG, "mainisconnected false"); return false; } } FATAL EXCEPTION: AsyncTask #1 Process: com.cursedchico.IstanbulEventPool, PID: 13068 java.lang.RuntimeException: An error occured while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:300) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) at java.util.concurrent.FutureTask.setException(FutureTask.java:222) at java.util.concurrent.FutureTask.run(FutureTask.java:242) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:841) Caused by: java.lang.NullPointerException at com.cursedchico.IstanbulEventPool.EventRetriever.doInBackground(UpdateDB.java:303) at com.cursedchico.IstanbulEventPool.EventRetriever.doInBackground(UpdateDB.java:109) at android.os.AsyncTask$2.call(AsyncTask.java:288) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)  at java.lang.Thread.run(Thread.java:841)  01-01 18:48:00.299 13068-13068/com.cursedchico.IstanbulEventPool E/WindowManager: android.view.WindowLeaked: Activity com.cursedchico.IstanbulEventPool.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{41e17480 V.E..... R.....I. 0,0-488,216} that was originally added here at android.view.ViewRootImpl.<init>(ViewRootImpl.java:388) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) at android.app.Dialog.show(Dialog.java:286) at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:902) at com.cursedchico.IstanbulEventPool.MainActivity.connectIfNecessary(MainActivity.java:106) at com.cursedchico.IstanbulEventPool.MainActivity$6.onClick(MainActivity.java:276) at android.view.View.performClick(View.java:4508) at android.view.View$PerformClick.run(View.java:18675) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5584) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) at dalvik.system.NativeStart.main(Native Method) 01-01 18:48:00.299 13068-13068/com.cursedchico.IstanbulEventPool E/WindowManager: android.view.WindowLeaked: Activity com.cursedchico.IstanbulEventPool.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{41e36258 V.E..... R......D 0,0-488,165} that was originally added here at android.view.ViewRootImpl.<init>(ViewRootImpl.java:388) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) at android.app.Dialog.show(Dialog.java:286) at android.app.ProgressDialog.show(ProgressDialog.java:117) at android.app.ProgressDialog.show(ProgressDialog.java:100) at com.cursedchico.IstanbulEventPool.EventRetriever.onPreExecute(UpdateDB.java:269) at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587) at android.os.AsyncTask.execute(AsyncTask.java:535) at com.cursedchico.IstanbulEventPool.UpdateDB.<init>(UpdateDB.java:56) at com.cursedchico.IstanbulEventPool.MainActivity$6.onClick(MainActivity.java:280) at android.view.View.performClick(View.java:4508) at android.view.View$PerformClick.run(View.java:18675) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5584) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) at dalvik.system.NativeStart.main(Native Method) 。从小哦的定义来看,如何证明f(n)O(g(n))

1 个答案:

答案 0 :(得分:2)

鉴于f(n) is in O(g(n))

使用big-O表示法的定义,我们可以将其写成:

f(n) is in O(g(n))

=> |f(n)| ≤ k*|g(n)|, for some constant k>0                 (+)
                      for n sufficiently large (say, n>N)

有关如上所述使用的big-O的定义,请参阅例如

证明:给定(+),然后f(n) is in o(n*g(n))

让我们首先说明一点点符号意味着什么:

  

正式地,f(n) = o(g(n))(或f(n) ∈ o(g(n)))为n → ∞表示   对于每个正常数ε,存在一个常数N

|f(n)| ≤ ε*|g(n)|, for all n > N                          (++)

来自https://en.wikipedia.org/wiki/Big_O_notation#Little-o_notation

现在,使用(+),我们可以写

|f(n)| ≤ k*|g(n)|, som k>0, n sufficiently large 

    <=> { n > 0 } <=> n*|f(n)| ≤ k*n*|g(n)|
                  <=> n*|f(n)| ≤ k*|n*g(n)|
                  <=>   |f(n)| ≤ (k/n)*|n*g(n)|            (+++)

返回little-o的定义,特别是(++),并且在不失一般性的情况下,k被修复。现在,每个正常数ε都可以描述为

ε = k/C, for some constant C>0 (with k fixed, k>0)         (*)

现在,在不失一般性的情况下,假设n大于此C,即n>C。然后,(*)(+++)会产生

|f(n)| ≤ (k/n)*|n*g(n)| < (k/C)*|n*g(n)| = ε*|n*g(n)|      (**)
                        ^                ^
                        |                |
                    since `n>C`         (*)

由于我们正在研究渐近行为,我们可以选择将n的下限分配给任何大于C的值(事实上,这是在大O和大二的定义中) little-o,“n够大”),因此---通过上面的小哦 - 的定义,我们有:

- As shown above, (+) implies (**) 
- By the definition of little-o, (**) shows that f(n) is in o(n*g(n))
- Subsequently, we've shown that, given (+), then: f(n) is in o(n*g(n))

结果:如果f(n) is in O(g(n)),那么f(n) is in o(n*g(n)),这两个关系分别引用big-O和litte-O渐近边界。

评论:事实上,结果非常微不足道。 big-O和little-o表示法仅在用于证明上界的两个常量之一上有所不同,即我们可以将big-O和little-O的定义写为:

    如果我们能够找到一组正常数f(n),那么
  • O(g(n))就会出现在(k, N)中,以便f(n) < k*g(n)适用于所有n>N }。

  • 如果我们能找到正常数f(n),则{li>

    o(g(n))被认为在Nf(n) < ε*g(n)适用于所有n>N,和为每个正常数 ε

后者显然是一个更严格的约束,但是如果我们可以在n的左侧使用f(n) < ε*g(n)的一个额外幂(即f(n) < ε*n*g(n)),然后,即使对于ε的无穷小值,我们总是可以自由地选择另一个常量N,以使ε*n足够大,以便为我们提供任何可用于k的常量f(n)表明O(g(n))位于n>N中(如召回,gdbserver --multi :4444)。