Вы находитесь на странице: 1из 70

ADVANCED DATA STRUCTURES LAB MANUAL

1) Program to implement functions of Dictionary using Hashing.


AIM : Program to implement functions of Dictionary using Hashing.
PROGRAM :
#include<stdio.h>
#include<stdlib.h>
struct dict
{
int k;
int v;
}a[10];
int insert(int key,int value)
{
int h=0,i=0,j;
h=key%10;
if(a[h].k==-1)
{
a[h].k=key;
a[h].v=value;
return 1;
}
for(i=h+1;i<10;i++)
{
if(a[i].k==-1)
{
a[i].k=key;
a[i].v=value;
return 1;
}
}
for(j=0;j<h;j++)
{
if(a[j].k==-1)
{
a[j].k=key;
a[j].v=value;
return 1;
}
}
return -1;
}
Page 1 of 70

ADVANCED DATA STRUCTURES LAB MANUAL

int delete(int key)


{
int h=0,i=0,j=0;
h=key%10;
if(a[h].k==key)
{
a[h].k=-1;
a[h].v=-1;
return 1;
}
for(i=h+1;i<10;i++)
{
if(a[i].k==key)
{
a[i].k=-1;
a[i].v=-1;
return 1;
}
}
for(j=0;j<h;j++)
{
if(a[j].k==key)
{
a[j].k=-1;
a[j].v=-1;
return 1;
}
}
return -1;
}
int search(int key)
{
int h=0,i,j;
h=key%10;
if(a[h].k==key)
{
printf("Value found at the index : %d value is : %d\n",h,a[h].v);
return 1;
}
for(i=h+1;i<10;i++)
{
Page 2 of 70

ADVANCED DATA STRUCTURES LAB MANUAL

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

ADVANCED DATA STRUCTURES LAB MANUAL

case 1 : printf("Enter key :");


scanf("%d",&key);
printf("Enter value :");
scanf("%d",&value);
check=insert(key,value);
if(check<0)
{
printf("Over Flow...\n");
}
break;
case 2 : printf("Enter key :");
scanf("%d",&key);
check=delete(key);
if(check<0)
{
printf("Key not Found...\n");
}
break;
case 3 : printf("Enter key :");
scanf("%d",&key);
check=search(key);
if(check<0)
{
printf("Key not found...");
}
break;
case 4 : display();
break;
case 5 : return 0;
default : printf("Inavalid option choosen...\n Choose a valid option");
}
}
}

OUTPUT :
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice : 1
Enter key : 45
Page 4 of 70

ADVANCED DATA STRUCTURES LAB MANUAL

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
-------------------------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

ADVANCED DATA STRUCTURES LAB MANUAL

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

ADVANCED DATA STRUCTURES LAB MANUAL

AIM : Program to implement functions of Dictionary using Sorted Chain.


PROGRAM :
#include<stdio.h>
#include<stdlib.h>
struct dict
{
int k;
int v;
struct dict *next;
};
struct dict *first=NULL,*last=NULL,*temp,*prev,*newnode;
void insert(int key,int value)
{
newnode=(struct dict *)malloc(sizeof(struct dict));
newnode->k=key;
newnode->v=value;
newnode->next=NULL;
if(first==NULL&&last==NULL)
{
first=newnode;
last=newnode;
}
else
{
last->next=newnode;
last=newnode;
}
}
void delete(int key)
{
int i=0;
temp=first;
while(temp!=NULL)
{
if(temp->k==key)
break;
prev=temp;
temp=temp->next;
Page 7 of 70

ADVANCED DATA STRUCTURES LAB MANUAL

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

ADVANCED DATA STRUCTURES LAB MANUAL

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

ADVANCED DATA STRUCTURES LAB MANUAL

}
}
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

ADVANCED DATA STRUCTURES LAB MANUAL

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

ADVANCED DATA STRUCTURES LAB MANUAL

AIM : Program to perform various operations on AVL Tree.


