Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
31-oct-night
  • Loading branch information
samirankulkarni authored Oct 30, 2024
0 parents commit 5bcff20
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 0 deletions.
27 changes: 27 additions & 0 deletions daa_1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <bits/stdc++.h>
using namespace std;

int fib_rec(int n){
if(n<=1) return n;
return fib_rec(n-1) + fib_rec(n-2);
}

int fib_iter(int n){
if(n<=1) return n;
int a = 0;
int b = 1;
int c;
for(int i=2;i<=n;i++){
c = a + b;
a = b;
b = c;
}
return c;
}

int main(){

cout<<fib_rec(5)<<' '<<fib_iter(5)<<'\n';

return 0;
}
46 changes: 46 additions & 0 deletions daa_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import heapq

# python abs_path.py cmd

class Node:
def __init__(self, val, symbol) -> None:
self.val = val
self.symbol = symbol
self.huffman = ''
self.l = None
self.r = None

def __lt__(self, next):
return self.val < next.val

nodes = []
char = ['a', 'b', 'c', 'd', 'e']
freq = [100, 12, 24, 105, 38]

for i in range(len(char)):
heapq.heappush(nodes, Node(freq[i], char[i]))

while len(nodes) > 1:
left = heapq.heappop(nodes)
right = heapq.heappop(nodes)
left.huffman = '0'
right.huffman = '1'

newNode = Node(left.val + right.val, left.symbol + right.symbol)
newNode.l = left
newNode.r = right
heapq.heappush(nodes, newNode)

def printNodes(node, val=''):
newVal = val + node.huffman

if node.l:
printNodes(node.l, newVal)

if node.r:
printNodes(node.r, newVal)

if not node.l and not node.r:
print(f"{node.symbol} -> {newVal}")

printNodes(nodes[0])
26 changes: 26 additions & 0 deletions daa_3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <bits/stdc++.h>
using namespace std;

// 0/1 knapsack

int knapsack(int i,int n,vector<int> &weights,vector<int> &values,int capacity,vector<vector<int>> &dp){
if(i == n || capacity == 0) return 0;
if(dp[i][capacity] != -1) return dp[i][capacity];
// if we could take we take
int ans1 = INT_MIN;
if(weights[i] <= capacity) ans1 = values[i] + knapsack(i+1,n,weights,values,capacity-weights[i],dp);
int ans2 = knapsack(i+1,n,weights,values,capacity,dp);
return dp[i][capacity] = max(ans1,ans2);
}

int main(){
vector<int> weights = {95, 4, 60, 32, 23, 72, 80, 62, 65, 46};
vector<int> values = {55, 10, 47, 5, 4, 50, 8, 61, 85, 87};
int n = 10;
int capacity = 269;

vector<vector<int>> dp(n+1,vector<int>(capacity+1,-1));
cout<<knapsack(0,n,weights,values,capacity,dp)<<'\n';

return 0;
}
45 changes: 45 additions & 0 deletions daa_4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <bits/stdc++.h>
using namespace std;

void print(vector<vector<char>> &a){
int n = a.size();
for(int i=0;i<n;i++){
for(int j=0;j<n;j++) cout<<a[i][j]<<' ';
cout<<'\n';
}
cout<<'\n';
}

bool isSafe(vector<vector<char>> &a,int i,int j){
int n = a.size();
for(int c=i-1;c>=0;c--) if(a[c][j] == 'Q') return false;
for(int x=i,y=j;x>=0 && y>=0;x--,y--) if(a[x][y] == 'Q') return false;
for(int x=i,y=j;x>=0 && y<n;x--,y++) if(a[x][y] == 'Q') return false;
return true;
}

void fun(int i,int n,vector<vector<char>> &a){
if(i >= n){
print(a);
return;
}
for(int j=0;j<n;j++){
if(isSafe(a,i,j)){
a[i][j] = 'Q';
fun(i+1,n,a);
a[i][j] = '-';
}
}
}

void solve(int n){
vector<vector<char>> a(n,vector<char>(n,'-'));
fun(0,n,a);
}

int main(){
int n = 5;
solve(n);

return 0;
}

0 comments on commit 5bcff20

Please sign in to comment.