chore(CPlusPlus): add matrix multiplication (#527)

pull/613/head
Akanksha Shukla 2021-10-08 18:29:37 +05:30 committed by GitHub
parent 745fa3d92d
commit cf7a713fd9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 254 additions and 0 deletions

View File

@ -0,0 +1,78 @@
//given two matrices of sizes n*m and n1*m1
//find multiplication of the two matrices and print it
#include <bits/stdc++.h>
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<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>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<n1;i++)
{
for(int j=0;j<m1;j++)
{
cin>>b[i][j];
}
}
if(m!=n1)
{
cout<<"Multiplication of the matrices is not possible";
}
else
{
int c[n][m1];
for(int i=0;i<n;i++)
{
for(int j=0;j<m1;j++)
{
c[i][j]=0;
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m1;j++)
{
for(int k=0;k<n1;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m1;j++)
{
cout<<c[i][j]<<" ";
}
cout<<"\n";
}
}
return 0;
}
//INPUT
//2 3
//1 2 1
//3 4 1
//3 2
//1 2
//1 1
//3 7
//OUTPUT
//6 11
//10 17
//TIME COMPLEXITY OF THE PROGRAM
//O(n^3)

View File

@ -0,0 +1,54 @@
//dfs traversal of undirected graph using stack approach
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;//enter the number of vertices and number of edges
vector<vector<int>>adj(n+1,vector<int>());
//create an adjacency list according to the edges given in input
for(int i=0;i<m;i++)
{
int u,v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
cout<<"The dfs traversal is \n";
vector<int>v(n+1,0);
stack<int>s;
s.push(1);
while(!s.empty())
{
int t=s.top();
if(!v[t])
{
v[t]=1;//mark the vertex as visited
cout<<t<<" ";//print the vertex you visited
}
s.pop();
for(int i=0;i<adj[t].size();i++)
{
if(!v[adj[t][i]])
{
s.push(adj[t][i]);//if a vertex is not visited push it in stack
}
}
}
return 0;
}
//INPUT
//6 5
//1 2
//2 6
//1 3
//3 4
//3 5
//OUTPUT
//The dfs traversal is
//1 3 5 4 2 6
//TIME COMPLEXITY OF THE PROGRAM
//O(V+E)
//where V is number of vertices
//E is number of edges

View File

@ -0,0 +1,49 @@
//dfs traversal of undirected graph using recursive approach
#include <bits/stdc++.h>
using namespace std;
void dfs(vector<vector<int>>&adj, vector<int>&v,int x)
{
v[x]=1;
cout<<x<<" ";//print the vertex visited
for(int i=0;i<adj[x].size();i++)
{
if(v[adj[x][i]]==0)
{
dfs(adj,v,adj[x][i]);//call the dfs function if a node is not visited
}
}
}
int main()
{
int n,m;
cin>>n>>m;//enter the number of vertices and number of edges
vector<vector<int>>adj(n+1,vector<int>());
//create an adjacency list according to the edges given in input
for(int i=0;i<m;i++)
{
int u,v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
cout<<"The dfs traversal is \n";
vector<int>v(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

View File

@ -0,0 +1,69 @@
//find total number of Connected Components in a graph
//implementation of dsu
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
ll find(ll x,vector<ll>&p)
{
if(p[x]==x)
return x;
else
return p[x]=find(p[x],p);
}
void merge(ll a,ll b,vector<ll>&r,vector<ll>&p)
{
ll s1=find(a,p);
ll s2=find(b,p);
if(s1!=s2)
{
if(r[s1]<r[s2])
{
p[s1]=s2;
r[s2]+=r[s1];
}
else
{
p[s2]=s1;
r[s1]+=r[s2];
}
}
}
int main()
{
int n,m;
cin>>n>>m;//enter the number of vertices and number of edges
vector<ll>r(n,0);
vector<ll>p;
for(int i=0;i<n;i++)
{
p.push_back(i);
}
for(int i=0;i<m;i++)
{
int u,v;
cin>>u>>v;
u--;
v--;
merge(u,v,r,p);
}
map<ll,ll>mp;
for(int i=0;i<n;i++)
{
ll x=find(i,p);
mp[x]++;
}
cout<<"Total Number of Connected Components are - ";
cout<<mp.size();
return 0;
}
//INPUT
//5 3
//1 2
//2 3
//4 5
//OUTPUT
//Total Number of Connected Components are - 2
//TIME COMPLEXITY OF THE PROGRAM
//O(V+E)
//where V is number of vertices
//E is number of edges

View File

@ -19,6 +19,7 @@
15. [Occurrence of one in sorted array](Arrays/occurence-of-one-in-sorted-array.cpp)
16. [Segregate 0s and 1s](Arrays/segregate-0-and-1.cpp)
17. [Search insert position](Arrays/search-insert-position.cpp)
18. [Matrix Multiplication](Arrays/matrix-multiplication.cpp)
## Dynamic-Programming
@ -34,6 +35,9 @@
2. [kruskal Algorithm](Graphs/kruskal-algorithm.cpp)
3. [Breadth First Search](Graphs/breadth-first-search.cpp)
4. [Topological sort](Graphs/topological-sort.cpp)
5. [Dfs traversal with stack](Graphs/dfs-through-stackdatastructure.cpp)
6. [Dfs traversal with recursion](Graphs/dfs-traversal.cpp)
7. [Connected Components](Graphs/total-connected-components.cpp)
## Multiplication