0% found this document useful (0 votes)
20 views

ASSESSM4DAA

Uploaded by

Anvita Manne
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

ASSESSM4DAA

Uploaded by

Anvita Manne
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

LAB ASSESSMENT-4

ANVITA MANNE

22BCE0272

Q1)

CODE:

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

// Structure to represent a 2D point

typedef struct {

int x, y;

} Point;

// Global variable to store the reference point for sorting

Point pivot;

// Utility function to swap two points

void swap(Point *p1, Point *p2) {

Point temp = *p1;


*p1 = *p2;

*p2 = temp;

// Utility function to calculate the square of the distance between two points

int distSq(Point p1, Point p2) {

return (p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y);

// Function to compute the orientation of ordered triplet (p, q, r)

// Returns:

// 0 -> p, q, r are collinear

// 1 -> Clockwise

// 2 -> Counterclockwise

int orientation(Point p, Point q, Point r) {

int val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);

if (val == 0) return 0; // Collinear

return (val > 0) ? 1 : 2; // Clockwise or Counterclockwise

// Function to compare points based on polar angle with respect to the pivot

int compare(const void *vp1, const void *vp2) {

Point *p1 = (Point *)vp1;

Point *p2 = (Point *)vp2;

int o = orientation(pivot, *p1, *p2);

if (o == 0)

return (distSq(pivot, *p2) >= distSq(pivot, *p1)) ? -1 : 1;

return (o == 2) ? -1 : 1;

// Function to perform Graham's scan algorithm to compute the convex hull


void convexHull(Point points[], int n) {

// Find the point with the lowest y-coordinate (and the leftmost in case of a tie) as the
pivot

int minIdx = 0;

for (int i = 1; i < n; i++) {

if (points[i].y < points[minIdx].y || (points[i].y == points[minIdx].y && points[i].x <


points[minIdx].x)) {

minIdx = i;

swap(&points[0], &points[minIdx]);

pivot = points[0];

// Sort the points based on polar angle with respect to the pivot point

qsort(&points[1], n - 1, sizeof(Point), compare);

// Build the convex hull using a stack

int m = 1; // Initialize the size of the convex hull

for (int i = 1; i < n; i++) {

while (i < n - 1 && orientation(pivot, points[i], points[i + 1]) == 0) {

i++; // Skip collinear points

points[m++] = points[i];

if (m < 3) return; // Convex hull is not possible with less than 3 points

// Use a stack to construct the convex hull in counterclockwise order

Point stack[m];

int top = 2; // Initial size of the stack

stack[0] = points[0];

stack[1] = points[1];

stack[2] = points[2];
for (int i = 3; i < m; i++) {

while (top > 0 && orientation(stack[top - 1], stack[top], points[i]) != 2) {

top--; // Pop from the stack if the current point makes a non-left turn

stack[++top] = points[i]; // Push the current point onto the stack

// Print the convex hull points in clockwise order

printf("The Boundary Coordinates are\n");

for (int i = top; i >= 0; i--) {

printf("%d %d\n", stack[i].x, stack[i].y);

// Driver program to test the convex hull function

int main() {

Point points[] = {{0, 3}, {1, 1}, {2, 2}, {4, 4}, {0, 0}, {1, 2}, {3, 1}, {3, 3}};

int n = sizeof(points) / sizeof(points[0]);

convexHull(points, n);

return 0;

}
Q2)

CODE:

#include <stdio.h>

typedef struct {

int x;

int y;

} Point;

int orientation(Point p, Point q, Point r) {

int val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);

if (val == 0) return 0;

return (val > 0) ? 1 : 2;

void convexHull(Point points[], int n) {

if (n < 3) return;

Point hull[n];
int idx = 0;

int l = 0;

for (int i = 1; i < n; i++) {

if (points[i].x < points[l].x) {

l = i;

int p = l;

do {

hull[idx++] = points[p];

int q = (p + 1) % n;

for (int i = 0; i < n; i++) {

if (orientation(points[p], points[i], points[q]) == 2) {

q = i;

p = q;

} while (p != l);

printf("The Boundary Coordinates are\n");

for (int i = 0; i < idx; i++) {

printf("%d %d\n", hull[i].x, hull[i].y);

int main() {

Point points[] = {{0, 3}, {2, 2}, {1, 1}, {2, 1}, {3, 0}, {0, 0}, {3, 3}};

int n = sizeof(points) / sizeof(points[0]);


convexHull(points, n);

return 0;

Q3)

CODE:

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#define MAX_SIZE 1000

// Function to swap two integers


void swap(int *a, int *b) {

int temp = *a;

*a = *b;

*b = temp;

// Partition function for Quick Sort

int partition(int arr[], int low, int high) {

// Randomly select a pivot index

int pivotIndex = low + rand() % (high - low + 1);

// Move the pivot to the end

swap(&arr[pivotIndex], &arr[high]);

// Set the pivot element

int pivot = arr[high];

// Partition the array around the pivot

int i = low - 1;

for (int j = low; j <= high - 1; j++) {

if (arr[j] <= pivot) {

i++;

swap(&arr[i], &arr[j]);

swap(&arr[i + 1], &arr[high]);

return i + 1;

// Quick Sort function with randomized pivot

void randomizedQuickSort(int arr[], int low, int high) {

if (low < high) {

// Partition the array


int pivotIndex = partition(arr, low, high);

// Recursively sort the left and right subarrays

randomizedQuickSort(arr, low, pivotIndex - 1);

randomizedQuickSort(arr, pivotIndex + 1, high);

// Main function

int main() {

int arr[MAX_SIZE];

int n;

// Seed the random number generator

srand(time(NULL));

// Prompt the user for the number of elements

scanf("%d", &n);

// Validate the input

if (n < 0 || n > MAX_SIZE) {

printf("Invalid number of elements.\n");

return 1;

// Read the elements from the user

for (int i = 0; i < n; i++) {

scanf("%d", &arr[i]);

// Sort the array using Randomized Quick Sort

randomizedQuickSort(arr, 0, n - 1);
// Output the sorted array

for (int i = 0; i < n; i++) {

printf("%d ", arr[i]);

printf("\n");

return 0;

Q4)

CODE:

#include <stdio.h>

#include <stdlib.h>
#include <time.h>

// Structure to represent an edge

struct Edge {

int src, dest;

};

// Structure to represent a graph

struct Graph {

int V, E;

struct Edge* edge;

};

// Create a graph with V vertices and E edges

struct Graph* createGraph(int V, int E) {

struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));

graph->V = V;

graph->E = E;

graph->edge = (struct Edge*)malloc(E * sizeof(struct Edge));

return graph;

// Find the subset of an element i

int find(int parent[], int i) {

if (parent[i] == -1)

return i;

return find(parent, parent[i]);

// Union of two sets of x and y

void Union(int parent[], int x, int y) {


int xset = find(parent, x);

int yset = find(parent, y);

parent[xset] = yset;

// Find the cut using randomized contraction algorithm

int findCut(struct Graph* graph) {

int V = graph->V;

int E = graph->E;

struct Edge* edge = graph->edge;

int* parent = (int*)malloc(V * sizeof(int));

// Initialize parent array

for (int i = 0; i < V; i++)

parent[i] = -1;

int vertices = V;

// Keep contracting until only 2 vertices are left

while (vertices > 2) {

// Choose a random edge

int randomEdge = rand() % E;

int subset1 = find(parent, edge[randomEdge].src);

int subset2 = find(parent, edge[randomEdge].dest);

if (subset1 != subset2) {

// Merge two subsets

Union(parent, subset1, subset2);

vertices--;

}
// Count the edges crossing the two final vertices

int cutEdges = 0;

for (int i = 0; i < E; i++) {

int subset1 = find(parent, edge[i].src);

int subset2 = find(parent, edge[i].dest);

if (subset1 != subset2)

cutEdges++;

free(parent);

return cutEdges;

// Print the contracting edge

void printContractEdge(struct Edge e) {

printf("Contracting edge %d-%d\n", e.src, e.dest);

// Example usage

int main() {

srand(time(NULL));

int V = 4, E = 5;

struct Graph* graph = createGraph(V, E);

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]);

int minCut = findCut(graph);

printf("Cut found by the randomized algorithm is %d\n", minCut);

free(graph->edge);

free(graph);

return 0;

You might also like