0% found this document useful (0 votes)
25 views20 pages

Sandhi Daa Lab Da-5

Uploaded by

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

Sandhi Daa Lab Da-5

Uploaded by

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

Name: SANDHI JAIN

Registration Number:
22BCE2980

Course Title: Design and


Analysis of Algorithms Lab

Course Code: BCSE204P

Date: 14/04/2024
Exercises to do:
1. Write a C-program/ C++ program to find the
convex hull using Graham’s Scan algorithm with at
least 6 nodes with one or more collinear and right
turn points.

AIM: To find the convex hull using Graham’s Scan


algorithm with at least 6 nodes with one or more
collinear and right turn points.

ALGORITHM:
GRAHAM-SCAN(Q)
1. Let po be the point with the minimum y-
coordinate or the leftmost such point in case of a
tie
2. let (P1, P2, ..., Pn) be the remaining points in Q,
sorted by polar angle in counter clockwise order
around po (If more than one point has the same
angle, remove all but one that is farthest from po)
3. Let S be an empty stack
4. PUSH(po, S)
5. PUSH(P1, S)
6. PUSH(P2, S)
7. for i3 to n
8. while ccw(next-top(S), p₁, top (S)) ≤ 0 Pi
9. POP(S)
10. PUSH(S)
11. return S
The clockwise and counter clockwise algorithm
ccw(P1, P2, P3) CCW
return (p3-P1 P2-P1)

C/C++ Code:
#include <iostream>
#include <stack>
#include <stdlib.h>

using namespace std;

struct Point
{

int x, y;
};

Point p0;

Point nextToTop(stack<Point> &S)


{
Point p = S.top();

S.pop();

Point res = S.top();

S.push(p);

return res;
}

void swap(Point &p1, Point &p2)


{

Point temp = p1;

p1 = p2;

p2 = temp;
}

int distSq(Point p1, Point p2)


{

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

(p1.y - p2.y)*(p1.y - p2.y);


}

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;


}

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


{

Point *p1 = (Point *)vp1;


Point *p2 = (Point *)vp2;

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

if (o == 0)

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

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


}

void convexHull(Point points[], int n)


{
int ymin = points[0].y, min = 0;

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

int y = points[i].y;

if ((y < ymin) || (ymin == y &&

points[i].x < points[min].x))

ymin = points[i].y, min = i;

swap(points[0], points[min]);

p0 = points[0];

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

int m = 1;

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

while (i < n-1 && orientation(p0, points[i],

points[i+1]) == 0)

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

m++;

if (m < 3) return;

stack<Point> S;

S.push(points[0]);

S.push(points[1]);

S.push(points[2]);

for (int i = 3; i < m; i++)

while (S.size()>1 && orientation(nextToTop(S), S.top(), points[i]) != 2)

S.pop();

S.push(points[i]);

while (!S.empty())

Point p = S.top();

cout << "(" << p.x << ", " << p.y <<")" << endl;

S.pop();

}
}

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;
}

OUTPUT:

TIME COMPLEXITY ANALYSIS:


1. Let n be the number of input points
2. The first step (finding the bottom-most
point) takes O(n) time.
3. The second step (sorting points) takes
O(nLogn) time.
4. Third step takes O(n) time. In third step,
every element is pushed and popped at
most one time.
5. The sixth step to process points one by
one takes O(n) time.
6. Using Graham’s scan algorithm, we can
find Convex Hull in O(nLogn) time.

RESULT:
C++ program to find the convex hull using
Graham’s Scan algorithm implemented
successfully.
VERIFICATION STATUS:
2. Write a C-program/ C++ program to find the
convex hull using Jarvis March algorithm with at
least 6 nodes with one or more collinear.

AIM: To find the convex hull using Jarvis March


algorithm with at least 6 nodes with one or more
collinear.

ALGORITHM:
JARVISMARCH(X[1..n],Y[1..n]):
1. l←1
2. for i←2 to n
a. if X[i] < X[l]
i. l←i
3. p←l
4. repeat
a. q←p+1 ((Make sure p ≠
q))
b. for i←2 to n
i. if CCW(p, i, q)
1. q←i
ii. next[p] ←q; prev[q] ← p
iii. p ← q
5. until p = l

C/C++ Code:
#include <bits/stdc++.h>

using namespace std;

struct Point
{

int x, y;
};

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;

vector<Point> hull;

int l = 0;

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

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

l = i;

int p = l, q;
do

{
hull.push_back(points[p]);

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

for (int i = 0; i < hull.size(); i++)

cout << "(" << hull[i].x << ", "

<< hull[i].y << ")\n";


}

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;
}
OUTPUT:

TIME COMPLEXITY ANALYSIS:


1. The algorithm spends O(n) time on each
convex hull vertex.
2. If there are h convex hull vertices, the total
time complexity of the algorithm would be
O(nh).
3. Since h is the number of output of the
algorithm, this algorithm is also called output
sensitive algorithm since the complexity also
depends on the number of output.