PROGRAM :
#include <stdio.h>
#include <stdlib.h>
#define max(a,b) ((a>b)?a:b)
typedef struct AvlNode
{
int data;
struct AvlNode *left,*right;
}avlnode;
avlnode *root;
avlnode* rotate_LL(avlnode *parent)
{
avlnode *child=parent->left;
parent->left=child->right;
child->right=parent;
return child;
}
avlnode* rotate_RR(avlnode *parent)
{
avlnode *child=parent->right;
parent->right=child->left;
child->left=parent;
return child;
}
avlnode* rotate_RL(avlnode *parent)
{
avlnode *child = parent->right;
parent->right=rotate_LL(child);
return rotate_RR(parent);
}
avlnode* rotate_LR(avlnode *parent)
{
avlnode *child=parent->left;
parent->left=rotate_RR(child);
Page 12 of 70

ADVANCED DATA STRUCTURES LAB MANUAL

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

ADVANCED DATA STRUCTURES LAB MANUAL

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

ADVANCED DATA STRUCTURES LAB MANUAL

case 1 :

case 2 :

case 3:
case 4:
}

printf("\nEnter a key to insert");


scanf("%d",&x);
insert(&root,x);
break;
printf("\nEnter search key");
scanf("%d",&x);
search(root,x);
break;
display(root);
break;
exit(0);

}
}
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

ADVANCED DATA STRUCTURES LAB MANUAL

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

ADVANCED DATA STRUCTURES LAB MANUAL

AIM : To perform various on 2-3 trees.


PROGRAM :

#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

ADVANCED DATA STRUCTURES LAB MANUAL

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

ADVANCED DATA STRUCTURES LAB MANUAL

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

ADVANCED DATA STRUCTURES LAB MANUAL

}
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

ADVANCED DATA STRUCTURES LAB MANUAL

}
};
// 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

ADVANCED DATA STRUCTURES LAB MANUAL

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

ADVANCED DATA STRUCTURES LAB MANUAL

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

ADVANCED DATA STRUCTURES LAB MANUAL

AIM : Program to perform various operations on Binary Heap.


