算法导论——lec 04 递归式

本文介绍了使用分治法解决问题时如何分析递归式的时间复杂度,主要探讨了代换法、递归树方法和主方法三种解决递归式的方法,并通过多个例题详细解释了每种方法的应用。通过这些方法,可以有效地求解算法的运行时间,例如在处理递归关系如T(n)=2T(⌊n/2⌋)+n时,分析其时间复杂度并找到正确的解决方案。

  在分治法中,我们经常会将一些问题分解为几个子问题,每个子问题的规模都比原问题要小,而求解子问题相对于解原问题要容易一些。如此递归地分割子问题,就得到更小的子子问题,当问题的规模小到一定的程度的时候,子问题就可以直接求解,当我们获得最下层子问题的解的时候,就可以通过合并子问题的解来获得上层子问题的解,如此类推,最后就能得到原问题的解。

        假设原问题的规模为n,我们利用分治法将原问题分解为a个子问题,每个子问题的规模是原问题的1/b,假设分割原问题的时间为D(n),合并问题的时间为C(n),最后就可以得到如下的递推关系:

那么得到以上的递推关系式以后,我们如何得到问题的时间复杂度T(n)呢?本文就介绍三种解决这类问题的方法。

一、 代换法

代换法是先猜测有某个界存在,然后用数学归纳法来证明这个猜测的正确性。用代换法求解需要两个步骤:猜测解的形式;用数学归纳法找出使解真正有效的常数。

一般先确定其界, 然后根据边界条件确定常数。

例题一:T(n)=2T(⌊n/2⌋)+n

我们猜测其解为T (n) = O(n lg n), 即存在常数c,使得T(n) <= c n lg n

于是,T(n) = 2T(⌊n/2⌋)+n <= 2 * (c ⌊n/2⌋ lg ⌊n/2⌋) + n <= 2 * (c n/2 lg n/2) + n  = cn lgn - cn + n <= c n lg n

当c > 1的时候上式成立。

边界条件:由定义只要找出n0,使得当n >= n0的时候T(n) <= c n lg n即可,

我们取n0 = 2,可证边界条件成立。


代换法的缺点:不存在通用的方法,需要经验。

试探法、 递归树、 类似的先例、 先证明递归式的上下界然后不断缩小不确定性区间等。


例题二:经验——减掉一个低阶项  T (n) = T (⌊n/2⌋) + T (⌈n/2⌉) + 1

我们猜测解为T(n) = O(n),这样就存在正常数c,是得对于n >= n0时有 T(n) <= cn

 T (n) = T (⌊n/2⌋) + T (⌈n/2⌉) + 1 <= c (⌊n/2⌋) + c (⌈n/2⌉) + 1 = cn + 1

与所猜测的解不一致;

这里我们可以考虑减掉一个低阶项,使得T(n) <= cn - b

### LEC Error Code 0x04 Meaning and Solution in Automotive CAN Bus In the context of an automotive Controller Area Network (CAN) bus, the Local Error State (LEC) is used to indicate the type of error that has occurred during message transmission or reception. When LEC equals `0x04`, this specifically refers to a **stuff error**[^3]. A stuff error occurs when more than five consecutive bits of the same value are detected without a bit stuffing violation being present. Bit stuffing is a technique employed by CAN protocol to ensure synchronization between nodes; it inserts additional bits into the data stream under certain conditions. #### Causes and Solutions for Stuff Errors Stuff errors can arise from several causes: - Physical layer issues such as poor wiring connections or damaged cables. - Interference caused by electromagnetic noise affecting signal integrity. - Faulty hardware components like transceivers not conforming to specifications. To resolve these problems: 1. Inspect all physical connections including terminators at both ends of the network segment ensuring they meet standard requirements. 2. Verify shielded twisted pair cabling with proper grounding practices implemented throughout installation. 3. Replace any suspect electronic parts suspected of malfunction based on diagnostic tests conducted using specialized tools designed for analyzing CAN networks. ```python def check_can_bus(): """ A function to simulate checking various aspects of a CAN bus system which could lead to resolving LEC=0x04 issue. Returns: str: Status after performing checks. """ connection_status = "Inspect all connectors" interference_level = measure_electromagnetic_interference() component_test_results = test_hardware_components() if connection_status != "All good": return f"Check your {connection_status}" elif interference_level > acceptable_threshold: return "Investigate sources of EMI" elif not all(component_test_results): faulty_parts = [part for part, status in zip(components, component_test_results) if not status] return f"Replace {' and '.join(faulty_parts)}" else: return "No apparent issues found." ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值