Atik Ada Assignment - Removed Att111
Atik Ada Assignment - Removed Att111
UNIVERSITY
Jnana Sangama, Belagavi – 590018
Assignment
ON
BCY401 Analysis & Design of Algorithm
Bachelor of Engineering
in
CSE (Cyber Security)
Submitted by:
Atik khan
1ST22CY0
Associate Professor
Department of CSE(CY)
CERTIFICATE
This is to certify that Mr. Atik khan bearing 1ST22CY009 has successfully
Marks Allotment:
FIGURES TABLE
Sl No Topic Page Number
1. Score Card 1
2.1 Mini Max Sum 3
2.2 Insertion Sort-Part 1 4
2.3 Insertion Sort-Part 2 5
2.4 Quicksort 1 Partition 6
2.5 Counting Sort 1 7
2.6 Counting Sort 2 8
2.7 Insertion Sort Advanced Analysis 10
2.8 Two Strings 11
2.9 String Construction 12
2.10 The Traveling Salesman 23
1. Score Card
1
long sum(int arr[], int n) {
long total = 0;
for(int i = 0; i < n; i++) {
total += arr[i];
}
return total;
int main() {
int arr[5];
long totalSum, minSum,
maxSum; for(int i = 0; i < 5; i++)
{
scanf("%d", &arr[i]);
#include <stdio.h>
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
void insertionSort1(int n, int arr[]) {
int value = arr[n - 1];
int i = n - 2;
while (i >= 0 && arr[i] > value) {
arr[i + 1] = arr[i];
printArray(arr, n);
i--;
}
arr[i + 1] = value;
printArray(arr, n);
}
int main() {
int n;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
3
insertionSort1(n, arr);
return 0;
}
Output
2.3Insertion Sort-Part 2
#include <stdio.h>
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
void insertionSort2(int n, int arr[]) {
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
printArray(arr, n);
}
}
int main() {
int n;
scanf("%d", &n);
int arr[n];
4
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
insertionSort2(n, arr);
return 0;
}
Output
#include <stdio.h>
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int partition(int arr[], int low, int high) {
int pivot = arr[low];
int left = low + 1;
int right = high;
while (left <= right) {
while (left <= right && arr[left] <= pivot) {
left++;
}
while (left <= right && arr[right] > pivot) {
right--;
}
if (left < right) {
int temp = arr[left];
5
arr[left] = arr[right];
arr[right] = temp;
}
}
int temp = arr[low];
arr[low] = arr[right];
arr[right] = temp;
return right;
}
void quickSortPartition(int arr[], int n) {
int partitionIndex = partition(arr, 0, n - 1);
printArray(arr, n);
}
int main() {
int n;
scanf("%d", &n)
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
quickSortPartition(arr, n);
return 0;
}
Output
#include <stdio.h>
void countingSort(int arr[], int n) {
int max = 100;
int count[max];
for (int i = 0; i < max; i++) {
count[i] = 0;
}
6
for (int i = 0; i < n; i++) {
count[arr[i]]++;
}
for (int i = 0; i < max; i++) {
printf("%d ", count[i]);
}
printf("\n");
}
int main() {
int n;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
countingSort(arr, n);
return 0;
}
Output
#include <stdio.h>
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
void countingSort(int arr[], int n) {
int max = 100;
int count[max];
7
for (int i = 0; i < max; i++) {
count[i] = 0;
}
for (int i = 0; i < n; i++) {
count[arr[i]]++;
}
int index = 0;
for (int i = 0; i < max; i++) {
while (count[i] > 0) {
arr[index++] = i;
count[i]--;
}
}
printArray(arr, n);
}
int main() {
int n;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
countingSort(arr, n);
return 0;
}
Output
#include <stdio.h>
8
#include <stdlib.h>
9
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
long long shifts = insertionSortCount(arr, n);
printf("%lld\n", shifts);
}
return 0;
}
Output
#include <stdio.h>
#include <string.h>
#define MAX_CHARS 26
char* twoStrings(char* s1, char* s2) {
int freq1[MAX_CHARS] = {0};
int freq2[MAX_CHARS] = {0};
for (int i = 0; s1[i] != '\0'; i++) {
freq1[s1[i] - 'a']++;
}
for (int i = 0; s2[i] != '\0'; i++) {
freq2[s2[i] - 'a']++;
}
1
for (int i = 0; i < MAX_CHARS; i++) {
if (freq1[i] > 0 && freq2[i] > 0) {
return "YES";
}
}
return "NO";
}
int main() {
int q;
scanf("%d", &q);
while (q--) {
char s1[100001], s2[100001];
scanf("%s %s", s1, s2);
char* result = twoStrings(s1, s2);
printf("%s\n", result);
}
return 0;
}
Output
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#define MAX_LEN 100001
int stringConstruction(char* s) {
bool present[26] = { false };
int cost = 0;
for (int i = 0; s[i] != '\0'; i++) {
int index = s[i] - 'a';
1
if (!present[index]) {
present[index] = true;
cost++;
}
}
return cost;
}
int main() {
int q;
scanf("%d", &q);
while (q--) {
char s[MAX_LEN];
scanf("%s", s);
int result = stringConstruction(s);
printf("%d\n", result);
}
return 0;
}
Output
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* readline();
1
char** split_string(char*);
struct betweenrows{
int *vertedge;
int edgemask;
int numpairs;
int *pairpos[2];
int *pairnum;
int *conto;
};
void printbet(struct betweenrows toprint, int n){
for(int i = 0; i < n; i++){
switch(toprint.vertedge[i]){
case 0:
printf(".");
break;
case 1:
printf("(");
break;
case 2:
printf(")");
break;
}
}
printf("\n");
}
void printhoriz(int bits, int n){
for(int i = 0; i < n - 1; i++)
{
if(((bits>>i)&1) == 0){
printf(".");
}
else{
printf("_");
}
}
printf("\n");
}
int tspGrid(int m, int n, int** horizontal, int** vertical) {
int hamming[1<<n];
hamming[0] = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < 1<<i; j++){
hamming[(1<<i) + j] = 1 + hamming[j];
}
}
if(n == 1 || m == 1){
return 0;
1
}
if(((m*n)&1) == 1){
return 0;
}
int pow3[n + 1];
pow3[0] = 1;
for(int i = 0; i < n; i++){
pow3[i + 1] = 3*pow3[i];
}
int *strtobet = malloc(pow3[n]*sizeof(int));
struct betweenrows *betlist = NULL;
int numbets = 0;
int currsymb[n];
for(int i = 0; i < n; i++){
currsymb[i] = 0;
}
while(true){
bool isgood = true;
int balance = 0;
for(int i = 0; i < n; i++){
if(currsymb[i] == 1){
balance++;
}
else if(currsymb[i] == 2){
balance--;
if(balance < 0){
isgood = false;
break;
}
}
}
if(isgood && balance == 0){
numbets++;
betlist = realloc(betlist, numbets*sizeof(struct betw
eenrows));
betlist[numbets - 1].vertedge = malloc(n*sizeof(int))
;
int strnum = 0;
betlist[numbets - 1].edgemask = 0;
betlist[numbets - 1].numpairs = 0;
betlist[numbets - 1].pairnum = malloc(n*sizeof(int));
betlist[numbets - 1].pairpos[0] = NULL;
betlist[numbets - 1].pairpos[1] = NULL;
betlist[numbets - 1].conto = malloc(n*sizeof(int));
int pairstack[n];
int stacksize = 0;
1
for(int a = 0; a < n; a++){
betlist[numbets - 1].vertedge[a] = currsymb[a];
betlist[numbets - 1].edgemask += (currsymb[a] > 0
? 1<<a : 0);
strnum += currsymb[a]*pow3[a];
if(currsymb[a] == 0){
betlist[numbets - 1].pairnum[a] = -1;
betlist[numbets - 1].conto[a] = -1;
}
else if(currsymb[a] == 1){
betlist[numbets - 1].pairnum[a] = betlist[num
bets - 1].numpairs;
pairstack[stacksize] = betlist[numbets - 1].n
umpairs;
betlist[numbets - 1].numpairs++;
betlist[numbets - 1].pairpos[0] = realloc(bet
list[numbets - 1].pairpos[0], betlist[numbets - 1].numpairs*sizeo
f(int));
betlist[numbets - 1].pairpos[1] = realloc(bet
list[numbets - 1].pairpos[1], betlist[numbets - 1].numpairs*sizeo
f(int));
betlist[numbets - 1].pairpos[0][betlist[numbe
ts - 1].numpairs - 1] = a;
stacksize++;
}
else{
betlist[numbets - 1].pairnum[a] = pairstack[s
tacksize - 1];
betlist[numbets - 1].pairpos[1][pairstack[sta
cksize - 1]] = a;
betlist[numbets - 1].conto[a] = betlist[numbe
ts - 1].pairpos[0][pairstack[stacksize - 1]];
betlist[numbets - 1].conto[betlist[numbets -
1].pairpos[0][pairstack[stacksize - 1]]] = a;
stacksize--;
}
}
strtobet[strnum] = numbets - 1;
}
int i;
for(i = 0; i < n; i++){
currsymb[i]++;
if(currsymb[i] != 3){
break;
}
1
else{
currsymb[i] = 0;
}
}
if(i == n){
break;
}
}
int connections[1<<(n - 1)][n];
for(int i = 0; i < 1<<(n - 1); i++){
int callback = -1;
for(int j = 0; j < n; j++){
bool leftedge = ((i<<1)>>j)&1;
bool rightedge = (i>>j)&1;
if(rightedge && !leftedge){
callback = j;
}
else if(leftedge && !rightedge){
connections[i][callback] = j;
connections[i][j] = callback;
callback = -1;
}
else if(!leftedge && !rightedge){
connections[i][j] = j;
}
else{
connections[i][j] = -1;
}
}
}
int *transition[numbets];
for(int i = 0; i < numbets; i++){
transition[i] = malloc(sizeof(int)<<(n - 1));
struct betweenrows currbet = betlist[i];
int betmask = currbet.edgemask;
for(int j = 0; j < 1<<(n - 1); j++){
if((j & (j<<1) & betmask) != 0){
transition[i][j] = -1;
continue;
}
if((j | (j<<1) | betmask) != ((1<<n) - 1)){
transition[i][j] = -1;
continue;
}
int nextmask = j ^ (j<<1) ^ betmask;
1
int transto = 0;
int nextconnections[currbet.numpairs][2];
for(int a = 0; a < currbet.numpairs; a++){
nextconnections[a][0] = -1;
nextconnections[a][1] = -1;
}
for(int a = 0; a < n; a++){
bool leftedge = ((j<<1)>>a)&1;
bool rightedge = (j>>a)&1;
bool upedge = currbet.vertedge[a];
if(upedge && (leftedge ^ rightedge)){
int currpos = currbet.conto[a];
bool gopair = false;
while(currpos != a && ((nextmask>>currpos)&1)
== 0)
{ if(gopair){
currpos = currbet.conto[currpos];
gopair = false;
}
else{
currpos = connections[j][currpos];
gopair = true;
}
}
if(currpos == a){
transto = -1;
break;
}
}
else if((upedge && !leftedge && !rightedge) || !u
pedge && (leftedge ^ rightedge)){
int currpos;
bool gopair;
if(upedge){
currpos = currbet.conto[a];
gopair = false;
}
else{
currpos = connections[j][a];
gopair = true;
}
while(((nextmask>>currpos)&1) == 0){
if(gopair){
currpos = currbet.conto[currpos];
gopair = false;
}
1
else{
currpos = connections[j][currpos];
gopair = true;
}
}
if(currpos < a){
transto += pow3[currpos] + 2*pow3[a];
}
}
}
if(transto != -1){
transition[i][j] = strtobet[transto];
printbet(betlist[i], n);
printhoriz(j, n);
printbet(betlist[transition[i][j]], n);
printf("\n");
}
else{
transition[i][j] = -1;
}
}
}
int bestout[m][numbets];
for(int i = 0; i < numbets; i++){
bestout[0][i] = INT_MAX/2;
}
bestout[0][0] = 0;
for(int i = 0; i < m - 1; i++){
int vertcost[1<<n];
vertcost[0] = 0;
for(int j = 0; j < n; j++){
for(int k = 0; k < 1<<j; k++){
vertcost[(1<<j) + k] = vertcost[k] + vertical[i][
j];
}
}
int horcost[1<<(n - 1)];
horcost[0] = 0;
for(int j = 0; j < n - 1; j++){
for(int k = 0; k < 1<<j; k++){
horcost[(1<<j) + k] = horcost[k] + horizontal[i][
j];
}
}
for(int j = 0; j < numbets; j++){
1
bestout[i + 1][j] = INT_MAX/2;
}
for(int j = 0; j < numbets; j++){
for(int k = 0; k < 1<<(n - 1); k++){
int nextbet = transition[j][k];
if(nextbet != -1){
int check = bestout[i][j] + horcost[k] + vert
cost[betlist[nextbet].edgemask];
bestout[i + 1][nextbet] = (check < bestout[i
+ 1][nextbet]? check : bestout[i + 1][nextbet]);
}
}
}
int hormask = 0;
for(int a = 0; a < n; a++){
hormask ^= betlist[i].edgemask<<a;
}
hormask &= (1<<n) - 1;
1
if((hormask | (hormask<<1) | betlist[i].edgemask) != (1<<
n) - 1){
continue;
}
int a = 0;
while(betlist[i].vertedge[a] == 0){
a++;
}
int numreps = 0;
int currpos = a;
do{
if(betlist[i].conto[currpos] == -1){
numreps = -1;
break;
}
currpos = connections[hormask][betlist[i].conto[currp
os]];
assert(currpos != -1);
numreps++;
}while(currpos != a);
if(numreps == betlist[i].numpairs){
int check = horcost[hormask] + bestout[m - 1][i];
best = (check < best? check : best);
}
}
return best;
}
int main()
{
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
char** mn = split_string(readline());
char* m_endptr;
char* m_str = mn[0];
int m = strtol(m_str, &m_endptr, 10);
if (m_endptr == m_str || *m_endptr != '\0') { exit(EXIT_FAILU
RE); }
char* n_endptr;
char* n_str = mn[1];
int n = strtol(n_str, &n_endptr, 10);
if (n_endptr == n_str || *n_endptr != '\0') { exit(EXIT_FAILU
RE); }
int** horizontal = malloc(m * sizeof(int*));
for (int horizontal_row_itr = 0; horizontal_row_itr < m; hori
zontal_row_itr++) {
2
*(horizontal + horizontal_row_itr) = malloc((n-
1) * (sizeof(int)));
char** horizontal_item_temp = split_string(readline());
for (int horizontal_column_itr = 0; horizontal_column_itr
< n-1; horizontal_column_itr++) {
char* horizontal_item_endptr;
char* horizontal_item_str = *(horizontal_item_temp +
horizontal_column_itr);
int horizontal_item = strtol(horizontal_item_str, &ho
rizontal_item_endptr, 10);
if (horizontal_item_endptr == horizontal_item_str ||
*horizontal_item_endptr != '\0') { exit(EXIT_FAILURE); }
*(*(horizontal + horizontal_row_itr) + horizontal_col
umn_itr) = horizontal_item;
}
}
int horizontal_rows = m;
int horizontal_columns = n-1;
int** vertical = malloc((m-1) * sizeof(int*));
for (int vertical_row_itr = 0; vertical_row_itr < m-
1; vertical_row_itr++) {
*(vertical + vertical_row_itr) = malloc(n * (sizeof(int))
);
char** vertical_item_temp = split_string(readline());
for (int vertical_column_itr = 0; vertical_column_itr < n
; vertical_column_itr++) {
char* vertical_item_endptr;
char* vertical_item_str = *(vertical_item_temp + vert
ical_column_itr);
int vertical_item = strtol(vertical_item_str, &vertic
al_item_endptr, 10);
if (vertical_item_endptr == vertical_item_str || *ver
tical_item_endptr != '\0') { exit(EXIT_FAILURE); }
*(*(vertical + vertical_row_itr) + vertical_column_it
r) = vertical_item;
}
}
int vertical_rows = m-1;
int vertical_columns = n;
int result = tspGrid(m, n, horizontal, vertical);
fprintf(fptr, "%d\n", result);
fclose(fptr);
return 0;
}
char* readline() {
size_t alloc_length = 1024;
2
size_t data_length = 0;
char* data = malloc(alloc_length);
while (true) {
char* cursor = data + data_length;
char* line = fgets(cursor, alloc_length - data_length, st
din);
if (!line) { break; }
data_length += strlen(cursor);
if (data_length < alloc_length - 1 || data[data_length -
1] == '\n') { break; }
size_t new_length = alloc_length << 1;
data = realloc(data, new_length);
if (!data) { break; }
alloc_length = new_length;
}
if (data[data_length - 1] == '\n') {
data[data_length - 1] = '\0';
}
if(data[data_length - 1] != '\0'){
data_length++;
data = realloc(data, data_length);
data[data_length - 1] = '\0';
}
data = realloc(data, data_length);
return data;
}
char** split_string(char* str) {
char** splits = NULL;
char* token = strtok(str, " ");
int spaces = 0;
while (token) {
splits = realloc(splits, sizeof(char*) * ++spaces);
if (!splits) {
return splits;
}
splits[spaces - 1] = token;
token = strtok(NULL, " ");
}
return splits;
}
2
Output
2
1 message
37
38
39
40
41
42
43
44
45