0% found this document useful (0 votes)
21 views28 pages

Jarvis CY Merged

The document contains information about a student named Avishek Chowdhury from VIT Vellore, including personal details such as email, roll number, and phone number. It also describes two coding problems related to forming convex hulls from a set of 2D points, providing example inputs, outputs, and solutions using C programming. The student achieved full marks on both problems.
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)
21 views28 pages

Jarvis CY Merged

The document contains information about a student named Avishek Chowdhury from VIT Vellore, including personal details such as email, roll number, and phone number. It also describes two coding problems related to forming convex hulls from a set of 2D points, providing example inputs, outputs, and solutions using C programming. The student achieved full marks on both problems.
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

VIT - Vellore

44

4
34

34

34
23
E2

E2

E2
E
BC

BC

BC

BC
Name: AVISHEK CHOWDHURY . Scan to verify results
23

23

23

23
Email: avishek.chowdhury2023@[Link]
Roll no: 23BCE2344
Phone: 9898989898
Branch: JENICKA S_DSA
Department: admin
Batch: VL2024250503251
Degree: admin
4

4
34

34

34

34
BCSE204P_Design and Analysis of Algorithms
E2

E2

E2

E2
BC

BC

BC

BC
Lab_VL2024250503251
23

23

23

23
VIT V_DAA_Week 9_Jarvis March_CY

Attempt : 1
Total Mark : 20
Marks Obtained : 20

Section 1 : Coding
4

44

4
1. Problem Statement
4

34

34
23

23

E2

E2
E

E
BC

BC

BC

Tom is mapping a geographical boundary for a project, and he needs to BC


23

23

23

23
wrap survey points to form the convex hull in a clockwise order.

Help Tom to identify the leftmost point and wrap the given set of 2D points
in a clockwise manner to generate the convex hull.

Example
Input:
5
4

4
34

34

34

34

00
E2

E2

E2

E2
BC

BC

BC

BC
23

23

23

23
44
4

44

4
34

34

34
23
E2

E2

E2
40

E
BC

BC

BC

BC
23

23

23

23
22
13
Explanation
The given points are (0,0), (4,4), (4,0), (2,2), and (1,3).Start at the leftmost
point, (0,0), and move clockwise to the next boundary [Link] (4,0)
and (4,4) are added as they maintain clockwise [Link], point
(1,3) is added before returning to (0,0).The convex hull includes (0,0), (4,0),
(4,4), and (1,3) in clockwise [Link]:
4

4
34

34

34

34
00
E2

E2

E2

E2
BC

BC

BC

BC
40
23

23

23

23
44
13

Answer
// You are using GCC
#include <stdio.h>
struct Point {
int x, y;
4

44

4
};
4

34

34
23

23

E2

E2
int orientation(struct Point p, struct Point q, struct Point r) {
E

E
BC

BC

BC

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

23

23

23
if (val == 0) return 0;
return (val > 0) ? 1 : 2;
}
void convexHull(struct Point points[], int n) {
if (n < 3) {
printf("Convex hull not possible\n");
return;
}
int hull[n];
int count = 0;
4

int l = 0;
34

34

34

34
E2

E2

E2

E2

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


BC

BC

BC

BC

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


23

23

23

23
points[l].y)) {
4

44

4
34

34

34
l = i;

23
E2

E2

E2
E
}
BC

BC

BC

BC
}
23

23

23

23
int p = l, q;
do {
hull[count++] = p;
q = (p + 1) % n;
for (int i = 0; i < n; i++) {
if (orientation(points[p], points[i], points[q]) == 1) {
q = i;
}
}
p = q;
4

4
34

34

34

34
} while (p != l);
E2

E2

E2

E2
for (int i = 0; i < count; i++) {
BC

BC

BC

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

23

23

23
}
}
int main() {
int n;
scanf("%d", &n);
struct Point points[n];
for (int i = 0; i < n; i++) {
scanf("%d %d", &points[i].x, &points[i].y);
}
convexHull(points, n);
4

44

4
4

34

34
return 0;
23

23

E2

E2
E

}
BC

BC

BC

BC
23

23

23

23
Status : Correct Marks : 10/10

2. Problem Statement

Ajay is mapping a geographical boundary for a project, and he needs to


wrap survey points to form the convex hull in a clockwise order.

Help Ajay to identify the leftmost point and wrap the given set of 2D points
4

in a clockwise manner to generate the convex hull.


34

34

34

34
E2

E2

E2

E2
BC

BC

BC

BC
23

23

23

23
Example
4

44

4
34

34

34
23
E2

E2

E2
Input:

E
BC

BC

BC

BC
23

23

23

23
6
00
22
20
13
31
04
4

4
34

34

34

34
E2

E2

E2

E2
Explanation
BC

BC

BC

BC
23

23

23

