The Wayback Machine - https://web.archive.org/web/20240917172454/https://www.geeksforgeeks.org/doolittle-algorithm-lu-decomposition/
Open In App

Doolittle Algorithm | LU Decomposition

Last Updated : 25 Jun, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Doolittle Algorithm: The Doolittle Algorithm is a method for performing LU Decomposition, where a given matrix is decomposed into a lower triangular matrix L and an upper triangular matrix U. This decomposition is widely used in solving systems of linear equations, inverting matrices, and computing determinants.

LU Decomposition

Let A be a square matrix. An LU factorization refers to the factorization of A, with proper row and/or column orderings or permutations, into two factors, a lower triangular matrix L and an upper triangular matrix U, A=LU.

Read More: LU Decomposition

Doolittle Algorithm

It is always possible to factor a square matrix into a lower triangular matrix and an upper triangular matrix. That is, [A] = [L][U]
Doolittle’s method provides an alternative way to factor A into an LU decomposition without going through the hassle of Gaussian Elimination.
For a general n×n matrix A, we assume that an LU decomposition exists, and write the form of L and U explicitly. We then systematically solve for the entries in L and U from the equations that result from the multiplications necessary for A=LU.

Terms of the U matrix are given by:

[Tex]\forall j\\ i=0 \rightarrow U_{ij} = A_{ij}\\ i>0 \rightarrow U_{ij} = A_{ij} – \sum_{k=0}^{i-1}L_{ik}U_{kj}            [/Tex] 

And the terms for L matrix:

[Tex] \forall i\\ j=0 \rightarrow L_{ij} = \frac{A_{ij}}{U_{jj}}\\ j>0 \rightarrow L_{ij} = \frac{A_{ij} – \sum_{k=0}^{j-1}L_{ik}U_{kj}}{U_{jj}}         [/Tex]

Example:

Input :

Output :

Implementation:

C++

// C++ Program to decompose a matrix into // lower and upper triangular matrix #include <bits/stdc++.h> using namespace std; const int MAX = 100; void luDecomposition(int mat[][MAX], int n) { int lower[n][n], upper[n][n]; memset(lower, 0, sizeof(lower)); memset(upper, 0, sizeof(upper)); // Decomposing matrix into Upper and Lower // triangular matrix for (int i = 0; i < n; i++) { // Upper Triangular for (int k = i; k < n; k++) { // Summation of L(i, j) * U(j, k) int sum = 0; for (int j = 0; j < i; j++) sum += (lower[i][j] * upper[j][k]); // Evaluating U(i, k) upper[i][k] = mat[i][k] - sum; } // Lower Triangular for (int k = i; k < n; k++) { if (i == k) lower[i][i] = 1; // Diagonal as 1 else { // Summation of L(k, j) * U(j, i) int sum = 0; for (int j = 0; j < i; j++) sum += (lower[k][j] * upper[j][i]); // Evaluating L(k, i) lower[k][i] = (mat[k][i] - sum) / upper[i][i]; } } } // setw is for displaying nicely cout << setw(6) << " Lower Triangular" << setw(32) << "Upper Triangular" << endl; // Displaying the result : for (int i = 0; i < n; i++) { // Lower for (int j = 0; j < n; j++) cout << setw(6) << lower[i][j] << "\t"; cout << "\t"; // Upper for (int j = 0; j < n; j++) cout << setw(6) << upper[i][j] << "\t"; cout << endl; } } // Driver code int main() { int mat[][MAX] = { { 2, -1, -2 }, { -4, 6, 3 }, { -4, -2, 8 } }; luDecomposition(mat, 3); return 0; }

Java

