0% found this document useful (0 votes)
2 views10 pages

8TH Merged

The document contains multiple programming experiments conducted by a student named Shaswat Paidisetty in the Department of Computer Science & Engineering. It includes implementations of dynamic programming, backtracking, and homophily in networks, with specific coding examples and objectives for each experiment. The experiments aim to solve problems related to array construction, chocolate distribution, binary palindromes, Fibonacci sequences, and network attributes.

Uploaded by

nsrinivasrao1945
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)
2 views10 pages

8TH Merged

The document contains multiple programming experiments conducted by a student named Shaswat Paidisetty in the Department of Computer Science & Engineering. It includes implementations of dynamic programming, backtracking, and homophily in networks, with specific coding examples and objectives for each experiment. The experiments aim to solve problems related to array construction, chocolate distribution, binary palindromes, Fibonacci sequences, and network attributes.

Uploaded by

nsrinivasrao1945
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/ 10

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment-3.1
Student Name: Shaswat Paidisetty UID: 21BCS2258
Branch: BE-CSE Section/Group: 21BCS-902-A
Semester: 5th Date of Performance: 20/10/23
Subject Name: Advance Programming Lab-1 Subject Code: 21CSP-314

Aim: Implement the problems based on Dynamic Programming.

Objective: (a) Your goal is to find the number of ways to construct an array such that
consecutive positions contain different values. Specifically, we want to construct an array with
elements such that each element between and, inclusive. We also want the first and last elements
of the array to be 1 and x.
Code:

import java.io.*; import


java.util.*; public class
Solution {

static long countArray(int n, int k, int x) { //


Return the number of ways to fill in the array.
long max = 1000000007;
long[] f = new long[n + 1]; //[1,...,1]
long[] g = new long[n + 1];
//[1,...,2] f[3] = k - 1; g[2] = 1; g[3]
= k - 2;
for (int i = 4; i <= n; i++) { f[i]
= (k - 1) * g[i - 1] % max;
g[i] = (f[i - 1] + (k - 2) * g[i - 1]) % max;
} return x == 1 ? f[n] :
g[n];}
public static void main(String[] args) { Scanner
in = new Scanner(System.in);
int n =
in.nextInt(); int k
= in.nextInt(); int
x = in.nextInt();
long answer = countArray(n, k, x);
System.out.println(answer);
in.close();
}}
Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

(b) Christy is interning at Hacker Rank. One day she must distribute some chocolates to her colleagues. She is
biased towards her friends and plans to give them more than the others. One of the program managers hears
of this and tells her to make sure everyone gets the same number.
To make things difficult, she must equalize the number of chocolates in a series of operations. For each
operation, she can give 1,2 or 5 pieces to all but one colleague. Everyone who gets a piece in a round
receives the same number of pieces.Given a starting distribution, calculate the minimum number of
operations needed so that every colleague has the same number of pieces.

Code:
import java.util.Scanner;
public class Solution { static
int offset = 100;
static Integer f[][] = new Integer[1111][1111]; static
int a[] = new int[11111];
// From i down to j static
Integer F(int i, int j) {
if (i < j) return Integer.MAX_VALUE / 2;
if (f[i + offset][j + offset] != null) return f[i + offset][j + offset]; if
(i == j) return 0;
int ans = Integer.MAX_VALUE / 2;
ans = Math.min(ans, F(i - 1, j) + 1);
ans = Math.min(ans, F(i - 2, j) + 1);
ans = Math.min(ans, F(i - 5, j) + 1);
return f[i + offset][j + offset] = ans;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int T = scanner.nextInt(); for (int t = 0; t <
T; t++) {
int n = scanner.nextInt();
int min = Integer.MAX_VALUE;
for (int i = 0; i < n; i++)
{ a[i] = scanner.nextInt();
min = Math.min(min,
a[i]);
}
int ans = Integer.MAX_VALUE;
for (int i = min; i >= min - 30; i--)
{ int tmp = 0; for (int j = 0; j < n;
j++) { tmp += F(a[j], i); }
ans = Math.min(ans, tmp);
}
System.out.println(ans);}}}

OUTPUT:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Experiment 9

Student Name: Shaswat Paidisetty UID: 21BCS2258


Branch: BE-CSE Section/Group: SN-902(A)
Semester: 5th Date of Performance: 20/10/23
Subject Name: Advanced Programming Lab-1 Subject Code: 21CSP-314

Aim: Implement the problems based on Backtracing.

Objective:
1. You are given a number . In one operation, you can either increase the value of by
1 or decrease the value of by 1. Determine the minimum number of operations
required (possibly zero) to convert number to a number such that binary
representation of is a palindrome. Note: A binary representation is said to be a
palindrome if it reads the same from left-right and right-left.
2. The Fibonacci sequence appears in nature all around us, in the arrangement of
seeds in a sunflower and the spiral of a nautilus for example. The Fibonacci
sequence begins with and as its first and second terms. After these first two
elements, each subsequent element is equal to the sum of the previous two
elements.