23
The given points are (0,0), (2,2), (2,0), (1,3), (3,1), and (0,4).The convex hull
boundary points (in counter-clockwise order) are (0,0), (2,0), (3,1), (1,3), and
(0,4).Using the Shoelace formula, calculate the polygon area:Area=1/2 |
0+2+9+4+0−(0+0+1+0+0)|=7.00The intermediate calculations confirm that
the area sum is 7.00 after applying the absolute value, rounded to two
decimal [Link]:
7.00

Answer
4

44

4
4

34

34
// You are using GCC
23

23

E2

E2
#include <stdio.h>
E

E
BC

BC

BC

#include <stdlib.h> BC
23

23

23

23

#define MAX_POINTS 20

typedef struct {
int x, y;
} Point;

// Function to determine orientation


// 0 -> collinear, 1 -> clockwise, 2 -> counterclockwise
int orientation(Point p, Point q, Point r) {
4

4
34

34

34

34

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

E2

E2

E2

if (val == 0) return 0;
BC

BC

BC

BC
23

23

23

23
return (val > 0) ? 1 : 2;
4

44

4
34

34

34
}

23
E2

E2

E2
E
BC

BC

BC

BC
// Function to find the convex hull using Gift Wrapping Algorithm (Jarvis March)
23

23

23

23
int convexHull(Point points[], int n, Point hull[]) {
if (n < 3) return 0;

int hullCount = 0;

// Step 1: Find the leftmost point


int leftmost = 0;
for (int i = 1; i < n; i++) {
if (points[i].x < points[leftmost].x) {
leftmost = i;
4

4
34

34

34

34
}
E2

E2

E2

E2
}
BC

BC

BC

BC
23

23

23

23
int p = leftmost, q;
do {
hull[hullCount++] = points[p];
q = (p + 1) % n;

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


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

44

4
4

34

34
p = q;
23

23

E2

E2
E

} while (p != leftmost);
BC

BC

BC

BC
23

23

23

23
return hullCount;
}

// Function to calculate the area using the Shoelace formula


double calculateArea(Point hull[], int hullCount) {
double area = 0.0;
for (int i = 0; i < hullCount; i++) {
int j = (i + 1) % hullCount;
area += (hull[i].x * hull[j].y) - (hull[i].y * hull[j].x);
}
4

4
34

34

34

34

return 0.5 * abs(area);


E2

E2

E2

E2

}
BC

BC

BC

BC
23

23

23

23
4

44

4
34

34

34
int main() {

23
E2

E2

E2
E
int n;
BC

BC

BC

BC
scanf("%d", &n);
23

23

23

23
Point points[MAX_POINTS], hull[MAX_POINTS];
for (int i = 0; i < n; i++) {
scanf("%d %d", &points[i].x, &points[i].y);
}

int hullCount = convexHull(points, n, hull);

double area = calculateArea(hull, hullCount);


printf("%.2f\n", area);
4

4
34

34

34

34
E2

E2

E2

E2
return 0;
BC

BC

BC

BC
}
23

23

23

23
Status : Correct Marks : 10/10
4

44

4
4

34

34
23

23

E2

E2
E

E
BC

BC

BC

BC
23

23

23

23
4

4
34

34

34

34
E2

E2

E2

E2
BC

BC

BC

BC
23

23

23

23
VIT - Vellore
4

44

4
34

34

34
23
E2

E2

E2
E
BC

BC

BC

BC
Name: AVISHEK CHOWDHURY . Scan to verify results
23

23

23

23
Email: avishek.chowdhury2023@[Link]
Roll no: 23BCE2344
Phone: 9898989898
Branch: JENICKA S_DSA
Department: admin
Batch: VL2024250503251
Degree: admin
4

4
34

34

34

34
BCSE204P_Design and Analysis of Algorithms
E2

E2

E2

E2
BC

BC

BC

BC
Lab_VL2024250503251
23

23

23

23
VIT V_DAA_Week 9_Jarvis March_COD_Medium

Attempt : 1
Total Mark : 10
Marks Obtained : 10

Section 1 : Coding
4

44

4
1. Problem Statement
4

34

34
23

23

E2

E2
E

E
BC

BC

BC

Liam is tasked with analyzing a set of 2D points to determine how many of BC


23

23

23

23
them lie on the boundary of the convex hull that encloses all the points.

Using the Gift Wrapping Algorithm, help Liam calculate the total number of
points forming the convex hull boundary.

Example
Input:
5
4

4
34

34

34

34

00
E2

E2

E2

E2
BC

BC

BC

BC
23

23

23

23
44
4

44

4
34

34

34
23
E2

E2

E2
40

E
BC

BC

BC

BC
23

23

23