PROGRAM :
#include <stdio.h>
#include<stdlib.h>
int size=0,heap[10];
void insert();
void deletemin();
void display();
void percolatedown(int);
main()
{
int op;
while(1)
{
printf("1.Insert\n2.DeleteMin\n3.Display\n4.Exit\n);
printf("Enter Your Choice : ");
scanf("%d",&op);
switch(op)
{
case 1 : insert();
break;
case 2 : deletemin();
break;
case 3 : display();
break;
case 4 : exit(0);
default : printf("Choose a valid Option....\n");
}
}
}
void insert()
{
int hole,x;
if(size>10)
printf("Heap is full...\n");
else
{
hole=++size;
Page 24 of 70

ADVANCED DATA STRUCTURES LAB MANUAL

printf("Enter element to be inserted : ");


scanf("%d",&x);
for(;hole>1&&heap[hole/2]>x;hole/=2)
{
heap[hole]=heap[hole/2];
}
heap[hole]=x;
}
}
void deletemin()
{
int x=heap[1];
if(size==0)
printf("Heap is empty...\n");
else
{
heap[1]=heap[size--];
percolatedown(1);
printf("Min Element %d is Deleted successfully...\n",x);
}
}
void percolatedown(int hole)
{
int left,right,temp,min,target;
min=heap[hole];
while(2*hole<=size)
{
left=2*hole;
right=left+1;
if(right<=size&&heap[right]<heap[left])
target=right;
else
target=left;
if(heap[target]<min)
{
temp=heap[hole];
heap[hole]=heap[target];
heap[target]=temp;
hole=target;
}
else
break;
Page 25 of 70

ADVANCED DATA STRUCTURES LAB MANUAL

}
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

ADVANCED DATA STRUCTURES LAB MANUAL

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

ADVANCED DATA STRUCTURES LAB MANUAL

AIM : Program to perform following operations on the Graph.


1.InsertVertex
2.Insert Edge
3.Delete Edge
4.Find Vertex
PROGRAM :
#include<stdio.h>
struct vertex
{
struct vertex *next;
char data;
struct edge *adj;
}*first=NULL,*p;
struct edge
{
char dest;
struct edge *link;
};
struct vertex *find_vertex(char);
main()
{
int choice;
char x,origin, destin;
while(1)
{
printf( "1.InsertVertex\n" );
printf( "2.InsertEdge\n" );
printf( "3.DeleteVertex\n" );
printf( "4.FindVertex\n");
printf( "5.Display\n" );
printf( "6.Exit\n" );
printf( "Enter your choice : " );
scanf( "%d",&choice );
switch(choice)
{
case 1: printf("Enter a vertex to be inserted : " );
scanf(" %c",&x);
insert_vertex(x);
break;
case 2: printf( "Enter an edge to be inserted : " );
scanf( " %c %c", &origin, &destin );
insert_edge( origin, destin );
break;
Page 28 of 70

ADVANCED DATA STRUCTURES LAB MANUAL

case 3:

case 4:

case 5:
case 6:

printf( "Enter a vertex to be deleted : " );


scanf( " %c",&x);
delete_vertex(x);
delvertex_edge(x);
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;
}
delete_vertex( char x)
{
struct vertex *temp,*ptr;
if (first->data==x)
{
temp=first;
Page 29 of 70

ADVANCED DATA STRUCTURES LAB MANUAL

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

ADVANCED DATA STRUCTURES LAB MANUAL

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

ADVANCED DATA STRUCTURES LAB MANUAL

}
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

ADVANCED DATA STRUCTURES LAB MANUAL

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

ADVANCED DATA STRUCTURES LAB MANUAL

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

ADVANCED DATA STRUCTURES LAB MANUAL

AIM : Program to perform the following operations on Graph.


1. Insert Vertex
2.Insert Edge
3.Delete Edge
4.Find Vertex
PROGRAM
#include<stdio.h>
struct vertex
{
struct vertex *next;
char data;
struct edge *adj;
}*first=NULL,*p;
struct edge
{
char dest;
struct edge *link;
};
struct vertex *find_vertex(char);
main()
{
int choice;
char x,origin, destin;
while(1)
{
printf( "1.InsertVertex\n" );
printf( "2.InsertEdge\n" );
printf( "3.DeleteEdge\n" );
printf( "4.FindVertex\n");
printf( "5.Display\n" );
printf( "6.Exit\n" );
printf( "Enter your choice : " );
scanf( "%d",&choice );
switch(choice)
{
case 1: printf("Enter a vertex to be inserted : " );
scanf(" %c",&x);
insert_vertex(x);
break;
case 2: printf( "Enter an edge to be inserted : " );
scanf( " %c %c", &origin, &destin );
insert_edge( origin, destin );
Page 35 of 70

ADVANCED DATA STRUCTURES LAB MANUAL

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

ADVANCED DATA STRUCTURES LAB MANUAL

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

ADVANCED DATA STRUCTURES LAB MANUAL

{
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

ADVANCED DATA STRUCTURES LAB MANUAL

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

ADVANCED DATA STRUCTURES LAB MANUAL

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

ADVANCED DATA STRUCTURES LAB MANUAL

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

ADVANCED DATA STRUCTURES LAB MANUAL

AIM : Program to implement Depth First Search for a graph non-recursively.


PROGRAM :
#include<stdio.h>
int top=-1,a[20][20],visit[20],stack[20];
void push(int ele)
{
if(top==19)
printf("Over Flow...");
else
stack[++top]=ele;
}
int pop()
{
int key;
if(top==-1)
return (0);
else
{
key=stack[top--];
return (key);
}
}
main()
{
int i,j,n,s,k;
printf("Enter no of Vertices : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("Enter edge (%d,%d) : ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("The Adjacency Matrix is : \n" );
Page 42 of 70

ADVANCED DATA STRUCTURES LAB MANUAL

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

ADVANCED DATA STRUCTURES LAB MANUAL

The DFS Graph is :


1->2->
AIM : Program to implement Breadth First Search for a graph nonrecursively.
PROGRAM :
#include<stdio.h>
int front=-1,rear=-1,a[20][20],visit[20],queue[20];
void insert(int item);
int delete();
main()
{
int n,i,j,s,k;
printf("Enter the number of the vertices : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("Enter the (%d,%d):",i,j);
scanf("%d",&a[i][j]);
}
}
printf("The adjacency martix is:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%d",a[i][j]);
}
printf("\n");
}
for(i=1;i<=n;i++)
visit[i]=0;
printf("Enter the source vertex : ");
scanf("%d",&s);
printf("The BFS graph is : \n");
insert(s);
visit[s]=1;
k=delete();
if(k!=0)
Page 44 of 70

ADVANCED DATA STRUCTURES LAB MANUAL

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

ADVANCED DATA STRUCTURES LAB MANUAL

Enter the (2,3):0


Enter the (3,1):0
Enter the (3,2):0
Enter the (3,3):0
The adjacency martix is:
011
000
000
Enter the source vertex : 1
The BFS graph is :
1->2->3

Page 46 of 70

ADVANCED DATA STRUCTURES LAB MANUAL

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

ADVANCED DATA STRUCTURES LAB MANUAL

}
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

ADVANCED DATA STRUCTURES LAB MANUAL

AIM : Program to find cost of Minimal Spanning tree using Kruskals


algorithm
#include<stdio.h>
int u,v,a,b,n,i,j,ne=1;
int min,mincost=0,cost[10][10],parent[10];
int find(int);
int uni(int,int);
void main()
{
printf("\n Enter The no.of Nodes:");
scanf("%d",&n);
printf("\n Enter The weight Matrix:");
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;
}
printf("\n");
while(ne<n)
{
for(i=1,min=999;i<=n;i++)
{
for(j=1;j<n;j++)
{
if(cost[i][j]<min)
{
min=cost[i][j];
u=i;
v=j;
}
}
}
a=find(u);
b=find(v);
if(uni(a,b))
{
printf("\n edge %d:(%d%d)cost:%d",ne++,u,v,min);
mincost+=min;
}
Page 49 of 70

ADVANCED DATA STRUCTURES LAB MANUAL

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

ADVANCED DATA STRUCTURES LAB MANUAL

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

ADVANCED DATA STRUCTURES LAB MANUAL

AIM : To implement Dijkstras algorithm to find shortest path in the graph


#include "stdio.h"
#include "conio.h"
#define infinity 999
void dij(int n,int v,int cost[10][10],int dist[])
{
int i,u,count,w,flag[10],min;
for(i=1;i<=n;i++)
flag[i]=0,dist[i]=cost[v][i];
count=2;
while(count<=n)
{
min=99;
for(w=1;w<=n;w++)
if(dist[w]<min && !flag[w])
min=dist[w],u=w;
flag[u]=1;
count++;
for(w=1;w<=n;w++)
if((dist[u]+cost[u][w]<dist[w]) && !flag[w])
dist[w]=dist[u]+cost[u][w];
}
}
void main()
{
int n,v,i,j,cost[10][10],dist[10];
clrscr();
printf("\n Enter the number of nodes:");
scanf("%d",&n);
printf("\n Enter the cost matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=infinity;
}
printf("\n Enter the source matrix:");
scanf("%d",&v);
Page 52 of 70

ADVANCED DATA STRUCTURES LAB MANUAL

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

ADVANCED DATA STRUCTURES LAB MANUAL

AIM:To implement pattern matching using Boyer-Moore algorithm


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *BoyerMoore( unsigned char *data, unsigned int dataLength, unsigned


char *string, unsigned int strLength )
{
unsigned int skipTable[256], i;
unsigned char *search;
register unsigned char lastChar;

if (strLength == 0)
return NULL;

for (i = 0; i < 256; i++)


skipTable[i] = strLength;

search = string;

i = --strLength;

do

Page 54 of 70

ADVANCED DATA STRUCTURES LAB MANUAL

{
skipTable[*search++] = i;
} while (i--);

lastChar = *--search;

search = data + strLength;


dataLength -= strLength+(strLength-1);

while ((int)dataLength > 0 )


{
unsigned int skip;

skip = skipTable[*search];
search += skip;
dataLength -= skip;
skip = skipTable[*search];
search += skip;
dataLength -= skip;
skip = skipTable[*search];

if (*search != lastChar) /*if (skip > 0)*/


{

search += skip;
dataLength -= skip;
continue;
Page 55 of 70

ADVANCED DATA STRUCTURES LAB MANUAL

i = strLength;

do
{

if (i-- == 0)
return search;
} while (*--search == string[i]);

search += (strLength - i + 1);


dataLength--;
}

return NULL;
}

int main(void)
{

static char data[] = "aaaabouaaa384982n chwercoiewar45u0943


twert3aaaaaaMarabou t9034u5t09t8493t43vkdsropgb";
static char search[] = "Marabou";

Page 56 of 70

ADVANCED DATA STRUCTURES LAB MANUAL

char *str = BoyerMoore( data, strlen(data), search, strlen(search) );


clrscr();
if (str == NULL)
puts( "String not found" );
else
puts( str );

return 0;
}
OUTPUT:
Marabou t9034u5t09t8493t43vkdsropgb

Page 57 of 70

ADVANCED DATA STRUCTURES LAB MANUAL

AIM: To implement Knuth-Morris-Pratt algorithm for pattern matching


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/*
* finds the position of the pattern in the given target string
* target - str, patter - word
*/

int kmpSearch(char *str, char *word, int *ptr) {


int i = 0, j = 0;

while ((i + j) < strlen(str)) {


/* match found on the target and pattern string char */
if (word[j] == str[i + j]) {
if (j == (strlen(word) - 1)) {
printf("%s located at the index %d\n",
word, i + 1);
return;
}
j = j + 1;
} else {
/* manipulating next indices to compare */
i = i + j - ptr[j];
if (ptr[j] > -1) {
Page 58 of 70

ADVANCED DATA STRUCTURES LAB MANUAL

j = ptr[j];
} else {
j = 0;
}
}
}
}

/* find the overlap array for the given pattern */


void findOverlap(char *word, int *ptr) {
int i = 2, j = 0, len = strlen(word);

ptr[0] = -1;
ptr[1] = 0;

while (i < len) {


if (word[i - 1] == word[j]) {
j = j + 1;
ptr[i] = j;
i = i + 1;
} else if (j > 0) {
j = ptr[j];
} else {
ptr[i] = 0;
i = i + 1;
}
}
Page 59 of 70

ADVANCED DATA STRUCTURES LAB MANUAL

return;
}

int main() {

char word[256], str[1024];


int *ptr, i;
clrscr();
/* get the target string from the user */
printf("Enter your target string:");
fgets(str, 1024, stdin);
str[strlen(str) - 1] = '\0';

/* get the pattern string from the user */


printf("Enter your pattern string:");
fgets(word, 256, stdin);
word[strlen(word) - 1] = '\0';

/* dynamic memory allocation for overlap array */


ptr = (int *)calloc(1, sizeof(int) * (strlen(word)));

/* finds overlap array */


findOverlap(word, ptr);
/* find the index of the pattern in target string */
kmpSearch(str, word, ptr);

return 0;
Page 60 of 70

ADVANCED DATA STRUCTURES LAB MANUAL

}
Output:
Enter your target string:aditya engineering college
Enter your pattern string:aditya
aditya located at the index 1

Page 61 of 70

ADVANCED DATA STRUCTURES LAB MANUAL

Additional Programs:

AIM: To implement All Pairs shortest path using Floyds algorithm


#include<stdio.h>
int a[10][10],n;
char vertex[10];
void Floyd();
int Min();

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

ADVANCED DATA STRUCTURES LAB MANUAL

{
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;

/*This is the main block of Floyd's algoritm which calculates the


minimum path
* from every vertex in the graph to every other vertex.*/
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{

Page 63 of 70

ADVANCED DATA STRUCTURES LAB MANUAL

for(j=0;j<n;j++)
{
a[i][j]=Min(a[i][j],(a[i][k]+a[k][j]));
}
}
}

printf("Minimum Path matrix is\n");


printf(" ");
for(i=0;i<n;i++)
printf("\t%c",vertex[i]);
printf("\n");
for(i=0;i<n;i++)
{
printf("%c",vertex[i]);
for(j=0;j<n;j++)
{
if(a[i][j]!=9999)
printf("\t%d",a[i][j]);
else
printf("\t%d",0);
}
printf("\n");
}
return;
}

Page 64 of 70

ADVANCED DATA STRUCTURES LAB MANUAL

int Min(int x,int y)


{
if(x>y)
return(y);
else
return(x);
}

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

ADVANCED DATA STRUCTURES LAB MANUAL

b-b=0

b-c=2

c-a=0

c-b=5

c-c=0

Minimum Path matrix is


a

99

12

14

87

92

Page 66 of 70

ADVANCED DATA STRUCTURES LAB MANUAL

AIM: To implement Warshalls Algorithm for generating path matrix

#include<stdio.h>
int a[10][10],n;
char vertex[10];
void warshall();

int main()
{
int i,j;

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 vertices are adjacent enter 1 else 0\n");

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
Page 67 of 70

ADVANCED DATA STRUCTURES LAB MANUAL

{
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]);
}
}
}

printf("Path matrix is\n");


printf(" ");
Page 68 of 70

ADVANCED DATA STRUCTURES LAB MANUAL

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

ADVANCED DATA STRUCTURES LAB MANUAL

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

Вам также может понравиться