diff --git a/cpp/matrix/AdjacencyMatrix.cpp b/cpp/matrix/AdjacencyMatrix.cpp new file mode 100644 index 000000000..46707adaf --- /dev/null +++ b/cpp/matrix/AdjacencyMatrix.cpp @@ -0,0 +1,62 @@ +// Adjacency Matrix representation in C++ + +#include +using namespace std; + +class Graph { + private: + bool** adjMatrix; + int numVertices; + + public: + // Initialize the matrix to zero + Graph(int numVertices) { + this->numVertices = numVertices; + adjMatrix = new bool*[numVertices]; + for (int i = 0; i < numVertices; i++) { + adjMatrix[i] = new bool[numVertices]; + for (int j = 0; j < numVertices; j++) + adjMatrix[i][j] = false; + } + } + + // Add edges + void addEdge(int i, int j) { + adjMatrix[i][j] = true; + adjMatrix[j][i] = true; + } + + // Remove edges + void removeEdge(int i, int j) { + adjMatrix[i][j] = false; + adjMatrix[j][i] = false; + } + + // Print the martix + void toString() { + for (int i = 0; i < numVertices; i++) { + cout << i << " : "; + for (int j = 0; j < numVertices; j++) + cout << adjMatrix[i][j] << " "; + cout << "\n"; + } + } + + ~Graph() { + for (int i = 0; i < numVertices; i++) + delete[] adjMatrix[i]; + delete[] adjMatrix; + } +}; + +int main() { + Graph g(4); + + g.addEdge(0, 1); + g.addEdge(0, 2); + g.addEdge(1, 2); + g.addEdge(2, 0); + g.addEdge(2, 3); + + g.toString(); +} \ No newline at end of file diff --git a/cpp/matrix/MatrixAdd.cpp b/cpp/matrix/MatrixAdd.cpp new file mode 100644 index 000000000..b015f98c7 --- /dev/null +++ b/cpp/matrix/MatrixAdd.cpp @@ -0,0 +1,49 @@ +#include +using namespace std; + +int main() +{ + int r, c, a[100][100], b[100][100], sum[100][100], i, j; + + cout << "Enter number of rows (between 1 and 100): "; + cin >> r; + + cout << "Enter number of columns (between 1 and 100): "; + cin >> c; + + cout << endl << "Enter elements of 1st matrix: " << endl; + + // Storing elements of first matrix entered by user. + for(i = 0; i < r; ++i) + for(j = 0; j < c; ++j) + { + cout << "Enter element a" << i + 1 << j + 1 << " : "; + cin >> a[i][j]; + } + + // Storing elements of second matrix entered by user. + cout << endl << "Enter elements of 2nd matrix: " << endl; + for(i = 0; i < r; ++i) + for(j = 0; j < c; ++j) + { + cout << "Enter element b" << i + 1 << j + 1 << " : "; + cin >> b[i][j]; + } + + // Adding Two matrices + for(i = 0; i < r; ++i) + for(j = 0; j < c; ++j) + sum[i][j] = a[i][j] + b[i][j]; + + // Displaying the resultant sum matrix. + cout << endl << "Sum of two matrix is: " << endl; + for(i = 0; i < r; ++i) + for(j = 0; j < c; ++j) + { + cout << sum[i][j] << " "; + if(j == c - 1) + cout << endl; + } + + return 0; +} \ No newline at end of file diff --git a/cpp/matrix/MatrixSub.cpp b/cpp/matrix/MatrixSub.cpp new file mode 100644 index 000000000..e2eb303ca --- /dev/null +++ b/cpp/matrix/MatrixSub.cpp @@ -0,0 +1,49 @@ +#include +using namespace std; + +int main() +{ + int r, c, a[100][100], b[100][100], sum[100][100], i, j; + + cout << "Enter number of rows (between 1 and 100): "; + cin >> r; + + cout << "Enter number of columns (between 1 and 100): "; + cin >> c; + + cout << endl << "Enter elements of 1st matrix: " << endl; + + // Storing elements of first matrix entered by user. + for(i = 0; i < r; ++i) + for(j = 0; j < c; ++j) + { + cout << "Enter element a" << i + 1 << j + 1 << " : "; + cin >> a[i][j]; + } + + // Storing elements of second matrix entered by user. + cout << endl << "Enter elements of 2nd matrix: " << endl; + for(i = 0; i < r; ++i) + for(j = 0; j < c; ++j) + { + cout << "Enter element b" << i + 1 << j + 1 << " : "; + cin >> b[i][j]; + } + + // Subtracting Two matrices + for(i = 0; i < r; ++i) + for(j = 0; j < c; ++j) + sum[i][j] = a[i][j] - b[i][j]; + + // Displaying the resultant sum matrix. + cout << endl << "Sum of two matrix is: " << endl; + for(i = 0; i < r; ++i) + for(j = 0; j < c; ++j) + { + cout << sum[i][j] << " "; + if(j == c - 1) + cout << endl; + } + + return 0; +} \ No newline at end of file diff --git a/cpp/matrix/matrixMul.cpp b/cpp/matrix/matrixMul.cpp new file mode 100644 index 000000000..37b058c06 --- /dev/null +++ b/cpp/matrix/matrixMul.cpp @@ -0,0 +1,48 @@ +#include +using namespace std; +int main() +{ +int a[10][10],b[10][10],mul[10][10],r,c,i,j,k; +cout<<"enter the number of row="; +cin>>r; +cout<<"enter the number of column="; +cin>>c; +cout<<"enter the first matrix element=\n"; +for(i=0;i>a[i][j]; +} +} +cout<<"enter the second matrix element=\n"; +for(i=0;i>b[i][j]; +} +} +cout<<"multiply of the matrix=\n"; +for(i=0;i +using namespace std; + +vector spiralOrder(vector >& matrix) +{ + int m = matrix.size(), n = matrix[0].size(); + vector ans; + + if (m == 0) + return ans; + + vector > seen(m, vector(n, false)); + int dr[] = { 0, 1, 0, -1 }; + int dc[] = { 1, 0, -1, 0 }; + + int x = 0, y = 0, di = 0; + + // Iterate from 0 to m * n - 1 + for (int i = 0; i < m * n; i++) { + ans.push_back(matrix[x][y]); + // on normal geeksforgeeks ui page it is showing + // 'ans.push_back(matrix[x])' which gets copied as + // this only and gives error on compilation, + seen[x][y] = true; + int newX = x + dr[di]; + int newY = y + dc[di]; + + if (0 <= newX && newX < m && 0 <= newY && newY < n + && !seen[newX][newY]) { + x = newX; + y = newY; + } + else { + di = (di + 1) % 4; + x += dr[di]; + y += dc[di]; + } + } + return ans; +} + +// Driver code +int main() +{ + vector > a{ { 1, 2, 3, 4 }, + { 5, 6, 7, 8 }, + { 9, 10, 11, 12 }, + { 13, 14, 15, 16 } }; + + // Function call + for (int x : spiralOrder(a)) { + cout << x << " "; + } + return 0; +} + +// This code is contributed by Yashvendra Singh diff --git a/java/matrix/AdjMatrix.java b/java/matrix/AdjMatrix.java new file mode 100644 index 000000000..528321e53 --- /dev/null +++ b/java/matrix/AdjMatrix.java @@ -0,0 +1,49 @@ +// Adjacency Matrix representation in Java + +public class Graph { + private boolean adjMatrix[][]; + private int numVertices; + + // Initialize the matrix + public Graph(int numVertices) { + this.numVertices = numVertices; + adjMatrix = new boolean[numVertices][numVertices]; + } + + // Add edges + public void addEdge(int i, int j) { + adjMatrix[i][j] = true; + adjMatrix[j][i] = true; + } + + // Remove edges + public void removeEdge(int i, int j) { + adjMatrix[i][j] = false; + adjMatrix[j][i] = false; + } + + // Print the matrix + public String toString() { + StringBuilder s = new StringBuilder(); + for (int i = 0; i < numVertices; i++) { + s.append(i + ": "); + for (boolean j : adjMatrix[i]) { + s.append((j ? 1 : 0) + " "); + } + s.append("\n"); + } + return s.toString(); + } + + public static void main(String args[]) { + Graph g = new Graph(4); + + g.addEdge(0, 1); + g.addEdge(0, 2); + g.addEdge(1, 2); + g.addEdge(2, 0); + g.addEdge(2, 3); + + System.out.print(g.toString()); + } +} \ No newline at end of file diff --git a/java/matrix/Identity.java b/java/matrix/Identity.java new file mode 100644 index 000000000..bd7895878 --- /dev/null +++ b/java/matrix/Identity.java @@ -0,0 +1,44 @@ +public class IdentityMatrix +{ + public static void main(String[] args) { + int rows, cols; + boolean flag = true; + + //Initialize matrix a + int a[][] = { + {1, 0, 0}, + {0, 1, 0}, + {0, 0, 1} + }; + + //Calculates the number of rows and columns present in the given matrix + + rows = a.length; + cols = a[0].length; + + //Checks whether given matrix is a square matrix or not + if(rows != cols){ + System.out.println("Matrix should be a square matrix"); + } + else { + //Checks if diagonal elements are equal to 1 and rest of elements are 0 + for(int i = 0; i < rows; i++){ + for(int j = 0; j < cols; j++){ + if(i == j && a[i][j] != 1){ + flag = false; + break; + } + if(i != j && a[i][j] != 0){ + flag = false; + break; + } + } + } + + if(flag) + System.out.println("Given matrix is an identity matrix"); + else + System.out.println("Given matrix is not an identity matrix"); + } + } +} \ No newline at end of file diff --git a/java/matrix/MatrixAddition.java b/java/matrix/MatrixAddition.java new file mode 100644 index 000000000..ddc0a2d5c --- /dev/null +++ b/java/matrix/MatrixAddition.java @@ -0,0 +1,18 @@ +public class MatrixAdditionExample{ +public static void main(String args[]){ +//creating two matrices +int a[][]={{1,3,4},{2,4,3},{3,4,5}}; +int b[][]={{1,3,4},{2,4,3},{1,2,4}}; + +//creating another matrix to store the sum of two matrices +int c[][]=new int[3][3]; //3 rows and 3 columns + +//adding and printing addition of 2 matrices +for(int i=0;i<3;i++){ +for(int j=0;j<3;j++){ +c[i][j]=a[i][j]+b[i][j]; //use - for subtraction +System.out.print(c[i][j]+" "); +} +System.out.println();//new line +} +}} \ No newline at end of file diff --git a/java/matrix/Transpose.java b/java/matrix/Transpose.java new file mode 100644 index 000000000..86c40b7d5 --- /dev/null +++ b/java/matrix/Transpose.java @@ -0,0 +1,30 @@ +public class MatrixTransposeExample{ +public static void main(String args[]){ +//creating a matrix +int original[][]={{1,3,4},{2,4,3},{3,4,5}}; + +//creating another matrix to store transpose of a matrix +int transpose[][]=new int[3][3]; //3 rows and 3 columns + +//Code to transpose a matrix +for(int i=0;i<3;i++){ +for(int j=0;j<3;j++){ +transpose[i][j]=original[j][i]; +} +} + +System.out.println("Printing Matrix without transpose:"); +for(int i=0;i<3;i++){ +for(int j=0;j<3;j++){ +System.out.print(original[i][j]+" "); +} +System.out.println();//new line +} +System.out.println("Printing Matrix After Transpose:"); +for(int i=0;i<3;i++){ +for(int j=0;j<3;j++){ +System.out.print(transpose[i][j]+" "); +} +System.out.println();//new line +} +}} \ No newline at end of file diff --git a/java/matrix/matrixSub.java b/java/matrix/matrixSub.java new file mode 100644 index 000000000..828a60a89 --- /dev/null +++ b/java/matrix/matrixSub.java @@ -0,0 +1,18 @@ +public class MatrixSubExample{ +public static void main(String args[]){ +//creating two matrices +int a[][]={{1,3,4},{2,4,3},{3,4,5}}; +int b[][]={{1,3,4},{2,4,3},{1,2,4}}; + +//creating another matrix to store the difference of two matrices +int c[][]=new int[3][3]; //3 rows and 3 columns + +//Subtracting and printing addition of 2 matrices +for(int i=0;i<3;i++){ +for(int j=0;j<3;j++){ +c[i][j]=a[i][j]-b[i][j]; //use - for subtraction +System.out.print(c[i][j]+" "); +} +System.out.println();//new line +} +}} \ No newline at end of file diff --git a/java/matrix/spiral.java b/java/matrix/spiral.java new file mode 100644 index 000000000..28e496433 --- /dev/null +++ b/java/matrix/spiral.java @@ -0,0 +1,102 @@ +public class SpiralPatternExample1 +{ +//defining method to print the spiral pattern or matrix +static void printSpiralPattern(int size) +{ +//create two variables row and col to traverse rows and columns +int row = 0, col = 0; +int boundary = size - 1; +int sizeLeft = size - 1; +int flag = 1; +//variables r, l, u and d are used to determine the movement +// r = right, l = left, d = down, u = upper +char move = 'r'; +//creating a 2D array for matrix +int[][] matrix =new int [size][size]; +for (int i = 1; i < size * size + 1; i++) +{ + //assigning values + matrix[row][col] = i; +//switch-case to determine the next index +switch (move) +{ +//if right, go right +case 'r': + col += 1; + break; +//if left, go left +case 'l': + col -= 1; + break; +//if up, go up +case 'u': + row -= 1; + break; +//if down, go down +case 'd': + row += 1; + break; +} +//checks if the matrix has reached the array boundary +if (i == boundary) + { + //adds the left size for the next boundary + boundary = boundary + sizeLeft; + //decrease the size left by 1, if 2 rotations have been made + if (flag != 2) + { + flag = 2; + } + else + { + flag = 1; + sizeLeft -= 1; + } + //switch-case to rotate the movement + switch (move) + { + //if right, rotate to down + case 'r': + move = 'd'; + break; + // if down, rotate to left + case 'd': + move = 'l'; + break; + // if left, rotate to up + case 'l': + move = 'u'; + break; + // if up, rotate to right + case 'u': + move = 'r'; + break; + } + } + } +//printing the spiral matrix or pattern +//outer for loop for rows + for (row = 0; row < size; row++) + { + //inner for loop for columns + for (col = 0; col < size; col++) + { + int n = matrix[row][col]; + if(n < 10) + System.out.print(n +" "); + else + System.out.print(n +" "); + } + System.out.println(); + } +} +//driver Code +public static void main(String args[]) +{ +//size of the array?s row and column +int size = 5; +System.out.println("Spiral Matrix or Pattern is: \n"); +//calling the method that prints the spiral pattern or matrix +printSpiralPattern(size); +} +} \ No newline at end of file diff --git a/java/matrix/spiral2.java b/java/matrix/spiral2.java new file mode 100644 index 000000000..42e84a609 --- /dev/null +++ b/java/matrix/spiral2.java @@ -0,0 +1,32 @@ +import java.util.Scanner; +import java.lang.Math; +public class SpiralPatternExample2 +{ +//function to print the spiral pattern +public static void printPattern(int n) +{ +//detrmines the boundary size of the array +int size = 2 * n - 1; +//inner loop +for(int i = 1; i <= size; i++) +{ +//outer loop +for(int j = 1; j <= size; j++) +{ +//calculates and prints the values for pattern +System.out.print(Math.max(Math.abs(i - n), Math.abs(j - n)) + 1 + " "); +} +System.out.println(); +} +} +//driver code +public static void main(String args[]) +{ +Scanner sc = new Scanner(System.in); +System.out.print("Enter the value of n: "); +int n = sc.nextInt(); +System.out.println(); +//function calling +printPattern(n); +} +} \ No newline at end of file diff --git a/java/matrix/spiral3.java b/java/matrix/spiral3.java new file mode 100644 index 000000000..3be42ae3e --- /dev/null +++ b/java/matrix/spiral3.java @@ -0,0 +1,45 @@ +public class SprialPatternExample3 +{ +public static void main(String args[]) +{ + int SIZE=10; + int i, j, N; + int[][] board = new int[SIZE][SIZE]; + int left, top; + left = 0; + top = SIZE - 1; + N = 1; + for(i=1; i<=SIZE/2; i++, left++, top--) + { + //fill from left to right + for(j=left; j<=top; j++, N++) + { + board[left][j] = N; + } + //fill from top to down + for(j=left+1; j<=top; j++, N++) + { + board[j][top] = N; + } + //fill from right to left + for(j=top-1; j>=left; j--, N++) + { + board[top][j] = N; + } + //fill from down to top + for(j=top-1; j>=left+1; j--, N++) + { + board[j][left] = N; + } + } + //print the pattern + for(i=0; i > v + = new ArrayList<>(Arrays.asList( + new ArrayList<>(Arrays.asList(5, 4, 7)), + new ArrayList<>(Arrays.asList(1, 3, 8)), + new ArrayList<>(Arrays.asList(2, 9, 6)))); + + int n = v.size(); + List x = new ArrayList<>(); + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + x.add(v.get(i).get(j)); + } + } + Collections.sort(x); + int k = 0; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + v.get(i).set(j, x.get(k++)); + } + } + + System.out.println("Sorted Matrix Will be:"); + for (List row : v) { + for (int num : row) { + System.out.print(num + " "); + } + System.out.println(); + } + } +}