CP4161 Ads Lab Final
CP4161 Ads Lab Final
CP4161 Ads Lab Final
OF
FIRST YEAR
REGULATION 2021
RECORD NOTE
LABORATORY
Bonafide Certificate
REGISTER NO:
Certified that this bonafide record of the work done by the candidate
OBJECTIVES:
To acquire the knowledge of using advanced tree structures
To learn the usage of heap structures
To understand the usage of graph structures and spanning trees
To understand the problems such as matrix chain multiplication, activity selection and
Huffman coding
To understand the necessary mathematical abstraction to solve problems.
LISTOF EXPERIMENTS:
1:Implementation of recursive function for tree traversal and Fibonacci
2: Implementation of iteration function for tree traversal and Fibonacci
3: Implementation of Merge Sort and Quick Sort
4:Implementation of a Binary Search Tree
5: Red-Black Tree Implementation
6:Heap Implementation
7:Fibonacci Heap Implementation
8: Graph Traversals
9:Spanning Tree Implementation
10:Shortest Path Algorithms (Dijkstra's algorithm, Bellman Ford Algorithm)
11: Implementation of Matrix Chain Multiplication
12:ActivitySelectionandHuffmanCodingImplementation
HARDWARE/SOFTWAREREQUIREMENTS
1. 64-bit Open source Linux or its derivative
2. Open Source C++Programming tool like G++/GCC
TOTAL:60 PERIODS
l O M oA R c P S D | 1 2 2 0 8 0 2 6
TABLE OF CONTENTS
PAGE STAFF
MARK
EX.NO DATE NAME THE EXPERIMENTS NO. SIGN
6 Heap Implementation
7 Fibonacci Heap Implementation
8 Graph Traversals
AIM:
Write to implementation of recursive function for tree traversal and fibonacci
using C++.
ALGORITHM:
STEP1: First, visit all the nodes in the left sub tree
STEP7: End
SOURCE CODE:
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node *left, *right;
Node(int data) {
this->data = data;
left = right = NULL;
}
};
// Preorder traversal
void preorderTraversal(struct Node* node) {
if (node == NULL)
return;
// Postorder traversal
void postorderTraversal(struct Node* node) {
if (node == NULL)
return;
FIBONACCI
#include <iostream>
using namespace std;
int fib(int x) {
if((x==1)||(x==0)) {
return(x);
}else {
return(fib(x-1)+fib(x-2));
}
}
int main() {
int x , i=0;
cout << "Enter the number of terms of series : ";
cin >> x;
cout << "\nFibonnaci Series : ";
while(i < x) {
cout << " " << fib(i);
i++;
return 0;
}
OUTPUT:
5 -> 12 -> 6 -> 1 -> 9
RESULT:
Thus implementation of recursive function for tree traversal and fibonacci using C++
Successfully completed.
l O M oA R c P S D | 1 2 2 0 8 0 2 6
AIM:
ALGORITHM:
STEP7: End
SOURCE CODE:
#include <bits/stdc++.h>
using namespace std;
// Tree Node
struct Node {
int data;
Node *left, *right;
Node(int data)
{
this->data = data;
this->left = this->right = NULL;
}
};
void preorderIterative(Node* root)
{
if (root == NULL)
return;
l O M oA R c P S D | 1 2 2 0 8 0 2 6
stack<Node*> st;
if (curr->right)
st.push(curr->right);
curr = curr->left;
}
if (st.empty() == false) {
curr = st.top();
st.pop();
}
}
}
int main()
{
Node* root = new Node(10);
root->left = new Node(20);
root->right = new Node(30);
root->left->left = new Node(40);
root->left->left->left = new Node(70);
root->left->right = new Node(50);
root->right->left = new Node(60);
root->left->left->right = new Node(80);
preorderIterative(root);
return 0;
}
FIBONACCI
#include <iostream>
using namespace std;
void fib(int num) {
int x = 0, y = 1, z = 0;
for (int i = 0; i < num; i++) {
cout << x << " ";
z = x + y;
x = y;
y = z;
}
}
int main() {
int num;
cout << "Enter the number : ";
cin >> num;
l O M oA R c P S D | 1 2 2 0 8 0 2 6
return 0;
}
OUTPUT:
10 20 40 70 80 50 30 60
RESULT:
Thus implementation of iteration function for tree traversal and fibonacci
using C++ Successfully completed.
l O M oA R c P S D | 1 2 2 0 8 0 2 6
AIM:
ALGORITHM:
MERGE SORT
SOURCE CODE:
#include <bits/stdc++.h>
using namespace std;
void Merge(int *a, int low, int high, int mid)
{
int i, j, k, temp[high-low+1];
i = low;
k = 0;
j = mid + 1;
int main()
{
int n, i;
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;
int arr[n];
for(i = 0; i < n; i++)
{
cout<<"Enter element "<<i+1<<": ";
cin>>arr[i];
}
MergeSort(arr, 0, n-1);
return 0;
}
OUTPUT: MERGE SORT
Enter the number of data element to be sorted: 10
Enter element 1: 23
Enter element 2: 987
Enter element 3: 45
Enter element 4: 65
Enter element 5: 32
Enter element 6: 9
Enter element 7: 475
Enter element 8: 1
Enter element 9: 17
Enter element 10: 3
QUICK SORT:
ALGORITHM:
Step 6: Traverse through the entire array using the variable i and compare each
element with pivot
Step 7: If arr[i] < pivot then increment j and swap element as position j and i
Step 8: Increment i by 1
PROGRAM:
#include
int temp;
temp = arr[pos1];
arr[pos1] = arr[pos2];
arr[pos2] = temp;
int i = low;
int j = low;
i++;
else{
swap(arr,i,j);
i++;
j++;
return j-1;
int main()
int n ;
cin>>n;
int arr[n];
cin>> arr[i];
quickSort(arr, 0 , n-1);
cout<< arr[i]<<"\t";
OUTPUT : QUICKSORT
RESULT:
Thus implementation of Merge sort and Quick sort using C++ Successfully
completed.
l O M oA R c P S D | 1 2 2 0 8 0 2 6
DATE:
AIM:
ALGORITHM:
STEP1: The right sub-tree of a node has a key greater than to its parent nodes.
STEP2:The left sub-tree of a node has a key lesser than to its parent node's.
STEP3:All key values are distinct.
STEP4:Each node cannot have more than two children.
STEP5: End
SOURCE CODE:
# include <iostream>
# include <cstdlib>
using namespace std;
struct node
{
int info;
struct node *left;
struct node *right;
}*root;
class BST
{
public:
void find(int, node **, node **);
void insert(node *, node *);
void del(int);
void case_a(node *,node *);
void case_b(node *,node *);
void case_c(node *,node *);
void preorder(node *);
void inorder(node *);
void postorder(node *);
void display(node *, int);
BST()
{
root = NULL;
}
};
int main()
{
int choice, num;
BST bst;
l O M oA R c P S D | 1 2 2 0 8 0 2 6
node *temp;
l O M oA R c P S D | 1 2 2 0 8 0 2 6
while (1)
{
cout<<" ---------------- "<<endl;
cout<<"Operations on BST"<<endl;
cout<<" ---------------- "<<endl;
cout<<"1.Insert Element "<<endl;
cout<<"2.Delete Element "<<endl;
cout<<"3.Inorder Traversal"<<endl;
cout<<"4.Preorder Traversal"<<endl;
cout<<"5.Postorder Traversal"<<endl;
cout<<"6.Display"<<endl;
cout<<"7.Quit"<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
temp = new node;
cout<<"Enter the number to be inserted : ";
cin>>temp->info;
bst.insert(root, temp);
case 2:
if (root == NULL)
{
cout<<"Tree is empty, nothing to delete"<<endl;
continue;
}
cout<<"Enter the number to be deleted : ";
cin>>num;
bst.del(num);
break;
case 3:
cout<<"Inorder Traversal of BST:"<<endl;
bst.inorder(root);
cout<<endl;
break;
case 4:
cout<<"Preorder Traversal of BST:"<<endl;
bst.preorder(root);
cout<<endl;
break;
case 5:
cout<<"Postorder Traversal of BST:"<<endl;
bst.postorder(root);
cout<<endl;
break;
case 6:
cout<<"Display BST:"<<endl;
bst.display(root,1);
cout<<endl;
l O M oA R c P S D | 1 2 2 0 8 0 2 6
break;
case 7:
exit(1);
default:
cout<<"Wrong choice"<<endl;
}
}
}
void BST::find(int item, node **par, node **loc)
{
node *ptr, *ptrsave;
if (root == NULL)
{
*loc = NULL;
*par = NULL;
return;
}
if (item == root->info)
{
*loc = root;
*par = NULL;
return;
}
if (item < root->info)
ptr = root->left;
else
ptr = root->right;
ptrsave = root;
while (ptr != NULL)
{
if (item == ptr->info)
{
*loc = ptr;
*par = ptrsave;
return;
}
ptrsave = ptr;
if (item < ptr->info)
ptr = ptr->left;
else
ptr = ptr->right;
}
*loc = NULL;
*par = ptrsave;
}
void BST::insert(node *tree, node *newnode)
{
if (root == NULL)
{
root = new node;
l O M oA R c P S D | 1 2 2 0 8 0 2 6
root->info = newnode->info;
root->left = NULL;
root->right = NULL;
cout<<"Root Node is Added"<<endl;
return;
}
if (tree->info == newnode->info)
{
cout<<"Element already in the tree"<<endl;
return;
}
if (tree->info > newnode->info)
{
if (tree->left != NULL)
{
insert(tree->left, newnode);
}
else
{
tree->left = newnode;
(tree->left)->left = NULL;
(tree->left)->right = NULL;
cout<<"Node Added To Left"<<endl;
return;
}
}
else
{
if (tree->right != NULL)
{
insert(tree->right, newnode);
}
else
{
tree->right = newnode;
(tree->right)->left = NULL;
(tree->right)->right = NULL;
cout<<"Node Added To Right"<<endl;
return;
}
}
}
void BST::del(int item)
{
node *parent, *location;
if (root == NULL)
{
cout<<"Tree empty"<<endl;
return;
}
l O M oA R c P S D | 1 2 2 0 8 0 2 6
{
node *ptr, *ptrsave, *suc, *parsuc;
ptrsave = loc;
ptr = loc->right;
while (ptr->left != NULL)
{
ptrsave = ptr;
ptr = ptr->left;
}
suc = ptr;
parsuc = ptrsave;
if (suc->left == NULL && suc->right == NULL)
case_a(parsuc, suc);
else
case_b(parsuc, suc);
if (par == NULL)
{
root = suc;
}
else
{
if (loc == par->left)
par->left = suc;
else
par->right = suc;
}
suc->left = loc->left;
suc->right = loc->right;
}
void BST::preorder(node *ptr)
{
if (root == NULL)
{
cout<<"Tree is empty"<<endl;
return;
}
if (ptr != NULL)
{
cout<<ptr->info<<" ";
preorder(ptr->left);
preorder(ptr->right);
}
}
}
if (ptr != NULL)
{
inorder(ptr->left);
cout<<ptr->info<<" ";
inorder(ptr->right);
}
}
void BST::postorder(node *ptr)
{
if (root == NULL)
{
cout<<"Tree is empty"<<endl;
return;
}
if (ptr != NULL)
{
postorder(ptr->left);
postorder(ptr->right);
cout<<ptr->info<<" ";
}
}
void BST::display(node *ptr, int level)
{
int i;
if (ptr != NULL)
{
display(ptr->right, level+1);
cout<<endl;
if (ptr == root)
cout<<"Root->: ";
else
{
for (i = 0;i < level;i++)
cout<<" ";
}
cout<<ptr->info;
display(ptr->left, level+1);
}
}
l O M oA R c P S D | 1 2 2 0 8 0 2 6
OUTPUT:
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 1
Enter the number to be inserted : 8
Root Node is Added
RESULT:
Thus implementation of Binary Search using C++ Successfully completed.
l O M oA R c P S D | 1 2 2 0 8 0 2 6
AIM:
ALGORITHM:
SOURCE CODE:
#include <bits/stdc++.h>
using namespace std;
struct node
{
int key;
node *parent;
char color;
node *left;
node *right;
};
class RBtree
{
node *root;
node *q;
public :
RBtree()
{
q=NULL;
root=NULL;
}
void insert();
void insertfix(node *);
void leftrotate(node *);
void rightrotate(node *);
void del();
l O M oA R c P S D | 1 2 2 0 8 0 2 6
insertfix(t);
}
void RBtree::insertfix(node *t)
{
node *u;
if(root==t)
{
t->color='b';
return;
}
while(t->parent!=NULL&&t->parent->color=='r')
{
node *g=t->parent->parent;
if(g->left==t->parent)
{
if(g->right!=NULL)
{
u=g->right;
if(u->color=='r')
{
t->parent->color='b';
u->color='b';
g->color='r';
t=g;
}
}
else
{
if(t->parent->right==t)
{
t=t->parent;
leftrotate(t);
}
t->parent->color='b';
g->color='r';
rightrotate(g);
}
}
else
{
l O M oA R c P S D | 1 2 2 0 8 0 2 6
if(g->left!=NULL)
{
u=g->left;
if(u->color=='r')
{
t->parent->color='b';
u->color='b';
g->color='r';
t=g;
}
}
else
{
if(t->parent->left==t)
{
t=t->parent;
right
rotate(t);
}
t->parent->color='b';
g->color='r'; left
rotate(g);
}
}
root->color='b';
}
}
void RBtree::del()
{
if(root==NULL)
{
cout<<"\nEmpty Tree." ;
return ;
}
int x;
cout<<"\nEnter the key of the node to be deleted: ";
cin>>x;
node *p;
p=root;
node *y=NULL;
l O M oA R c P S D | 1 2 2 0 8 0 2 6
node *q=NULL;
int found=0;
while(p!=NULL&&found==0)
{
if(p->key==x)
found=1;
if(found==0)
{
if(p->key<x)
p=p->right;
else
p=p->left;
}
}
if(found==0)
{
cout<<"\nElement Not Found.";
return ;
}
else
{
cout<<"\nDeleted Element: "<<p->key;
cout<<"\nColour: ";
if(p->color=='b')
cout<<"Black\n";
else
cout<<"Red\n";
if(p->parent!=NULL)
cout<<"\nParent: "<<p->parent->key;
else
cout<<"\nThere is no parent of the node. ";
if(p->right!=NULL)
cout<<"\nRight Child: "<<p->right->key;
else
cout<<"\nThere is no right child of the node. ";
if(p->left!=NULL)
cout<<"\nLeft Child: "<<p->left->key;
else
cout<<"\nThere is no left child of the node. ";
l O M oA R c P S D | 1 2 2 0 8 0 2 6
cout<<"\nNode Deleted.";
if(p->left==NULL||p->right==NULL)
y=p;
else
y=successor(p);
if(y->left!=NULL)
q=y->left;
else
{
if(y->right!=NULL)
q=y->right;
else
q=NULL;
}
if(q!=NULL)
q->parent=y->parent;
if(y->parent==NULL)
root=q;
else
{
if(y==y->parent->left)
y->parent->left=q;
else
y->parent->right=q;
}
if(y!=p)
{
p->color=y->color;
p->key=y->key;
}
if(y->color=='b')
delfix(q);
}
}
if(p->parent->left==p)
{
s=p->parent->right;
if(s->color=='r')
{
s->color='b';
p->parent->color='r';
leftrotate(p->parent);
s=p->parent->right;
}
if(s->right->color=='b'&&s->left->color=='b')
{
s->color='r';
p=p->parent;
}
els
e
{
if(s->right->color=='b')
{
s->left->color=='b';
s->color='r';
rightrotate(s);
s=p->parent->right;
}
s->color=p->parent->color;
p->parent->color='b';
s->right->color='b';
leftrotate(p->parent);
p=root;
}
}
else
{
s=p->parent->left;
if(s->color=='r')
{
s->color='b';
p->parent->color='r';
rightrotate(p->parent);
s=p->parent->left;
l O M oA R c P S D | 1 2 2 0 8 0 2 6
}
if(s->left->color=='b'&&s->right->color=='b')
{
s->color='r';
p=p->parent;
}
else
{
if(s->left->color=='b')
{
s->right->color='b';
s->color='r';
leftrotate(s);
s=p->parent->left;
}
s->color=p->parent->color;
p->parent->color='b';
s->left->color='b';
rightrotate(p->parent);
p=root;
}
}
p->color='b';
root->color='b';
}
}
p->right=NULL;
if(p->parent!=NULL)
y->parent=p->parent;
if(p->parent==NULL)
root=y;
else
{
if(p==p->parent->left)
p->parent->left=y;
else
p->parent->right=y;
}
y->left=p;
p->parent=y;
}
}
void RB tree::right rotate(node *p)
{
if(p->left==NULL)
return ;
else
{
node *y=p->left;
if(y->right!=NULL)
{
p->left=y->right;
y->right->parent=p;
}
else
p- >left=NULL;
if(p->parent!=NULL)
y->parent=p->parent;
if(p->parent==NULL)
root=y;
else
{
if(p==p->parent->left)
p->parent->left=y;
else
p->parent->right=y;
l O M oA R c P S D | 1 2 2 0 8 0 2 6
}
y->right=p;
p->parent=y;
}
}
void RBtree::disp()
{
display(root);
}
void RBtree::display(node *p)
{
if(root==NULL)
{
cout<<"\nEmpty Tree.";
return ;
}
if(p!=NULL)
{
cout<<"\n\t NODE: ";
cout<<"\n Key: "<<p->key;
cout<<"\n Colour: ";
l O M oA R c P S D | 1 2 2 0 8 0 2 6
if(p->color=='b')
cout<<"Black";
else
cout<<"Red";
if(p->parent!=NULL)
cout<<"\n Parent: "<<p->parent->key;
else
cout<<"\n There is no parent of the node. ";
if(p->right!=NULL)
cout<<"\n Right Child: "<<p->right->key;
else
cout<<"\n There is no right child of the node. ";
if(p->left!=NULL)
cout<<"\n Left Child: "<<p->left->key;
else
cout<<"\n There is no left child of the node. ";
cout<<endl;
if(p->left)
{
cout<<"\n\nLeft:\n";
display(p->left);
}
/*else
cout<<"\nNo Left Child.\n";*/
if(p->right)
{
cout<<"\n\nRight:\n";
display(p->right);
}
/*else
cout<<"\nNo Right Child.\n"*/
}
}
void RBtree::search()
{
if(root==NULL)
{
cout<<"\nEmpty Tree\n" ;
return ;
}
l O M oA R c P S D | 1 2 2 0 8 0 2 6
int x;
cout<<"\n Enter key of the node to be searched: ";
cin>>x;
node *p=root;
int found=0;
while(p!=NULL&& found==0)
{
if(p->key==x)
found=1;
if(found==0)
{
if(p->key<x)
p=p->right;
else
p=p->left;
}
}
if(found==0)
cout<<"\nElement Not Found.";
else
{
cout<<"\n\t FOUND NODE: ";
cout<<"\n Key: "<<p->key;
cout<<"\n Colour: ";
if(p->color=='b')
cout<<"Black";
else
cout<<"Red";
if(p->parent!=NULL)
cout<<"\n Parent: "<<p->parent->key;
else
cout<<"\n There is no parent of the node. ";
if(p->right!=NULL)
cout<<"\n Right Child: "<<p->right->key;
else
cout<<"\n There is no right child of the node. ";
if(p->left!=NULL)
cout<<"\n Left Child: "<<p->left->key;
else
cout<<"\n There is no left child of the node. ";
l O M oA R c P S D | 1 2 2 0 8 0 2 6
cout<<endl;
}
}
int main()
{
int ch,y=0;
RBtree obj;
do
{
cout<<"\n\t RED BLACK TREE " ;
cout<<"\n 1. Insert in the tree ";
cout<<"\n 2. Delete a node from the tree";
cout<<"\n 3. Search for an element in the tree";
cout<<"\n 4. Display the tree ";
cout<<"\n 5. Exit " ;
cout<<"\nEnter Your Choice: ";
cin>>ch;
switch(ch)
{
case 1 : obj.insert();
cout<<"\nNode Inserted.\n";
break;
case 2 : obj.del();
break;
case 3 : obj.search();
break;
case 4 : obj.disp();
break;
case 5 : y=1;
break;
default : cout<<"\nEnter a Valid Choice.";
}
cout<<endl;
}while(y!=1);
return 1;
}
l O M oA R c P S D | 1 2 2 0 8 0 2 6
OUTPUT:
1. Insert in the tree
2. Delete a node from the tree
3. Search for an element in the tree
4. Display the tree
5. Exit
Enter Your Choice: 1
Enter key of the node to be inserted: 4
Node Inserted.
RESULT:
Thus implementation of Red-Black Tree using C++ Successfully completed.
l O M oA R c P S D | 1 2 2 0 8 0 2 6
DATE:
AIM:
ALGORITHM:
STEP3:We will compare the data of the node with the Parent node.
STEP4: we will repeat the same process till we hit the root node.
STEP5: End
SOURCE CODE:
#include <iostream>
#include <cstdlib>
#include <vector>
#include <iterator>
using namespace std;
class Heap
{
private:
vector <int> heap;
int left(int parent);
int right(int parent);
int parent(int child);
void heapifyup(int index);
void heapifydown(int index);
public:
Heap()
{}
void Insert(int element);
void DeleteMin();
int ExtractMin();
void DisplayHeap();
int Size();
};
int Heap::Size()
{
return heap.size();
}
void Heap::Insert(int element)
l O M oA R c P S D | 1 2 2 0 8 0 2 6
{
heap.push_back(element);
heapifyup(heap.size() -1);
}
void Heap::DeleteMin()
{
if (heap.size() == 0)
{
cout<<"Heap is Empty"<<endl;
return;
}
heap[0] = heap.at(heap.size() - 1);
heap.pop_back();
heapifydown(0);
cout<<"Element Deleted"<<endl;
}
int Heap::ExtractMin()
{
if (heap.size() == 0)
{
return -1;
}
else
return heap.front();
}
void Heap::DisplayHeap()
{
vector <int>::iterator pos = heap.begin();
cout<<"Heap --> ";
while (pos != heap.end())
{
cout<<*pos<<" ";
pos++;
}
cout<<endl;
}
int r = 2 * parent + 2;
if(r < heap.size())
return r;
else
return -1;
}
OUTPUT:
1. Insert Element
2. Delete Minimum Element
3.Extract Minimum Element
4.Print Heap
5.Exit
Enter your choice: 1
Enter the element to be inserted: 7
RESULT:
Thus implementation of Heap using C++ Successfully completed.
l O M oA R c P S D | 1 2 2 0 8 0 2 6
AIM:
ALGORITHM:
SOURCE CODE:
#include <bits/stdc++.h>
using namespace std;
struct node
{
int n;
int degree;
node* parent;
node* child;
node* left;
node* right;
char mark;
char C;
};
/*
* Class Declaration
*/
class FibonacciHeap
{
private:
int nH;
node *H;
public:
node* InitializeHeap();
int Fibonnaci_link(node*, node*, node*);
node *Create_node(int);
node *Insert(node *, node *);
node *Union(node *, node *);
node *Extract_Min(node *);
int Consolidate(node *);
l O M oA R c P S D | 1 2 2 0 8 0 2 6
else
{
H = x;
}
nH = nH + 1;
return H;
}
/*
* Link Nodes in Fibonnaci Heap
*/
int FibonacciHeap::Fibonnaci_link(node* H1, node* y, node* z)
{
(y->left)->right = y->right;
(y->right)->left = y->left;
if (z->right == z)
H1 = z;
y->left = y;
y->right = y;
y->parent = z;
if (z->child == NULL)
z->child = y;
y->right = z->child;
y->left = (z->child)->left;
((z->child)->left)->right = y;
(z->child)->left = y;
if (y->n < (z->child)->n)
z->child = y;
z->degree++;
}
/*
* Union Nodes in Fibonnaci Heap
*/
node* FibonacciHeap::Union(node* H1, node* H2)
{
node* np;
node* H = InitializeHeap();
H = H1;
(H->left)->right = H2;
(H2->left)->right = H;
np = H->left;
H->left = H2->left;
H2->left = np;
return H;
}
/*
* Display Fibonnaci Heap
*/
int FibonacciHeap::Display(node* H)
{
node* p = H;
l O M oA R c P S D | 1 2 2 0 8 0 2 6
if (p == NULL)
{
cout<<"The Heap is Empty"<<endl;
return 0;
}
cout<<"The root nodes of Heap are: "<<endl;
do
{
cout<<p->n;
p = p->right;
if (p != H)
{
cout<<"-->";
}
}
while (p != H && p->right != NULL);
cout<<endl;
}
/*
* Extract Min Node in Fibonnaci Heap
*/
node* FibonacciHeap::Extract_Min(node* H1)
{
node* p;
node* ptr;
node* z = H1;
p = z;
ptr = z;
if (z == NULL)
return z;
node* x;
node* np;
x = NULL;
if (z->child != NULL)
x = z->child;
if (x != NULL)
{
ptr = x;
do
{
np = x->right;
(H1->left)->right = x;
x->right = H1;
x->left = H1->left;
H1->left = x;
if (x->n < H1->n)
H1 = x;
x->parent = NULL;
x = np;
}
l O M oA R c P S D | 1 2 2 0 8 0 2 6
}
A[d] = x;
x = x->right;
}
while (x != H1);
H = NULL;
for (int j = 0; j <= D; j++)
{
if (A[j] != NULL)
{
A[j]->left = A[j];
A[j]->right =A[j];
if (H != NULL)
{
(H->left)->right = A[j];
A[j]->right = H;
A[j]->left = H->left;
H->left = A[j];
if (A[j]->n < H->n)
H = A[j];
}
else
{
H = A[j];
}
if(H == NULL)
H = A[j];
else if (A[j]->n < H->n)
H = A[j];
}
}
}
/*
* Decrease key of Nodes in Fibonnaci Heap
*/
int FibonacciHeap::Decrease_key(node*H1, int x, int k)
{
node* y;
if (H1 == NULL)
{
cout<<"The Heap is Empty"<<endl;
return 0;
}
node* ptr = Find(H1, x);
if (ptr == NULL)
{
cout<<"Node not found in the Heap"<<endl;
return 1;
}
l O M oA R c P S D | 1 2 2 0 8 0 2 6
if (ptr->n < k)
{
cout<<"Entered key greater than current key"<<endl;
return 0;
}
ptr->n = k;
y = ptr->parent;
if (y != NULL && ptr->n < y->n)
{
Cut(H1, ptr, y);
Cascase_cut(H1, y);
}
if (ptr->n < H->n)
H = ptr;
return 0;
}
/*
* Cut Nodes in Fibonnaci Heap
*/
int FibonacciHeap::Cut(node* H1, node* x, node* y)
{
if (x == x->right)
y->child = NULL;
(x->left)->right = x->right;
(x->right)->left = x->left;
if (x == y->child)
y->child = x->right;
y->degree = y->degree - 1;
x->right = x;
x->left = x;
(H1->left)->right = x;
x->right = H1;
x->left = H1->left;
H1->left = x;
x->parent = NULL;
x->mark = 'F';
}
/*
* Cascade Cutting in Fibonnaci Heap
*/
int FibonacciHeap::Cascase_cut(node* H1, node* y)
{
node* z = y->parent;
if (z != NULL)
{
if (y->mark == 'F')
{
y->mark = 'T';
}
l O M oA R c P S D | 1 2 2 0 8 0 2 6
else
{
Cut(H1, y, z);
Cascase_cut(H1, z);
}
}
}
/*
* Find Nodes in Fibonnaci Heap
*/
node* FibonacciHeap::Find(node* H, int k)
{
node* x = H;
x->C = 'Y';
node* p = NULL;
if (x->n == k)
{
p = x;
x->C = 'N';
return p;
}
if (p == NULL)
{
if (x->child != NULL )
p = Find(x->child, k);
if ((x->right)->C != 'Y' )
p = Find(x->right, k);
}
x->C = 'N';
return p;
}
/*
* Delete Nodes in Fibonnaci Heap
*/
int FibonacciHeap::Delete_key(node* H1, int k)
{
node* np = NULL;
int t;
t = Decrease_key(H1, k, -5000);
if (!t)
np = Extract_Min(H);
if (np != NULL)
cout<<"Key Deleted"<<endl;
else
cout<<"Key not Deleted"<<endl;
return 0;
}
/*
* Main Contains Menu
l O M oA R c P S D | 1 2 2 0 8 0 2 6
*/
int main()
{
int n, m, l;
FibonacciHeap fh;
node* p;
node* H;
H = fh.InitializeHeap();
while (1)
{
cout<<" "<<endl;
cout<<"Operations on Binomial heap"<<endl;
cout<<" "<<endl;
cout<<"1)Insert Element in the heap"<<endl;
cout<<"2)Extract Minimum key node"<<endl;
cout<<"3)Decrease key of a node"<<endl;
cout<<"4)Delete a node"<<endl;
cout<<"5)Display Heap"<<endl;
cout<<"6)Exit"<<endl;
cout<<"Enter Your Choice: ";
cin>>l;
switch(l)
{
case 1:
cout<<"Enter the element to be inserted: ";
cin>>m;
p = fh.Create_node(m);
H = fh.Insert(H, p);
break;
case 2:
p = fh.Extract_Min(H);
if (p != NULL)
cout<<"The node with minimum key: "<<p->n<<endl;
else
cout<<"Heap is empty"<<endl;
break;
case 3:
cout<<"Enter the key to be decreased: ";
cin>>m;
cout<<"Enter new key value: ";
cin>>l;
fh.Decrease_key(H, m, l);
break;
case 4:
cout<<"Enter the key to be deleted: ";
cin>>m;
fh.Delete_key(H, m);
break;
case 5:
cout<<"The Heap is: "<<endl;
l O M oA R c P S D | 1 2 2 0 8 0 2 6
fh.Display(H);
break;
case 6:
exit(1);
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
}
OUTPUT:
RESULT:
Thus implementation Fibonacci heap using C++ Successfully completed.
l O M oA R c P S D | 1 2 2 0 8 0 2 6
DATE:
AIM:
ALGORITHM:
STEP 1: Insert the root node or starting node of a tree or a graph in the stack.
STEP2: Pop the top item from the stack and add it to the visited list.
STEP 3: Find all the adjacent nodes of the node marked visited and add the ones
that are not yet visited, to the stack.
STEP 4: Repeat steps 2 and 3 until the stack is empty
STEP5: End
SOURCE CODE:
#include <iostream>
#include <list>
#include <memory>
class Graph
{
int _V;
bool _directed;
std::unique_ptr< std::list<int> > adj;
public:
Graph(int V, bool directed);
void AddEdge(int v, int w);
void BreadthFirstSearch(int s);
};
if (!_directed)
{
l O M oA R c P S D | 1 2 2 0 8 0 2 6
adjacency[w].push_back(v);
}
}
void Graph::BreadthFirstSearch(int s)
{
bool *visited = new bool[_V];
for(int i = 0; i < _V; i++)
visited[i] = false;
std::list<int> queue;
visited[s] = true;
queue.push_back(s);
std::list<int>::iterator i;
while(!queue.empty())
{
s = queue.front();
std::cout << s << " ";
queue.pop_front();
int main()
{
Graph g(7, true);
g.AddEdge(0, 1);
g.AddEdge(0, 2);
g.AddEdge(0, 3);
g.AddEdge(1, 0);
g.AddEdge(1, 5);
g.AddEdge(2, 5);
g.AddEdge(3, 0);
g.AddEdge(3, 4);
g.AddEdge(4, 6);
g.AddEdge(5, 1);
ddEdge(6, 5);
readthFirstSearch(2);
return 0;
}
#include <iostream>
#include <list>
#include <memory>
class Graph
{
private:
int _V;
bool _directed;
std::unique_ptr< std::list<int> > adj;
void DFSUtil(int v, bool visited[]);
public:
Graph(int V, bool directed);
void AddEdge(int v, int w);
void DepthFirstSearch(int s);
};
if (!_directed)
{
adjacency[w].push_back(v);
}
}
std::list<int>::iterator i;
for (i = (adj.get())[v].begin(); i != (adj.get())[v].end(); ++i)
if (!visited[*i])
l O M oA R c P S D | 1 2 2 0 8 0 2 6
DFSUtil(*i, visited);
}
void Graph::DepthFirstSearch(int v)
{
std::unique_ptr<bool[]> visited(new bool[_V]);
DFSUtil(v, visited.get());
}
int main()
{
Graph g(7, true);
g.AddEdge(0, 1);
g.AddEdge(0, 2);
g.AddEdge(0, 3);
g.AddEdge(1, 0);
g.AddEdge(1, 5);
g.AddEdge(2, 5);
g.AddEdge(3, 0);
g.AddEdge(3, 4);
g.AddEdge(4, 6);
g.AddEdge(5, 1);
g.AddEdge(6, 5);
return 0;
}
OUTPUT:
Breadth First Traversal from vertex 2: 2 5 1 0 3 4 6
RESULT:
Thus implementation of Graph traversal using C++ Successfully completed.
l O M oA R c P S D | 1 2 2 0 8 0 2 6
DATE:
AIM:
ALGORITHM:
SOURCE CODE:
#include<bits/stdc++.h>
using namespace std;
<int, int> iPair;
struct Graph
{
int V, E;
vector< pair<int, iPair> > edges;
Graph(int V, int E)
{
this->V = V;
this->E = E;
}
int kruskalMST();
};
struct DisjointSets
{
int *parent, *rnk;
int n;
DisjointSets(int n)
{
l O M oA R c P S D | 1 2 2 0 8 0 2 6
this->n = n;
parent = new int[n+1];
rnk = new int[n+1];
for (int i = 0; i <= n; i++)
{
rnk[i] = 0;
parent[i] = i;
}
}
int find(int u)
{
if (u != parent[u])
parent[u] = find(parent[u]);
return parent[u];
}
if (rnk[x] == rnk[y])
rnk[y]++;
}
};
int Graph::kruskalMST()
{
int mst_wt = 0; // Initialize result
sort(edges.begin(), edges.end());
DisjointSets ds(V);
if (set_u != set_v)
l O M oA R c P S D | 1 2 2 0 8 0 2 6
{
cout << u << " - " << v << endl;
mst_wt += it->first;
ds.merge(set_u, set_v);
}
}
return mst_wt;
}
int main()
{
int V = 9, E = 14;
Graph g(V, E);
g.addEdge(0, 1, 4);
g.addEdge(0, 7, 8);
g.addEdge(1, 2, 8);
g.addEdge(1, 7, 11);
g.addEdge(2, 3, 7);
g.addEdge(2, 8, 2);
g.addEdge(2, 5, 4);
g.addEdge(3, 4, 9);
g.addEdge(3, 5, 14);
g.addEdge(4, 5, 10);
g.addEdge(5, 6, 2);
g.addEdge(6, 7, 1);
g.addEdge(6, 8, 6);
g.addEdge(7, 8, 7);
return 0;
}
l O M oA R c P S D | 1 2 2 0 8 0 2 6
OUTPUT:
Edges of MST are
6-7
2-8
5-6
0-1
2-5
2-3
0-7
3-4
Weight of MST is 37
RESULT:
Thus implementation of Spanning Tree C++ Successfully completed.
l O M oA R c P S D | 1 2 2 0 8 0 2 6
AIM:
ALGORITHM:
STEP1: Initialize distances from the source to all vertices as infinite and
distance to source itself as 0.
STEP2: for each edge a->b of the graph
STEP3: If dis[b] > dis[a] + weight of edge (a->b) then
STEP4: dis[b] = dis[a] + weight of edge (a->b)
STEP5: repeat step 2 for nv-1 times (nv is no. of vertices).
STEP6: If dis[b] > dis[a] + weight of edge (a->b) then
STEP7: report a negative cycle in the graph.
SOURCE CODE:
#include<iostream>
#include<climits>
using namespace std;
for(int k=0;k<6;k++)
{
if(Tset[k]==false && distance[k]<=minimum)
{
minimum=distance[k];
ind=k;
}
}
return ind;
}
int main()
{
int graph[6][6]={
{0, 1, 2, 0, 0, 0},
{1, 0, 0, 5, 1, 0},
{2, 0, 0, 2, 3, 0},
{0, 5, 2, 0, 2, 2},
{0, 1, 3, 2, 0, 1},
{0, 0, 0, 2, 1, 0}};
DijkstraAlgo(graph,0);
return 0;
}
OUTPUT :
Vertex Distance from source
A 0
B 1
C 2
D 4
E 2
F 3
l O M oA R c P S D | 1 2 2 0 8 0 2 6
SOURCE CODE:
#include<iostream>
#define MAX 10
using namespace std;
typedef struct edge
{
int src;
int dest;
int wt;
}edge;
void bellman_ford(int nv,edge e[],int src_graph,int ne)
{
int u,v,weight,i,j=0;
int dis[MAX];
for(i=0;i<nv;i++)
{
dis[i]=999;
}
dis[src_graph]=0;
for(i=0;i<nv-1;i++)
{
for(j=0;j<ne;j++)
{
u=e[j].src;
v=e[j].dest;
weight=e[j].wt;
for(j=0;j<ne;j++)
{
u=e[j].src;
v=e[j].dest;
weight=e[j].wt;
for(int i=0;i<ne;i++)
{
cout<<"\nFor edge "<<i+1<<"=>";
cout<<"\nEnter source vertex :";
cin>>e[i].src;
cout<<"Enter destination vertex :";
cin>>e[i].dest;
cout<<"Enter weight :";
cin>>e[i].wt;
}
bellman_ford(nv,e,src_graph,ne);
return 0;
}
return 0;
}
l O M oA R c P S D | 1 2 2 0 8 0 2 6
OUTPUT:
RESULT:
DATE:
AIM:
ALGORITHM:
PROGRAM:
#include<iostream>
#include<limits.h>
return m[1][n-1];
l O M oA R c P S D | 1 2 2 0 8 0 2 6
int main()
{
int n,i;
cout<<"Enter number of matrices\n";
cin>>n;
n++;
int arr[n];
for(i=0;i<n;i++)
{
cout<<"Enter d"<<i<<" :: ";
cin>>arr[i];
}
return 0;
}
OUTPUT:
Enter number of matrices
4
Enter dimensions
Enter d0 :: 10
Enter d1 :: 100
Enter d2 :: 20
Enter d3 :: 5
Enter d4 :: 80
Minimum number of multiplications is 19000
RESULT:
DATE:
AIM:
ALGORITHM:
Step 1: Sort the given activities in ascending order according to their finishing time.
Step 2: Select the first activity from sorted array act[] and add it to sol[] array.
Step 4: If the start time of the currently selected activity is greater than or equal to the
finish time of previously selected activity, then add it to the sol[] array.
PROGRAM:
#include <bits/stdc++.h>
int i = 0;
cout<< "(" <<arr[i].start<< ", " <<arr[i].finish << ")\n";
l O M oA R c P S D | 1 2 2 0 8 0 2 6
int main()
{
Activity arr[N];
for(int i=0; i<=N-1; i++)
{
cout<<"Enter the start and end time of "<<i+1<<" activity \n";
cin>>arr[i].start>>arr[i].finish;
}
print_Max_Activities(arr, N);
return 0;
}
OUTPUT:
Enter the start and end time of 1 activity5
9
Enter the start and end time of 1 activity1
2
Enter the start and end time of 1 activity3
4
Enter the start and end time of 1 activity0
6
Enter the start and end time of 1 activity5
7
Enter the start and end time of 1 activity8
9
Following activity are selected
(1,2)
(3,4)
(5,7)
(8,9)
l O M oA R c P S D | 1 2 2 0 8 0 2 6
RESULT:
Thus implementation of Activity Selection And Huffman Coding using C++
Successfully completed.