0% found this document useful (0 votes)
9 views

Program To Travers A Graph by BFS

Uploaded by

trialmail089
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Program To Travers A Graph by BFS

Uploaded by

trialmail089
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

PROGRAM TO TRAVERS A GRAPH BY BFS

#include<stdio.h>

#include<stdlib.h>

struct queue

int size;

int f;

int r;

int* arr;

};

int isEmpty(struct queue *q){

if(q->r==q->f){

return 1;

return 0;

int isFull(struct queue *q){

if(q->r==q->size-1){

return 1;

return 0;

void enqueue(struct queue *q, int val){

if(isFull(q)){

printf("This Queue is full\n");


}

else{

q->r++;

q->arr[q->r] = val;

// printf("Enqued element: %d\n", val);

int dequeue(struct queue *q){

int a = -1;

if(isEmpty(q)){

printf("This Queue is empty\n");

else{

q->f++;

a = q->arr[q->f];

return a;

int main(){

// Initializing Queue (Array Implementation)

struct queue q;

q.size = 400;

q.f = q.r = 0;

q.arr = (int*) malloc(q.size*sizeof(int));

// BFS Implementation

int node;

int i = 1;
int visited[7] = {0,0,0,0,0,0,0};

int a [7][7] = {

{0,1,1,1,0,0,0},

{1,0,1,0,0,0,0},

{1,1,0,1,1,0,0},

{1,0,1,0,1,0,0},

{0,0,1,1,0,1,1},

{0,0,0,0,1,0,0},

{0,0,0,0,1,0,0}

};

printf("%d", i);

visited[i] = 1;

enqueue(&q, i); // Enqueue i for exploration

while (!isEmpty(&q))

int node = dequeue(&q);

for (int j = 0; j < 7; j++)

if(a[node][j] ==1 && visited[j] == 0){

printf("%d", j);

visited[j] = 1;

enqueue(&q, j);

return 0;

}
OUTPUT:-
1. WHEN THE ROOT IS 1

2. WHEN THE ROOT IS 0


PROGRAM TO TRAVERS A GRAPH BY DFS

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

int visited[7] = {0,0,0,0,0,0,0};


int A [7][7] = {
{0,1,1,1,0,0,0},
{1,0,1,0,0,0,0},
{1,1,0,1,1,0,0},
{1,0,1,0,1,0,0},
{0,0,1,1,0,1,1},
{0,0,0,0,1,0,0},
{0,0,0,0,1,0,0}
};

void DFS(int i){


printf("%d ", i);
visited[i] = 1;
for (int j = 0; j < 7; j++)
{
if(A[i][j]==1 && !visited[j]){
DFS(j);
}
}
}

int main(){
// DFS Implementation
DFS(0);
return 0;
}

OUTPUT:-

1. WHEN THE ROOT IS 0

PROGRAM TO IMPLEMENT A STACK BY DMA


#include<stdio.h>

#include<stdlib.h>

struct Node{

int data;

struct Node * next;

};

struct Node* top = NULL;

void linkedListTraversal(struct Node *ptr)

while (ptr != NULL)

printf("Element: %d\n", ptr->data);

ptr = ptr->next;

int isEmpty(struct Node* top){

if (top==NULL){

return 1;
}

else{

return 0;

int isFull(struct Node* top){

struct Node* p = (struct Node*)malloc(sizeof(struct Node));

if(p==NULL){

return 1;

else{

return 0;

struct Node* push(struct Node* top, int x){

if(isFull(top)){

printf("Stack Overflow\n");

else{

struct Node* n = (struct Node*) malloc(sizeof(struct Node));


n->data = x;

n->next = top;

top = n;

return top;

int pop(struct Node* tp){

if(isEmpty(tp)){

printf("Stack Underflow\n");

else{

struct Node* n = tp;

top = (tp)->next;

int x = n->data;

free(n);

return x;

int main(){

top = push(top, 78);


top = push(top, 7);

top = push(top, 8);

// linkedListTraversal(top);

int element = pop(top);

printf("Popped element is %d\n", element);

linkedListTraversal(top);

return 0;

OUTPUT:-

PROGRAM TO IMPLEMENT A QUEUE BY DMA

#include <stdio.h>

#include <stdlib.h>

struct Node *f = NULL;


struct Node *r = NULL;

struct Node

int data;

struct Node *next;

};

void linkedListTraversal(struct Node *ptr)

printf("Printing the elements of this linked list\n");

while (ptr != NULL)

printf("Element: %d\n", ptr->data);

ptr = ptr->next;

void enqueue(int val)

struct Node *n = (struct Node *) malloc(sizeof(struct Node));

if(n==NULL){
printf("Queue is Full");

else{

n->data = val;

n->next = NULL;

if(f==NULL){

f=r=n;

else{

r->next = n;

r=n;

int dequeue()

int val = -1;

struct Node *ptr = f;

if(f==NULL){

printf("Queue is Empty\n");

}
else{

f = f->next;

val = ptr->data;

free(ptr);

return val;

int main()

linkedListTraversal(f);

printf("Dequeuing element %d\n", dequeue());

enqueue(34);

enqueue(4);

enqueue(7);

enqueue(17);

printf("Dequeuing element %d\n", dequeue());

printf("Dequeuing element %d\n", dequeue());

printf("Dequeuing element %d\n", dequeue());

printf("Dequeuing element %d\n", dequeue());

linkedListTraversal(f);

return 0;
}

OUTPUT:-

PROGRAM TO IMPLEMENT A CIRCULAR QUEUE BY DMA

#include<stdio.h>

#include<stdlib.h>

struct circularQueue

int size;

int f;
int r;

int* arr;

};

int isEmpty(struct circularQueue *q){

if(q->r==q->f){

return 1;

return 0;

int isFull(struct circularQueue *q){

if((q->r+1)%q->size == q->f){

return 1;

return 0;

void enqueue(struct circularQueue *q, int val){

if(isFull(q)){

printf("This Queue is full");


}

else{

q->r = (q->r +1)%q->size;

q->arr[q->r] = val;

printf("Enqued element: %d\n", val);

int dequeue(struct circularQueue *q){

int a = -1;

if(isEmpty(q)){

printf("This Queue is empty");

else{

q->f = (q->f +1)%q->size;

a = q->arr[q->f];

return a;

int main(){
struct circularQueue q;

q.size = 4;

q.f = q.r = 0;

q.arr = (int*) malloc(q.size*sizeof(int));

// Enqueue few elements

enqueue(&q, 12);

enqueue(&q, 15);

enqueue(&q, 1);

printf("Dequeuing element %d\n", dequeue(&q));

printf("Dequeuing element %d\n", dequeue(&q));

printf("Dequeuing element %d\n", dequeue(&q));

enqueue(&q, 45);

enqueue(&q, 45);

enqueue(&q, 45);

if(isEmpty(&q)){

printf("Queue is empty\n");

if(isFull(&q)){

printf("Queue is full\n");

}
return 0;

OUTPUT:-

You might also like