23
22
13
Explanation
The given points are (1,2), (2,4), (3,1), (0,0), (4,4), (2,2), and (0,4).Start at the
leftmost point, (0,0), and traverse counter-clockwise to find boundary
[Link] points on the convex hull are (0,0), (0,4), (4,4), (3,1), and
(1,2).Points like (2,2) lie inside the boundary and are not included in the
convex [Link] total number of points on the convex hull boundary is
4

4
[Link]:
34

34

34

34
E2

E2

E2

E2
5
BC

BC

BC

BC
23

23

23

23
Answer
// You are using GCC
#include <stdio.h>

#define MAX_POINTS 20

typedef struct {
int x, y;
} Point;
4

44

4
4

34

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

23

E2

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

E
BC

BC

BC

if (val == 0) return 0; BC
23

23

23

23
return (val > 0) ? 1 : 2;
}

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


if (n < 3) return n;

int hullIndices[MAX_POINTS];
int hullCount = 0;

int leftmost = 0;
4

4
34

34

34

34

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


E2

E2

E2

E2

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


BC

BC

BC

BC
23

23

23

23
leftmost = i;
4

44

4
34

34

34
}

23
E2

E2

E2
E
}
BC

BC

BC

BC
23

23

23

23
int p = leftmost, q;
do {
hullIndices[hullCount++] = p;
q = (p + 1) % n;

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


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

4
34

34

34

34
E2

E2

E2

E2
p = q;
BC

BC

BC

BC
} while (p != leftmost);
23

23

23

23
return hullCount;
}

int main() {
int n;
scanf("%d", &n);

Point points[MAX_POINTS];
for (int i = 0; i < n; i++) {
4

44

4
4

34

34
scanf("%d %d", &points[i].x, &points[i].y);
23

23

E2

E2
E

}
BC

BC

BC

BC
23

23

23

23
int result = convexHull(points, n);
printf("%d\n", result);

return 0;
}

Status : Correct Marks : 10/10


4

4
34

34

34

34
E2

E2

E2

E2
BC

BC

BC

BC
23

23

23

23
VIT - Vellore
4

44

4
34

34

34
23
E2

E2

E2
E
BC

BC

BC

BC
Name: AVISHEK CHOWDHURY . Scan to verify results
23

23

23

23
Email: avishek.chowdhury2023@[Link]
Roll no: 23BCE2344
Phone: 9898989898
Branch: JENICKA S_DSA
Department: admin
Batch: VL2024250503251
Degree: admin
4

4
34

34

34

34
BCSE204P_Design and Analysis of Algorithms
E2

E2

E2

E2
BC

BC

BC

BC
Lab_VL2024250503251
23

23

23

23
VIT V_DAA_Week 9_Jarvis March_COD_Easy

Attempt : 1
Total Mark : 10
Marks Obtained : 10

Section 1 : Coding
4

44

4
1. Problem Statement
4

34

34
23

23

E2

E2
E

E
BC

BC

BC

Lisa is planning a land survey in a vast open field, and she needs to BC
23

23

23

23
determine the leftmost boundary point to outline the field using the gift-
wrapping algorithm. Give a set of 2D coordinates representing surveyed
points.

Help Emma identify the leftmost point, the starting point for constructing
the convex hull.

Example
Input:
4

4
34

34

34

34

5
E2

E2

E2

E2
BC

BC

BC

BC
23

23

23

23
44
4

44

4
34

34

34
23
E2

E2

E2
11

E
BC

BC

BC

BC
23

23

23

23
33
00
22
Explanation
The given points are (4, 4), (1, 1), (3, 3), (0, 0), and (2, 2).Start by assuming
(4, 4) as the leftmost point and compare each point’s [Link]
the leftmost point to (1, 1), then to (0, 0), as their x-coordinates are
[Link] iterating through all points, (0, 0) has the smallest x-
4

4
34

34

34

34
[Link], the output is. 0 0Output:
E2

E2

E2

E2
BC

BC

BC

BC
00
23

23

23

23
Answer
#include <stdio.h>

typedef struct {
int x, y;
} Point;
4

44

4
4

34

34
23

23

Point findLeftmostPoint(Point points[], int n) {


E2

E2
E

E
BC

BC

BC

BC
Point leftmost = points[0];
23

23

23

23
for (int i = 1; i < n; i++) {
if (points[i].x < leftmost.x || (points[i].x == leftmost.x && points[i].y <
leftmost.y)) {
leftmost = points[i];
}
}
return leftmost;
}

int main() {
int n;
4

4
34

34

34

34

scanf("%d", &n);
E2

E2

E2

E2
BC

BC

BC

BC
23

23

23

23
4

44

4
34

34

34
if (n < 3) {

23
E2

E2

E2
E
printf("Convex hull not possible with less than 3 points\n");
BC

BC

BC

BC
return 0;
23

23

23

23
}

Point points[n];
for (int i = 0; i < n; i++) {
scanf("%d %d", &points[i].x, &points[i].y);
}

Point leftmost = findLeftmostPoint(points, n);


printf("%d %d\n", leftmost.x, leftmost.y);
4

4
34

34

34

34
return 0;
E2

E2

E2

E2
}
BC

