Posts

Showing posts from January, 2018

Biconnected components & Articulation points

#include<stdio.h> #include<stdlib.h> #define max 20 typedef struct { int u,v; }edge; int dfn[max],l[max],num=1,n; int a[max][max]; edge stack[max]; int top=-1; void push(edge e) {     if(top==max-1)     printf("Stack is full");     else     stack[++top]=e;  } edge pop() { edge e;   if(top==-1)     printf("stack is empty");     else     e=stack[top--];   return e; } void bicomp(int u,int v) { int w;   edge e;   dfn[u]=num;l[u]=num;num++;   for(w=1;w<=n;w++)   if(a[u][w]==1)   { if((v!=w) && (dfn[w]<dfn[u]))     { e.u=u; e.v=w;       printf("\nEdge %d -- %d is pushed to stack",e.u,e.v);       push(e);     }     if(dfn[w]==0)     { bicomp(w,u);       ...

BINARY TREE TRAVERSALS.

#include<stdio.h> #include<stdlib.h> struct NODE { struct NODE *pleft,*pright; int ch; }; struct NODE *proot; int top=-1,n; struct NODE * stack[50]; void push(struct NODE * r){ stack[++top]=r; return ; } struct NODE *pop(){ struct NODE * k; if(top<0) k=NULL; else{ k=stack[top]; top--; } return k; } struct NODE* insert(struct NODE* proot, int v) {     if(proot==NULL)     {         proot = (struct NODE*) malloc(sizeof(struct NODE));         proot->ch = v;         proot->pleft = NULL;         proot->pright = NULL;     }     else if(v < proot->ch){         proot->pleft = insert(proot->pleft, v);     }     else {         proot->pright = insert(proot->pright, v);     }     return proot; } void inord...

MERGE SORT USING PARALLEL PROGRAMMING.

#include<stdio.h> #include<omp.h> #include<stdlib.h> void merge(int arr[],int first,int mid,int last){ int i,j,k; i=first; k=first; j=mid+1; int t[first+last]; while((i<=mid)&&(j<=last)){ if(arr[i]<arr[j]){ t[k]=arr[i]; i++; } else{ t[k]=arr[j]; j++; } k++; } while(i<mid+1){ t[k]=arr[i];i++; k++; } while(j<last+1){ t[k]=arr[j];j++; k++; } for(i=first;i<=last;i++){ arr[i]=t[i]; } return ; } void merge_sort(int arr[],int first,int last){ if(first<last){ int mid; mid=(first+last)/2; #pragma omp task firstprivate(arr,first,last) merge_sort(arr,first,mid); #pragma omp task firstprivate(arr,first,last) merge_sort(arr,mid+1,last); #pragma omp tastwait merge(arr,first,mid,last); } } int main() { int i,n; double s,c; printf("Enter number of elements :\n"); scanf("%d",&n); int arr[n]; for(i=0;i<n;i++){...

QUICK SORT METHOD TO DETERMINE TIME CONSUMED.

#include<stdio.h> #include<time.h> #include<stdlib.h> int partition(int list[],int first,int last){  int t,loc=list[(first+last)/2];   while(first<=last){    while(list[first]<loc)    first++;    while(list[last]>loc)    last--;    if(first<=last){     t=list[first];     list[first]=list[last];     list[last]=t;     first++;     last--;    }   } return first; } void quick_sort(int arr[],int first,int last){  int loc;  if(first>=last)  return ;   loc=partition(arr,first,last);   quick_sort(arr,first,loc-1);   quick_sort(arr,loc,last);  return ; } int main() { int i,n; clock_t s,c; printf("Enter number of elements :\n"); scanf("%d",&n); int arr[n]; for(i=0;i<n;i++){ arr[i]=rand()%100; } s=clock(); quick_sort(arr,0,n-1); c=clock(); float ...