📜  用多项式归约法求解齐次递归方程

📅  最后修改于: 2021-04-22 02:57:49             🧑  作者: Mango

递归关系是一个方程式,一旦给出一个或多个初始项,就可以递归地定义值的序列或多维数组。序列或数组的每个其他术语均定义为先前术语的函数。以下是使用多项式归约法求解递归方程所需的步骤:

  1. 为给定的递归方程形成一个特征方程。
  2. 求解特征方程并找到特征方程的根。
  3. 用未知系数简化解决方案。
  4. 根据初始条件求解方程,以得到特定的解。

例子:
考虑下面的二阶递推方程:

T(n) = a1T(n-1) + a2T(n-2)

为了求解该方程,将其公式化为特征方程。
让我们重新排列方程式,如下所示:

T(n) - a1T(n-1) - a2T(n-2) = 0

T(n)= x n
现在我们可以说T(n-1)= x n-1T(n-2)= x n-2
现在的等式为:

xn + a1xn-1 + a2xn-2  = 0

将整个方程除以x n-2 [由于x不等于0 ],我们得到:

x2 + a1x + a2 = 0

我们得到了特征方程为x 2 + a 1 x + a 2 = 0
现在,找到该方程式的根。
找到特征方程式的根时可能存在三种情况,它们是:

情况1:特征方程式的根是真实且不同的
如果特征方程有r个不同的根,那么可以说r个基本解是可能的。可以取根的任何线性组合来获得线性递归方程的一般解。

如果r 1 ,r 2 ,r 3 ……,r k是特征方程的根,则递归方程的一般解为:

tn = c1r1n + c2r2n + c3r3n +...........+ ckrkn

情况2:特征方程式的根是真实的但不完全相同
让我们考虑特征方程的根不是唯一的,并且根r在m的多重性中。在这种情况下,特征方程的解为:

tn = rn
tn = nrn
tn = n2rn
.........
tn = n m - 1rn

因此,包括所有解以得到给定递归方程的一般解。

情况3:特征方程的根是不同的但不是实数
如果特征方程的根是复数,则找到共轭根。
如果r 1r 2是特征方程的两个根,并且它们彼此共轭,则可以表示为:

r1 = reix
r2 = re-ix

通用解决方案将是:

tn = c1rncos nx + c2rnsin nx

例子:

让我们解决给定的递归关系:

T(n) = 7*T(n-1) - 12*T(n-2)

令T(n)= x n
现在我们可以说T(n-1)= x n-1和T(n-2)= x n-2
将整个方程除以x n-2 ,我们得到:

x2 - 7*x + 12= 0

下面是解决给定二次方程的实现:

C++
// C++ program to find roots
// of a quadratic equation
#include
using namespace std;
 
class Quadratic{
   
public:
 
// Prints roots of quadratic
// equation ax * 2 + bx + x
void findRoots(int a, int b, int c)
{
     
    // If a is 0, then equation is not
    // quadratic, but linear
    if (a == 0)
    {
        cout << "Invalid";
        return;
    }
 
    int d = b * b - 4 * a * c;
    float sqrt_val = sqrt(abs(d));
 
    // Real Roots
    if (d > 0)
    {
       cout << "Roots are real and different" << endl;
       cout << fixed << std::setprecision(1)
            << float((-b + sqrt_val) / (2 * a)) << endl;
       cout << fixed << std::setprecision(1)
            << float((-b - sqrt_val) / (2 * a)) << endl;
    }
 
    // Imaginary Roots
    else
    {
        cout << "Roots are complex" << endl;
        cout << fixed << std::setprecision(1)
             << float(b / (2.0 * a)) << " + i"
             << sqrt_val << endl;
        cout << fixed << std::setprecision(1)
             << float(b / (2.0 * a)) << " - i"
             << sqrt_val << endl;
    }
}
};
 
// Driver code
int main()
{
    Quadratic obj;
 
    // Given value of coefficients
    int a = 1, b = -7, c = 12;
     
    obj.findRoots(a, b, c);
}
 