BC

BC

BC
23

23

23

23
Status : Correct Marks : 10/10
4

44

4
4

34

34
23

23

E2

E2
E

E
BC

BC

BC

BC
23

23

23

23
4

4
34

34

34

34
E2

E2

E2

E2
BC

BC

BC

BC
23

23

23

23
VIT - Vellore
4

44

4
34

34

34
23
E2

E2

E2
E
BC

BC

BC

BC
Name: AVISHEK CHOWDHURY . Scan to verify results
23

23

23

23
Email: avishek.chowdhury2023@[Link]
Roll no: 23BCE2344
Phone: 9898989898
Branch: JENICKA S_DSA
Department: admin
Batch: VL2024250503251
Degree: admin
4

4
34

34

34

34
BCSE204P_Design and Analysis of Algorithms
E2

E2

E2

E2
BC

BC

BC

BC
Lab_VL2024250503251
23

23

23

23
VIT V_DAA_Week 8_CY_Ford Fulkerson

Attempt : 1
Total Mark : 30
Marks Obtained : 30

Section 1 : Coding
4

44

4
1. Problem Statement
4

34

34
23

23

E2

E2
E

E
BC

BC

BC

Design a system for managing water flow in a network of pipes. BC


23

23

23

23

Each pipe has a maximum flow capacity, and the goal is to determine the
maximum amount of water that can be sent from a source pipe to a
destination pipe without exceeding the capacity of any pipe.

Answer
// You are using GCC
// You are using GCC
#include <stdio.h>
4

#include <limits.h>
34

34

34

34
E2

E2

E2

E2

#include <string.h>
BC

BC

BC

BC

#include <stdbool.h>
23

23

23

23
4

44

4
34

34

34
#define MAX_NODES 10

23
E2

E2

E2
E
BC

BC

BC

BC
// Function to perform BFS and find an augmenting path
23

23

23

23
bool bfs(int residualGraph[MAX_NODES][MAX_NODES], int N, int source, int sink,
int parent[]) {
bool visited[MAX_NODES];
memset(visited, 0, sizeof(visited));

int queue[MAX_NODES], front = 0, rear = 0;


queue[rear++] = source;
visited[source] = true;
parent[source] = -1;
4

4
34

34

34

34
while (front < rear) {
E2

E2

E2

E2
int u = queue[front++];
BC

BC

BC

BC
23

23

23

23
for (int v = 0; v < N; v++) {
if (!visited[v] && residualGraph[u][v] > 0) {
queue[rear++] = v;
parent[v] = u;
visited[v] = true;
if (v == sink) return true;
}
}
}
return false;
4

44

4
4

34

34
}
23

23

E2

E2
E

E
BC

BC

BC

// Ford-Fulkerson algorithm using BFS (Edmonds-Karp method) BC


23

23

23

23
int fordFulkerson(int graph[MAX_NODES][MAX_NODES], int N, int source, int
sink) {
int u, v;
int residualGraph[MAX_NODES][MAX_NODES];
for (u = 0; u < N; u++)
for (v = 0; v < N; v++)
residualGraph[u][v] = graph[u][v];

int parent[MAX_NODES];
int maxFlow = 0;
4

4
34

34

34

34
E2

E2

E2

E2

while (bfs(residualGraph, N, source, sink, parent)) {


BC

BC

BC

BC
23

23

23

23
int pathFlow = INT_MAX;
4

44

4
34

34

34
for (v = sink; v != source; v = parent[v]) {

23
E2

E2

E2
E
u = parent[v];
BC

BC

BC

BC
pathFlow = (pathFlow < residualGraph[u][v]) ? pathFlow : residualGraph[u]
23

23

23

23
[v];
}
for (v = sink; v != source; v = parent[v]) {
u = parent[v];
residualGraph[u][v] -= pathFlow;
residualGraph[v][u] += pathFlow;
}
maxFlow += pathFlow;
}
return maxFlow;
4

4
34

34

34

34
}
E2

E2

E2

E2
BC

BC

BC

BC
int main() {
23

23

23

23
int N, M;
scanf("%d %d", &N,&M);
//scanf("%d", &M);

int graph[MAX_NODES][MAX_NODES] = {0};

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


int u, v, capacity;
scanf("%d %d %d", &u, &v, &capacity);
graph[u][v] = capacity;
4

44

4
4

34

34
}
23

23

E2

E2
E

E
BC

BC

BC

int source = 0, sink = N - 1; BC


23

23

23

