0% found this document useful (0 votes)
10 views15 pages

Week 7

The document discusses an implementation of depth first search (DFS) on a graph represented using an adjacency list. It includes functions to add edges to the graph, perform DFS traversal and print the output. It also contains a sample main function and output.
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)
10 views15 pages

Week 7

The document discusses an implementation of depth first search (DFS) on a graph represented using an adjacency list. It includes functions to add edges to the graph, perform DFS traversal and print the output. It also contains a sample main function and output.
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/ 15

WEEK 7

PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#define n 20
typedef struct Node {
int x;
struct Node* next;
}Node;
Node * graph[n];
int visited[n];
void addEdge(int v, int w) {
Node* newNode=(Node*)malloc(sizeof(Node));
newNode->x=w;
newNode->next=graph[v];
graph[v]=newNode;
}
void DFS(int x){
visited[x] = 1;
printf("%d ",x);
Node* temp=graph[x];
while(temp!=NULL){
int connected=temp->x;
if (!visited[connected]) {
DFS(connected);
}
temp=temp->next;
}
}
int main() {
int num,count;
printf("enter no.of nodes and edges:");
scanf("%d %d",&num,&count);
for(int i=0;i<count;i++) {
int v,w;
printf("Enter edge (V, W): ");
scanf("%d %d", &v, &w);
addEdge(v,w);
}
for(int i=0;i<n;i++) {
visited[i]=0;
}
printf("DFS Traversal: ");
for (int i=0;i<num;i++) {
if (!visited[i]) {
DFS(i);
}
}
return 0;
}
OUTPUT: enter no.of nodes and edges:5
2
Enter edge (V, W): 6
2
Enter edge (V, W): 3
2
DFS Traversal: 0 1 2 3 4
Week-8
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;

class Huffman {
public static void printCode(HuffmanNode root, String s)
{
if (root.left == null && root.right == null
&& Character.isLetter(root.c)) {
System.out.println(root.c + ":" + s);

return;
}
printCode(root.left, s + "0");
printCode(root.right, s + "1");
}
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
int n = 5;
char[] charArray = { 'a', 'b', 'c', 'd', 'e' };
int[] charfreq = { 5, 9, 12, 13, 16 };
PriorityQueue<HuffmanNode> q
= new PriorityQueue<HuffmanNode>(
n, new MyComparator());

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


HuffmanNode hn = new HuffmanNode();
hn.c = charArray[i];
hn.data = charfreq[i];
hn.left = null;
hn.right = null;
q.add(hn);
}
HuffmanNode root = null;
while (q.size() > 1) {
HuffmanNode x = q.peek();
q.poll();
HuffmanNode y = q.peek();
q.poll();
HuffmanNode f = new HuffmanNode();
f.data = x.data + y.data;
f.c = '-';
f.left = x;
f.right = y;
root = f;
q.add(f);
}
printCode(root, "");
}
}
class HuffmanNode {
int data;
char c;
HuffmanNode left;
HuffmanNode right;
}
class MyComparator implements Comparator<HuffmanNode> {
public int compare(HuffmanNode x, HuffmanNode y)
{
return x.data - y.data;
}
}

Output:
a:1100
b:1101
c:100
d:101
e:111
WEEK 9
Program:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int weight;
int value;
float ratio;
} Item;
int compare(const void *a, const void *b) {
Item *itemA = (Item *)a;
Item *itemB = (Item *)b;
return (itemB->ratio - itemA->ratio);
}
float fractionalKnapsack(int capacity, Item items[], int n) {
qsort(items, n, sizeof(Item), compare);
int currentWeight = 0;
float totalValue = 0.0;
for (int i = 0; i < n; i++) {
if (currentWeight + items[i].weight <= capacity) {
currentWeight += items[i].weight;
totalValue += items[i].value;
} else {
int remainingWeight = capacity - currentWeight;
totalValue += (items[i].ratio * remainingWeight);
break;
}
}
return totalValue;
}
int main() {
int capacity = 50;
Item items[] = {
{10, 60, 0.0},
{20, 100, 0.0},
{30, 120, 0.0}
};
int n = sizeof(items) / sizeof(items[0]);
for (int i = 0; i < n; i++) {
items[i].ratio = (float)items[i].value / items[i].weight;
}
float maxValue = fractionalKnapsack(capacity, items, n);
printf("Maximum value that can be obtained = %.2f\n", maxValue);
return 0;
}
OUTPUT: Maximum value that can be obtained = 240.00