// This code is contributed by SURENDRA_GANGWAR


Java
// Java program to find roots
// of a quadratic equation
import java.io.*;
import static java.lang.Math.*;
class Quadratic {
 
    // Prints roots of quadratic
    // equation ax * 2 + bx + x
    void findRoots(int a, int b, int c)
    {
        // If a is 0, then equation is not
        // quadratic, but linear
        if (a == 0) {
            System.out.println("Invalid");
            return;
        }
 
        int d = b * b - 4 * a * c;
        double sqrt_val = sqrt(abs(d));
 
        // Real Roots
        if (d > 0) {
            System.out.println(
                "Roots are real"
                + " and different \n");
 
            System.out.println(
                (double)(-b + sqrt_val)
                    / (2 * a)
                + "\n"
 
                + (double)(-b - sqrt_val)
                      / (2 * a));
        }
 
        // Imaginary Roots
        else {
            System.out.println(
                "Roots are complex \n");
 
            System.out.println(
                -(double)b / (2 * a)
                + " + i"
                + sqrt_val + "\n"
                + -(double)b / (2 * a)
                + " - i" + sqrt_val);
        }
    }
 
    // Driver code
    public static void main(String args[])
    {
        Quadratic obj = new Quadratic();
 
        // Given value of coefficients
        int a = 1, b = -7, c = 12;
        obj.findRoots(a, b, c);
    }
}


Python3
# Python3 program to find roots
# of a quadratic equation
from math import sqrt
 
# Prints roots of quadratic
# equation ax * 2 + bx + x
def findRoots(a, b, c):
     
    # If a is 0, then equation is not
    # quadratic, but linear
    if (a == 0):
        print("Invalid")
        return
 
    d = b * b - 4 * a * c
    sqrt_val = sqrt(abs(d))
 
    # Real Roots
    if (d > 0):
        print("Roots are real and different")
 
        print((-b + sqrt_val) / (2 * a))
        print((-b - sqrt_val) / (2 * a))
 
    # Imaginary Roots
    else:
        print("Roots are complex \n")
 
        print(-b / (2 * a), " +  i",
              sqrt_val, "\n",
              -b / (2 * a), " - i",
              sqrt_val)
               
# Driver code
if __name__ == '__main__':
     
    # Given value of coefficients
    a = 1
    b = -7
    c = 12
     
    findRoots(a, b, c)
 
# This code is contributed by mohit kumar 29


C#
// C# program to find roots
// of a quadratic equation
using System;
class Quadratic{
 
// Prints roots of quadratic
// equation ax * 2 + bx + x
void findRoots(int a,
               int b, int c)
{
  // If a is 0, then equation
  // is not quadratic, but linear
  if (a == 0)
  {
    Console.WriteLine("Invalid");
    return;
  }
 
  int d = b * b - 4 *
          a * c;
  double sqrt_val =
         Math.Sqrt(Math.Abs(d));
 
  // Real Roots
  if (d > 0)
  {
    Console.WriteLine("Roots are real" +
                      " and different \n");
 
    Console.WriteLine((double)
                      (-b + sqrt_val) /
                      (2 * a) + "\n" +
                      (double)
                      (-b - sqrt_val) /
                      (2 * a));
  }
 
  // Imaginary Roots
  else
  {
    Console.WriteLine("Roots are complex \n");
 
    Console.WriteLine(-(double)b / (2 * a) +
                      " + i" + sqrt_val +
                      "\n" + -(double)b /
                      (2 * a) + " - i" +
                      sqrt_val);
  }
}
 
// Driver code
public static void Main(String []args)
{
  Quadratic obj = new Quadratic();
 
  // Given value of coefficients
  int a = 1, b = -7, c = 12;
  obj.findRoots(a, b, c);
}
}
 
// This code is contributed by Princi Singh


输出
Roots are real and different
4.0
3.0

时间复杂度: O(√N)
辅助空间: O(1)