23
int maxFlow = fordFulkerson(graph, N, source, sink);
printf("Maximum flow from source pipe to destination pipe: %d units\n",
maxFlow);
return 0;
}

Status : Correct Marks : 10/10

2. Problem Statement
4

4
34

34

34

34
E2

E2

E2

E2

You are a transportation engineer tasked with optimizing traffic flow


BC

BC

BC

BC
23

23

23

23
through a road network to minimize congestion.
4

44

4
34

34

34
23
E2

E2

E2
E
Design a program that prompts users to input information about the road
BC

BC

BC

BC
23

23

23

23
network, including the number of nodes, edges, capacities of roads

Then, implement the maximum flow algorithm to calculate the maximum


number of vehicles that can pass through the network.

Answer
// You are using GCC
#include <stdio.h>
#include <limits.h>
#include <string.h>
4

4
34

34

34

34
#include <stdbool.h>
E2

E2

E2

E2
BC

BC

BC

BC
23

23

23

23
#define MAX_NODES 10

// Function to perform BFS and find an augmenting path


bool bfs(int residualGraph[MAX_NODES][MAX_NODES], int N, int source, int sink,
int parent[]) {
bool visited[MAX_NODES];
memset(visited, 0, sizeof(visited));

int queue[MAX_NODES], front = 0, rear = 0;


queue[rear++] = source;
visited[source] = true;
4

44

4
4

34

34
23

23

parent[source] = -1;
E2

E2
E

E
BC

BC

BC

BC
23

23

23

23
while (front < rear) {
int u = queue[front++];

for (int v = 0; v < N; v++) {


if (!visited[v] && residualGraph[u][v] > 0) {
queue[rear++] = v;
parent[v] = u;
visited[v] = true;
if (v == sink) return true;
}
}
4

4
34

34

34

34

}
E2

E2

E2

E2

return false;
BC

BC

BC

BC
23

23

23

23
}
4

44

4
34

34

34
23
E2

E2

E2
E
BC

BC

BC

BC
int fordFulkerson(int graph[MAX_NODES][MAX_NODES], int N, int source, int
23

23

23

23
sink) {
int u, v;
int residualGraph[MAX_NODES][MAX_NODES];
for (u = 0; u < N; u++)
for (v = 0; v < N; v++)
residualGraph[u][v] = graph[u][v];

int parent[MAX_NODES];
int maxFlow = 0;
4

4
34

34

34

34
while (bfs(residualGraph, N, source, sink, parent)) {
E2

E2

E2

E2
int pathFlow = INT_MAX;
BC

BC

BC

BC
for (v = sink; v != source; v = parent[v]) {
23

23

23

23
u = parent[v];
pathFlow = (pathFlow < residualGraph[u][v]) ? pathFlow : residualGraph[u]
[v];
}
for (v = sink; v != source; v = parent[v]) {
u = parent[v];
residualGraph[u][v] -= pathFlow;
residualGraph[v][u] += pathFlow;
}
maxFlow += pathFlow;
4

44

4
4

34

34
}
23

23

E2

E2
E

return maxFlow;
BC

BC

BC

} BC
23

23

23

23

int main() {
int N, M;
scanf("%d", &N);
scanf("%d", &M);

int graph[MAX_NODES][MAX_NODES] = {0};

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


int u, v, capacity;
4

4
34

34

34

34

scanf("%d %d %d", &u, &v, &capacity);


E2

E2

E2

E2

graph[u][v] = capacity;
BC

BC

BC

BC
23

23

23

23
}
4

44

4
34

34

34
23
E2

E2

E2
E
int source = 0, sink = N - 1;
BC

BC

BC

BC
int maxFlow = fordFulkerson(graph, N, source, sink);
23

23

23

23
printf("Maximum Flow: %d\n", maxFlow);
return 0;
}

Status : Correct Marks : 10/10

3. Problem Statement

Raju is currently learning about the Ford-Fulkerson algorithm. He wants to


4

4
34

34

34

34
understand how it works and how to implement it to solve problems
E2

E2

E2

E2
BC

BC

BC

BC
related to flow networks. Raju is given a directed graph with 6 vertices and
23

23

23

23
E edges. Each edge has an associated capacity representing the maximum
flow that can pass through it.

His task is to implement the Ford-Fulkerson algorithm to find the flow for
each edge from the source to the sink. Help him to accomplish his task.

Answer
// You are using GCC
#include <stdio.h>
4

44

4
#include <limits.h>
4

34

34
23

23

#include <string.h>
E2

E2
E

E
BC

BC

BC

BC
#include <stdbool.h>
23

23

23

23

#define MAX_NODES 6

// Function to perform BFS and find an augmenting path


