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
61 changes: 61 additions & 0 deletions parenthesis checker
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include<iostream>
using namespace std;
#include<string.h>
#define n 20
char push(char);
char pop();
int top=-1;
int stack[20];
main()
{
char s[20],temp;
cout<<"enter expression"<<endl;
cin>>s;
int x=1,i;
for(i=0;i<strlen(s);i++)
{
if(s[i]=='('||s[i]=='{'||s[i]=='[')
push(s[i]);
if(s[i]==')'||s[i]=='}'||s[i]==']')
{
if(top==-1)
x=0;
else
{
temp=pop();
if(s[i]==')'&&(temp=='{'||temp=='['))
x=0;
if(s[i]=='}'&&(temp=='('||temp=='['))
x=0;
if(s[i]==']'&&(temp=='{'||temp=='('))
x=0;
}
}

}
if(top>=0)
x=0;
if(x==0)
cout<<"invalid\n";
else
cout<<"valid\n";
}
char push(char a)
{
if(top==n-1)
cout<<"stack overflow"<<endl;
else
{
top++;
stack[top]=a;
}
}
char pop()
{
if(top==-1)
cout<<"stack underflow"<<endl;
else
{
return(stack[top--]);
}
}
45 changes: 45 additions & 0 deletions transitive closure
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include<stdio.h>
#include<conio.h>
#include<iostream>
using namespace std;
void printSolution(int reach[][4])
{
cout<<"Following matrix is transitive closure of the given graph\n";
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
cout<<reach[i][j]<<" ";
}
cout<<endl;
}
}
void transitiveClosure(int graph[][4])
{
int reach[4][4], i, j, k;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
reach[i][j] = graph[i][j];
}
}
for (k = 0; k < 4; k++)
{
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
reach[i][j] = reach[i][j] || (reach[i][k] && reach[k][j]);
}
}
}
printSolution(reach);
}
int main()
{
int graph[4][4] = { {1, 1, 0, 1},{0, 1, 1, 0},{0, 0, 1, 1},{0, 0, 0, 1}};

transitiveClosure(graph);
getch();
}