Week 10
Program: #include<stdio.h>
#include<stdbool.h>
#define n 9999999
#define V 5
int G[V][V] = {
{0,2,3,1,5},
{6,0,3,9,10},
{0,12,13,1,15},
{16,7,8,1,2},
{21,0,1,3,2}};
int main() {
int no_edge;
int selected[V];
memset(selected, false, sizeof(selected));
no_edge = 0;
selected[0] = true;
int x;
int y;
printf("Edge : Weight\n");
while (no_edge<V-1) {
int min=n;
x=0;
y=0;
for(int i=0;i<V;i++) {
if (selected[i]) {
for (int j = 0; j < V; j++) {
if (!selected[j] && G[i][j]){
if (min > G[i][j]) {
min=G[i][j];
x=i;
y=j;
}
}
}
}
}
printf("%d - %d : %d\n", x, y, G[x][y]);
selected[y] = true;
no_edge++;
}
return 0;
}
OUTPUT: Edge : Weight
0-3:1
0-1:2
3-4:2
4-2:1

WEEK 12
PROGRAM: #include <stdio.h>
#include <string.h>
int max(int a, int b) {
return (a > b) ? a : b;
}
int lcs(char* X, char* Y, int m, int n) {
int L[m + 1][n + 1];
int i, j;
for (i = 0; i <= m; i++) {
for (j = 0; j <= n; j++) {
if (i == 0 || j == 0)
L[i][j] = 0;
else if (X[i - 1] == Y[j - 1])
L[i][j] = L[i - 1][j - 1] + 1;
else
L[i][j] = max(L[i - 1][j], L[i][j - 1]);
}
}
return L[m][n];
}
int main() {
char X[] = "READ";
char Y[] = "DEAD";
int m = strlen(X);
int n = strlen(Y);
printf("Length of Longest Common Subsequence: %d\n", lcs(X, Y,
m, n));
return 0;
}
OUTPUT: Length of longest common subsequence 3
WEEK 11
PROGRAM: #include <stdio.h>
#include <limits.h>
int m[100][100];
int s[100][100];
int n;
int MatrixChainOrder(int p[], int i, int j) {
if (i == j)
return 0;
if (m[i][j] != -1)
return m[i][j];
int k;
m[i][j] = INT_MAX;
for (k = i; k < j; k++) {
int q = MatrixChainOrder(p, i, k) + MatrixChainOrder(p, k + 1, j) +
p[i - 1] * p[k] * p[j];
if (q < m[i][j]) {
m[i][j] = q;
s[i][j] = k;
}
}
return m[i][j];
}
void printOptimalParenthesis(int i, int j) {
if (i == j) {
printf("A%d", i);
} else {
printf("(");
printOptimalParenthesis(i, s[i][j]);
printOptimalParenthesis(s[i][j] + 1, j);
printf(")");
}
}
int main() {
int arr[] = {1, 2, 3, 4,};
int size = sizeof(arr) / sizeof(arr[0]);
n = size;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
m[i][j] = -1;
}
}
printf("Minimum number of multiplications is %d\n",
MatrixChainOrder(arr, 1, n - 1));
printOptimalParenthesis(1, n - 1);
return 0;
}
OUTPUT: Minimum no of multiplications is 18
(((A1A2)A3)A4)

You might also like