bool bfs(int residualGraph[MAX_NODES][MAX_NODES], int N, int source, int sink,
int parent[]) {
bool visited[MAX_NODES];
memset(visited, 0, sizeof(visited));

int queue[MAX_NODES], front = 0, rear = 0;


4

queue[rear++] = source;
34

34

34

34

visited[source] = true;
E2

E2

E2

E2
BC

BC

BC

BC

parent[source] = -1;
23

23

23

23
4

44

4
34

34

34
while (front < rear) {

23
E2

E2

E2
E
int u = queue[front++];
BC

BC

BC

BC
23

23

23

23
for (int v = 0; v < N; v++) {
if (!visited[v] && residualGraph[u][v] > 0) {
queue[rear++] = v;
parent[v] = u;
visited[v] = true;
if (v == sink) return true;
}
}
}
return false;
4

4
34

34

34

34
}
E2

E2

E2

E2
BC

BC

BC

BC
int fordFulkerson(int graph[MAX_NODES][MAX_NODES], int N, int source, int sink,
23

23

23

23
int flowGraph[MAX_NODES][MAX_NODES]) {
int u, v;
int residualGraph[MAX_NODES][MAX_NODES];
for (u = 0; u < N; u++)
for (v = 0; v < N; v++)
residualGraph[u][v] = graph[u][v];

int parent[MAX_NODES];
int maxFlow = 0;
4

44

4
4

34

34
while (bfs(residualGraph, N, source, sink, parent)) {
23

23

E2

E2
E

int pathFlow = INT_MAX;


BC

BC

BC

for (v = sink; v != source; v = parent[v]) { BC


23

23

23

23
u = parent[v];
pathFlow = (pathFlow < residualGraph[u][v]) ? pathFlow : residualGraph[u]
[v];
}
for (v = sink; v != source; v = parent[v]) {
u = parent[v];
residualGraph[u][v] -= pathFlow;
residualGraph[v][u] += pathFlow;
flowGraph[u][v] += pathFlow;
}
4

4
34

34

34

34

maxFlow += pathFlow;
E2

E2

E2

E2

}
BC

BC

BC

BC
23

23

23

23
return maxFlow;
4

44

4
34

34

34
}

23
E2

E2

E2
E
BC

BC

BC

BC
int main() {
23

23

23

23
int N = MAX_NODES;
int graph[MAX_NODES][MAX_NODES];
int flowGraph[MAX_NODES][MAX_NODES] = {0};

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


for (int j = 0; j < N; j++)
scanf("%d", &graph[i][j]);

int source, sink;


scanf("%d %d", &source, &sink);
4

4
34

34

34

34
E2

E2

E2

E2
fordFulkerson(graph, N, source, sink, flowGraph);
BC

BC

BC

BC
23

23

23

23
for (int u = 0; u < N; u++) {
for (int v = 0; v < N; v++) {
if (flowGraph[u][v] > 0) {
printf("Edge (%d -> %d): %d\n", u, v, flowGraph[u][v]);
}
}
}
return 0;
}
4

44

4
4

34

34
Status : Correct Marks : 10/10
23

23

E2

E2
E

E
BC

BC

BC

BC
23

23

23

23
4

4
34

34

34

34
E2

E2

E2

E2
BC

BC

BC

BC
23

23

23

23
VIT - Vellore
4

44

4
34

34

34
23
E2

E2

E2
E
BC

BC

BC

BC
Name: AVISHEK CHOWDHURY . Scan to verify results
23

23

23

23
Email: avishek.chowdhury2023@[Link]
Roll no: 23BCE2344
Phone: 9898989898
Branch: JENICKA S_DSA
Department: admin
Batch: VL2024250503251
Degree: admin
4

4
34

34

34

34
BCSE204P_Design and Analysis of Algorithms
E2

E2

E2

E2
BC

BC

BC

BC
Lab_VL2024250503251
23

23

23

23
VIT V_DAA_Week 8_COD_Ford Fulkerson_Medium

Attempt : 1
Total Mark : 20
Marks Obtained : 20

Section 1 : Coding
4

44

4
1. Problem Statement
4

34

34
23

23

E2

E2
E

E
BC

BC

BC

You are presented with a directed graph that consists of 6 vertices and 6 BC
23

23

23

23
edges. Each edge in the graph carries a distinct capacity value, which
denotes the maximum flow that the edge can accommodate.

Your primary objective is to apply the Ford-Fulkerson algorithm, a


fundamental method in network flow theory, to determine the highest
achievable flow from a designated source vertex to a target sink vertex
within the graph.

Answer
4

4
34

34

34

34

// You are using GCC


E2

E2

E2

E2

#include<stdio.h>
BC

BC

BC

BC
23

23

23

23
#include<stdlib.h>
4

44

4
34

34

34
#include<string.h>

23
E2

E2

E2
E
#include<stdbool.h>
BC

BC

BC

BC
#include<limits.h>
23

23

23

