ASSESSM4DAA
ASSESSM4DAA
ANVITA MANNE
22BCE0272
Q1)
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct {
int x, y;
} Point;
Point pivot;
*p2 = temp;
// Utility function to calculate the square of the distance between two points
// Returns:
// 1 -> Clockwise
// 2 -> Counterclockwise
int val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
// Function to compare points based on polar angle with respect to the pivot
if (o == 0)
return (o == 2) ? -1 : 1;
// Find the point with the lowest y-coordinate (and the leftmost in case of a tie) as the
pivot
int minIdx = 0;
minIdx = i;
swap(&points[0], &points[minIdx]);
pivot = points[0];
// Sort the points based on polar angle with respect to the pivot point
points[m++] = points[i];
if (m < 3) return; // Convex hull is not possible with less than 3 points
Point stack[m];
stack[0] = points[0];
stack[1] = points[1];
stack[2] = points[2];
for (int i = 3; i < m; i++) {
top--; // Pop from the stack if the current point makes a non-left turn
int main() {
Point points[] = {{0, 3}, {1, 1}, {2, 2}, {4, 4}, {0, 0}, {1, 2}, {3, 1}, {3, 3}};
convexHull(points, n);
return 0;
}
Q2)
CODE:
#include <stdio.h>
typedef struct {
int x;
int y;
} Point;
int val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
if (val == 0) return 0;
if (n < 3) return;
Point hull[n];
int idx = 0;
int l = 0;
l = i;
int p = l;
do {
hull[idx++] = points[p];
int q = (p + 1) % n;
q = i;
p = q;
} while (p != l);
int main() {
Point points[] = {{0, 3}, {2, 2}, {1, 1}, {2, 1}, {3, 0}, {0, 0}, {3, 3}};
return 0;
Q3)
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
*a = *b;
*b = temp;
swap(&arr[pivotIndex], &arr[high]);
int i = low - 1;
i++;
swap(&arr[i], &arr[j]);
return i + 1;
// Main function
int main() {
int arr[MAX_SIZE];
int n;
srand(time(NULL));
scanf("%d", &n);
return 1;
scanf("%d", &arr[i]);
randomizedQuickSort(arr, 0, n - 1);
// Output the sorted array
printf("\n");
return 0;
Q4)
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct Edge {
};
struct Graph {
int V, E;
};
graph->V = V;
graph->E = E;
return graph;
if (parent[i] == -1)
return i;
parent[xset] = yset;
int V = graph->V;
int E = graph->E;
parent[i] = -1;
int vertices = V;
if (subset1 != subset2) {
vertices--;
}
// Count the edges crossing the two final vertices
int cutEdges = 0;
if (subset1 != subset2)
cutEdges++;
free(parent);
return cutEdges;
// Example usage
int main() {
srand(time(NULL));
int V = 4, E = 5;
graph->edge[0].src = 0;
graph->edge[0].dest = 1;
graph->edge[1].src = 1;
graph->edge[1].dest = 3;
graph->edge[2].src = 0;
graph->edge[2].dest = 2;
graph->edge[3].src = 0;
graph->edge[3].dest = 3;
graph->edge[4].src = 2;
graph->edge[4].dest = 3;
printContractEdge(graph->edge[0]);
printContractEdge(graph->edge[1]);
free(graph->edge);
free(graph);
return 0;