Code:
a.)
import java.util.*; import
java.lang.Math;

public class BinaryPalindromicJava { static


ArrayList<Long> v = new ArrayList<>();
public static void main(String[] args)
{ Scanner scanner = new
Scanner(System.in); v.add(0L); for (int i =
1; i < 32; i++) {
String s = "";
String p = "1";
bin(p, Math.ceil(i / 2.0) - 1, i);
}
int t = scanner.nextInt();
while (t-- > 0) {
long n = scanner.nextLong();
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
int index = Collections.binarySearch(v, n);
if (index < 0)
{ index = -index - 1;
long y = v.get(index);
long x = v.get(index - 1);
System.out.println(Math.min(Math.abs(n - x), Math.abs(n - y)));
} else {
System.out.println(0);
}
}
}
static void bin(String p, double si, int sz) {
if (si <= 0) { String res1 = p;
int x = res1.length(); int y = sz % 2
== 1 ? 2 : 1; for (int i = x - y; i >= 0;
i--) {
res1 += res1.charAt(i);
}
long res = bitToInt(res1);
v.add(res); return;}
p += '0'; bin(p, si - 1, sz);
p = p.substring(0, p.length() - 1);
p += '1';
bin(p, si - 1, sz);
}
static long bitToInt(String p) {
long sz = p.length(); long
ans = 0;
for (int i = 0; i < sz; i++) {
ans += (p.charAt(i) - '0') * Math.pow(2, sz - i - 1);
}
return ans;
}
}

Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

b.) import java.util.*; public class


Solution { public static int
fibonacci(int n) { int[] fib = new
int[2]; fib[0] = 0; fib[1] = 1;
for (int i = 2; i <= n; ++i)
{ fib[i % 2] = fib[0] + fib[1];
}
return fib[n % 2];
} public static void main(String[] args)
{ Scanner scanner = new
Scanner(System.in);
int n = scanner.nextInt();
scanner.close();
System.out.println(fibonacci(n));
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

OUTPUT:

COMPUTER
SCIENCE
DEPARTMENT OF
& ENGINEERING

Experiment 8

Student Name: Shaswat Paidisetty UID: 21BCS2258


Branch: BE-CSE Section/Group: 902-A
Semester:5th Date of Performance: 20/10/23
Subject Name: Social Networks Subject Code: 21CSP-345

1. Aim: Demonstrate homophily in the network by measuring the tendency of nodes to


form connections with similar or dissimilar nodes based on attributes or characteristics
by using python.

2. Objective:
 To Demonstrate homophily in a network.
 To Measure the tendency of nodes to form connections based on their attributes or
characteristics.

3. Code:
import networkx as nx import
matplotlib.pyplot as plt
G=nx.Graph()

G.add_nodes_from([1,2,3,4,5,6,7,8,9,10,11,12,13,14])
edges = [(1, 2), (1,5),(2, 6), (3, 6), (2, 5), (5, 4), (6, 4), (7, 8), (8, 5), (9, 11), (10,11),
(11,12), (7,12), (8,4), (7,10), (9,12) , (8,12), (9,13), (10,12), (11,13), (7,14), (6,14), (3,5),
(6,7), (2,6), (4,6), (4,7),(3,7),(8,10),(1,4),(1,8)]
G.add_edges_from(edges)

COMPUTER SCIENCE
attributes = {1: 'Teenage', 2: 'Teenage', 3: 'old', 4: 'Adult', 5: 'Adult', 6: 'old', 7: 'old', 8:
'Adult', 9: 'Teenage', 10: 'Teenage',11: 'old',12: 'Teenage',13: 'Teenage',14: 'Teenage'}

# set color for different communities


DEPARTMENT OF
& ENGINEERING
node_colors = ['yellow' if attributes[node] == 'Teenage' else 'red' if attributes[node]==
'Adult' else 'pink' for node in G.nodes()]

pos = nx.circular_layout(G)
nx.draw(G, pos, with_labels=True, node_size=2000,labels=attributes
node_color=node_colors) plt.title("Homophily Network") plt.show()

#calculate homophily
nx.set_node_attributes(G,attributes,'attribute')
homophily_coefficient = nx.attribute_assortativity_coefficient(G,'attribute')
print("Homophily Coefficient:", homophily_coefficient)

4. OUTPUT:

Fig 1: network connectivity among 3 communities

5. LEARNING OUTCOMES:

1. Learnt about concept of Homophily and heterophily.


DEPARTMENT OF
& ENGINEERING
2. Learnt about community distribution. 3. Learnt about
assortativity coefficient.

You might also like