Sunteți pe pagina 1din 12

Quick Sort -----------#include<stdio.h> #include<conio.

h> int split(int [],int,int); void quicksort(int [],int,int); void main() { int arr[20],n,i; clrscr(); printf("\nQUICk SORT\n"); printf("Enter the no.of elements:"); scanf("%d",&n); printf("Enter the elements:"); for(i=0;i<n;i++) scanf("%d",&arr[i]); printf("\nArray before sorting:\n"); for(i=0;i<n;i++) printf("%d\t",arr[i]); quicksort(arr,0,n); printf("\nArray after sorting:\n"); for(i=0;i<n;i++) printf("%d\t",arr[i]); getch(); } void quicksort(int a[],int lower,int upper) { int i; if(upper>lower) { i=split(a,lower,upper); quicksort(a,lower,i-1); quicksort(a,i+1,upper); } } int split(int a[],int lower,int upper) {

int i,p,q,t; p=lower+1; q=upper; i=a[lower]; while(q>=p) { while(a[p]<i) p++; while(a[q]>i) q--; if(q>p) { t=a[p]; a[p]=a[q]; a[q]=t; } } t=a[lower]; a[lower]=a[q]; a[q]=t; return(q); }

Develop a program for constructing a Graph and construct minimal spanning tree using Prims algorithm

#include<stdio.h> #define INF 1000 int vertex[10]; int wght[10][10]; int new_wght[10][10]; int closed[10]; int n; int inclose(int i,int n1) { /*chk for the ith vertex presence in closed*/ int j; for(j=0;j<=n1;j++) if(closed[j]==i) return 1; return 0; } void buildtree() { int i=0,j,count=0; int min,k,v1=0,v2=0; closed[0]=0; while(count<n-1) { min=INF; for(i=0;i<=count;i++) for(j=0;j<n;j++) if(wght[closed[i]][j]<min && !inclose(j,count)) { min=wght[closed[i]][j]; v1=closed[i]; v2=j; } new_wght[v1][v2]=new_wght[v2][v1]=min; count++; closed[count]=v2; printf("\nScan : %d %d---------%d wght = %d \n",count,v1+1,v2+1,min);

getch(); } } void main() { int i,j,ed,sum=0; clrscr(); printf("\n\n\tPRIM'S ALGORITHM TO FIND SPANNING TREE\n\n"); printf("\n\tEnter the No. of Nodes : "); scanf("%d",&n); for(i=0;i<n;i++) { vertex[i]=i+1; for(j=0;j<n;j++) { wght[i][j]=INF; new_wght[i][j]=INF; } } printf("\n\nGetting Weight.\n"); printf("\n\tEnter 0 if path doesn't exist between {v1,v2} else enter the wght\n"); for(i=0;i<n;i++) for(j=i+1;j<n;j++) { printf("\n\t%d -------- %d : ",vertex[i],vertex[j]); scanf("%d",&ed); if(ed>=1) wght[i][j]=wght[j][i]=ed; } getch(); clrscr(); printf("\n\n\t\tNODES CURRENTLY ADDED TO SPANNING TREE\n\n"); buildtree(); printf("\n\tNEW GRAPH WEIGHT MATRIX\n\n"); printf("\n\tweight matrix\n\n\t"); for(i=0;i<n;i++,printf("\n\t")) for(j=0;j<n;j++,printf("\t")) printf("%d",new_wght[i][j]); printf("\n\n\t\tMINIMUM SPANNING TREE\n\n");

printf("\n\t\tLIST OF EDGES\n\n"); for(i=0;i<n;i++) for(j=i+1;j<n;j++) if(new_wght[i][j]!=INF) { printf("\n\t\t%d ------ %d = %d ",vertex[i],vertex[j],new_wght[i][j]); sum+=new_wght[i][j]; } printf("\n\n\t Total Weight : %d ",sum); getch(); }