23
#define M 100
bool bfs(int residualGraph[M][M],int s,int t,int parent[M],int vertices){
int queue[vertices];
int front=0;
int rear=0;
bool visited[vertices];
for(int i=0;i<vertices;i++){
visited[i]=0;
}
queue[rear++]=s;
4

4
34

34

34

34
visited[s]=1;
E2

E2

E2

E2
parent[s]=-1;
BC

BC

BC

BC
while(front<rear){
23

23

23

23
int u=queue[front++];
for(int v=0;v<vertices;v++){
if(!visited[v] && residualGraph[u][v]>0){
visited[v]=1;
parent[v]=u;
queue[rear++]=v;
if(v==t){
return true;
}
}
4

44

4
4

34

34
}
23

23

E2

E2
E

}
BC

BC

BC

return false; BC
23

23

23

23
}
int fordFulkerson(int graph[M][M],int s,int t,int vertices){
int parent[M];
int residualGraph[M][M];
for(int u=0;u<vertices;u++){
for(int v=0;v<vertices;v++){
residualGraph[u][v]=graph[u][v];
}
}
int max_flow=0;
4

4
34

34

34

34

while(bfs(residualGraph,s,t,parent,vertices)){
E2

E2

E2

E2

int path_flow=INT_MAX;
BC

BC

BC

BC
23

23

23

23
for(int v=t;v!=s;v=parent[v]){
4

44

4
34

34

34
int u=parent[v];

23
E2

E2

E2
E
path_flow=(path_flow<residualGraph[u][v])?path_flow:residualGraph[u][v];
BC

BC

BC

BC
}
23

23

23

23
for(int v=t;v!=s;v=parent[v]){
int u=parent[v];
residualGraph[u][v]-=path_flow;
residualGraph[v][u]+=path_flow;
}
max_flow+=path_flow;
}
return max_flow;
}
int main(){
4

4
34

34

34

34
int graph[M][M],s,t,u,v,vertices=6;
E2

E2

E2

E2
//scanf("%d",&vertices);
BC

BC

BC

BC
23

23

23

23
for(u=0;u<vertices;u++){
for(v=0;v<vertices;v++){
scanf("%d",&graph[u][v]);
}
}
scanf("%d",&s);
scanf("%d",&t);
printf("The maximum possible flow is %d",fordFulkerson(graph,s,t,vertices));
return 0;
}
4

44

4
4

34

34
23

23

E2

E2
E

Status : Correct Marks : 10/10


BC

BC

BC

BC
23

23

23

23

2. Problem Statement

Develop a system to manage the flow of electricity in a power grid. Each


transmission line has a maximum capacity, and the goal is to determine
the maximum amount of electricity that can be transmitted from power
plants to consumers without overloading any transmission line.

Create a program to achieve this task.


4

4
34

34

34

34
E2

E2

E2

E2

Answer
BC

BC

BC

BC
23

23

23

23
// You are using GCC
4

44

4
34

34

34
#include<stdio.h>

23
E2

E2

E2
E
#include<stdlib.h>
BC

BC

BC

BC
#include<string.h>
23

23

23

23
#include<stdbool.h>
#include<limits.h>
#define M 10
bool bfs(int residualGraph[M][M],int s,int t,int parent[M],int n){
bool visited[M]={0};
int queue[M];
int front=0;
int rear=0;
visited[s]=1;
parent[s]=-1;
4

4
34

34

34

34
queue[rear++]=s;
E2

E2

E2

E2
while(front<rear){
BC

BC

BC

BC
int u=queue[front++];
23

23

23

23
for(int v=0;v<n;v++){
if(!visited[v] && residualGraph[u][v]>0){
queue[rear++]=v;
visited[v]=1;
parent[v]=u;
if(v==t){
return true;
}
}
}
4

44

4
4

34

34
}
23

23

E2

E2
E

return false;
BC

BC

BC

} BC
23

23

23