// Java Program to decompose a matrix into // lower and upper triangular matrix class GFG { static int MAX = 100; static String s = ""; static void luDecomposition(int[][] mat, int n) { int[][] lower = new int[n][n]; int[][] upper = new int[n][n]; // Decomposing matrix into Upper and Lower // triangular matrix for (int i = 0; i < n; i++) { // Upper Triangular for (int k = i; k < n; k++) { // Summation of L(i, j) * U(j, k) int sum = 0; for (int j = 0; j < i; j++) sum += (lower[i][j] * upper[j][k]); // Evaluating U(i, k) upper[i][k] = mat[i][k] - sum; } // Lower Triangular for (int k = i; k < n; k++) { if (i == k) lower[i][i] = 1; // Diagonal as 1 else { // Summation of L(k, j) * U(j, i) int sum = 0; for (int j = 0; j < i; j++) sum += (lower[k][j] * upper[j][i]); // Evaluating L(k, i) lower[k][i] = (mat[k][i] - sum) / upper[i][i]; } } } // setw is for displaying nicely System.out.println(setw(2) + " Lower Triangular" + setw(10) + "Upper Triangular"); // Displaying the result : for (int i = 0; i < n; i++) { // Lower for (int j = 0; j < n; j++) System.out.print(setw(4) + lower[i][j] + "\t"); System.out.print("\t"); // Upper for (int j = 0; j < n; j++) System.out.print(setw(4) + upper[i][j] + "\t"); System.out.print("\n"); } } static String setw(int noOfSpace) { s = ""; for (int i = 0; i < noOfSpace; i++) s += " "; return s; } // Driver code public static void main(String arr[]) { int mat[][] = { { 2, -1, -2 }, { -4, 6, 3 }, { -4, -2, 8 } }; luDecomposition(mat, 3); } } /* This code contributed by PrinciRaj1992 */

Python3

# Python3 Program to decompose # a matrix into lower and # upper triangular matrix MAX = 100 def luDecomposition(mat, n): lower = [[0 for x in range(n)] for y in range(n)] upper = [[0 for x in range(n)] for y in range(n)] # Decomposing matrix into Upper # and Lower triangular matrix for i in range(n): # Upper Triangular for k in range(i, n): # Summation of L(i, j) * U(j, k) sum = 0 for j in range(i): sum += (lower[i][j] * upper[j][k]) # Evaluating U(i, k) upper[i][k] = mat[i][k] - sum # Lower Triangular for k in range(i, n): if (i == k): lower[i][i] = 1 # Diagonal as 1 else: # Summation of L(k, j) * U(j, i) sum = 0 for j in range(i): sum += (lower[k][j] * upper[j][i]) # Evaluating L(k, i) lower[k][i] = int((mat[k][i] - sum) / upper[i][i]) # setw is for displaying nicely print("Lower Triangular\t\tUpper Triangular") # Displaying the result : for i in range(n): # Lower for j in range(n): print(lower[i][j], end="\t") print("", end="\t") # Upper for j in range(n): print(upper[i][j], end="\t") print("") # Driver code mat = [[2, -1, -2], [-4, 6, 3], [-4, -2, 8]] luDecomposition(mat, 3) # This code is contributed by mits

C#

// C# Program to decompose a matrix into // lower and upper triangular matrix using System; class GFG { static int MAX = 100; static String s = ""; static void luDecomposition(int[, ] mat, int n) { int[, ] lower = new int[n, n]; int[, ] upper = new int[n, n]; // Decomposing matrix into Upper and Lower // triangular matrix for (int i = 0; i < n; i++) { // Upper Triangular for (int k = i; k < n; k++) { // Summation of L(i, j) * U(j, k) int sum = 0; for (int j = 0; j < i; j++) sum += (lower[i, j] * upper[j, k]); // Evaluating U(i, k) upper[i, k] = mat[i, k] - sum; } // Lower Triangular for (int k = i; k < n; k++) { if (i == k) lower[i, i] = 1; // Diagonal as 1 else { // Summation of L(k, j) * U(j, i) int sum = 0; for (int j = 0; j < i; j++) sum += (lower[k, j] * upper[j, i]); // Evaluating L(k, i) lower[k, i] = (mat[k, i] - sum) / upper[i, i]; } } } // setw is for displaying nicely Console.WriteLine(setw(2) + " Lower Triangular" + setw(10) + "Upper Triangular"); // Displaying the result : for (int i = 0; i < n; i++) { // Lower for (int j = 0; j < n; j++) Console.Write(setw(4) + lower[i, j] + "\t"); Console.Write("\t"); // Upper for (int j = 0; j < n; j++) Console.Write(setw(4) + upper[i, j] + "\t"); Console.Write("\n"); } } static String setw(int noOfSpace) { s = ""; for (int i = 0; i < noOfSpace; i++) s += " "; return s; } // Driver code public static void Main(String[] arr) { int[, ] mat = { { 2, -1, -2 }, { -4, 6, 3 }, { -4, -2, 8 } }; luDecomposition(mat, 3); } } // This code is contributed by Princi Singh

JavaScript

<script> // Javascript Program to decompose a matrix // into lower and upper triangular matrix // function MAX = 100; var s = ""; function luDecomposition(mat, n) { var lower = Array(n).fill(0).map( x => Array(n).fill(0)); var upper = Array(n).fill(0).map( x => Array(n).fill(0)); // Decomposing matrix into Upper and // Lower triangular matrix for(var i = 0; i < n; i++) { // Upper Triangular for(var k = i; k < n; k++) { // Summation of L(i, j) * U(j, k) var sum = 0; for(var j = 0; j < i; j++) sum += (lower[i][j] * upper[j][k]); // Evaluating U(i, k) upper[i][k] = mat[i][k] - sum; } // Lower Triangular for(var k = i; k < n; k++) { if (i == k) // Diagonal as 1 lower[i][i] = 1; else { // Summation of L(k, j) * U(j, i) var sum = 0; for(var j = 0; j < i; j++) sum += (lower[k][j] * upper[j][i]); // Evaluating L(k, i) lower[k][i] = parseInt((mat[k][i] - sum) / upper[i][i]); } } } // Setw is for displaying nicely document.write(setw(2) + "Lower Triangular" + setw(10) + "Upper Triangular<br>"); // Displaying the result : for(var i = 0; i < n; i++) { // Lower for(var j = 0; j < n; j++) document.write(setw(4) + lower[i][j] + "\t"); document.write(setw(10)); // Upper for(var j = 0; j < n; j++) document.write(setw(4) + upper[i][j] + "\t"); document.write("<br>"); } } function setw(noOfSpace) { var s = ""; for(i = 0; i < noOfSpace; i++) s += "&nbsp;"; return s; } // Driver code var mat = [ [ 2, -1, -2 ], [ -4, 6, 3 ], [ -4, -2, 8 ] ]; luDecomposition(mat, 3); // This code is contributed by Amit Katiyar </script>

PHP

<?php // PHP Program to decompose // a matrix into lower and // upper triangular matrix $MAX = 100; function luDecomposition($mat, $n) { $lower; $upper; for($i = 0; $i < $n; $i++) for($j = 0; $j < $n; $j++) { $lower[$i][$j]= 0; $upper[$i][$j]= 0; } // Decomposing matrix // into Upper and Lower // triangular matrix for ($i = 0; $i < $n; $i++) { // Upper Triangular for ($k = $i; $k < $n; $k++) { // Summation of // L(i, j) * U(j, k) $sum = 0; for ($j = 0; $j < $i; $j++) $sum += ($lower[$i][$j] * $upper[$j][$k]); // Evaluating U(i, k) $upper[$i][$k] = $mat[$i][$k] - $sum; } // Lower Triangular for ($k = $i; $k < $n; $k++) { if ($i == $k) $lower[$i][$i] = 1; // Diagonal as 1 else { // Summation of L(k, j) * U(j, i) $sum = 0; for ($j = 0; $j < $i; $j++) $sum += ($lower[$k][$j] * $upper[$j][$i]); // Evaluating L(k, i) $lower[$k][$i] = (int)(($mat[$k][$i] - $sum) / $upper[$i][$i]); } } } // setw is for // displaying nicely echo "\t\tLower Triangular"; echo "\t\t\tUpper Triangular\n"; // Displaying the result : for ($i = 0; $i < $n; $i++) { // Lower for ($j = 0; $j < $n; $j++) echo "\t" . $lower[$i][$j] . "\t"; echo "\t"; // Upper for ($j = 0; $j < $n; $j++) echo $upper[$i][$j] . "\t"; echo "\n"; } } // Driver code $mat = array(array(2, -1, -2), array(-4, 6, 3), array(-4, -2, 8)); luDecomposition($mat, 3); // This code is contributed by mits ?>


Output

Lower Triangular Upper Triangular 1 0 0 2 -1 -2 -2 1 0 0 4 -1 -2 -1 1 0 0 3