Develop a program for constructing a Graph and find the shortest path from a node (source) in a graph to another node (destination) using Dijkstras algorithm
/* PRIGRAM TO FIND ALL SOURCE SHORTEST PATH USING DIJKSTRA'S ALGORITHM */ #include<stdio.h> #include<stdlib.h> void AdjacencyMatrix( int a[ ][ 100 ], int n, FILE *fp ){ /* GET ADJACENCY MATRIX OF SIZE N X N */ int i , j ; for(i = 0; i < n; i++) { for(j = 0;j < i; j++) { a[ i ][ j ] = rand( ) % 100; /* GENERATE RANDOM NUMBERS */ a[ j ][ i ] = rand( ) % 50; } a[ i ][ i ] = 0; } for(i = 0; i < n; i++) { for(j = 0; j < n; j++) { if( a[ i ][ j ] != 0) fprintf( fp, "%d -> %d\n", i, j); /* WRITE TO FILE */ } } } void dijkastra( int n, int a[ ][ 100 ],int source,int des, int *d,int *p,int *s){ /* FIND SHORTEST PATH FROM SOURCE TO DESTINATION */ int i, j, u, v, min; /* LOCAL VARIABLES */ for(i = 0; i < n; i++) /* INITIALIZE ALL ARRAYS */ { d[ i ] = a[ source ][ i ]; s[ i ] = 0; p[ i ] = source; } s[ source ] = 1; /* INITIALIZE S[SOURCE] TO 1 */ for(i = 1; i < n;i++) { min = 99; u =- 1;

for(j = 0; j < n; j++) { if(s[ j ] == 0) /* IF NODE NOT VISITED */ { if(d[ j ] < min) /* IS ANY DIRECT PATH FROM SOURCE TO DESTINATION */ { min = d[ j ]; /* IF TRUE CHANGE MIN TI d[j] */ u = j; } } } } if(u == -1) return ; /* IF DESTINATION IS REACHED THE RETURN FROM FUNCTION*/ s[ u ] = 1; /* MARK u AS VISITED */ if( u == des) return; for( v = 0; v < n; v++) /* CONSIDER U AS INTERMIDIATE NODE AND GO TO NEXT */ { if(s[ v ] == 0) /* V WHICH IS REACHABLE FROM U */ { if(d[ u ] + a[ u ][ v ] < d[ v ]) { d[ v ] = d[ u ] + a[ u ][ v ]; /* CHANGE SHORTEST DISTANCE */ p[ v ] = u; } } } } void print(int a[ ][ 100 ],int n,int *d,int *p,int *s){ /* FUNCTION TO PRINT SHORTEST PATH FROM ALL SOURCES */ int i,source,des; for(source = 0; source < n; source++) /* SOURCE VARIES FROM 0 TO N */ { for(des = 0;des < n; des++) /* DESTINATION VARIES FROM 0 TO N */ { dijkastra( n, a, source, des, d, p, s); /* CALL FUNCTION TO FIND SHORTEST PATH FROM SOURCE */ { /* TO DESTINATION */ if(d[ des ] == 99) /* IF NO PATH FROM GIVEN SOURCE TO DESTINATION */ printf("destination is not reachable from %d to %d \n",source,des); else {

printf("\n"); i = des; while( i != source) /* REPEATEDLY PRINT PATH FROM SOURCE TO DESTINATION */ { printf("%d<-", i); i = p[ i ]; } printf("%d of lenght %d\n", i, d[ des ]); } } } } } void printArray(int a[][100],int n){ /* DISPLAY THE MATRIX */ int i, j; for(i = 0;i < n; i++) { for(j = 0;j < n; j++) { printf("\t%d", a[ i ][ j ]); } printf("\n"); } } int main() { int n,a[100][100],i,j,*p,*d,*s; FILE *fp=fopen("amatrix.dot","w"); /* FILE POINTER TO WRITE THE ADJACENCY MATRIX */ fprintf(fp,"digraph A {\n"); printf("Enter the no vertices of the digraph\n"); scanf("%d",&n); /* READ REQUIRED NUMBER OF VERTICES IN THE DIGRAPH */ s = (int *) malloc (n * sizeof(int)); /* ALLOCATE MEMORY */ d = (int *) malloc (n * sizeof(int)); p = (int *) malloc (n * sizeof(int)); AdjacencyMatrix( a, n, fp); printf("\t\tAdjacency Matrix of the graph\n"); /* PRINT ADJACENCY MATRIX */ printArray(a,n);

print(a,n,d,p,s); fprintf(fp,"\n}\n\n"); fclose( fp ); /* CLOSE FILE */ return 0; }

