Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions C++/kosarajus_algorithm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include<bits/stdc++.h>
using namespace std;
void DFS(vector<vector<int>> grid,stack<int> &temp,int start,vector<bool> &visited)
{
visited[start]=true;
for(int i=0;i<grid.size();i++)
if(grid[start][i]==1 and i!=start and visited[i]==false)
{
DFS(grid,temp,i,visited);
}
temp.push(start);
}
void DFS(vector<vector<int>> grid,int start,vector<bool> &visited)
{
visited[start]=true;
for(int i=0;i<grid.size();i++)
if(grid[start][i]==1 and i!=start and visited[i]==false)
{
DFS(grid,i,visited);
}
cout<<start<<",";
}
void kosaraju(vector<vector<int>> grid,int n)
{
stack<int> temp;
vector<bool> visited={false,false,false,false,false,false,false,false};
// visited[0]=true;
DFS(grid,temp,0,visited);
for(int i=0;i<n;i++)
if(visited[i]==false)
DFS(grid,temp,i,visited);
for(int i=0;i<n;i++)
for(int j=0;j<i;j++)
{
int temp = grid[i][j];
grid[i][j]=grid[j][i];
grid[j][i]=temp;
}
visited={false,false,false,false,false,false,false,false};
int start=temp.top();
temp.pop();
cout<<"(";
DFS(grid,start,visited);
cout<<")";
while(temp.size()>0)
{
start=temp.top();
temp.pop();
if(visited[start]==false)
{
cout<<"(";
DFS(grid,start,visited);
cout<<")";
}
}
}
int main()
{
vector<vector<int>> grid={{0,1,0,0,0,0,0,0},
{0,0,1,0,0,0,0,0},
{1,0,0,1,0,0,0,0},
{0,0,0,0,1,0,0,0},
{0,0,0,0,0,1,0,1},
{0,0,0,0,0,0,1,0},
{0,0,0,0,1,0,0,1},
{0,0,0,0,0,0,0,0}
};
cout<<"Strongly connected components are: ";
kosaraju(grid,8);
return 0;
}
79 changes: 79 additions & 0 deletions MCM.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include<iostream>
using namespace std;
void paranthesis(int **para,int i,int j)
{
if(i==j)
cout<<"A"<<i<<" ";
else
{
cout<<"( ";
paranthesis(para,i,para[i][j]);
paranthesis(para,para[i][j]+1,j);
cout<<" ) ";
}
}
int main()
{
int n;
cout<<"Enter the number of matrices";
cin>>n;
int dim[n+1];
cout<<"Enter the dimensions: ";
for(int i=0;i<=n;i++)
{
cin>>dim[i];
}
int cost[n+1][n+1]={0};
int** para= new int*[n];
for(int i=0;i<=n;i++){
para[i] = new int[n+1];
for(int j=0;j<=n;j++)
para[i][j]=0;}
for(int g=1;g<=n;g++)
for(int i=1,j=g;j<=n;j++,i++)
{
if(i==j)
{
cost[i][j]=0;
para[i][j]=0;
}
else
{
int m=INT_MAX;
int p;
for(int k=i;k<j;k++)
{
int temp = cost[i][k]+cost[k+1][j]+dim[i-1]*dim[k]*dim[j];
if(m>temp)
{
m=temp;
p=k;
}
}
cost[i][j]=m;
para[i][j]=p;
}
}
cout<<endl;
cout<<"************** Cost Matrix ****************"<<endl;
for(int i=1;i<=n;i++){

for(int j=1;j<=n;j++)
{
cout<<cost[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
cout<<"************ paraenthesis Matrix ***********"<<endl;
for(int i=1;i<=n;i++){

for(int j=1;j<=n;j++)
{
cout<<para[i][j]<<" ";
}
cout<<endl;
}
paranthesis(para,1,4);
return 0;
}