From cf7a713fd940ff6ce6bcd7d4d67c2d245cc9a174 Mon Sep 17 00:00:00 2001 From: Akanksha Shukla <63357474+sakanksha19@users.noreply.github.com> Date: Fri, 8 Oct 2021 18:29:37 +0530 Subject: [PATCH] chore(CPlusPlus): add matrix multiplication (#527) --- .../Arrays/matrix-multiplication.cpp | 78 +++++++++++++++++++ .../Graphs/dfs-through-stackdatastructure.cpp | 54 +++++++++++++ algorithms/CPlusPlus/Graphs/dfs-traversal.cpp | 49 ++++++++++++ .../Graphs/total-connected-components.cpp | 69 ++++++++++++++++ algorithms/CPlusPlus/README.md | 4 + 5 files changed, 254 insertions(+) create mode 100644 algorithms/CPlusPlus/Arrays/matrix-multiplication.cpp create mode 100644 algorithms/CPlusPlus/Graphs/dfs-through-stackdatastructure.cpp create mode 100644 algorithms/CPlusPlus/Graphs/dfs-traversal.cpp create mode 100644 algorithms/CPlusPlus/Graphs/total-connected-components.cpp diff --git a/algorithms/CPlusPlus/Arrays/matrix-multiplication.cpp b/algorithms/CPlusPlus/Arrays/matrix-multiplication.cpp new file mode 100644 index 00000000..9acfccf5 --- /dev/null +++ b/algorithms/CPlusPlus/Arrays/matrix-multiplication.cpp @@ -0,0 +1,78 @@ +//given two matrices of sizes n*m and n1*m1 +//find multiplication of the two matrices and print it +#include +using namespace std; + +int main() +{ + int n,m,n1,m1; + // cout<<"Enter the dimensions of first matrix\n"; + cin>>n>>m; + int a[n][m]; + // cout<<"Enter the elements of first matrix\n"; + for(int i=0;i>a[i][j]; + } + } + //cout<<"Enter the dimensions of Second matrix\n"; + cin>>n1>>m1; + int b[n1][m1]; + //cout<<"Enter the elements of second matrix\n"; + for(int i=0;i>b[i][j]; + } + } + if(m!=n1) + { + cout<<"Multiplication of the matrices is not possible"; + } + else + { + int c[n][m1]; + for(int i=0;i +using namespace std; + +int main() +{ + int n,m; + cin>>n>>m;//enter the number of vertices and number of edges + vector>adj(n+1,vector()); + //create an adjacency list according to the edges given in input + for(int i=0;i>u>>v; + adj[u].push_back(v); + adj[v].push_back(u); + } + cout<<"The dfs traversal is \n"; + vectorv(n+1,0); + stacks; + s.push(1); + while(!s.empty()) + { + int t=s.top(); + if(!v[t]) + { + v[t]=1;//mark the vertex as visited + cout< +using namespace std; + +void dfs(vector>&adj, vector&v,int x) +{ + v[x]=1; + cout<>n>>m;//enter the number of vertices and number of edges + vector>adj(n+1,vector()); + //create an adjacency list according to the edges given in input + for(int i=0;i>u>>v; + adj[u].push_back(v); + adj[v].push_back(u); + } + cout<<"The dfs traversal is \n"; + vectorv(n+1,0); + dfs(adj,v,1); + return 0; +} + +//INPUT +//6 5 +//1 2 +//2 6 +//1 3 +//3 4 +//3 5 +//OUTPUT +//The dfs traversal is +//1 2 6 3 4 5 +//TIME COMPLEXITY OF THE PROGRAM +//O(V+E) +//where V is number of vertices +//and E is number of edges diff --git a/algorithms/CPlusPlus/Graphs/total-connected-components.cpp b/algorithms/CPlusPlus/Graphs/total-connected-components.cpp new file mode 100644 index 00000000..fb784be9 --- /dev/null +++ b/algorithms/CPlusPlus/Graphs/total-connected-components.cpp @@ -0,0 +1,69 @@ +//find total number of Connected Components in a graph +//implementation of dsu +#include +#define ll long long int +using namespace std; +ll find(ll x,vector&p) +{ + if(p[x]==x) + return x; + else + return p[x]=find(p[x],p); +} +void merge(ll a,ll b,vector&r,vector&p) +{ + ll s1=find(a,p); + ll s2=find(b,p); + if(s1!=s2) + { + if(r[s1]>n>>m;//enter the number of vertices and number of edges + vectorr(n,0); + vectorp; + for(int i=0;i>u>>v; + u--; + v--; + merge(u,v,r,p); + } + mapmp; + for(int i=0;i