6
#include <stdio.h> #include <conio.h> #include <alloc.h> #define TRUE 1 #define FALSE 0 struct btreenode { struct btreenode *leftchild ; int data ; struct btreenode *rightchild ; } ; void insert ( struct btreenode **, int ) ; void delete ( struct btreenode **, int ) ; void search ( struct btreenode **, int, struct btreenode **, struct btreenode **, int * ) ; void inorder ( struct btreenode * ) ; void main( ) { struct btreenode *bt ; int req, i = 0, num, a[ ] = { 11, 9, 13, 8, 10, 12, 14, 15, 7 } ; bt = NULL ; clrscr( ) ; while ( i <= 8 ) { insert ( &bt, a[i] ) ; i++ ; } clrscr( ) ; printf ( "Binary tree before deletion:\n" ) ; inorder ( bt ) ; /* empty tree */

delete ( &bt, 10 ) ; printf ( "\nBinary tree after deletion:\n" ) ; inorder ( bt ) ; delete ( &bt, 14 ) ; printf ( "\nBinary tree after deletion:\n" ) ; inorder ( bt ) ; delete ( &bt, 8 ) ; printf ( "\nBinary tree after deletion:\n" ) ; inorder ( bt ) ; delete ( &bt, 13 ) ; printf ( "\nBinary tree after deletion:\n" ) ; inorder ( bt ) ; } /* inserts a new node in a binary search tree */ void insert ( struct btreenode **sr, int num ) { if ( *sr == NULL ) { *sr = malloc ( sizeof ( struct btreenode ) ) ; ( *sr ) -> leftchild = NULL ; ( *sr ) -> data = num ; ( *sr ) -> rightchild = NULL ; } else/* search the node to which new node will be attached */ { /* if new data is less, traverse to left */ if ( num < ( *sr ) -> data ) insert ( &( ( *sr ) -> leftchild ), num ) ; else/* else traverse to right */ insert ( &( ( *sr ) -> rightchild ), num ) ; } } /* deletes a node from the binary search tree */ void delete ( struct btreenode **root, int num ) { int found ; struct btreenode *parent, *x, *xsucc ; /* if tree is empty */ if ( *root == NULL ) { printf ( "\nTree is empty" ) ; return ; } parent = x = NULL ;

/* call to search function to find the node to be deleted */ search ( root, num, &parent, &x, &found ) ; /* if the node to deleted is not found */ if ( found == FALSE ) { printf ( "\nData to be deleted, not found" ) ; return ; } /* if the node to be deleted has two children */ if ( x -> leftchild != NULL && x -> rightchild != NULL ) { parent = x ; xsucc = x -> rightchild ; while ( xsucc -> leftchild != NULL ) { parent = xsucc ; xsucc = xsucc -> leftchild ; } x -> data = xsucc -> data ; x = xsucc ; } /* if the node to be deleted has no child */ if ( x -> leftchild == NULL && x -> rightchild == NULL ) { if ( parent -> rightchild == x ) parent -> rightchild = NULL ; else parent -> leftchild = NULL ; free ( x ) ; return ; } /* if the node to be deleted has only rightchild */ if ( x -> leftchild == NULL && x -> rightchild != NULL ) { if ( parent -> leftchild == x ) parent -> leftchild = x -> rightchild ; else parent -> rightchild = x -> rightchild ; free ( x ) ; return ; } /* if the node to be deleted has only left child */ if ( x -> leftchild != NULL && x -> rightchild == NULL ) { if ( parent -> leftchild == x ) parent -> leftchild = x -> leftchild ;

else parent -> rightchild = x -> leftchild ; free ( x ) ; return ; } } /*returns the address of the node to be deleted, address of its parent and whether the node is found or not */ void search ( struct btreenode **root, int num, struct btreenode **par, struct btreenode **x, int *found ) { struct btreenode *q ; q = *root ; *found = FALSE ; *par = NULL ; while ( q != NULL ) { /* if the node to be deleted is found */ if ( q -> data == num ) { *found = TRUE ; *x = q ; return ; } *par = q ; if ( q -> data > num ) q = q -> leftchild ; else q = q -> rightchild ; } } /* traverse a binary search tree in a LDR (Left-Data-Right) fashion */ void inorder ( struct btreenode *sr ) { if ( sr != NULL ) { inorder ( sr -> leftchild ) ; /* print the data of the node whose leftchild is NULL or the path has already been traversed */ printf ( "%d\t", sr -> data ) ; inorder ( sr -> rightchild ) ; } }

S-ar putea să vă placă și