RESULT:
C++ program to find the convex hull using Jarvis
March algorithm implemented successfully.
VERIFICATION STATUS:
3. Write a C-program/C++ program to perform the
intersection of line segments algorithm.
Case1: At least 2 lines with intersection.
Case2: At least 2 lines without intersection.
Case3: At least 2 lines with Collinearity.

AIM: To perform the intersection of line segments


algorithm:
Case1: At least 2 lines with intersection.
Case2: At least 2 lines without intersection.
Case3: At least 2 lines with Collinearity.

ALGORITHM:
SEGMENTS-INTERSECT (P1, P2, P3, P4)
1. d₁ =DIRECTION (P3, P4, P1)
2. d2 =DIRECTION (P3, P4, P2)
3. d3 =DIRECTION (P1, P2, P3)
4. d4 =DIRECTION (P1, P2, P4)
5. if ((d₁>0 and d₂<0) or (d₁<0 and d2>0)) and
((d3>0 and d₁<0) or (d3<0 and d₁ >0))
6. then return TRUE
7. elseif d₁ = 0 and ON-SEGMENT(P3, P4, P1)
8. then return TRUE
9. elseif d₂ = 0 and ON-SEGMENT (P3, P4, P2)
10. then return TRUE
11. elseif d3 = 0 and ON-SEGMENT (P1, P2, P3)
12. then return TRUE
13. elseif d4 = 0 and ON-SEGMENT (P1, P2, P4)
14. then return TRUE
15. else return FALSE
DIRECTION (Pi, Pj, Pk)
return (pk-Pi) x (Pj-Pi)
ON-SEGMENT (Pi, Pj, Pk)
if min(xi, xj) ≤ x ≤ max(xi, xj) and min(yi, yj) ≤ y ≤
max(yi, yj)
then return TRUE
else return FALSE
C/C++ Code:
#include <iostream>
#include <cmath>

using namespace std;

struct Point {
double x, y;
};

// Function to check if two line segments intersect


bool segmentsIntersect(double x1, double y1, double x2, double y2,
double x3, double y3, double x4, double y4,
Point& intersection) {
// Calculate slopes and y-intercepts of the two lines
double m1 = (y2 - y1) / (x2 - x1);
double b1 = y1 - m1 * x1;
double m2 = (y4 - y3) / (x4 - x3);
double b2 = y3 - m2 * x3;

// Check if the lines are parallel (or collinear)


if (fabs(m1 - m2) < 1e-9) {
// Collinear case
if (fabs(b1 - b2) < 1e-9) {
// Overlapping segments
double minX = max(min(x1, x2), min(x3, x4));
double maxX = min(max(x1, x2), max(x3, x4));
double minY = max(min(y1, y2), min(y3, y4));
double maxY = min(max(y1, y2), max(y3, y4));
intersection.x = minX;
intersection.y = minY;
return true;
} else {
// No intersection
return false;
}
}

// Calculate the intersection point


intersection.x = (b2 - b1) / (m1 - m2);
intersection.y = m1 * intersection.x + b1;

// Check if the intersection point lies within the line segments


if (min(x1, x2) <= intersection.x && intersection.x <= max(x1, x2) &&
min(y1, y2) <= intersection.y && intersection.y <= max(y1, y2) &&
min(x3, x4) <= intersection.x && intersection.x <= max(x3, x4) &&
min(y3, y4) <= intersection.y && intersection.y <= max(y3, y4)) {
return true;
} else {
return false;
}
}

int main() {
// Case 1: At least 2 lines with intersection
Point intersection;
if (segmentsIntersect(0, 0, 2, 2, 0, 2, 2, 0, intersection)) {
cout << "Intersection point: (" << intersection.x << ", " <<
intersection.y << ")" << endl;
} else {
cout << "No intersection" << endl;
}

// Case 2: At least 2 lines without intersection


if (segmentsIntersect(0, 0, 2, 2, 3, 3, 5, 5, intersection)) {
cout << "Intersection point: (" << intersection.x << ", " <<
intersection.y << ")" << endl;
} else {
cout << "No intersection" << endl;
}

// Case 3: At least 2 lines with collinearity


if (segmentsIntersect(0, 0, 2, 2, 1, 1, 3, 3, intersection)) {
cout << "Intersection point: (" << intersection.x << ", " <<
intersection.y << ")" << endl;
} else {
cout << "No intersection" << endl;
}

return 0 ;
}

OUTPUT:

TIME COMPLEXITY ANALYSIS:


1. The number of intersections of n line
segments ranges from zero, in case no
pair of lines intersect, to O(n²), in case
all pairs intersect.
2. The simple naive method checks for
intersections between all pairs of line
segments.
3. Because all pairs of line segments can
potentially intersect, the naive
algorithm is optimal for the worst-case
inputs with a time complexity of O(n²).

RESULT:
C++ program to perform the intersection of line
segments algorithm implemented successfully.
VERIFICATION STATUS:

You might also like