Posts

Showing posts from July, 2024

prims algo

  #include <stdio.h> #include <limits.h> #define V_MAX 100  int minKey(int key[], int mstSet[], int V) {     int min = INT_MAX, min_index;     for (int v = 0; v < V; v++)         if (mstSet[v] == 0 && key[v] < min)             min = key[v], min_index = v;     return min_index; } void printMST(int parent[], int V, int graph[V_MAX][V_MAX]) {     printf("Edge Weight\n");     for (int i = 1; i < V; i++)         printf("%d - %d %d \n", parent[i], i, graph[i][parent[i]]); } void primMST(int graph[][V_MAX], int V) {     int parent[V_MAX];     int key[V_MAX];     int mstSet[V_MAX];     for (int i = 0; i < V; i++) {         key[i] = INT_MAX;         mstSet[i] = 0;     }     key[0] = 0;     parent[0] = -1;     for (int ...

kruskal aglo

  #include <stdio.h> #include <stdlib.h> #define MAX_EDGES 1000 typedef struct Edge {     int src, dest, weight; } Edge; typedef struct Graph {     int V, E;     Edge edges[MAX_EDGES]; } Graph; typedef struct Subset {     int parent, rank; } Subset; Graph* createGraph(int V, int E) {     Graph* graph = (Graph*) malloc(sizeof(Graph));     graph->V = V;     graph->E = E;     return graph; } int find(Subset subsets[], int i) {     if (subsets[i].parent != i) {         subsets[i].parent = find(subsets, subsets[i].parent);     }     return subsets[i].parent; } void Union(Subset subsets[], int x, int y) {     int xroot = find(subsets, x);     int yroot = find(subsets, y);     if (subsets[xroot].rank < subsets[yroot].rank) {         subsets[xroot].parent = yroot;     } else if (sub...

topological

  #include <stdio.h> #include <stdlib.h> #define MAX_VERTICES 100 typedef struct {     int V;     int** adjMatrix; } Graph; Graph* createGraph(int V) {     Graph* graph = (Graph*)malloc(sizeof(Graph));     graph->V = V;     graph->adjMatrix = (int**)calloc(V, sizeof(int*));     for (int i = 0; i < V; i++) {         graph->adjMatrix[i] = (int*)calloc(V, sizeof(int));     }     return graph; } void addEdge(Graph* graph, int src, int dest) {     graph->adjMatrix[src][dest] = 1; } void topologicalSort(Graph* graph) {     int V = graph->V;     int inDegree[MAX_VERTICES] = {0};     int queue[MAX_VERTICES], front = 0, rear = -1;     for (int i = 0; i < V; i++) {         for (int j = 0; j < V; j++) {             if (graph->adjMatrix[i][j] == 1) {   ...

quicksort

 #include <stdio.h> #include <stdlib.h> #include <time.h> int partition(int arr[], int low, int high) {     int pivot = arr[high];     int i = (low - 1);     for (int j = low; j <= high - 1; j++) {         if (arr[j] <= pivot) {             i++;              int temp = arr[i];             arr[i] = arr[j];             arr[j] = temp;         }     }     int temp = arr[i + 1];     arr[i + 1] = arr[high];     arr[high] = temp;     return (i + 1); } void quickSort(int arr[], int low, int high) {     if (low < high) {         int pi = partition(arr, low, high);         quickSort(arr, low, pi - 1);         quickSort(arr, pi + 1, high);     } }...

mergesort

 #include <stdio.h> #include <stdlib.h> #include <time.h> void merge(int arr[], int l, int m, int r) {     int i, j, k;     int n1 = m - l + 1;     int n2 = r - m;     int* L = (int*)malloc(n1 * sizeof(int));     int* R = (int*)malloc(n2 * sizeof(int));     for (i = 0; i < n1; i++)         L[i] = arr[l + i];     for (j = 0; j < n2; j++)         R[j] = arr[m + 1 + j];     i = 0;     j = 0;     k = l;     while (i < n1 && j < n2) {         if (L[i] <= R[j]) {             arr[k] = L[i];             i++;         } else {             arr[k] = R[j];             j++;         }         k++;     }...

selectionsort

 #include <stdio.h> #include <stdlib.h> #include <time.h> void selectionSort(int arr[], int n) {     int i, j, minIndex, temp;     for (i = 0; i < n - 1; i++) {         minIndex = i;         for (j = i + 1; j < n; j++) {             if (arr[j] < arr[minIndex]) {                 minIndex = j;             }         }         temp = arr[minIndex];         arr[minIndex] = arr[i];         arr[i] = temp;     } } int main() {     int n, i;     clock_t start, end;     double cpu_time_used;     printf("Enter the number of elements (n): ");     scanf("%d", &n);     if (n < 5000) {         printf("Please enter a value of n greater than...