ADSA file
ADSA file
if(head==NULL){
cout<<"Empty list , Reversal not possible!!";
}
Node* nn=curr->next;
while(nn!=head){
curr->next=prev;
prev=curr;
curr=nn;
nn=nn->next;
}
curr->next=prev;
head=curr;
oriHead->next=head;
}
void printll(Node* &head){
Node* temp=head;
do{
cout << temp->data << " ";
temp=temp->next;
}
while(temp!=head);
cout<<endl;
}
int main() {
Node* head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
head->next->next->next = new Node(4);
head->next->next->next->next=head;
cout<<"Original List is : ";
printll(head);
reverseCLL(head);
cout<<"Reversed list is: ";
printll(head);
return 0;
}
OUTPUT:
int dequeue(){
while(!s1.empty()){
int x=s1.top();
s1.pop();
s2.push(x);
}
int deleted=s2.top();
s2.pop();
while(!s2.empty()){
int y=s2.top();
s2.pop();
s1.push(y);
}
return deleted;
}
void printQ(stack<int>s1){
while(!s1.empty()){
cout<<s1.top()<<" ";
s1.pop();
}
}};
int main() {
Queue Q;
Q.enqueue(100);
Q.enqueue(200);
Q.enqueue(300);
Q.enqueue(400);
Q.enqueue(500);
cout<<"This is stack view of Queue!!"<<endl;
cout<<"The queue is : ";
Q.printQ(Q.s1);
cout<<endl;
cout<<"Deleted element is : "<<Q.dequeue()<<endl;
cout<<"Deleted element is : "<<Q.dequeue()<<endl;
cout<<"Queue after deletion is : ";
Q.printQ(Q.s1);
return 0;
}
}
if(ele<root->data){
root->left=insert(root->left,ele);
}
else{
root->right=insert(root->right,ele);
}
return root;
}
void inorder(Node*root){
if(!root) return ;
inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
int main() {
int input[]={1,4,2,5,8,12,10,11,6,9};
Node * root=NULL;
for(int i=0;i<10;i++){
root=insert(root,input[i]);
}
cout<<"The (inorder traversal) of tree after insertion is : ";
inorder(root);
Code:
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int selected=activity[0].first;
cout<<"("<<activity[0].second<<","<<activity[0].first<<")";
for(int i=1;i<size;i++){
if(activity[i].second>=selected){
cout<<"("<<activity[i].second<<","<<activity[i].first<<")";
selected=activity[i].first;
}
}
}
int main() {
int start[]={1,2,5,6,0,4};
int finish[]={2,4,7,6,8,9};
int size=sizeof(start)/sizeof(start[0]);
ActSelection(start,finish,size);
}
void kClusters(int k) {
int edges[MAX][3];
int edgeCount = 0;
for (int i = 1; i < V; i++) {
edges[edgeCount][0] = parent[i];
edges[edgeCount][1] = i;
edges[edgeCount][2] = graph[parent[i]][i];
edgeCount++;
}
for (int i = 0; i < edgeCount - 1; i++) {
for (int j = 0; j < edgeCount - i - 1; j++) {
if (edges[j][2] < edges[j + 1][2]) {
swap(edges[j], edges[j + 1]);
}}}
cout << k << " clusters formed using edges:\n";
for (int i = k - 1; i < edgeCount; i++) {
cout << edges[i][0] << " - " << edges[i][1]
<< " (weight: " << edges[i][2] << ")\n";
}}
int main() {
V = 6;
E = 7;
for (int i = 0; i < V; i++)
for (int j = 0; j < V; j++)
graph[i][j] = 0;
int inputEdges[7][3] = {
{0, 1, 4}, {0, 2, 3}, {1, 2, 1},
{1, 3, 2}, {2, 3, 4}, {3, 4, 2},
{4, 5, 6}
};
primsAlgo();
int k = 3;
kClusters(k);
return 0;
}
Output:
return dp[n][C];
}
int main() {
vector<int> wt = {15, 10, 50};
vector<int> val = {80, 50, 200};
int C = 50;
int n = wt.size();
cout << "Max value that can be made using DP is : " << knapsackDP(C,
wt, val, n) << endl;
return 0;
}
5(i). Perform Sequence Alignment.
Code:
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
const int match = 1;
const int mismatch_penalty = -1;
const int gap = -2;
int maximum(int a, int b, int c) {
return max(a, max(b, c));
}
void SequenceAlignment(string seq1, string seq2) {
int x = seq1.length();
int y = seq2.length();
vector<vector<int>> cost(x + 1, vector<int>(y + 1));
vector<vector<char>> direction(x + 1, vector<char>(y + 1));
for (int i = 0; i <= x; i++) {
cost[i][0] = i * gap;
direction[i][0] = 'u'; }
for (int j = 0; j <= y; j++) {
cost[0][j] = j * gap;
direction[0][j] = 'l';
}
direction[0][0] = '0';
for (int i = 1; i <= x; i++) {
for (int j = 1; j <= y; j++) {
int matchedcost = (seq1[i - 1] == seq2[j - 1]) ? match :
mismatch_penalty;
int diagonal = cost[i - 1][j - 1] + matchedcost;
int up = cost[i - 1][j] + gap;
int left = cost[i][j - 1] + gap;
cost[i][j] = maximum(diagonal, up, left);
if (cost[i][j] == diagonal) direction[i][j] = 'D';
else if (cost[i][j] == up) direction[i][j] = 'u';
else direction[i][j] = 'l';
}}
int i = x, j = y;
string aligned1 = "", aligned2 = "";
while (i > 0 || j > 0) {
if (direction[i][j] == 'D') {
aligned1 = seq1[i - 1] + aligned1;
aligned2 = seq2[j - 1] + aligned2;
i--, j--;
} else if (direction[i][j] == 'u') {
aligned1 = seq1[i - 1] + aligned1;
aligned2 = '-' + aligned2;
i--;
} else if (direction[i][j] == 'l') {
aligned1 = '-' + aligned1;
aligned2 = seq2[j - 1] + aligned2;
j--; }}
cout << "Sequence 1: " << aligned1 << endl;
cout << "Sequence 2: " << aligned2 << endl;
cout << "Alignment Cost: " << cost[x][y] << endl;
}
int main() {
string seq1 = "TEMPLE";
string seq2 = "TEMPER";
SequenceAlignment(seq1, seq2);
return 0;
}
Output:
5(ii). 0/1 Knapsack using Branch and Bound.
Code:
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
struct Item {
int weight, profit;
float ratio;
};
struct Node {
int level, profit, weight;
float bound; };
bool compare(Item a, Item b) {
return a.ratio > b.ratio;
}
float calculateBound(Node u, int n, int W, Item items[]) {
if (u.weight >= W) return 0;
float profit_bound = u.profit;
int j = u.level + 1;
int totalWeight = u.weight;
while (j < n && totalWeight + items[j].weight <= W) {
totalWeight += items[j].weight;
profit_bound += items[j].profit;
j++;
}
if (j < n)
profit_bound += (W - totalWeight) * items[j].ratio;
return profit_bound;
}
int knapsack(Item items[], int n, int W) {
sort(items, items + n, compare);
queue<Node> q;
Node u, v;
u.level = -1; u.profit = 0; u.weight = 0;
u.bound = calculateBound(u, n, W, items);
q.push(u);
int maxProfit = 0;
while (!q.empty()) {
u = q.front(); q.pop();
if (u.level == n - 1) continue;
v.level = u.level + 1;
v.weight = u.weight + items[v.level].weight;
v.profit = u.profit + items[v.level].profit;
if (v.weight <= W && v.profit > maxProfit)
maxProfit = v.profit;
v.bound = calculateBound(v, n, W, items);
if (v.bound > maxProfit)
q.push(v);
v.weight = u.weight;
v.profit = u.profit;
v.bound = calculateBound(v, n, W, items);
if (v.bound > maxProfit)
q.push(v);
}
return maxProfit;
}
int main() {
int W = 10;
Item items[] = {{2, 40}, {3, 50}, {4, 65}, {5, 35}};
int n = sizeof(items) / sizeof(items[0]);
for (int i = 0; i < n; i++)
items[i].ratio = (float)items[i].profit / items[i].weight;
cout << "Maximum Profit = " << knapsack(items, n, W) << endl;
return 0;
}
Output:
6.(i).Perform Djikstra’s Algorithm.
Code:
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
class DjikstraAlgo
{
public:
vector <int> dijkstra(int V, vector<vector<int>> adj[], int S)
{
set<pair<int,int>> st;
vector<int> dist(V, 1e9);
st.insert({0, S});
dist[S] = 0;
while(!st.empty()) {
auto it = *(st.begin());
int node = it.second;
int distance= it.first;
st.erase(it);
for(auto it : adj[node]) {
int adjNode = it[0];
int edge = it[1];
return dist;
}
};
int main()
{
int V = 3, E = 3, S = 2;
vector<vector<int>> adj[V];
vector<vector<int>> edges;
vector<int> v1{1, 1}, v2{1, 6}, v3{2,3}, v4{0, 1}, v5{1, 2}, v6{5, 3};
int i = 0;
adj[0].push_back(v1);
adj[0].push_back(v2);
adj[1].push_back(v3);
adj[1].push_back(v4);
adj[2].push_back(v5);
adj[2].push_back(v6);
DjikstraAlgo ans;
vector<int> res = ans.dijkstra(V, adj, S);
int V = 4;
vector<vector<int>> matrix(V, vector<int>(V, -1));
matrix[0][1] = 2;
matrix[1][0] = 1;
matrix[1][2] = 9;
matrix[3][0] = 3;
matrix[3][1] = 7;
matrix[3][2] = 4;
Solution obj;
obj.shortest_path(matrix);
for (auto row : matrix) {
for (auto cell : row) {
cout << cell << " ";
}
cout << endl;
}
return 0;
}
Output: