Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- string
- simulation
- hash table
- Counting
- 자바
- dynamic programming
- implement
- Binary Tree
- 구현
- Binary Search
- bit manipulation
- database
- 파이썬
- java
- two pointers
- 코테
- Math
- Method
- 코딩테스트
- Number Theory
- Tree
- Matrix
- Stack
- Class
- SQL
- greedy
- geometry
- sorting
- array
- Data Structure
Archives
- Today
- Total
코린이의 소소한 공부노트
Matrix Multiplication 본문
1. Problem
- 두 n*n 행렬의 곱(product)을 구해보자
2. Input
1) 양수 n
2) 2차원 배열 A indexed from 1 to n
3) 2차원 배열 B indexed from 1 to n
3. Output
1) A*B의 결과가 담긴 2차원 배열 C indexed from 1 to n
4. PseudoCode
void matrixmult(int n, const number[][] A, const number[][] B, number[][] C){
index i, j, k;
for(i=1 ; i<=n ; i++)
for(j=1 ; j<=n ; j++){
C[i][j] = 0;
for(k=1 ; k<=n ; k++)
C[i][j] = C[i][j] + A[i][k]*B[k][j];
}
}
5. Example
class Test {
public static void main(String[] args){
int[][] A = {{2,3},{4,1}};
int[][] B = {{5,7},{6,8}};
int[][] C = new int[2][2]; // C[i][j] == 0
matrixmult(A.length, A, B, C);
for(int i=0 ; i<A.length ; i++) {
for(int j=0 ; j<A.length ; j++)
System.out.print(C[i][j] + " ");
System.out.println();
}
// C의 요소 출력 결과
// 28 38
// 26 36
}
static void matrixmult(int n, int[][] A, int[][] B, int[][] C){
for(int i=0 ; i<n ; i++) {
for(int j=0 ; j<n ; j++){
for(int k=0 ; k<n ; k++)
C[i][j] += A[i][k]*B[k][j];
} // j
} // i
}
}
- T(n) = n^3
'Back-End > Algorithm' 카테고리의 다른 글
n-th Fibonacci Term (Recursive) (0) | 2023.02.22 |
---|---|
Binary Search (iterative) (0) | 2023.02.22 |
Exchange Sort (0) | 2023.02.22 |
Add Array Members (0) | 2023.02.22 |
Sequential Search (0) | 2023.02.22 |