23
int fordFulkerson(int residualGraph[M][M],int s, int t,int n){
int parent[M];
int max_flow=0;
while(bfs(residualGraph,s,t,parent,n)){
int path_flow=INT_MAX;
for(int v=t;v!=s;v=parent[v]){
int u=parent[v];
path_flow=(path_flow<residualGraph[u][v])?path_flow:residualGraph[u][v];
}
for(int v=t;v!=s;v=parent[v]){
4

4
34

34

34

34

int u=parent[v];
E2

E2

E2

E2

residualGraph[u][v]-=path_flow;
BC

BC

BC

BC
23

23

23

23
residualGraph[v][u]+=path_flow;
4

44

4
34

34

34
}

23
E2

E2

E2
E
max_flow+=path_flow;
BC

BC

BC

BC
}
23

23

23

23
return max_flow;

}
int main(){
int n,m,u,v,graph[M][M];
scanf("%d",&n);
scanf("%d",&m);
for(u=0;u<n;u++){
for(v=0;v<n;v++){
graph[u][v]=0;
4

4
34

34

34

34
}
E2

E2

E2

E2
}
BC

BC

BC

BC
for(int i=0;i<m;i++){
23

23

23

23
scanf("%d %d",&u,&v);
scanf("%d",&graph[u][v]);
}
printf("Maximum amount for electricity: %d",fordFulkerson(graph,0,n-1,n));
return 0;

Status : Correct Marks : 10/10


4

44

4
4

34

34
23

23

E2

E2
E

E
BC

BC

BC

BC
23

23

23

23
4

4
34

34

34

34
E2

E2

E2

E2
BC

BC

BC

BC
23

23

23

23
VIT - Vellore
4

44

4
34

34

34
23
E2

E2

E2
E
BC

BC

BC

BC
Name: AVISHEK CHOWDHURY . Scan to verify results
23

23

23

23
Email: avishek.chowdhury2023@[Link]
Roll no: 23BCE2344
Phone: 9898989898
Branch: JENICKA S_DSA
Department: admin
Batch: VL2024250503251
Degree: admin
4

4
34

34

34

34
BCSE204P_Design and Analysis of Algorithms
E2

E2

E2

E2
BC

BC

BC

BC
Lab_VL2024250503251
23

23

23

23
VIT V_DAA_Week 8_COD_Ford Fulkerson_Easy

Attempt : 1
Total Mark : 10
Marks Obtained : 10

Section 1 : Coding
4

44

4
1. Problem Statement
4

34

34
23

23

E2

E2
E

E
BC

BC

BC

In a network of cities connected by various roads, each road has a defined BC


23

23

23

23
capacity to accommodate the flow of data packets between the cities.

The objective is to compute the maximum flow of data packets that can
traverse from a specified source city to a destination city (sink), optimizing
the flow through this network.

This problem involves analyzing the given network, represented as a 4x4


matrix, where each cell indicates the road capacity from one city to
another, to find the maximum flow of data packets possible from the
4

source to the sink city.


34

34

34

34
E2

E2

E2

E2
BC

BC

BC

BC

Answer
23

23

23

23
// You are using GCC
4

44

4
34

34

34
#include <stdio.h>

23
E2

E2

E2
E
#include <limits.h>
BC

BC

BC

BC
#include <string.h>
23

23

23

23
#include <stdbool.h>

#define MAX 4

bool dfs(int u,int n, int residualGraph[MAX][MAX],int visited[MAX],int


parent[MAX],int sink){
visited[u]=1;

if (u==sink) return true;


4

4
34

34

34

34
for(int v=0;v<n;v++){
E2

E2

E2

E2
if(!visited[v] && residualGraph[u][v]>0){
BC

BC

BC

BC
parent[v]=u;
23

23

23

23
if(dfs(v,n,residualGraph,visited,parent,sink)) return true;
}
}
return false;
}

int fordFulkerson(int n, int graph[MAX][MAX], int source, int sink) {


int residualGraph[MAX][MAX];
for (int u = 0; u < n; u++)
for (int v = 0; v < n; v++)
4

44

4
4

34

34
residualGraph[u][v] = graph[u][v];
23

23

E2

E2
E

E
BC

BC

BC

int parent[MAX]; BC
23

23

23

23
int maxFlow = 0;

while (1) {
int visited[MAX] = {0};

if (!dfs(source, n, residualGraph, visited, parent, sink)) break;

int pathFlow = INT_MAX;


for (int v = sink; v != source; v = parent[v]) {
4

4
34

34

34

34

int u = parent[v];
E2

E2

E2

E2

pathFlow = (pathFlow < residualGraph[u][v]) ? pathFlow : residualGraph[u]


BC

BC

BC

BC

[v];
23

23

23

23
}
4

44

4
34

34

34
23
E2

E2

E2
E
BC

BC

BC

BC
for (int v = sink; v != source; v = parent[v]) {
23

23

23

23
int u = parent[v];
residualGraph[u][v] -= pathFlow;
residualGraph[v][u] += pathFlow;
}

maxFlow += pathFlow;
}

return maxFlow;
}
4

4
34

34

34

34
E2

E2

E2

E2
int main() {
BC

BC

BC

BC
int n=4, graph[MAX][MAX];
23

23

23

23
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
scanf("%d", &graph[i][j]);

int maxFlow = fordFulkerson(n, graph, 0, 3);


printf("The maximum possible flow is %d\n", maxFlow);
4

44

4
4

34

34
23

23

E2

E2
E

return 0;
BC

BC

BC

} BC
23

23

23

23

Status : Correct Marks : 10/10


4

4
34

34

34

34
E2

E2

E2

E2
BC

BC

BC

BC
23

23

23

23

You might also like