Posts

topological sort P6

 #include <stdio.h> #include <stdlib.h> #define MAX_VERTICES 100 // Structure to represent a graph typedef struct {     int V;     int** adjMatrix; } Graph; // Function to create a new 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; } // Function to add an edge to the graph void addEdge(Graph* graph, int src, int dest) {     graph->adjMatrix[src][dest] = 1; } // Function to perform topological sorting void topologicalSort(Graph* graph) {     int V = graph->V;     int inDegree[MAX_VERTICES] = {0};     int queue[MAX_VERTICES], front = 0, rear = -1;     // Calculate in-degree (number o...

prim's algo P5

 #include <stdio.h> #include <limits.h> #define V_MAX 100 // Maximum number of vertices // Function to find the vertex with the minimum key value, from the set of vertices not yet included in the MST 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; } // Function to print the constructed MST stored in parent[] 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]]); } // Function to construct and print MST for a graph represented using adjacency matrix representation void primMST(int graph[][V_MAX], int V) {     int parent[V_MAX]...

kruskal algo P4

 #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 (subs...

quick sort P3

 #include <stdio.h> #include <stdlib.h> #include <time.h> // Function to partition the array and return the pivot index int partition(int arr[], int low, int high) {     int pivot = arr[high]; // Pivot element     int i = (low - 1); // Index of smaller element     for (int j = low; j <= high - 1; j++) {         // If current element is smaller than or equal to pivot         if (arr[j] <= pivot) {             i++; // Increment index of smaller element             // Swap arr[i] and arr[j]             int temp = arr[i];             arr[i] = arr[j];             arr[j] = temp;         }     }     // Swap arr[i + 1] and arr[high] (or pivot)     int temp = arr[i + 1];     arr[i + 1] = arr[...

merge sort P2

#include <stdio.h> #include <stdlib.h> #include <time.h> // Merge two subarrays of arr[]. // First subarray is arr[l..m] // Second subarray is arr[m+1..r] void merge(int arr[], int l, int m, int r) {     int i, j, k;     int n1 = m - l + 1;     int n2 = r - m;     // Create temporary arrays     int* L = (int*)malloc(n1 * sizeof(int));     int* R = (int*)malloc(n2 * sizeof(int));     // Copy data to temporary arrays L[] and R[]     for (i = 0; i < n1; i++)         L[i] = arr[l + i];     for (j = 0; j < n2; j++)         R[j] = arr[m + 1 + j];     // Merge the temporary arrays back into arr[l..r]     i = 0;     j = 0;     k = l;     while (i < n1 && j < n2) {         if (L[i] <= R[j]) {             arr[k] = L[i];   ...

selection sort P1

 #include <stdio.h> #include <stdlib.h> #include <time.h> // Function to perform Selection Sort 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;             }         }         // Swap the found minimum element with the first element         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...