├── p13 ├── memoryfuction.c ├── napsack.c ├── napsackplotter.c └── memoryplot.c ├── p14 ├── prims.c ├── prims1.c ├── primsplotter.c └── primsheap.c ├── p3 ├── selectionsort.c ├── selectionsorttest.c ├── insertionsort.c └── bubblesort.c ├── p12 ├── warshalltest.c ├── floyd.c ├── warshallplot.c └── floydplotter.c ├── p9 ├── topodfstest.c └── dfstopopolt.c ├── p1 ├── gcdtest.c └── gcdplot.c ├── p4 ├── stringtest.c └── string_matching.c ├── p15 ├── easydiji.c ├── diji.c └── dijiplotter.c ├── p5 ├── mergesorttest.c └── mergesortplot.c ├── p6 ├── quicksorttest.c └── quicksortplot.c ├── p11 ├── heapsorttest.c └── heapsotplot.c ├── p7 ├── dfsplot.c └── dfstest.c ├── p2 ├── searchtest.c └── searchplot.c ├── p8 ├── bfstest.c └── bfsplot.c ├── p10 ├── toposcrtest.c └── toposrcplot.c └── README.md /p13/memoryfuction.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int max(int a, int b){ 5 | return (a>b) ? a : b; 6 | } 7 | 8 | int t[100][100], v[100], w[100], n, m, i, j; 9 | 10 | int knap(int i, int j){ 11 | if (t[i][j]==-1){ 12 | if (j> "); 23 | scanf("%d",&n); 24 | printf("Sack Capacity>> "); 25 | scanf("%d",&m); 26 | 27 | 28 | printf("Weight\tValue\n"); 29 | for(i=1;i0;i--){ 46 | if (t[i][m] != t[i-1][m]){ 47 | printf("%d ",i); 48 | m = m-w[i]; 49 | } 50 | } 51 | printf("\n"); 52 | } 53 | -------------------------------------------------------------------------------- /p14/prims.c: -------------------------------------------------------------------------------- 1 | /*program to find the minimum spanning tree with prims algorithm */ 2 | //the below program can be implemented wit heap so u can improve this with heap then complexity will decreases 3 | #include 4 | 5 | int cost[40][40], n, visited[40]={0}; 6 | 7 | void createGraph(){ 8 | printf("No. of vertices>> "); 9 | scanf("%d", &n); 10 | printf("Enter cost matrix:\n"); 11 | for(int i=0;icost[i][j] && !visited[j]){ 32 | min = cost[i][j]; 33 | a = i; 34 | b = j; 35 | } 36 | } 37 | } 38 | } 39 | 40 | printf("%c-->%c | Cost: %d\n",a+65,b+65,min); 41 | visited[b] = 1; 42 | min_cost += min; 43 | edges++; 44 | } 45 | 46 | printf("Minimum Cost: %d\n",min_cost); 47 | } 48 | -------------------------------------------------------------------------------- /p3/selectionsort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int selectionsort(int *a,int n) 6 | { 7 | int i,j,min,t,f,count=0; 8 | for(i=0;i 2 | #include 3 | 4 | void selectionsort(int *a,int n) 5 | { 6 | int i,j,min,t,f,count=0; 7 | for(i=0;i 5 | #include 6 | 7 | int max(int a, int b){ 8 | return (a>b) ? a : b; 9 | } 10 | int t[100][100], v[100], w[100]; 11 | 12 | void napsack(int n,int m) 13 | { 14 | int i,j; 15 | for(i=0;i0;i--){ 32 | if (t[i][m] != t[i-1][m]){ 33 | printf("%d ",i); 34 | m = m-w[i]; 35 | } 36 | } 37 | 38 | 39 | } 40 | void main(){ 41 | int n, m, i, j; 42 | printf("ENTER NO OF ITEMS:\n"); 43 | scanf("%d",&n); 44 | printf("ENTER THE STACK CAPACITY:\n"); 45 | scanf("%d",&m); 46 | 47 | 48 | printf("WEIGHT\tVALUE\n"); 49 | for(i=1;i 4 | #include 5 | 6 | int graph[100][100]; 7 | int counter=0; 8 | void warshall (int n) 9 | { 10 | for(int k=1; k<=n; k++) 11 | { 12 | for(int i=1; i<=n; i++) 13 | { 14 | 15 | if(graph[i][k]!=0) 16 | { 17 | for(int j=1; j<=n; j++) 18 | {// graph[i][j] = (graph[i][k] && graph[k][j])); 19 | graph[i][j] = (graph[i][j] || (graph[i][k] && graph[k][j])); 20 | counter++; 21 | } 22 | } 23 | } 24 | } 25 | } 26 | 27 | void main() { 28 | int n, i, j; 29 | printf("\nEnter the number of vertices:\n"); 30 | scanf("%d", &n); 31 | printf("\nEnter the adjacency matrix:\n"); 32 | for(i=1; i<=n; i++) { 33 | for(j=1; j<=n; j++) { 34 | scanf("%d", &graph[i][j]); 35 | } 36 | } 37 | 38 | warshall(n); 39 | 40 | printf("\nTransitive closure matrix is:\n"); 41 | for(i=1; i<=n; i++) { 42 | for(j=1; j<=n; j++) { 43 | printf("%d ", graph[i][j]); 44 | } 45 | printf("\n"); 46 | } 47 | } 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /p9/topodfstest.c: -------------------------------------------------------------------------------- 1 | /*program to implement the topological sorting with dfs on adjacency matrix*/ 2 | 3 | #include 4 | #include 5 | #define MAX 100 6 | int graph[MAX][MAX], visited[MAX],path[MAX], count=0; 7 | int stack[MAX], top=-1; 8 | int c=0; 9 | 10 | void dfs(int n, int start) { 11 | visited[start] = 1; 12 | path[start] =1; 13 | for(int i=0; i %c", stack[i]+65); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /p1/gcdtest.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define x 10 4 | #define y 100 5 | 6 | int test=0; 7 | 8 | float euclid(int m,int n) 9 | { 10 | int r; 11 | float count=0; 12 | while(n) 13 | { 14 | count++; 15 | r=m%n; 16 | m=n; 17 | n=r; 18 | } 19 | printf("THE GCD IS %d\n",m); 20 | return count; 21 | } 22 | float consec(int m, int n) 23 | { 24 | int min; 25 | float count=0; 26 | min=m; 27 | if(n0) 51 | { 52 | if(n>m) 53 | { 54 | temp=m;m=n;n=temp; 55 | } 56 | m=m-n; 57 | count +=1 ; 58 | } 59 | 60 | printf("THE GCD IS %d\n",m); 61 | return count; // m is the GCD 62 | } 63 | 64 | 65 | void main() 66 | { 67 | int ch; 68 | while(1) 69 | { 70 | printf("GCD\n"); 71 | printf("1.Euclid\n2.modified Euclid\n3.consecutive integer method\n0to exit\n"); 72 | scanf("%d",&ch); 73 | if(ch==0) 74 | break; 75 | printf("ENTER THE VALUES M AND N\n"); 76 | int m,n; 77 | scanf("%d",&m); 78 | scanf("%d",&n); 79 | switch(ch) 80 | { 81 | case 1:euclid(m,n);break; 82 | case 2:modified(m,n);break; 83 | case 3:consec(m,n);break; 84 | default:break; 85 | } 86 | } 87 | } 88 | 89 | -------------------------------------------------------------------------------- /p12/floyd.c: -------------------------------------------------------------------------------- 1 | //PROGRAM TO IMPLEMENT THE KNAPSAC PROBLEM WITH BOTTOM UP APPROACH 2 | // this is only 4 testing and ploting will be updated soon as like warshal 3 | 4 | // Floyd's Algorithm 5 | #include 6 | #include 7 | #define MAX 100 8 | int graph[MAX][MAX]; 9 | 10 | int minimum (int a, int b) { 11 | int min = (a 2 | #include 3 | #include 4 | #include 5 | 6 | int count = 0; 7 | 8 | int stringmatching(char *text, char *pattern, int n, int m) { 9 | count = 0; 10 | for (int i = 0; i <= n - m; i++) { 11 | int j = 0; 12 | while (j < m) { 13 | count++; 14 | if (pattern[j] != text[i + j]) 15 | break; 16 | j++; 17 | } 18 | if (j == m) { 19 | printf("THE PATTERN FOUND \n"); 20 | return count; 21 | } 22 | } 23 | printf("THE PATTERN not found \n"); 24 | return count; 25 | } 26 | 27 | int main() { 28 | int m, n; 29 | char text[100], pattern[100]; 30 | 31 | printf("ENTER THE PATTERN LENGTH\n"); 32 | scanf("%d", &m); 33 | printf("ENTER THE PATTERN\n"); 34 | getchar(); // Consume the newline character left in the input buffer 35 | 36 | fgets(pattern, sizeof(pattern), stdin); 37 | pattern[strcspn(pattern, "\n")] = '\0'; // Remove the newline character from the input 38 | 39 | printf("ENTER THE TEXT LENGTH\n"); 40 | scanf("%d", &n); 41 | printf("ENTER THE TEXT\n"); 42 | getchar(); // Consume the newline character left in the input buffer 43 | 44 | fgets(text, sizeof(text), stdin); 45 | text[strcspn(text, "\n")] = '\0'; // Remove the newline character from the input 46 | 47 | int comparisons = stringmatching(text, pattern, n, m); 48 | printf("Number of comparisons: %d\n", comparisons); 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /p15/easydiji.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void djikstras(int n, int cost[10][10], int s, int dist[10]); 5 | 6 | int main() { 7 | int i, j, n, cost[10][10], dist[10], s; 8 | printf("Number of nodes: "); 9 | scanf("%d", &n); 10 | printf("Read cost matrix:\n"); 11 | for (i = 0; i < n; i++) { 12 | for (j = 0; j < n; j++) { 13 | scanf("%d", &cost[i][j]); 14 | if (cost[i][j] == 0) 15 | cost[i][j] = 999; 16 | } 17 | } 18 | printf("Read source vertex: "); 19 | scanf("%d", &s); 20 | djikstras(n, cost, s, dist); 21 | printf("Shortest path from %d:\n", s); 22 | for (i = 0; i < n; i++) { 23 | if (s != i) 24 | printf("%d->%d=%d\n", s, i, dist[i]); 25 | } 26 | return 0; 27 | } 28 | 29 | void djikstras(int n, int cost[10][10], int s, int dist[10]) { 30 | int i, v, count = 1, min, visited[10]; 31 | for (i = 0; i < n; i++) { 32 | visited[i] = 0; 33 | dist[i] = cost[s][i]; 34 | } 35 | visited[s] = 1; 36 | dist[s] = 0; 37 | while (count < n) { 38 | min = 999; 39 | for (i = 0; i < n; i++) { 40 | if (dist[i] < min && visited[i] == 0) { 41 | min = dist[i]; 42 | v = i; 43 | } 44 | } 45 | visited[v] = 1; 46 | count++; 47 | for (i = 0; i < n; i++) { 48 | if (dist[i] > dist[v] + cost[v][i]) { 49 | dist[i] = dist[v] + cost[v][i]; 50 | } 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /p5/mergesorttest.c: -------------------------------------------------------------------------------- 1 | /*TESTER IS USED TO CHECK THE CORRECTNESS OF THE ALGORITH*/ 2 | #include 3 | #include 4 | #include 5 | int count; 6 | void merge(int *arr,int beg,int mid,int end) 7 | { 8 | int i,j,k; 9 | int n1=(mid-beg)+1; 10 | int n2=end-mid; 11 | int left[n1],right[n2]; 12 | for(i=0;i 5 | #include 6 | #include 7 | 8 | 9 | int count; 10 | 11 | 12 | void swap(int *a,int * b) 13 | { 14 | int temp=*a; 15 | *a=*b; 16 | *b=temp; 17 | 18 | } 19 | 20 | 21 | int partition(int * arr,int beg,int end) 22 | { 23 | int pivot =arr[beg]; 24 | int i=beg,j=end+1; 25 | 26 | do{ 27 | 28 | do{ 29 | count++; 30 | i++; 31 | }while(arr[i]pivot); 37 | swap(&arr[i],&arr[j]); 38 | 39 | }while(i 2 | #include 3 | 4 | int max(int a, int b) { 5 | return (a > b) ? a : b; 6 | } 7 | 8 | int t[100][100], v[100], w[100]; 9 | int operationCount = 0; // To count the number of operations 10 | 11 | void knapsack(int n, int m) { 12 | int i, j; 13 | for (i = 0; i < n + 1; i++) { 14 | for (j = 0; j < m + 1; j++) { 15 | operationCount++; // Incrementing operation count for each iteration 16 | if (i == 0 || j == 0) 17 | t[i][j] = 0; 18 | else if (j < w[i]) 19 | t[i][j] = t[i - 1][j]; 20 | else 21 | t[i][j] = max(t[i - 1][j], v[i] + t[i - 1][j - w[i]]); 22 | } 23 | } 24 | 25 | printf("THE MAXIMUM VALUE IS : %d\n", t[n][m]); 26 | 27 | printf("THE COMPOSITION IS \n"); 28 | for (i = n; i > 0; i--) { 29 | if (t[i][m] != t[i - 1][m]) { 30 | printf("%d ", i); 31 | m = m - w[i]; 32 | } 33 | } 34 | printf("\n"); 35 | } 36 | 37 | void main() { 38 | FILE *f1; 39 | f1 = fopen("knapsackgraph.txt", "a"); 40 | int n, m, i; 41 | 42 | printf("ENTER NO OF ITEMS:\n"); 43 | scanf("%d", &n); 44 | printf("ENTER THE STACK CAPACITY:\n"); 45 | scanf("%d", &m); 46 | 47 | printf("WEIGHT\tVALUE\n"); 48 | for (i = 1; i < n + 1; i++) { 49 | scanf("%d\t%d", &w[i], &v[i]); 50 | } 51 | 52 | operationCount = 0; // Resetting the operation count before the function call 53 | knapsack(n, m); 54 | 55 | printf("Operation Count: %d\n", operationCount); 56 | fprintf(f1, "Items: %d, Capacity: %d, Operation Count: %d\n", n, m, operationCount); 57 | 58 | fclose(f1); 59 | } 60 | -------------------------------------------------------------------------------- /p12/warshallplot.c: -------------------------------------------------------------------------------- 1 | /* program to implement the Warshall's Algorithm*/ 2 | 3 | #include 4 | #include 5 | 6 | int graph[100][100]; 7 | int counter=0; 8 | void warshall (int n) 9 | { 10 | for(int k=1; k<=n; k++) 11 | { 12 | for(int i=1; i<=n; i++) 13 | { 14 | 15 | if(graph[i][k]!=0) 16 | { 17 | for(int j=1; j<=n; j++) 18 | {// graph[i][j] = (graph[i][k] && graph[k][j])); 19 | graph[i][j] = (graph[i][j] || (graph[i][k] && graph[k][j])); 20 | counter++; 21 | } 22 | } 23 | } 24 | } 25 | } 26 | 27 | 28 | void ploter(int c) 29 | { 30 | 31 | FILE *f1=fopen("warshalbest.txt","a"); 32 | FILE *f2=fopen("warshallworst.txt","a"); 33 | 34 | for(int i=1;i<=10;i++) 35 | { 36 | int n=i; 37 | if(c==1) 38 | { 39 | for(int i=1;i<=n;i++) 40 | { 41 | for(int j=1;j<=n;j++) 42 | { 43 | if(i!=j) 44 | { 45 | graph[i][j] =1; 46 | } 47 | else 48 | graph[i][j] =0; 49 | } 50 | } 51 | } 52 | 53 | if(c==0) 54 | { 55 | for(int i=1;i<=n;i++) 56 | { 57 | for(int j=1;j<=n;j++) 58 | graph[i][j] =0; 59 | } 60 | for(int i=1;i 3 | #include 4 | #include 5 | 6 | int count,count2=0; 7 | void swap(int *a, int *b) { 8 | int temp = *a; 9 | *a = *b; 10 | *b = temp; 11 | return; 12 | } 13 | 14 | void heapify(int *heap, int n, int root) { 15 | int largest = root; 16 | int left = 2*root+1; 17 | int right = 2*root+2; 18 | if(left < n ) 19 | { 20 | count++; 21 | if(heap[left] > heap[largest]) { 22 | largest = left; 23 | } 24 | } 25 | if(right < n ) 26 | { 27 | count++; 28 | if(heap[right] > heap[largest]) { 29 | largest = right; 30 | } 31 | } 32 | if(largest != root) { 33 | swap(&heap[root], &heap[largest]); 34 | heapify(heap, n, largest); 35 | } 36 | } 37 | 38 | 39 | void heapSort(int *heap, int n) { 40 | for(int i = (n/2)-1; i>=0; i--) { 41 | heapify(heap, n, i); 42 | } 43 | count2=count; 44 | count=0; 45 | for(int i = n-1; i>=0; i--) { 46 | swap(&heap[0], &heap[i]); 47 | heapify(heap, i, 0); 48 | } 49 | 50 | } 51 | 52 | int max(int a, int b) { 53 | 54 | int temp =a>b ? a:b; 55 | return temp; 56 | } 57 | void main() 58 | { 59 | int *arr, n; 60 | printf("ENTER THE NUMBER OF ELEMENTS\n"); 61 | scanf("%d",&n); 62 | 63 | arr=(int *)malloc(sizeof(int)*n); 64 | printf("ENTER THE ELEMENTS OF THE ARRAY\n"); 65 | for(int i=0;i 4 | #include 5 | #define MAX 100 6 | 7 | int graph[MAX][MAX], visited[MAX],path[MAX], count=0; 8 | int stack[MAX], top=-1; 9 | int c=0; 10 | 11 | void dfs(int n, int start) { 12 | visited[start] = 1; 13 | path[start] =1; 14 | for(int i=0; i 2 | #include 3 | 4 | int max(int a, int b) { 5 | return (a > b) ? a : b; 6 | } 7 | 8 | int t[100][100], v[100], w[100], n, m, i, j; 9 | int operationCount = 0; // To count the number of operations 10 | 11 | int knap(int i, int j) { 12 | operationCount++; // Incrementing operation count for each call 13 | 14 | if (t[i][j] == -1) { 15 | if (j < w[i]) 16 | t[i][j] = knap(i - 1, j); 17 | else 18 | t[i][j] = max(knap(i - 1, j), v[i] + knap(i - 1, j - w[i])); 19 | } 20 | return t[i][j]; 21 | } 22 | 23 | void initializeMemoizationTable() { 24 | for (i = 0; i <= n; i++) { 25 | for (j = 0; j <= m; j++) { 26 | t[i][j] = -1; 27 | } 28 | } 29 | 30 | // Initialize the base cases 31 | for (i = 0; i <= n; i++) { 32 | t[i][0] = 0; 33 | } 34 | for (j = 0; j <= m; j++) { 35 | t[0][j] = 0; 36 | } 37 | } 38 | 39 | void printComposition(int n, int m) { 40 | printf("THE COMPOSITION IS \n"); 41 | for (int i = n; i > 0; i--) { 42 | if (t[i][m] != t[i - 1][m]) { 43 | printf("%d ", i); 44 | m = m - w[i]; 45 | } 46 | } 47 | printf("\n"); 48 | } 49 | 50 | void main() { 51 | FILE *f1; 52 | f1 = fopen("knapsackgraph.txt", "a"); 53 | 54 | printf("ENTER NO OF ITEMS:\n"); 55 | scanf("%d", &n); 56 | printf("ENTER THE STACK CAPACITY:\n"); 57 | scanf("%d", &m); 58 | 59 | printf("WEIGHT\tVALUE\n"); 60 | for (i = 1; i <= n; i++) { 61 | scanf("%d\t%d", &w[i], &v[i]); 62 | } 63 | 64 | operationCount = 0; // Resetting the operation count before the function call 65 | 66 | // Initialize the memoization table 67 | initializeMemoizationTable(); 68 | 69 | int maxValue = knap(n, m); 70 | 71 | printf("THE MAXIMUM VALUE IS : %d\n", maxValue); 72 | printComposition(n, m); 73 | 74 | printf("Operation Count: %d\n", operationCount); 75 | fprintf(f1, "Items: %d, Capacity: %d, Operation Count: %d\n", n, m, operationCount); 76 | 77 | fclose(f1); 78 | } 79 | -------------------------------------------------------------------------------- /p4/string_matching.c: -------------------------------------------------------------------------------- 1 | //Program to perform analysis of brute force string matching 2 | #include 3 | #include 4 | #include 5 | 6 | int count = 0; 7 | 8 | int stringmatching(char *text, char *pattern, int n, int m) { 9 | count = 0; 10 | for(int i=0; i<=n-m; i++) 11 | { 12 | int j=0; 13 | while(j 5 | #include 6 | #include 7 | 8 | 9 | int count; 10 | 11 | 12 | void swap(int *a,int * b) 13 | { 14 | int temp=*a; 15 | *a=*b; 16 | *b=temp; 17 | 18 | } 19 | 20 | 21 | int partition(int * arr,int beg,int end) 22 | { 23 | int pivot =arr[beg]; 24 | int i=beg,j=end+1; 25 | 26 | do{ 27 | 28 | do{ 29 | count++; 30 | i++; 31 | }while(arr[i]pivot); 37 | swap(&arr[i],&arr[j]); 38 | 39 | }while(i 4 | #include 5 | 6 | int graph[100][100], visited[100], isCyclic = 0; 7 | int dfsCount = 0, count = 0; 8 | int dcount=0; 9 | int path[100]; 10 | int d; 11 | 12 | void dfs1(int n, int start, int parent) { 13 | visited[start] = 1; 14 | count++; 15 | for(int i=0; i 2 | #include 3 | #define x 10 4 | #define y 100 5 | 6 | int test=0; 7 | 8 | float euclid(int m,int n) 9 | { 10 | int r; 11 | float count=0; 12 | while(n) 13 | { 14 | count++; 15 | r=m%n; 16 | m=n; 17 | n=r; 18 | } 19 | 20 | return count; 21 | } 22 | float consec(int m, int n) 23 | { 24 | int min; 25 | float count=0; 26 | min=m; 27 | if(n0) 49 | { 50 | if(n>m) 51 | { 52 | temp=m;m=n;n=temp; 53 | } 54 | m=m-n; 55 | count +=1 ; 56 | } 57 | 58 | return count; // m is the GCD 59 | } 60 | void analysis(int ch) 61 | { 62 | int m,n,i,j,k; 63 | float count,maxcount,mincount; 64 | FILE *fp1,*fp2; 65 | for(i=x;i<=y;i+=10) 66 | { 67 | maxcount=0; mincount=10000; 68 | for (j=2;j<=i; j++ ) // To generate the data 69 | { 70 | for(k=2;k<=i; k++) 71 | { 72 | count=0; 73 | m=j; 74 | n=k; 75 | switch(ch) 76 | { 77 | case 1:count=euclid(m,n); 78 | break; 79 | case 2:count=consec(m,n); 80 | break; 81 | case 3:count=modified(m,n); 82 | break; 83 | } 84 | if(count>maxcount) // To find the maximum basic operations among all the combinations between 2 to n 85 | maxcount=count; 86 | if(count 4 | #include 5 | 6 | int graph[100][100]; 7 | int counter = 0; 8 | int count = 0; 9 | 10 | int minimum(int a, int b) { 11 | return (a < b) ? a : b; 12 | } 13 | 14 | void floyd(int n) { 15 | int t; 16 | for (int k = 1; k <= n; k++) { 17 | for (int i = 1; i <= n; i++) { 18 | t = graph[i][k]; 19 | for (int j = 1; j <= n; j++) { 20 | if (t < graph[i][j]) { 21 | count++; 22 | graph[i][j] = minimum(graph[i][j], (graph[i][k] + graph[k][j])); 23 | } 24 | counter++; 25 | } 26 | } 27 | } 28 | } 29 | 30 | void plotter(int c) { 31 | FILE *f1 = fopen("floydBest.txt", "a"); 32 | FILE *f2 = fopen("floydWorst.txt", "a"); 33 | 34 | for (int i = 1; i <= 10; i++) { 35 | int n = i; 36 | if (c == 1) { // Worst case: Dense graph (complete graph) 37 | for (int i = 1; i <= n; i++) { 38 | for (int j = 1; j <= n; j++) { 39 | if (i != j) { 40 | graph[i][j] = rand() % 10 + 1; // Random weights 41 | } else { 42 | graph[i][j] = 0; // No self-loops 43 | } 44 | } 45 | } 46 | } 47 | 48 | if (c == 0) { // Best case: Sparse graph (minimal edges) 49 | for (int i = 1; i <= n; i++) { 50 | for (int j = 1; j <= n; j++) { 51 | graph[i][j] = (i == j) ? 0 : INT_MAX; // Initialize graph with infinity (no direct path) 52 | } 53 | } 54 | for (int i = 1; i < n; i++) { 55 | graph[i][i+1] = rand() % 10 + 1; // Create a path 56 | } 57 | graph[n][1] = rand() % 10 + 1; // Complete the cycle 58 | } 59 | 60 | counter = 0; 61 | count = 0; 62 | floyd(n); 63 | 64 | if (c == 0) 65 | fprintf(f1, "%d\t%d\t%d\n", n, counter, count); 66 | else 67 | fprintf(f2, "%d\t%d\t%d\n", n, counter, count); 68 | } 69 | 70 | fclose(f1); 71 | fclose(f2); 72 | } 73 | 74 | void main() { 75 | for (int i = 0; i < 2; i++) { 76 | plotter(i); 77 | } 78 | printf("The graph plotting is completed\n"); 79 | } 80 | -------------------------------------------------------------------------------- /p2/searchtest.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int linearSearch(int *a, int k, int n) { 6 | int i; 7 | for (i = 0; i < n; i++) { 8 | if (*(a + i) == k) { 9 | return i; 10 | } 11 | } 12 | return -1; 13 | } 14 | 15 | int binarySearch(int key, int *a, int low, int high) { 16 | if (low > high) { 17 | return -1; 18 | } 19 | 20 | int mid = (high + low) / 2; 21 | 22 | if (*(a + mid) == key) { 23 | return mid; 24 | } else if (*(a + mid) > key) { 25 | return binarySearch(key, a, low, mid - 1); 26 | } else { 27 | return binarySearch(key, a, mid + 1, high); 28 | } 29 | } 30 | 31 | int main() { // Changed the return type to int 32 | int arr[100]; 33 | int n, key, r; 34 | 35 | for (;;) { 36 | printf("ENTER 1.TO LINEAR SEARCH\n2.TO BINARY SEARCH\n3.TO EXIT\n"); 37 | int ch; 38 | scanf("%d", &ch); 39 | switch (ch) { 40 | case 1: 41 | printf("ENTER THE NUMBER OF ELEMENTS\n"); 42 | scanf("%d", &n); 43 | printf("ENTER THE ELEMENTS OF THE ARRAY\n"); 44 | for (int i = 0; i < n; i++) { 45 | scanf("%d", &arr[i]); 46 | } 47 | printf("ENTER THE KEY ELEMENT\n"); 48 | scanf("%d", &key); 49 | r = linearSearch(arr, key, n); 50 | if (r != -1) { 51 | printf("The element is present at the index %d\n", r); 52 | } else { 53 | printf("Element not found\n"); 54 | } 55 | break; 56 | case 2: 57 | printf("ENTER THE NUMBER OF ELEMENTS\n"); 58 | scanf("%d", &n); 59 | printf("ENTER THE ELEMENTS OF THE ARRAY IN SORTED ORDER\n"); 60 | for (int i = 0; i < n; i++) { 61 | scanf("%d", &arr[i]); 62 | } 63 | printf("ENTER THE KEY ELEMENT\n"); 64 | scanf("%d", &key); 65 | r = binarySearch(key, arr, 0, n - 1); // Changed the argument order 66 | if (r != -1) { 67 | printf("The element is present at the index %d\n", r); 68 | } else { 69 | printf("Element not found\n"); 70 | } 71 | break; 72 | default: 73 | exit(0); 74 | } 75 | } 76 | 77 | return 0; // Return a value from main 78 | } 79 | -------------------------------------------------------------------------------- /p7/dfstest.c: -------------------------------------------------------------------------------- 1 | /*program to implement the dfs algorithm and to check connectivity and acyclicity with adjacency matrix */ 2 | 3 | #include 4 | #include 5 | 6 | int graph[100][100], visited[100], isCyclic = 0; 7 | int dfsCount = 0, count = 0; 8 | int dcount=0; 9 | int path[100]; 10 | int d; 11 | void dfs(int n, int start, int parent) { 12 | visited[start] = 1; 13 | path [start]=1; 14 | count++; 15 | printf("--> %c ", start+65); 16 | for(int i=0; i 4 | #include 5 | int bfsCount = 0, cyclic=0; 6 | int count = 0;//to count how many vertex visited 7 | int orderCount = 0; 8 | 9 | int graph[100][100], visited[100]; 10 | 11 | void bfs(int n, int start){ 12 | int queue[n], parent[n]; 13 | int rear = -1, front = -1, i, parentNode; 14 | visited[start] = 1; count++; 15 | queue[++rear] = start; 16 | parent[rear] = -1; 17 | while(rear != front){ 18 | start = queue[++front]; 19 | parentNode = parent[front]; 20 | printf("-->%c", start+65); 21 | for(i=0; i 4 | #include 5 | int bfsCount = 0, cyclic=0; 6 | int count = 0;//to count how many vertex visited 7 | int orderCount = 0; 8 | 9 | int graph[100][100], visited[100]; 10 | 11 | void bfs(int n, int start){ 12 | int queue[n], parent[n]; 13 | int rear = -1, front = -1, i, parentNode; 14 | visited[start] = 1; count++; 15 | queue[++rear] = start; 16 | parent[rear] = -1; 17 | while(rear != front){ 18 | start = queue[++front]; 19 | parentNode = parent[front]; 20 | for(i=0; i 3 | #include 4 | #include 5 | 6 | int count,count2=0; 7 | void swap(int *a, int *b) { 8 | int temp = *a; 9 | *a = *b; 10 | *b = temp; 11 | return; 12 | } 13 | 14 | void heapify(int *heap, int n, int root) { 15 | int largest = root; 16 | int left = 2*root+1; 17 | int right = 2*root+2; 18 | if(left < n ) 19 | { 20 | count++; 21 | if(heap[left] > heap[largest]) { 22 | largest = left; 23 | } 24 | } 25 | if(right < n ) 26 | { 27 | count++; 28 | if(heap[right] > heap[largest]) { 29 | largest = right; 30 | } 31 | } 32 | if(largest != root) { 33 | swap(&heap[root], &heap[largest]); 34 | heapify(heap, n, largest); 35 | } 36 | } 37 | 38 | 39 | void heapSort(int *heap, int n) { 40 | for(int i = (n/2)-1; i>=0; i--) { 41 | heapify(heap, n, i); 42 | } 43 | count2=count; 44 | count=0; 45 | for(int i = n-1; i>=0; i--) { 46 | swap(&heap[0], &heap[i]); 47 | heapify(heap, i, 0); 48 | } 49 | 50 | } 51 | 52 | int max(int a, int b) { 53 | 54 | int temp =a>b ? a:b; 55 | return temp; 56 | } 57 | 58 | void plotter() 59 | { 60 | 61 | int *arr,n; 62 | srand(time(NULL)); 63 | FILE *f1,*f2,*f3; 64 | 65 | f1=fopen("HEAPSORTBEST.txt","a"); 66 | f2=fopen("HEAPSORTWORST.txt","a"); 67 | f3=fopen("HEAPSORTAVG.txt","a"); 68 | n=100; 69 | 70 | while(n<=1000) 71 | { 72 | arr=(int *)malloc(sizeof(int)*(n+1)); 73 | for(int i=0;i 4 | #include 5 | int count=0; 6 | 7 | typedef struct queue 8 | { 9 | int f,r,*arr,cnt; 10 | }QUE; 11 | 12 | int s[10]; 13 | 14 | void indegree(int *a[],int v,int inq[],QUE *temp,int flag[]) 15 | { 16 | for(int i=0;ir=(temp->r+1)%v; 26 | temp->arr[temp->r]=i; 27 | temp->cnt=temp->cnt+1; 28 | flag[i]=1; 29 | } 30 | } 31 | } 32 | 33 | void sourceremove(int *a[],int v,QUE *temp,int inq[],int flag[]) 34 | { 35 | int cnt=0; 36 | while(temp->cnt!=0) 37 | { 38 | int source=temp->arr[temp->f]; 39 | temp->f=(temp->f+1)%v; 40 | s[cnt]=source; 41 | temp->cnt=temp->cnt-1; 42 | cnt++; 43 | for(int i=0;ir=(temp->r+1)%v; 50 | temp->arr[temp->r]=i; 51 | temp->cnt=temp->cnt+1; 52 | flag[i]=1; 53 | } 54 | } 55 | } 56 | 57 | if(cnt!=v) 58 | { 59 | printf("Cycles exist no topological sorting possible\n"); 60 | //printf("abhishek"); 61 | } 62 | else 63 | { 64 | 65 | printf("The topological sorting is\n"); 66 | for(int i=0;i 5 | #include 6 | #include 7 | int count; 8 | void merge(int *arr,int beg,int mid,int end) 9 | { 10 | int i,j,k; 11 | int n1=(mid-beg)+1; 12 | int n2=end-mid; 13 | int left[n1],right[n2]; 14 | for(i=0;i 2 | #include 3 | 4 | // Insertion Sort Function 5 | void insertionSort(int *arr, int n) { 6 | for (int i = 1; i < n; i++) { 7 | int value = arr[i]; 8 | int j = i - 1; 9 | while (j >= 0 && arr[j] > value) { 10 | arr[j + 1] = arr[j]; 11 | j--; 12 | } 13 | arr[j + 1] = value; 14 | } 15 | } 16 | 17 | // Tester for Insertion Sort 18 | void tester() { 19 | int *arr, n; 20 | printf("ENTER THE NUMBER OF ELEMENTS\n"); 21 | scanf("%d", &n); 22 | 23 | arr = (int *)malloc(sizeof(int) * n); 24 | printf("ENTER THE ELEMENTS OF THE ARRAY\n"); 25 | for (int i = 0; i < n; i++) { 26 | scanf("%d", &arr[i]); 27 | } 28 | 29 | printf("THE ELEMENTS OF THE ARRAY BEFORE SORTING\n"); 30 | for (int i = 0; i < n; i++) { 31 | printf("%d ", arr[i]); 32 | } 33 | printf("\n"); 34 | 35 | insertionSort(arr, n); 36 | 37 | printf("THE ELEMENTS OF THE ARRAY AFTER SORTING\n"); 38 | for (int i = 0; i < n; i++) { 39 | printf("%d ", arr[i]); 40 | } 41 | printf("\n"); 42 | 43 | free(arr); 44 | } 45 | 46 | // Plotter for Insertion Sort 47 | void plotter() { 48 | int *arr, n, count; 49 | FILE *f1, *f2, *f3; 50 | 51 | f1 = fopen("INSERTIONBEST.txt", "a"); 52 | f2 = fopen("INSERTIONWORST.txt", "a"); 53 | f3 = fopen("INSERTIONAVG.txt", "a"); 54 | n = 10; 55 | 56 | while (n <= 30000) { 57 | arr = (int *)malloc(sizeof(int) * n); 58 | 59 | // Worst case 60 | for (int i = 0; i < n; i++) { 61 | arr[i] = n - i; 62 | } 63 | count = 0; 64 | insertionSort(arr, n); 65 | fprintf(f2, "%d\t%d\n", n, count); 66 | 67 | // Best case 68 | for (int i = 0; i < n; i++) { 69 | arr[i] = i + 1; 70 | } 71 | count = 0; 72 | insertionSort(arr, n); 73 | fprintf(f1, "%d\t%d\n", n, count); 74 | 75 | // Average case 76 | for (int i = 0; i < n; i++) { 77 | arr[i] = rand() % n; 78 | } 79 | count = 0; 80 | insertionSort(arr, n); 81 | fprintf(f3, "%d\t%d\n", n, count); 82 | 83 | if (n < 10000) { 84 | n = n * 10; 85 | } else { 86 | n = n + 10000; 87 | } 88 | free(arr); 89 | } 90 | 91 | fclose(f1); 92 | fclose(f2); 93 | fclose(f3); 94 | } 95 | 96 | int main() { 97 | for (;;) { 98 | int key; 99 | printf("ENTER THE CHOICE \n1. TO TEST \n2. TO PLOT\n0. TO EXIT\n"); 100 | scanf("%d", &key); 101 | 102 | switch (key) { 103 | case 1: 104 | tester(); 105 | break; 106 | case 2: 107 | plotter(); 108 | break; 109 | case 0: 110 | return 0; 111 | default: 112 | printf("Invalid choice, try again.\n"); 113 | } 114 | } 115 | return 0; 116 | } 117 | -------------------------------------------------------------------------------- /p2/searchplot.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int count; 6 | int linearSearch(int *a, int k, int n) 7 | { 8 | int i; 9 | count = 0; 10 | for (i = 0; i < n; i++) 11 | { 12 | count++; 13 | if (*(a + i) == k) 14 | { 15 | return count; 16 | } 17 | } 18 | return count; 19 | } 20 | int binarySearch(int key, int *a, int high, int low) 21 | { 22 | 23 | int mid; 24 | count++; 25 | mid = (high + low) / 2; 26 | if (low > high) 27 | return count-1; 28 | if (*(a + mid) == key) 29 | return count; 30 | else if (*(a + mid) > key) 31 | return binarySearch(key,a,mid - 1,low); 32 | else 33 | return binarySearch(key, a, high, mid + 1); 34 | } 35 | 36 | 37 | 38 | void plotter1() 39 | { 40 | 41 | srand(time(NULL)); 42 | int *arr; 43 | int n,key,r; 44 | FILE *f1,*f2,*f3; 45 | f1=fopen("linearbest.txt","a"); 46 | f2=fopen("linearavg.txt","a"); 47 | f3=fopen("linearworst.txt","a"); 48 | 49 | 50 | n=2; 51 | while(n<=1024) 52 | { 53 | arr=(int *)malloc(n*sizeof(int)); 54 | for(int i=0;i 2 | #include 3 | 4 | int bubblesort(int *a, int n) { 5 | int count = 0; 6 | int i, j, t, flag; 7 | 8 | for (i = 0; i < n - 1; i++) { 9 | flag = 0; 10 | for (j = 0; j < n - i - 1; j++) { 11 | count++; 12 | if (a[j] > a[j + 1]) { 13 | t = *(a + j); 14 | *(a + j) = *(a + j + 1); 15 | *(a + j + 1) = t; 16 | flag = 1; 17 | } 18 | } 19 | if (flag == 0) { 20 | break; 21 | } 22 | } 23 | return count; 24 | } 25 | 26 | void plotter() { 27 | int *arr, n; 28 | FILE *f1, *f2, *f3; 29 | 30 | f1 = fopen("BUBBLEBEST.txt", "a"); 31 | f2 = fopen("BUBBLEWORST.txt", "a"); 32 | f3 = fopen("BUBBLEAVG.txt", "a"); 33 | n = 10; 34 | 35 | while (n <= 30000) { 36 | arr = (int *)malloc(sizeof(int) * n); 37 | 38 | // Worst case 39 | for (int i = 0; i < n; i++) { 40 | *(arr + i) = n - i; 41 | } 42 | int count = bubblesort(arr, n); 43 | fprintf(f2, "%d\t%d\n", n, count); 44 | 45 | // Best case 46 | for (int i = 0; i < n; i++) { 47 | *(arr + i) = i + 1; 48 | } 49 | count = bubblesort(arr, n); 50 | fprintf(f1, "%d\t%d\n", n, count); 51 | 52 | // Average case 53 | for (int i = 0; i < n; i++) { 54 | *(arr + i) = rand() % n; 55 | } 56 | count = bubblesort(arr, n); 57 | fprintf(f3, "%d\t%d\n", n, count); 58 | 59 | if (n < 10000) { 60 | n *= 10; 61 | } else { 62 | n += 10000; 63 | } 64 | free(arr); 65 | } 66 | 67 | fclose(f1); 68 | fclose(f2); 69 | fclose(f3); 70 | } 71 | 72 | void tester() { 73 | int *arr, n; 74 | printf("ENTER THE NUMBER OF ELEMENTS\n"); 75 | scanf("%d", &n); 76 | 77 | arr = (int *)malloc(sizeof(int) * n); 78 | printf("ENTER THE ELEMENTS OF THE ARRAY\n"); 79 | for (int i = 0; i < n; i++) { 80 | scanf("%d", &arr[i]); 81 | } 82 | 83 | printf("THE ELEMENTS OF THE ARRAY BEFORE SORTING\n"); 84 | for (int i = 0; i < n; i++) { 85 | printf("%d ", arr[i]); 86 | } 87 | printf("\n"); 88 | 89 | bubblesort(arr, n); 90 | 91 | printf("THE ELEMENTS OF THE ARRAY AFTER SORTING\n"); 92 | for (int i = 0; i < n; i++) { 93 | printf("%d ", arr[i]); 94 | } 95 | printf("\n"); 96 | 97 | free(arr); 98 | } 99 | 100 | int main() { // Changed to int and added return 0 101 | for (;;) { 102 | int key; 103 | printf("ENTER THE CHOICE \n1.TO TEST \n2.TO PLOT\n0 TO EXIT\n"); 104 | scanf("%d", &key); 105 | 106 | switch (key) { 107 | case 1: 108 | tester(); 109 | break; 110 | case 2: 111 | plotter(); 112 | break; 113 | case 0: 114 | return 0; 115 | default: 116 | printf("Invalid choice, try again.\n"); 117 | } 118 | } 119 | return 0; 120 | } 121 | -------------------------------------------------------------------------------- /p10/toposrcplot.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int count = 0; 5 | 6 | typedef struct queue { 7 | int f, r, *arr, cnt; 8 | } QUE; 9 | 10 | int s[10]; 11 | 12 | void indegree(int *a[], int v, int inq[], QUE *temp, int flag[]) { 13 | for (int i = 0; i < v; i++) { 14 | for (int j = 0; j < v; j++) { 15 | if (a[j][i] == 1) 16 | inq[i] = inq[i] + 1; 17 | } 18 | if (inq[i] == 0) { 19 | temp->r = (temp->r + 1) % v; 20 | temp->arr[temp->r] = i; 21 | temp->cnt = temp->cnt + 1; 22 | flag[i] = 1; 23 | } 24 | } 25 | } 26 | 27 | void sourceremove(int *a[], int v, QUE *temp, int inq[], int flag[]) { 28 | int cnt = 0; 29 | while (temp->cnt != 0) { 30 | int source = temp->arr[temp->f]; 31 | temp->f = (temp->f + 1) % v; 32 | s[cnt] = source; 33 | temp->cnt = temp->cnt - 1; 34 | cnt++; 35 | count++; 36 | for (int i = 0; i < v; i++) { 37 | if (a[source][i] == 1) 38 | inq[i] = inq[i] - 1; 39 | if (inq[i] == 0 && flag[i] == 0) { 40 | temp->r = (temp->r + 1) % v; 41 | temp->arr[temp->r] = i; 42 | temp->cnt = temp->cnt + 1; 43 | flag[i] = 1; 44 | } 45 | } 46 | } 47 | 48 | if (cnt != v) { 49 | printf("Cycles exist, no topological sorting possible\n"); 50 | } else { 51 | printf("The topological sorting is\n"); 52 | for (int i = 0; i < v; i++) 53 | printf("%c\t", s[i] + 65); 54 | } 55 | } 56 | 57 | void ploter(int k) { 58 | FILE *f1 = fopen("TopSortBEST.txt", "a"); 59 | FILE *f2 = fopen("TopSortWORST.txt", "a"); 60 | int v; 61 | 62 | for (int i = 1; i <= 10; i++) { 63 | v = i; 64 | int *arr[v]; 65 | for (int j = 0; j < v; j++) 66 | arr[j] = (int *)malloc(sizeof(int) * v); 67 | 68 | if (k == 0) { // Best case: Complete graph (no cycles) 69 | for (int j = 0; j < v; j++) { 70 | for (int l = 0; l < v; l++) { 71 | if (j != l) 72 | arr[j][l] = 1; 73 | else 74 | arr[j][l] = 0; 75 | } 76 | } 77 | } 78 | 79 | if (k == 1) { // Worst case: Chain graph (linear, no cycles) 80 | for (int j = 0; j < v; j++) { 81 | for (int l = 0; l < v; l++) 82 | arr[j][l] = 0; 83 | } 84 | for (int j = 0; j < v - 1; j++) { 85 | arr[j][j + 1] = 1; 86 | } 87 | } 88 | 89 | QUE q; 90 | q.f = 0; 91 | q.r = -1; 92 | q.cnt = 0; 93 | q.arr = (int*)malloc(sizeof(int) * v); 94 | 95 | int *inq = (int *)malloc(sizeof(int) * v); 96 | for (int j = 0; j < v; j++) 97 | inq[j] = 0; 98 | 99 | int *flag = (int *)malloc(sizeof(int) * v); 100 | for (int j = 0; j < v; j++) 101 | flag[j] = 0; 102 | 103 | indegree(arr, v, inq, &q, flag); 104 | sourceremove(arr, v, &q, inq, flag); 105 | 106 | if (k == 0) 107 | fprintf(f2, "%d\t%d\n", v, count); 108 | else 109 | fprintf(f1, "%d\t%d\n", v, count); 110 | 111 | count = 0; // Reset count for the next graph 112 | 113 | for (int j = 0; j < v; j++) 114 | free(arr[j]); 115 | free(q.arr); 116 | free(inq); 117 | free(flag); 118 | } 119 | 120 | fclose(f1); 121 | fclose(f2); 122 | } 123 | 124 | void main() { 125 | for (int i = 0; i < 2; i++) 126 | ploter(i); 127 | } 128 | -------------------------------------------------------------------------------- /p15/diji.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define AL 10; 5 | 6 | int n, i, j, src, cost[10][10], d[10] = {0}, removed[10] = {0}, count = 0; 7 | 8 | int heapcount,graphcount; 9 | int heapsize; 10 | struct vertex 11 | { 12 | int id; 13 | int dist; 14 | } heap[10]; 15 | 16 | typedef struct vertex ver; 17 | 18 | // Min Heap function declaration 19 | void swap(struct vertex *a, struct vertex *b) 20 | { 21 | struct vertex temp = *a; 22 | *a = *b; 23 | *b = temp; 24 | } 25 | void heapSort(struct vertex arr[], int n) 26 | { 27 | for (int i = n / 2 - 1; i >= 0; i--) 28 | { 29 | heapify(arr, n, i); 30 | } 31 | } 32 | // Min heap function declaration end 33 | int dcount=0; 34 | void heapify(struct vertex arr[], int n, int i) 35 | { 36 | heapcount++; 37 | int largest = i; 38 | int left = 2 * i + 1; 39 | int right = 2 * i + 2; 40 | 41 | if (left < n && arr[left].dist < arr[largest].dist) 42 | largest = left; 43 | if (right < n && arr[right].dist < arr[largest].dist) 44 | largest = right; 45 | 46 | if (largest != i) 47 | { 48 | swap(&arr[i], &arr[largest]); 49 | heapify(arr, n, largest); 50 | } 51 | } 52 | void makegraph() 53 | { 54 | // Make Graph 55 | printf("Enter the total number of vertices:"); 56 | scanf("%d", &n); 57 | printf("Enter the cost matrix of the Graph\n"); 58 | for (i = 0; i < n; i++) 59 | { 60 | for (j = 0; j < n; j++) 61 | { 62 | scanf("%d", &cost[i][j]); 63 | if (cost[i][j] == 0) 64 | cost[i][j] = INT_MAX; 65 | } 66 | } 67 | 68 | // Initialise the source vertex distance to 0 and rest all to infinity(INT_MAX) 69 | printf("Enter the source vertex:"); 70 | scanf("%d", &src); 71 | for (i = 0; i < n; i++) 72 | { 73 | d[i] = INT_MAX; 74 | } 75 | d[src] = 0; 76 | } 77 | 78 | 79 | // returns the min of the heap and heapifies the rest of the elements 80 | ver deleteheap(ver heap[]) 81 | { 82 | ver min = heap[0]; 83 | heap[0] = heap[heapsize - 1]; 84 | heapsize = heapsize - 1; 85 | // heapify(heap, heapsize, 0); 86 | return min; 87 | } 88 | 89 | 90 | void dijkstra() 91 | { 92 | for (i = 0; i < n; i++) 93 | { 94 | heap[i].id = i; 95 | heap[i].dist = INT_MAX; 96 | } 97 | heap[src].dist = 0; 98 | heapsize = n; 99 | // pulling source to index 0 100 | 101 | heapSort(heap, heapsize); 102 | while (count < n) 103 | { 104 | ver minvertex = deleteheap(heap); 105 | int u = minvertex.id; 106 | removed[u] = 1; 107 | count++; 108 | 109 | for (i = 0; i < n; i++) 110 | { 111 | if (!removed[i] && cost[u][i] != INT_MAX) 112 | { 113 | graphcount++; 114 | if ((d[u] + cost[u][i]) < d[i]) 115 | { 116 | d[i] = (d[u] + cost[u][i]); 117 | for (int o = 0; o < heapsize; o++) 118 | { 119 | if (heap[o].id == i) 120 | { 121 | heap[o].dist = heap[o].dist < d[i] ? heap[o].dist : d[i]; 122 | break; 123 | } 124 | } 125 | 126 | 127 | } 128 | } 129 | } 130 | 131 | heapSort(heap, heapsize); 132 | } 133 | } 134 | 135 | 136 | void main() 137 | { 138 | heapcount=0; 139 | graphcount=0; 140 | makegraph(); 141 | dijkstra(); 142 | printf("Shortest path id %d is:\n", src); 143 | for (i = 0; i < n; i++) 144 | { 145 | if (src != i) 146 | printf("%d -> %d = %d\n", src, i, d[i]); 147 | } 148 | int max=(graphcount 4 | #include 5 | #include 6 | int n, i, j, cost[10][10], cnt = 0, visited[10], removed[10]; 7 | int heapsize = 0; 8 | int heapcount,graphcount; 9 | struct edge 10 | { 11 | int v; 12 | int dist; 13 | int u; 14 | } heap[10] /*heapsize*/, VT[10] /*cnt*/; 15 | typedef struct edge edg; 16 | // Min Heap function declaration 17 | void swap(struct edge *a, struct edge *b) 18 | { 19 | struct edge temp = *a; 20 | *a = *b; 21 | *b = temp; 22 | } 23 | 24 | 25 | void heapify(struct edge arr[], int n, int i) 26 | { 27 | int largest = i; 28 | int left = 2 * i + 1; 29 | int right = 2 * i + 2; 30 | 31 | heapcount++; 32 | if (left < n && arr[left].dist < arr[largest].dist) 33 | largest = left; 34 | if (right < n && arr[right].dist < arr[largest].dist) 35 | largest = right; 36 | 37 | if (largest != i) 38 | { 39 | swap(&arr[i], &arr[largest]); 40 | heapify(arr, n, largest); 41 | } 42 | } 43 | void heapSort(struct edge arr[], int n) 44 | { 45 | for (int i = n / 2 - 1; i >= 0; i--) 46 | { 47 | heapify(arr, n, i); 48 | } 49 | } 50 | // Min heap function declaration end 51 | 52 | 53 | void makegraph() 54 | { 55 | // Make Graph 56 | printf("Enter the total number of vertices:"); 57 | scanf("%d", &n); 58 | printf("Enter the cost matrix of the Graph\n"); 59 | for (i = 0; i < n; i++) 60 | { 61 | for (j = 0; j < n; j++) 62 | { 63 | scanf("%d", &cost[i][j]); 64 | if (cost[i][j] == 0) 65 | cost[i][j] = INT_MAX; 66 | } 67 | } 68 | } 69 | // returns the min of the heap //astey 70 | edg deleteheap(edg heap[]) 71 | { 72 | edg min = heap[0]; 73 | heap[0] = heap[heapsize - 1]; 74 | heapsize = heapsize - 1; 75 | return min; 76 | } 77 | void prim() 78 | { 79 | // Appending Souce vertex to heap and incrementing heap size 80 | visited[0] = 1; 81 | heap[heapsize].v = -1; 82 | heap[heapsize].u = 0; 83 | heap[heapsize].dist = 0; 84 | heapsize++; 85 | 86 | while (cnt != n) 87 | { 88 | // fetching the min and appending to the visited array of edges and deleting from heap 89 | edg min = deleteheap(heap); 90 | VT[cnt].v = min.v; 91 | VT[cnt].u = min.u; 92 | VT[cnt].dist = min.dist; 93 | cnt++; 94 | int v = min.u; 95 | removed[v] = 1; 96 | for (i = 1; i < n; i++) 97 | { 98 | if (!visited[i] && cost[v][i] != INT_MAX && !removed[i]) 99 | { 100 | // not visited and not removed from heap 101 | visited[i] = 1; 102 | heap[heapsize].v = v; 103 | heap[heapsize].u = i; 104 | heap[heapsize].dist = cost[v][i]; 105 | heapsize++; 106 | } 107 | if (visited[i] && cost[v][i] != INT_MAX && !removed[i]) 108 | { // visited but not removed from heap --> scope for minimisation? 109 | graphcount++; 110 | for (j = 0; j < heapsize; j++) 111 | { // finding that edge in the sorted heap 112 | if (heap[j].u == i && cost[v][i] < heap[j].dist) 113 | { // replacing if optimal 114 | heap[j].dist = cost[v][i]; 115 | heap[j].v = v; 116 | break; 117 | } 118 | } 119 | } 120 | } 121 | heapSort(heap, heapsize); // sorting after deletions and value modifications 122 | } 123 | } 124 | void main() 125 | { 126 | int sum = 0; 127 | heapcount=0; 128 | graphcount=0; 129 | makegraph(); 130 | prim(); 131 | for (int i = 1; i < cnt; i++) 132 | { 133 | printf("%c --> %c == %d\n", VT[i].v + 65, VT[i].u + 65, VT[i].dist); 134 | sum += VT[i].dist; 135 | } 136 | printf("Minimum Distance is: %d", sum); 137 | int max=(graphcount 2 | #include 3 | #include 4 | 5 | int n, i, j, cost[10][10], cnt = 0, visited[10], removed[10]; 6 | int heapsize = 0; 7 | int heapcount, graphcount; 8 | struct edge 9 | { 10 | int v; 11 | int dist; 12 | int u; 13 | } heap[10], VT[10]; 14 | 15 | typedef struct edge edg; 16 | 17 | // Function to swap two edges in the heap 18 | void swap(struct edge *a, struct edge *b) 19 | { 20 | struct edge temp = *a; 21 | *a = *b; 22 | *b = temp; 23 | } 24 | 25 | // Min-heapify function to maintain the heap property 26 | void heapify(struct edge arr[], int n, int i) 27 | { 28 | int smallest = i; 29 | int left = 2 * i + 1; 30 | int right = 2 * i + 2; 31 | 32 | heapcount++; 33 | if (left < n && arr[left].dist < arr[smallest].dist) 34 | smallest = left; 35 | if (right < n && arr[right].dist < arr[smallest].dist) 36 | smallest = right; 37 | 38 | if (smallest != i) 39 | { 40 | swap(&arr[i], &arr[smallest]); 41 | heapify(arr, n, smallest); 42 | } 43 | } 44 | 45 | // Heap sort function to build the initial min-heap 46 | void heapSort(struct edge arr[], int n) 47 | { 48 | for (int i = n / 2 - 1; i >= 0; i--) 49 | { 50 | heapify(arr, n, i); 51 | } 52 | } 53 | 54 | // Function to build the graph from user input 55 | void makegraph() 56 | { 57 | printf("Enter the total number of vertices: "); 58 | scanf("%d", &n); 59 | printf("Enter the cost matrix of the Graph\n"); 60 | for (i = 0; i < n; i++) 61 | { 62 | for (j = 0; j < n; j++) 63 | { 64 | scanf("%d", &cost[i][j]); 65 | if (cost[i][j] == 0) 66 | cost[i][j] = INT_MAX; 67 | } 68 | } 69 | } 70 | 71 | // Function to remove and return the minimum edge from the heap 72 | edg deleteheap(edg heap[]) 73 | { 74 | edg min = heap[0]; 75 | heap[0] = heap[heapsize - 1]; 76 | heapsize = heapsize - 1; 77 | return min; 78 | } 79 | 80 | // Prim's algorithm implementation using a min-heap 81 | void prim() 82 | { 83 | visited[0] = 1; 84 | heap[heapsize].v = -1; 85 | heap[heapsize].u = 0; 86 | heap[heapsize].dist = 0; 87 | heapsize++; 88 | 89 | while (cnt != n) 90 | { 91 | edg min = deleteheap(heap); 92 | VT[cnt].v = min.v; 93 | VT[cnt].u = min.u; 94 | VT[cnt].dist = min.dist; 95 | cnt++; 96 | int v = min.u; 97 | removed[v] = 1; 98 | for (i = 1; i < n; i++) 99 | { 100 | if (!visited[i] && cost[v][i] != INT_MAX && !removed[i]) 101 | { 102 | visited[i] = 1; 103 | heap[heapsize].v = v; 104 | heap[heapsize].u = i; 105 | heap[heapsize].dist = cost[v][i]; 106 | heapsize++; 107 | } 108 | if (visited[i] && cost[v][i] != INT_MAX && !removed[i]) 109 | { 110 | graphcount++; 111 | for (j = 0; j < heapsize; j++) 112 | { 113 | if (heap[j].u == i && cost[v][i] < heap[j].dist) 114 | { 115 | heap[j].dist = cost[v][i]; 116 | heap[j].v = v; 117 | break; 118 | } 119 | } 120 | } 121 | } 122 | heapSort(heap, heapsize); 123 | } 124 | } 125 | 126 | // Function to run Prim's algorithm and display the results 127 | void run() 128 | { 129 | makegraph(); 130 | heapcount = 0; 131 | graphcount = 0; 132 | prim(); 133 | 134 | int sum = 0; 135 | printf("Minimum Spanning Tree:\n"); 136 | for (int i = 1; i < cnt; i++) 137 | { 138 | printf("%c --> %c == %d\n", VT[i].v + 65, VT[i].u + 65, VT[i].dist); 139 | sum += VT[i].dist; 140 | } 141 | printf("Minimum Distance is: %d\n", sum); 142 | 143 | int max = (graphcount > heapcount) ? graphcount : heapcount; 144 | printf("Basic operation count (Max of graph and heap counts): %d\n", max); 145 | } 146 | 147 | // Main function with a menu-driven approach 148 | void main() 149 | { 150 | FILE *f1; 151 | f1 = fopen("primgraph.txt", "a"); 152 | int ch; 153 | 154 | while (1) 155 | { 156 | printf("Enter choice 1 to continue and 0 to exit: "); 157 | scanf("%d", &ch); 158 | switch (ch) 159 | { 160 | case 1: 161 | run(); 162 | fprintf(f1, "Vertices: %d, Max Operation Count: %d\n", n, (graphcount > heapcount) ? graphcount : heapcount); 163 | break; 164 | case 0: 165 | fclose(f1); 166 | exit(0); 167 | default: 168 | printf("Invalid choice. Please enter 1 to continue or 0 to exit.\n"); 169 | } 170 | } 171 | } 172 | -------------------------------------------------------------------------------- /p15/dijiplotter.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int n, i, j, src, cost[10][10], d[10] = {0}, removed[10] = {0}, count = 0; 6 | int heapsize; 7 | int graphcount, heapcount, max; 8 | 9 | struct vertex 10 | { 11 | int id; 12 | int dist; 13 | } heap[10]; 14 | 15 | typedef struct vertex ver; 16 | 17 | // Function to swap two vertices 18 | void swap(struct vertex *a, struct vertex *b) 19 | { 20 | struct vertex temp = *a; 21 | *a = *b; 22 | *b = temp; 23 | } 24 | 25 | // Min-heapify function to maintain the heap property 26 | void heapify(struct vertex arr[], int n, int i) 27 | { 28 | int smallest = i; 29 | int left = 2 * i + 1; 30 | int right = 2 * i + 2; 31 | heapcount++; 32 | 33 | if (left < n && arr[left].dist < arr[smallest].dist) 34 | smallest = left; 35 | 36 | if (right < n && arr[right].dist < arr[smallest].dist) 37 | smallest = right; 38 | 39 | if (smallest != i) 40 | { 41 | swap(&arr[i], &arr[smallest]); 42 | heapify(arr, n, smallest); 43 | } 44 | } 45 | 46 | // Heap sort function to build a min-heap 47 | void heapSort(struct vertex arr[], int n) 48 | { 49 | for (int i = n / 2 - 1; i >= 0; i--) 50 | { 51 | heapify(arr, n, i); 52 | } 53 | } 54 | 55 | // Function to build the graph from user input 56 | void makegraph() 57 | { 58 | printf("Enter the total number of vertices: "); 59 | scanf("%d", &n); 60 | printf("Enter the cost matrix of the Graph\n"); 61 | for (i = 0; i < n; i++) 62 | { 63 | for (j = 0; j < n; j++) 64 | { 65 | scanf("%d", &cost[i][j]); 66 | if (i == j) 67 | cost[i][j] = 0; // No self-loop cost 68 | else if (cost[i][j] == 0) 69 | cost[i][j] = INT_MAX; 70 | } 71 | } 72 | 73 | // Initialize the source vertex distance to 0 and others to infinity (INT_MAX) 74 | printf("Enter the source vertex: "); 75 | scanf("%d", &src); 76 | for (i = 0; i < n; i++) 77 | { 78 | d[i] = INT_MAX; 79 | } 80 | d[src] = 0; 81 | } 82 | 83 | // Function to remove and return the minimum vertex from the heap 84 | ver deleteheap(ver heap[]) 85 | { 86 | ver min = heap[0]; 87 | heap[0] = heap[heapsize - 1]; 88 | heapsize = heapsize - 1; 89 | heapify(heap, heapsize, 0); 90 | return min; 91 | } 92 | 93 | // Dijkstra's algorithm implementation using a min-heap 94 | void dijkstra() 95 | { 96 | for (i = 0; i < n; i++) 97 | { 98 | heap[i].id = i; 99 | heap[i].dist = INT_MAX; 100 | } 101 | heap[src].dist = 0; 102 | heapsize = n; 103 | 104 | // Building the initial min-heap 105 | heapSort(heap, heapsize); 106 | 107 | while (count < n) 108 | { 109 | ver minvertex = deleteheap(heap); 110 | int u = minvertex.id; 111 | removed[u] = 1; 112 | count++; 113 | 114 | for (i = 0; i < n; i++) 115 | { 116 | if (!removed[i] && cost[u][i] != INT_MAX) 117 | { 118 | graphcount++; 119 | if ((d[u] + cost[u][i]) < d[i]) 120 | { 121 | d[i] = (d[u] + cost[u][i]); 122 | for (int o = 0; o < heapsize; o++) 123 | { 124 | if (heap[o].id == i) 125 | { 126 | heap[o].dist = d[i]; 127 | break; 128 | } 129 | } 130 | // Re-sort heap after updating 131 | heapSort(heap, heapsize); 132 | } 133 | } 134 | } 135 | } 136 | } 137 | 138 | // Function to run the Dijkstra algorithm and display the results 139 | void run() 140 | { 141 | makegraph(); 142 | max = 0; 143 | graphcount = 0; 144 | heapcount = 0; 145 | count = 0; 146 | dijkstra(); 147 | 148 | printf("Shortest paths from vertex %d:\n", src); 149 | for (i = 0; i < n; i++) 150 | { 151 | if (src != i) 152 | printf("%d -> %d = %d\n", src, i, d[i]); 153 | } 154 | 155 | max = (graphcount > heapcount) ? graphcount : heapcount; 156 | printf("Basic operation count (Max of graph and heap counts): %d\n", max); 157 | } 158 | 159 | // Main function with a menu-driven approach 160 | void main() 161 | { 162 | FILE *f1; 163 | f1 = fopen("dijkstrasgraph.txt", "a"); 164 | int ch; 165 | 166 | while (1) 167 | { 168 | printf("Enter choice 1 to continue and 0 to exit: "); 169 | scanf("%d", &ch); 170 | switch (ch) 171 | { 172 | case 1: 173 | run(); 174 | fprintf(f1, "Vertices: %d, Max Operation Count: %d\n", n, max); 175 | break; 176 | case 0: 177 | fclose(f1); 178 | exit(0); 179 | default: 180 | printf("Invalid choice. Please enter 1 to continue or 0 to exit.\n"); 181 | } 182 | } 183 | } 184 | -------------------------------------------------------------------------------- /p14/primsheap.c: -------------------------------------------------------------------------------- 1 | /*prims with min heap*/ 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define V 10 // Maximum number of vertices in the graph 8 | 9 | struct Vertex { 10 | int index; 11 | int key; 12 | }; 13 | 14 | struct MinHeap { 15 | int size; 16 | int capacity; 17 | struct Vertex* heapArr; 18 | }; 19 | 20 | struct Vertex newVertex(int index, int key) { 21 | struct Vertex vertex; 22 | vertex.index = index; 23 | vertex.key = key; 24 | return vertex; 25 | } 26 | 27 | struct MinHeap* createMinHeap(int capacity) { 28 | struct MinHeap* minHeap = (struct MinHeap*)malloc(sizeof(struct MinHeap)); 29 | minHeap->size = 0; 30 | minHeap->capacity = capacity; 31 | minHeap->heapArr = (struct Vertex*)malloc(capacity * sizeof(struct Vertex)); // <-- Corrected line 32 | return minHeap; 33 | } 34 | 35 | void swap(struct Vertex* a, struct Vertex* b) { 36 | struct Vertex temp = *a; 37 | *a = *b; 38 | *b = temp; 39 | } 40 | 41 | // Min-heapify function 42 | void minHeapify(struct MinHeap* minHeap, int index) { 43 | int smallest = index; 44 | int leftChild = 2 * index + 1; 45 | int rightChild = 2 * index + 2; 46 | 47 | if (leftChild < minHeap->size && minHeap->heapArr[leftChild].key < minHeap->heapArr[smallest].key) 48 | smallest = leftChild; 49 | 50 | if (rightChild < minHeap->size && minHeap->heapArr[rightChild].key < minHeap->heapArr[smallest].key) 51 | smallest = rightChild; 52 | 53 | if (smallest != index) { 54 | swap(&minHeap->heapArr[index], &minHeap->heapArr[smallest]); 55 | minHeapify(minHeap, smallest); 56 | } 57 | } 58 | 59 | 60 | void print(struct MinHeap* minHeap) 61 | { 62 | printf("the heap contents\n"); 63 | for(int i = 0; i < minHeap->size; i++) 64 | { 65 | printf("%c ",(minHeap->heapArr[i].index)+65); 66 | printf("%d ",minHeap->heapArr[i].key); 67 | } 68 | printf("\n"); 69 | } 70 | 71 | // Build min-heap from the given array 72 | void buildMinHeap(struct MinHeap* minHeap) { 73 | int n = minHeap->size - 1; 74 | for (int i = (n - 1) / 2; i >= 0; i--) 75 | minHeapify(minHeap, i); 76 | } 77 | 78 | // Extract the minimum node from min-heap 79 | struct Vertex extractMin(struct MinHeap* minHeap) { 80 | struct Vertex minVertex = minHeap->heapArr[0]; 81 | minHeap->heapArr[0] = minHeap->heapArr[minHeap->size - 1]; 82 | minHeap->size--; 83 | minHeapify(minHeap, 0); 84 | return minVertex; 85 | } 86 | 87 | // Function to decrease the key value of a given vertex index 88 | void decreaseKey(struct MinHeap* minHeap, int index, int newKey) 89 | { 90 | int k; 91 | for(int i = 0; i < minHeap->size;i++) 92 | { 93 | if(minHeap->heapArr[i].index==index) 94 | k=i; 95 | } 96 | minHeap->heapArr[k].key = newKey; 97 | while (index > 0 && minHeap->heapArr[(index - 1) / 2].key > minHeap->heapArr[index].key) { 98 | swap(&minHeap->heapArr[index], &minHeap->heapArr[(index - 1) / 2]); 99 | index = (index - 1) / 2; 100 | } 101 | } 102 | 103 | bool isInMinHeap(struct MinHeap* minHeap, int v) { 104 | for (int i = 0; i < minHeap->size; i++) { 105 | if (minHeap->heapArr[i].index == v) 106 | return true; 107 | } 108 | return false; 109 | } 110 | 111 | // Prim's algorithm using min-heap 112 | void primMST(int graph[V][V], int vertices) { 113 | struct MinHeap* minHeap = createMinHeap(vertices); 114 | int parent[V]; // Array to store constructed MST 115 | int key[V]; // Key values to pick minimum weight edge 116 | 117 | // Initialize key values and parent array 118 | for (int v = 0; v < vertices; v++) { 119 | parent[v] = -1; 120 | key[v] = 999; 121 | minHeap->heapArr[v] = newVertex(v, key[v]); 122 | } 123 | 124 | // Make the first vertex the root of MST 125 | key[0] = 0; 126 | minHeap->heapArr[0].key = 0; 127 | minHeap->size = vertices; 128 | 129 | // Loop through all vertices to construct MST 130 | while (minHeap->size > 0) { 131 | // print(minHeap); 132 | struct Vertex minVertex = extractMin(minHeap); 133 | int u = minVertex.index; 134 | //if(u !=0) 135 | //printf("%c-->%c | Cost: %d\n",parent[u]+65,u+65,key[u]); 136 | for (int v = 0; v < vertices; v++) { 137 | if (graph[u][v] && isInMinHeap(minHeap, v) && graph[u][v] < key[v]) { 138 | parent[v] = u; 139 | key[v] = graph[u][v]; 140 | decreaseKey(minHeap, v, key[v]); 141 | } 142 | } 143 | 144 | //print(minHeap); 145 | } 146 | 147 | int total = 0; 148 | // Print the constructed MST 149 | printf("Edge \tWeight\n"); 150 | for (int i = 1; i < vertices; i++) { 151 | printf("%c - %c \t%d\n", parent[i]+65, i+65 ,graph[i][parent[i]]); 152 | total += key[i]; 153 | } 154 | printf("THE TOTAL WEIGHT OF THE MST IS %d\n",total); 155 | 156 | 157 | free(minHeap->heapArr); 158 | free(minHeap); 159 | } 160 | 161 | int main() { 162 | int vertices; 163 | printf("Enter the total number of vertices in the graph: "); 164 | scanf("%d", &vertices); 165 | 166 | int graph[V][V]; 167 | printf("Enter the adjacency matrix for the graph:\n"); 168 | for (int i = 0; i < vertices; i++) { 169 | for (int j = 0; j < vertices; j++) { 170 | scanf("%d", &graph[i][j]); 171 | } 172 | } 173 | 174 | /* for (int i = 0; i < vertices; i++) { 175 | for (int j = 0; j < vertices; j++) { 176 | printf("%d ", graph[i][j]); 177 | } 178 | printf("\n"); 179 | }*/ 180 | 181 | primMST(graph, vertices); 182 | return 0; 183 | } 184 | --------------------------------------------------------------------------------