Applications on Doolittle Algorithm

  1. Solving Linear Equations: LU Decomposition simplifies solving AX = B by solving LY = B and then UX = Y.
  2. Matrix Inversion: Decompose A into LU, then invert L and U.
  3. Determinant Calculation: The determinant of A is the product of the diagonal elements of U.

Conclusion – Doolittle Algorithm

The Doolittle Algorithm is an efficient method for LU Decomposition, enabling various applications in numerical analysis and linear algebra.

FAQs on Doolittle Algorithm : LU Decomposition

What is the Doolittle Algorithm?

The Doolittle Algorithm is a method used for LU Decomposition, which decomposes a given matrix into a lower triangular matrix L and an upper triangular matrix U.

Why is LU Decomposition important?

LU Decomposition simplifies solving systems of linear equations, inverting matrices, and calculating determinants, which are fundamental operations in numerical analysis and linear algebra.

What are the key steps in the Doolittle Algorithm?

The key steps include initializing L as an identity matrix and U as a zero matrix, and then using forward elimination to compute the elements of U and L.

Can the Doolittle Algorithm be used for any matrix?

The Doolittle Algorithm can be used for any square matrix that can be decomposed into lower and upper triangular matrices. However, it may require pivoting for numerical stability in some cases.

How does the Doolittle Algorithm handle the diagonal elements of L?

In the Doolittle Algorithm, the diagonal elements of L are set to 1, simplifying the calculations and ensuring that L remains a lower triangular matrix with unit diagonal elements.



Previous Article
Next Article

Similar Reads

