Академический Документы
Профессиональный Документы
Культура Документы
if(a[i].k==key)
{
printf("Value found at the index : %d value is : %d\n",h,a[h].v);
return 1;
}
}
for(j=0;j<h;j++)
{
if(a[j].k==key)
{
printf("Key found at the index : %d value is : %d\n",h,a[h].v);
return 1;
}
}
return -1;
}
void display()
{
int i;
printf(" Dictionary\n");
printf("---------------\n");
printf(" key | value \n");
for(i=0;i<10;i++)
{
printf("%3d | %5d\n",a[i].k,a[i].v);
}
}
int main()
{
int c,i,key,value,check;
for(i=0;i<10;i++)
{
a[i].k=-1;
a[i].v=-1;
}
while(1)
{
printf("1.Insert\n2.Delete\n3.Search\n4.Display\n5.Exit\n);
printf("Enter your choice : ");
scanf("%d",&c);
switch(c)
{
Page 3 of 70
OUTPUT :
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice : 1
Enter key : 45
Page 4 of 70
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice : 1
Enter key : 98
Enter value : 800
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice : 4
DICTIONARY
-------------------------Key
| value
-1
|
-1
-1
|
-1
-1
|
-1
-1
|
-1
-1
|
-1
45
|
500
-1
|
-1
-1
|
-1
98
|
800
-1
|
-1
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice : 3
Enter key : 45
Key found at the index 5 and value is : 500
Page 5 of 70
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice : 2
Enter key : 45
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice : 5
Page 6 of 70
i++;
}
if(temp==NULL)
{
printf("Key not Found...\n");
}
else
{
if(temp==first)
{
first=first->next;
printf("Key found at position : %d and successfully
deleted from Dictionary \n",i);
}
else
{
prev->next=temp->next;
printf("Key found at position : %d\n",i);
free(temp);
}
}
}
void search(int key)
{
int count=0;
temp=first;
while(temp!=NULL)
{
if(temp->k==key)
{
count++;
}
temp=temp->next;
}
if(count==1)
{
printf("Key Element found the Dictionary...\n");
}
else
{
printf("Key Not found...\n");
}
}
Page 8 of 70
void display()
{
int i=0;
temp=first;
printf("DICTIONARY\n");
printf("Position \t Key \t Value \n");
printf("--------------------------------\n");
while(temp!=NULL)
{
printf("%4d %14d %8d \n",i,temp->k,temp->v);
temp=temp->next;
i++;
}
}
main()
{
int c,key,value;
while(1)
{
printf("1.Insert\n2.Delete\n3.Search\n4.Display\n5.Exit\n);
printf("Enter your choice : ");
scanf("%d",&c);
switch(c)
{
case 1 : printf("Enter key :");
scanf("%d",&key);
printf("Enter value :");
scanf("%d",&value);
insert(key,value);
break;
case 2 : printf("Enter key :");
scanf("%d",&key);
delete(key);
break;
case 3 : printf("Enter key :");
scanf("%d",&key);
search(key);
break;
case 4 : display();
break;
case 5 : return 0;
}
Page 9 of 70
}
}
OUTPUT :
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice : 1
Enter key : 45
Enter value : 500
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice : 1
Enter key : 98
Enter value : 800
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice : 4
DICTIONARY
Position
Key
value
----------------------------------0
45
500
1
98
800
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice : 3
Enter key : 45
Key element found in the Dictionary.
Page 10 of 70
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice : 2
Enter key : 45
Key found at position 1 and successfully deleted from Dictionary
6. Insert
7. Delete
8. Search
9. Display
10. Exit
Enter your choice : 5
Page 11 of 70
return rotate_LL(parent);
}
int get_height(avlnode *node)
{
int height=0;
if(node!=NULL)
height=1+max(get_height(node->left),get_height(node->right));
return height;
}
int get_balance(avlnode *node)
{
if(node==NULL) return 0;
return get_height(node->left)-get_height(node->right);
}
avlnode* balance_tree(avlnode **node)
{
int bal_factor= get_balance(*node);
if(bal_factor>1)
{
if(get_balance((*node)->left) > 0)
*node=rotate_LL(*node);
else
*node=rotate_LR(*node);
}
else if(bal_factor<-1)
{
if(get_balance((*node)->right) < 0)
*node = rotate_RR(*node);
else
*node = rotate_RL(*node);
}
return *node;
}
avlnode* insert(avlnode **root,int key)
{
if(*root==NULL)
{
*root=(avlnode*)malloc(sizeof(avlnode));
(*root)->data = key;
(*root)->left=(*root)->right=NULL;
}
Page 13 of 70
else if(key<(*root)->data)
{
(*root)->left=insert(&((*root)->left),key);
(*root)=balance_tree(root);
}
else if(key>(*root)->data)
{
(*root)->right=insert(&((*root)->right),key);
(*root)=balance_tree(root);
}
return *root;
}
avlnode* search(avlnode *node, int key)
{
if(node==NULL) return NULL;
printf("%d->",node->data);
if(key==node->data)
return node;
else if(key<node->data)
search(node->left,key);
else
search(node->right,key);
}
void display(avlnode *root)
{
if(root==NULL)
return;
printf(" %d",root->data);
display(root->left);
display(root->right);
}
main()
{
int ch,x;
while(1)
{
printf("\n1.INSERT\n2.SEARCH\n3.DISPLAY\n4.EXIT");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
Page 14 of 70
case 1 :
case 2 :
case 3:
case 4:
}
}
}
OUTPUT :
1. INSERT
2. SEARCH
3. DISPLAY
4. EXIT
Enter Your choice : 1
Enter a key to insert : 3
1. INSERT
2. SEARCH
3. DISPLAY
4. EXIT
Enter Your choice : 1
Enter a key to insert : 2
1. INSERT
2. SEARCH
3. DISPLAY
4. EXIT
Enter Your choice : 1
Enter a key to insert : 4
1. INSERT
2. SEARCH
3. DISPLAY
4. EXIT
Enter Your choice : 3
3
2
4
Page 15 of 70
1. INSERT
2. SEARCH
3. DISPLAY
4. EXIT
Enter Your choice : 1
Enter a key to insert : 5
1. INSERT
2. SEARCH
3. DISPLAY
4. EXIT
Enter Your choice : 1
Enter a key to insert : 6
1. INSERT
2. SEARCH
3. DISPLAY
4. EXIT
Enter Your choice : 3
3
2
5
4
1. INSERT
2. SEARCH
3. DISPLAY
4. EXIT
Enter Your choice : 2
Enter the key to search : 4
354
1. INSERT
2. SEARCH
3. DISPLAY
4. EXIT
Enter Your choice : 4
Page 16 of 70
#include<iostream.h>
#include<fstream.h>
using namespace std;
// TwoThreeNode class
class TwoThreeNode {
private:
// Gets the value of the smallest data item in the subtree
// rooted by this node
int getSmallest() {
TwoThreeNode *node = this;
while (!node->isLeaf()) node = node->child[0];
return node->key[0];
}
// Insert into a node with 1 child
void insert1Siblings(TwoThreeNode *newChild, int newSmallest) {
int newKey = newChild->key[0];
newChild->parent = this;
if (newKey < child[0]->key[0]) {
// newNode is inserted as first child of root
child[1] = child[0];
child[0] = newChild;
key[0] = child[1]->getSmallest();
}
else {
// newNode is iserted as second child of root
child[1] = newChild;
key[0] = newSmallest;
}
}
// Insert into a node with 2 children
void insert2Siblings(TwoThreeNode *newChild, int newSmallest) {
int newKey = newChild->key[0];
newChild->parent = this;
if (newKey < child[0]->key[0]) {
Page 17 of 70
child[2] = child[1];
child[1] = child[0];
child[0] = newChild;
key[1] = key[0];
key[0] = child[1]->getSmallest();
updateParentSmallest(newSmallest);
}
else if (newKey < child[1]->key[0]) {
child[2] = child[1];
child[1] = newChild;
key[1] = key[0];
key[0] = newSmallest;
}
else {
child[2] = newChild;
key[1] = newSmallest;
}
}
// Insert into a node with 3 children
void insert3Siblings(TwoThreeNode *newChild, int newSmallest) {
int newKey = newChild->key[0];
int splitSmallest = -1;
TwoThreeNode *splitNode = new TwoThreeNode();
splitNode->parent = parent;
if (newKey < child[0]->key[0] || newKey < child[1]->key[0]) {
// newChild is inserted in current node
splitSmallest = key[0];
splitNode->child[0] = child[1];
splitNode->child[1] = child[2];
splitNode->key[0] = key[1];
child[1]->parent = splitNode;
child[2]->parent = splitNode;
newChild->parent = this;
if (newKey < child[0]->key[0]) {
// newChild is inserted as first child
child[1] = child[0];
Page 18 of 70
child[0] = newChild;
key[0] = child[1]->getSmallest();
updateParentSmallest(newSmallest);
}
else {
// newChild is inserted as second child
child[1] = newChild;
key[0] = newSmallest;
}
}
else {
// newChild is inserted in split node
child[2]->parent = splitNode;
newChild->parent = splitNode;
if (newKey < child[2]->key[0]) {
// newChild is inserted as first child
splitSmallest = newSmallest;
splitNode->child[0] = newChild;
splitNode->child[1] = child[2];
splitNode->key[0] = key[1];
}
else {
// newChild is inserted as second child
splitSmallest = key[1];
splitNode->child[0] = child[2];
splitNode->child[1] = newChild;
splitNode->key[0] = newSmallest;
}
}
child[2] = NULL;
key[1] = -1;
if (parent->parent == NULL) {
// At root, so new root needs to be created
TwoThreeNode *newNode = new TwoThreeNode();
parent->child[0] = newNode;
newNode->parent = parent;
newNode->child[0] = this;
parent = newNode;
Page 19 of 70
}
parent->insert(splitNode, splitSmallest);
}
// Update the parent nods efor the smallest child value
void updateParentSmallest(int data) {
switch (sibNumber()) {
case 0: if (parent->parent != NULL) parent>updateParentSmallest(data); break;
case 1: parent->key[0] = data; break;
case 2: parent->key[1] = data; break;
}
}
public:
int key[2];
TwoThreeNode *parent, *child[3];
// Constructor
TwoThreeNode(int data = -1) {
key[0] = data;
key[1] = -1;
parent = child[0] = child[1] = child[2] = NULL;
}
// Check if node is a leaf
bool isLeaf() {
return (child[0] == NULL);
}
// Get which sibling the node is
int sibNumber() {
for (int i = 0; i < 3; ++i) {
if (this == parent->child[i]) return i;
}
return -1;
}
// Insertion
void insert(TwoThreeNode *newChild, int newSmallest) {
if (child[1] == NULL) insert1Siblings(newChild, newSmallest);
else if (child[2] == NULL) insert2Siblings(newChild, newSmallest);
else insert3Siblings(newChild, newSmallest);
Page 20 of 70
}
};
// TwoThreeTree class
class TwoThreeTree {
private:
TwoThreeNode *root;
// Find the appropriate operation point
TwoThreeNode* findSpot(TwoThreeNode *node, int data) {
if (node == NULL) return NULL;
while (!node->isLeaf()) {
if (node->key[0] == data || node->key[1] == data)
return NULL;
if (node->key[0] == -1 || data < node->key[0])
node = node->child[0];
else if (node->key[1] == -1 || data < node->key[1])
node = node->child[1];
else
node = node->child[2];
}
if (node->key[0] == data) return NULL;
return node->parent;
}
// Recursively print the subtree starting from the given node
void print(TwoThreeNode *node, int tabs = 0) {
for (int i = 0; i < tabs; ++i) {
cout << "\t";
}
if (node == NULL) {
cout << "`--> NULL" << endl;
return;
}
cout << "`--> " << node->sibNumber()
<< ": ( " << node->key[0] << ", " << node->key[1] << ")" << endl;
if (!node->isLeaf()) {
++tabs;
print(node->child[0], tabs);
Page 21 of 70
print(node->child[1], tabs);
print(node->child[2], tabs);
}
}
public:
// Constructor
TwoThreeTree() {
root = new TwoThreeNode();
root->child[0] = new TwoThreeNode();
root->child[0]->parent = root;
}
// Insert
bool insert(int data) {
TwoThreeNode *newNode = new TwoThreeNode(data);
TwoThreeNode *spot = root->child[0];
if (spot->child[0] == NULL) {
// First insertion
newNode->parent = spot;
spot->child[0] = newNode;
}
else {
spot = findSpot(spot, data);
if (spot == NULL) return false;
spot->insert(new TwoThreeNode(data), data);
}
return true;
}
// Print
void print() {
print(root->child[0]);
cout << endl;
}
};
// Main function
int main(int argc, char **argv) {
if (argc <= 1) {
cout << argv[0] << ": too few arguments" << endl;
Page 22 of 70
cout << "Usage: " << argv[0] << " filename" << endl;
exit(1);
}
// Open file
ifstream infile;
infile.open(argv[1]);
// Create tree and insert data
TwoThreeTree ttTree;
int x;
while (infile.good()) {
infile >> x;
if (!infile.eof()) {
cout << x << endl;
ttTree.insert(x);
ttTree.print();
}
}
infile.close();
return 0;
}
Page 23 of 70
}
heap[hole]=min;
}
void display()
{
int i;
if(size==0)
printf("Heap is Empty...\n");
else
{
printf("BINARY HEAP\n");
for(i=0;i<=size;i++)
{
printf("%5d",heap[i]);
}
printf("\n");
}
}
OUTPUT :
1. Insert
2. Delete Min
3. Display
4. Exit
Enter Your Choice : 1
Enter element to be inserted : 44
1. Insert
2. Delete Min
3. Display
4. Exit
Enter Your Choice : 1
Enter element to be inserted : 54
1. Insert
2. Delete Min
3. Display
4. Exit
Enter Your Choice : 1
Enter element to be inserted : 64
Page 26 of 70
1. Insert
2. Delete Min
3. Display
4. Exit
Enter Your Choice : 3
BINARY HEAP
0
44
54
64
1. Insert
2. Delete Min
3. Display
4. Exit
Enter Your Choice : 2
Min Element 44 is Deleted successfully...
1. Insert
2. Delete Min
3. Display
4. Exit
Enter Your Choice : 3
BINARY HEAP
0
54
64
1. Insert
2. Delete Min
3. Display
4. Exit
Enter Your Choice : 4
Page 27 of 70
case 3:
case 4:
case 5:
case 6:
}
}
}
insert_vertex( char x)
{
struct vertex *temp,*newnode;
newnode=(struct vertex *) malloc( sizeof( struct vertex));
newnode->data=x;
newnode->next = NULL;
newnode->adj=NULL;
if(first==NULL)
{
first=newnode;
return;
}
temp=first;
while(temp->next!=NULL )
temp=temp->next;
temp->next =newnode;
}
delete_vertex( char x)
{
struct vertex *temp,*ptr;
if (first->data==x)
{
temp=first;
Page 29 of 70
first=first->next;
free(temp);
return;
}
temp=first;
while(temp->next->next!=NULL)
{
if(temp->next->data==x)
{
ptr=temp->next;
temp->next=ptr->next;
free(ptr);
return;
}
temp=temp->next;
}
if ( temp->next->data==x)
{
ptr= temp->next;
free(ptr);
temp->next=NULL;
}
}
delvertex_edge(char x)
{
struct vertex *temp;
struct edge *ptr,*temp1;
temp=first;
while (temp!=NULL )
{
if ( temp->adj->dest==x)
{
temp1=temp->adj;
temp->adj=temp->adj->link;
free(temp1);
}
temp1=temp->adj;
while(temp1->link->link!=NULL )
{
if (temp1->link->dest==x
{
ptr=temp1->link;
Page 30 of 70
temp1->link=ptr->link;
free(ptr);
}
temp1=temp1->link;
}
if (temp1->link->dest==x)
{
ptr=temp1->link;
free(ptr);
temp1->link=NULL;
}
temp=temp->next;
}
}
insert_edge(char s, char d)
{
struct vertex *locs, *locd;
struct edge *newnode,*temp;
locs=find_vertex(s);
newnode=(struct edge *)malloc(sizeof( struct edge));
newnode->dest=d;
newnode->link=NULL;
if(locs->adj==NULL)
{
locs->adj=newnode;
return;
}
temp=locs->adj;
while(temp->link!=NULL)
temp=temp->link;
temp->link=newnode;
}
struct vertex *find_vertex( char x)
{
struct vertex *temp,*loc=NULL;
temp=first;
while (temp!=NULL )
{
if(temp->data==x)
{
loc=temp;
return loc;
Page 31 of 70
}
else
temp=temp->next;
}
return loc;
}
display()
{
struct node *temp;
struct edge *temp1;
temp=first;
while(temp!=NULL )
{
printf( "%c ->",temp->data);
temp1=temp->adj;
while (temp1!=NULL)
{
printf( "%c->",temp1->dest);
temp1=temp1->link;
}
printf( "\n" );
temp=temp->next;
}
}
OUTPUT :
1. Insert Vertex
2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice : 1
Enter a vertex to be inserted : A
1. Insert Vertex
2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice : 1
Enter a vertex to be inserted : B
Page 32 of 70
1. Insert Vertex
2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice : 1
Enter a vertex to be inserted : C
1. Insert Vertex
2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice : 1
Enter a vertex to be inserted : D
1. Insert Vertex
2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice : 2
Enter an edge to be inserted : A B
1. Insert Vertex
2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice: 2
Enter an edge to be inserted: A C
1. Insert Vertex
2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice: 2
Enter an edge to be inserted : B D
Page 33 of 70
1. Insert Vertex
2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice: 2
Enter an edge to be inserted : C D
1. Insert Vertex
2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice: 5
ABC
BD
CD
D
1. Insert Vertex
2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice: 3
Enter a vertex to be deleted : A
BD
CD
D
1. Insert Vertex
2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice: 4
Enter a vertex to Find : D
Vertex Found.
Page 34 of 70
case 3:
case 4:
case 5:
case 6:
}
break;
printf( "Enter an edge to be deleted : " );
scanf( " %c %c", &origin, &destin );
delete_edge( origin, destin );
break;
printf( "Enter a vertex to find:" );
scanf( " %c",&x);
p=find_vertex(x);
if(p==NULL)
printf("\n vertex not found\n");
else
printf("\n vertex %c found\n",p->data);
break;
display();
break;
exit();
}
}
insert_vertex( char x)
{
struct vertex *temp,*newnode;
newnode=(struct vertex *) malloc( sizeof( struct vertex));
newnode->data=x;
newnode->next = NULL;
newnode->adj=NULL;
if(first==NULL)
{
first=newnode;
return;
}
temp=first;
while(temp->next!=NULL )
temp=temp->next;
temp->next =newnode;
}
insert_edge(char s, char d)
{
struct vertex *locs, *locd;
struct edge *newnode,*temp;
locs=find_vertex(s);
newnode=(struct edge *)malloc(sizeof( struct edge));
Page 36 of 70
newnode->dest=d;
newnode->link=NULL;
if(locs->adj==NULL)
{
locs->adj=newnode;
return;
}
temp=locs->adj;
while(temp->link!=NULL)
temp=temp->link;
temp->link=newnode;
}
struct vertex *find_vertex( char x)
{
struct vertex *temp,*loc=NULL;
temp=first;
while (temp!=NULL )
{
if(temp->data==x)
{
loc=temp;
return loc;
}
else
temp=temp->next;
}
return loc;
}
delete_edge(char s, char d)
{
struct vertex *locs,*locd;
struct edge *ptr,*temp;
locs=find_vertex(s);
if (locs->adj->dest==d)
{
temp=locs->adj;
locs->adj=locs->adj->link;
free(temp);
return ;
}
temp=locs->adj;
while (temp->link->link!=NULL )
Page 37 of 70
{
if (temp->link->dest==d)
{
ptr=temp->link;
temp->link =ptr->link;
free(temp);
return;
}
temp=temp->link;
}
if ( temp->link->dest==d)
{
ptr=temp->link;
free(ptr);
temp->link=NULL;
return;
}
}
display()
{
struct node *temp;
struct edge *temp1;
temp=first;
while(temp!=NULL )
{
printf( "%c ->",temp->data);
temp1=temp->adj;
while (temp1!=NULL)
{
printf( "%c->",temp1->dest);
temp1=temp1->link;
}
printf( "\n" );
temp=temp->next;
}
}
Page 38 of 70
OUTPUT :
1. Insert Vertex
2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice : 1
Enter a vertex to be inserted : A
1. Insert Vertex
2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice : 1
Enter a vertex to be inserted : B
1. Insert Vertex
2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice : 1
Enter a vertex to be inserted : C
1. Insert Vertex
2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice : 1
Enter a vertex to be inserted : D
1. Insert Vertex
2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
Page 39 of 70
6. Exit
Enter your choice : 2
Enter an edge to be inserted : A B
1. Insert Vertex
2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice: 2
Enter an edge to be inserted: A C
1. Insert Vertex
2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice: 2
Enter an edge to be inserted : B D
1. Insert Vertex
2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice: 2
Enter an edge to be inserted : C D
1. Insert Vertex
2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice: 5
ABC
BD
CD
D
Page 40 of 70
1. Insert Vertex
2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice: 3
Enter an edge to be deleted : A C
1. Insert Vertex
2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice: 5
AB
BD
CD
D
1. Insert Vertex
2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice: 6
Page 41 of 70
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%3d",a[i][j]);
}
printf("\n");
}
for(i=1;i<=n;i++)
visit[i]=0;
printf("Enter Source Vertex : ");
scanf("%d",&s);
printf("The DFS of Graph is :\n");
push(s);
visit[s]=1;
k=pop();
if(k!=0)
printf("%d ->",k);
while(k!=0)
{
for(i=1;i<=n;i++)
if((a[k][i]!=0)&&(visit[i]==0))
{
push(i);
visit[i]=1;
}
k=pop();
if(k!=0)
printf("%d ->",k);
}
}
OUTPUT :
Enter no of Vertives : 2
Enter edge (1,1) : 0
Enter edge (1,2) : 1
Enter edge (2,1) : 1
Enter edge (2,2) : 0
The Adjacency Matrix is :
0 1
1 0
Enter the Source Vertex : 1
Page 43 of 70
printf("%d",k);
while(k!=0)
{
for(i=1;i<=n;i++)
if((a[k][i]!=0)&&(visit[i]==0))
{
insert(i);
visit[i]=1;
}
k=delete();
if(k!=0)
printf("->%d",k);
}
printf("\n");
}
void insert(int item)
{
if(front==19)
printf("Queue is full...\n");
else
queue[++rear]=item;
}
int delete()
{
int k;
if(front==rear)
return (0);
else
{
k=queue[++front];
return(k);
}
}
OUTPUT :
Enter the number of the vertices : 3
Enter the (1,1):0
Enter the (1,2):1
Enter the (1,3):1
Enter the (2,1):0
Enter the (2,2):0
Page 45 of 70
Page 46 of 70
AIM : Program to find cost of Minimal Spanning tree using Prims algorithm
PROGRAM:
#include<stdio.h>
main()
{
int u,v,n,i,j,ne=1;
int visited[10]={0},min,mincost=0,cost[10][10];
printf("\n Enter the number of nodes:");
scanf("%d",&n);
printf("\n Enter the adjacency matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
printf("\n enter edge(%d,%d)cost:",i,j);
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
visited[1]=1;
printf("\n Minimum Spanning Tree");
while(ne<n)
{
for(i=1,min=999;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(cost[i][j]<min &&visited[i]!=0)
{
min=cost[i][j];
u=i;
v=j;
}
}
}
if(visited[u]==0||visited[v]==0)
{
printf("\n Edge %d:(%d,%d) cost:%d",ne++,u,v,min);
mincost+=min;
visited[v]=1;
Page 47 of 70
}
cost[u][v]=cost[v][u]=999;
}
printf("\nCost of Minimum Spanning Tree=%d",mincost);
}
OUT PUT:
Enter no of vertices : 4
Enter the cost of edge (1,1) : 0
Enter the cost of edge (1,2) : 2
Enter the cost of edge (1,3) : 4
Enter the cost of edge (1,4) : 0
Enter the cost of edge (2,1) : 2
Enter the cost of edge (2,2) : 0
Enter the cost of edge (2,3) : 0
Enter the cost of edge (2,4) : 5
Enter the cost of edge (3,1) : 4
Enter the cost of edge (3,2) : 0
Enter the cost of edge (3,3) : 0
Enter the cost of edge (3,4) : 10
Enter the cost of edge (4,1) : 0
Enter the cost of edge (4,2) : 5
Enter the cost of edge (4,3) : 10
Enter the cost of edge (4,4) : 0
The minimal Spanning tree is
The edge 1 (1,2) cost : 2
The edge 2 (1,3) cost : 4
The edge 3 (2,4) cost : 5
Cost of Minimal Spanning tree is : 11
Page 48 of 70
cost[u][v]=cost[v][u]=999;
}
printf("\n minimum cost=%d",mincost);
}
int find(int i)
{
while(parent[i])
i=parent[i];
return i;
}
int uni(int i,int j)
{
if(i!=j)
{
parent[j]=i;
return i;
}
return 0;
}
output
Enter The no.of Nodes:5
Enter The weight Matrix:
Enter Edge(1,1)cost:0
Enter Edge(1,2)cost:3
Enter Edge(1,3)cost:6
Enter Edge(1,4)cost:1
Enter Edge(1,5)cost:0
Enter Edge(2,1)cost:3
Enter Edge(2,2)cost:0
Enter Edge(2,3)cost:7
Enter Edge(2,4)cost:0
Page 50 of 70
Enter Edge(2,5)cost:4
Enter Edge(3,1)cost:6
Enter Edge(3,2)cost:7
Enter Edge(3,3)cost:0
Enter Edge(3,4)cost:2
Enter Edge(3,5)cost:1
Enter Edge(4,1)cost:1
Enter Edge(4,2)cost:0
Enter Edge(4,3)cost:2
Enter Edge(4,4)cost:0
Enter Edge(4,5)cost:2
Enter Edge(5,1)cost:0
Enter Edge(5,2)cost:4
Enter Edge(5,3)cost:1
Enter Edge(5,4)cost:2
Enter Edge(5,5)cost:0
edge 1:(14)cost:1
edge 2:(53)cost:1
edge 3:(34)cost:2
edge 4:(12)cost:3
minimum cost=7
Page 51 of 70
dij(n,v,cost,dist);
printf("\n Shortest path:\n");
for(i=1;i<=n;i++)
if(i!=v)
printf("%d->%d,cost=%d\n",v,i,dist[i]);
getch();
}
OUTPUT:
Enter the number of nodes:3
Enter the cost matrix:
126
201
798
Enter the source matrix:1
Shortest path:
1->2,cost=2
1->3,cost=3
Page 53 of 70
if (strLength == 0)
return NULL;
search = string;
i = --strLength;
do
Page 54 of 70
{
skipTable[*search++] = i;
} while (i--);
lastChar = *--search;
skip = skipTable[*search];
search += skip;
dataLength -= skip;
skip = skipTable[*search];
search += skip;
dataLength -= skip;
skip = skipTable[*search];
search += skip;
dataLength -= skip;
continue;
Page 55 of 70
i = strLength;
do
{
if (i-- == 0)
return search;
} while (*--search == string[i]);
return NULL;
}
int main(void)
{
Page 56 of 70
return 0;
}
OUTPUT:
Marabou t9034u5t09t8493t43vkdsropgb
Page 57 of 70
/*
* finds the position of the pattern in the given target string
* target - str, patter - word
*/
j = ptr[j];
} else {
j = 0;
}
}
}
}
ptr[0] = -1;
ptr[1] = 0;
return;
}
int main() {
return 0;
Page 60 of 70
}
Output:
Enter your target string:aditya engineering college
Enter your pattern string:aditya
aditya located at the index 1
Page 61 of 70
Additional Programs:
int main()
{
int i,j;
clrscr();
printf("Enter no.of nodes in the graph\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the name of vertex %d\n",i+1);
scanf(" %c",&vertex[i]);
}
printf("If the if the vertices are adjacent enter edge lenght else 0\n");
for(i=0;i<n;i++)
Page 62 of 70
{
for(j=0;j<n;j++)
{
printf("%c-%c=",vertex[i],vertex[j]);
scanf("%d",&a[i][j]);
if(a[i][j]==0)
a[i][j]=9999;
printf("\n");
}
}
Floyd();
return(0);
}
void Floyd()
{
int i,j,k;
Page 63 of 70
for(j=0;j<n;j++)
{
a[i][j]=Min(a[i][j],(a[i][k]+a[k][j]));
}
}
}
Page 64 of 70
OUTPUT:
Enter no.of nodes in the graph
3
Enter the name of vertex 1
a
Enter the name of vertex 2
b
Enter the name of vertex 3
c
If the if the vertices are adjacent enter edge lenght else 0
a-a=0
a-b=12
a-c=34
b-a=87
Page 65 of 70
b-b=0
b-c=2
c-a=0
c-b=5
c-c=0
99
12
14
87
92
Page 66 of 70
#include<stdio.h>
int a[10][10],n;
char vertex[10];
void warshall();
int main()
{
int i,j;
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the name of vertex %d\n",i+1);
scanf(" %c",&vertex[i]);
}
printf("If the vertices are adjacent enter 1 else 0\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
Page 67 of 70
{
printf("%c-%c=",vertex[i],vertex[j]);
scanf("%d",&a[i][j]);
printf("\n");
}
}
warshall();
return(0);
}
void warshall()
{
int i,j,k;
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=a[i][j]|(a[i][k]&a[k][j]);
}
}
}
for(i=0;i<n;i++)
printf("%3c",vertex[i]);
printf("\n");
for(i=0;i<n;i++)
{
printf("%c",vertex[i]);
for(j=0;j<n;j++)
{
printf("%3d",a[i][j]);
}
printf("\n");
}
return;
}
output
Enter no.of nodes in the graph
3
Enter the name of vertex 1
A
Enter the name of vertex 2
B
Enter the name of vertex 3
C
If the vertices are adjacent enter 1 else 0
A-A=0
Page 69 of 70
A-B=1
A-C=1
B-A=0
B-B=0
B-C=1
C-A=0
C-B=1
C-C=0
Path matrix is
A B C
A 0 1 1
B 0 1 1
C 0 1 1
Page 70 of 70