Cholesky Decomposition : Matrix Decomposition
In linear algebra, a matrix decomposition or matrix factorization is a factorization of a matrix into a product of matrices. There are many different matrix decompositions. One of them is Cholesky Decomposition. The Cholesky decomposition or Cholesky factorization is a decomposition of a Hermitian, positive-definite matrix into the product of a low
9 min read
MO's Algorithm (Query Square Root Decomposition) | Set 1 (Introduction)
Let us consider the following problem to understand MO's Algorithm.We are given an array and a set of query ranges, we are required to find the sum of every query range. Example: Input: arr[] = {1, 1, 2, 1, 3, 4, 5, 2, 8}; query[] = [0, 4], [1, 3] [2, 4] Output: Sum of arr[] elements in range [0, 4] is 8 Sum of arr[] elements in range [1, 3] is 4 S
15+ min read
Square Root (Sqrt) Decomposition Algorithm
Square Root Decomposition Technique is one of the most common query optimization techniques used by competitive programmers. This technique helps us to reduce Time Complexity by a factor of sqrt(N) The key concept of this technique is to decompose a given array into small chunks specifically of size sqrt(N) Follow the below steps to solve the probl
15+ min read
Sum of decomposition values of all suffixes of an Array
Given an array arr[], the task is to find the sum of the decomposition value of the suffixes subarray.Decomposition Value: The decomposition value of a subarray is the count of the partition in the subarray possible. The partition in the array at index [Tex]i [/Tex]can be done only if the elements of the array before if it less than the current ind
8 min read
Calculate sum in Diagonal Matrix Decomposition by removing elements in L-shape
Given two integers N representing the dimension of a square matrix and an integer A with which the matrix is initialized. Given another integer mod. Calculate the required sum by following the given steps: Select the product of all elements in L shape beginning from the topmost right element, add it to the sum and remove all of them from the matrix
6 min read
K-shell decomposition on Social Networks
Prerequisite: Introduction to Social Networks K-shell decomposition is the method in which we can divide nodes on the basis of the number of its degree like nodes with degree 1 in one bucket etc. Consider an example, assume there are n nodes and you apply k-shell decomposition in it. So nodes with degree 1 will be in bucket1 then we will see that a
4 min read
Range Minimum Query (Square Root Decomposition and Sparse Table)
We have an array arr[0 . . . n-1]. We should be able to efficiently find the minimum value from index L (query start) to R (query end) where 0 &lt;= L &lt;= R &lt;= n-1. Consider a situation when there are many range queries. Example: Input: arr[] = {7, 2, 3, 0, 5, 10, 3, 12, 18}; query[] = [0, 4], [4, 7], [7, 8]Output: Minimum of [0, 4] is 0 Minim
15+ min read
Introduction to Heavy Light Decomposition
Heavy Light decomposition (HLD) is one of the most used techniques in competitive programming. Example Problem: Let us understand Heavy-light decomposition (HLD) with the help of below example. Suppose we have an unbalanced tree (not necessarily a Binary Tree) of n nodes, and we have to perform operations on the tree to answer a number of queries,
4 min read
Centroid Decomposition of Tree
Background : What is centroid of Tree? Centroid of a Tree is a node which if removed from the tree would split it into a ‘forest’, such that any tree in the forest would have at most half the number of vertices in the original tree. Suppose there are n nodes in the tree. ‘Subtree size’ for a node is the size of the tree rooted at the node. Let S(v)
15+ min read
Sqrt (or Square Root) Decomposition | Set 2 (LCA of Tree in O(sqrt(height)) time)
Prerequisite: Introduction and DFSThe task is to find LCA of two given nodes in a tree (not necessarily a Binary Tree). In previous posts, we have seen how to calculate LCA using the Sparse Matrix DP approach. In this post, we will see an optimization done on the Naive method by sqrt decomposition technique that works well over the Naive Approach.
15+ min read
Implementation of Heavy Light Decomposition
We strongly recommend to refer below post as a prerequisite of this.Heavy Light Decomposition | Set 1 (Introduction)In the above post, we discussed the Heavy-light decomposition (HLD) with the help of below example. Suppose we have an unbalanced tree (not necessarily a Binary Tree) of n nodes, and we have to perform operations on the tree to answer
15+ min read
LU Decomposition
LU decomposition of a matrix is the factorization of a given square matrix into two triangular matrices, one upper triangular matrix and one lower triangular matrix, such that the product of these two matrices gives the original matrix. It was introduced by Alan Turing in 1948, who also created the Turing machine. LU Decomposition is a fundamental
7 min read
Singular Value Decomposition (SVD)
The Singular Value Decomposition (SVD) of a matrix is a factorization of that matrix into three matrices. It has some interesting algebraic properties and conveys important geometrical and theoretical insights about linear transformations. It also has some important applications in data science. In this article, I will try to explain the mathematic
6 min read
Edge Relaxation Property for Dijkstra’s Algorithm and Bellman Ford's Algorithm
In the field of graph theory, various shortest path algorithms especially Dijkstra’s algorithm and Bellmann-Ford’s algorithm repeatedly employ the use of the technique called Edge Relaxation. The idea of relaxation is the same in both algorithms and it is by understanding, the 'Relaxation property' we can fully grasp the working of the two algorith
4 min read
Difference between Greedy Algorithm and Divide and Conquer Algorithm
Greedy algorithm and divide and conquer algorithm are two common algorithmic paradigms used to solve problems. The main difference between them lies in their approach to solving problems. Greedy Algorithm:The greedy algorithm is an algorithmic paradigm that follows the problem-solving heuristic of making the locally optimal choice at each stage wit
3 min read
Algorithm Library | C++ Magicians STL Algorithm
For all those who aspire to excel in competitive programming, only having a knowledge about containers of STL is of less use till one is not aware what all STL has to offer. STL has an ocean of algorithms, for all &lt; algorithm &gt; library functions : Refer here.Some of the most used algorithms on vectors and most useful one's in Competitive Prog
7 min read
What is the stupidest sorting algorithm? (Worst Sorting Algorithm)
Bogo sort stands out as the undisputed champion of stupidity. Unlike other sorting algorithms that follow a structured approach, Bogo sort relies on sheer luck and randomness to achieve its goal. How Bogo Sort Works?Bogo sort operates on the following principle: Randomly shuffle the elements in the list.Check if the list is sorted.If the list is no
2 min read
Difference Between Dijkstra's Algorithm and A* Search Algorithm
Dijkstra's Algorithm and A* Algorithm are two of the most widely used techniques. Both are employed to the find the shortest path between the nodes in a graph but they have distinct differences in their approaches and applications. Understanding these differences is crucial for the selecting the appropriate algorithm for the given problem. What is
3 min read
Z algorithm (Linear time pattern searching Algorithm)
This algorithm efficiently locates all instances of a specific pattern within a text in linear time. If the length of the text is "n" and the length of the pattern is "m," then the total time taken is O(m + n), with a linear auxiliary space. It is worth noting that the time and auxiliary space of this algorithm is the same as the KMP algorithm, but
13 min read
Karatsuba algorithm for fast multiplication using Divide and Conquer algorithm
Given two binary strings that represent value of two integers, find the product of two strings. For example, if the first bit string is "1100" and second bit string is "1010", output should be 120. For simplicity, let the length of two strings be same and be n. A Naive Approach is to follow the process we study in school. One by one take all bits o
15+ min read
Reversal algorithm for Array rotation
Given an array arr[] of size N, the task is to rotate the array by d position to the left. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6, 7}, d = 2Output: 3, 4, 5, 6, 7, 1, 2Explanation: If the array is rotated by 1 position to the left, it becomes {2, 3, 4, 5, 6, 7, 1}.When it is rotated further by 1 position,it becomes: {3, 4, 5, 6, 7, 1, 2} Input:
15 min read
Block swap algorithm for array rotation
Write a function rotate(arr[], d, n) that rotates arr[] of size n by d elements. Rotation of the above array by 2 will make an array Algorithm : Initialize A = arr[0..d-1] and B = arr[d..n-1] 1) Do following until size of A is equal to size of B a) If A is shorter, divide B into Bl and Br such that Br is of same length as A. Swap A and Br to change
15+ min read
Boruvka's algorithm for Minimum Spanning Tree
Following two algorithms are generally taught for Minimum Spanning Tree (MST) problem. Prim's algorithm Kruskal's algorithm There is a third algorithm called Boruvka's algorithm for MST which (like the above two) is also Greedy algorithm. The Boruvka's algorithm is the oldest minimum spanning tree algorithm was discovered by Boruvka in 1926, long b
1 min read
Introduction to Push Relabel Algorithm
Given a graph which represents a flow network where every edge has a capacity. Also given two vertices source ‘s’ and sink ‘t’ in the graph, find the maximum possible flow from s to t with following constraints: Flow on an edge doesn’t exceed the given capacity of the edge. Incoming flow is equal to outgoing flow for every vertex except s and t. Fo
6 min read
An in-place algorithm for String Transformation
Given a string, move all even positioned elements to the end of the string. While moving elements, keep the relative order of all even positioned and odd positioned elements the same. For example, if the given string is "a1b2c3d4e5f6g7h8i9j1k2l3m4", convert it to "abcdefghijklm1234567891234" in-place and in O(n) time complexity. Below are the steps
15+ min read
Union By Rank and Path Compression in Union-Find Algorithm
In the previous post, we introduced union find algorithm and used it to detect cycles in a graph. We used the following union() and find() operations for subsets. C/C++ Code // Naive implementation of find int find(int parent[], int i) { if (parent[i] == -1) return i; return find(parent, parent[i]); } // Naive implementation of union() void Union(i
15+ min read
Closest Pair of Points using Divide and Conquer algorithm
We are given an array of n points in the plane, and the problem is to find out the closest pair of points in the array. This problem arises in a number of applications. For example, in air-traffic control, you may want to monitor planes that come too close together, since this may indicate a possible collision. Recall the following formula for dist
15+ min read
Fleury's Algorithm for printing Eulerian Path or Circuit
Eulerian Path is a path in a graph that visits every edge exactly once. Eulerian Circuit is an Eulerian Path that starts and ends on the same vertex. We strongly recommend first reading the following post on Euler Path and Circuit. "https://www.cdn.geeksforgeeks.org/eulerian-path-and-circuit/" In the above-mentioned post, we discussed the problem o
15+ min read
Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 3
In Manacher's Algorithm Part 1 and Part 2, we gone through some of the basics, understood LPS length array and how to calculate it efficiently based on four cases. Here we will implement the same.We have seen that there are no new character comparison needed in case 1 and case 2. In case 3 and case 4, necessary new comparison are needed. In followi
15+ min read
Boruvka's algorithm | Greedy Algo-9
We have discussed the following topics on Minimum Spanning Tree.Applications of Minimum Spanning Tree Problem Kruskal’s Minimum Spanning Tree Algorithm Prim’s Minimum Spanning Tree AlgorithmIn this post, Boruvka's algorithm is discussed. Like Prim's and Kruskal's, Boruvka’s algorithm is also a Greedy algorithm. Below is a complete algorithm. 1) Inp
15+ min read