0% found this document useful (0 votes)
57 views11 pages

Wiproturbo

This document contains code implementations of various graph traversal algorithms, pattern matching, priority queues, and other data structures and algorithms. Specifically, it includes: 1. Code to implement breadth-first search (BFS) and depth-first search (DFS) graph traversal algorithms. 2. Code for a naive pattern searching algorithm to find patterns in text. 3. Code to implement a priority queue with functions to add and delete elements based on priority. 4. Additional code examples for checking palindromes, implementing sliding window protocol, and other concepts.

Uploaded by

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

Wiproturbo

This document contains code implementations of various graph traversal algorithms, pattern matching, priority queues, and other data structures and algorithms. Specifically, it includes: 1. Code to implement breadth-first search (BFS) and depth-first search (DFS) graph traversal algorithms. 2. Code for a naive pattern searching algorithm to find patterns in text. 3. Code to implement a priority queue with functions to add and delete elements based on priority. 4. Additional code examples for checking palindromes, implementing sliding window protocol, and other concepts.

Uploaded by

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

Graph Traversal:

* C program to implement BFS(breadth-first search) and DFS(depth-first search) algorithm */

#include<stdio.h>

int q[20],top=-1,front=-1,rear=-1,a[20][20],vis[20],stack[20];
int delete();
void add(int item);
void bfs(int s,int n);
void dfs(int s,int n);
void push(int item);
int pop();

void main()
{
int n,i,s,ch,j;
char c,dummy;
printf("ENTER THE NUMBER VERTICES ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("ENTER 1 IF %d HAS A NODE WITH %d ELSE 0 ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("THE ADJACENCY MATRIX IS\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf(" %d",a[i][j]);
}
printf("\n");
}

do
{
for(i=1;i<=n;i++)
vis[i]=0;
printf("\nMENU");
printf("\n1.B.F.S");
printf("\n2.D.F.S");
printf("\nENTER YOUR CHOICE");
scanf("%d",&ch);
printf("ENTER THE SOURCE VERTEX :");
scanf("%d",&s);

switch(ch)
{
case 1:bfs(s,n);
break;
case 2:
dfs(s,n);
break;
}
printf("DO U WANT TO CONTINUE(Y/N) ? ");
scanf("%c",&dummy);
scanf("%c",&c);
}while((c=='y')||(c=='Y'));
}

//**************BFS(breadth-first search) code**************//


void bfs(int s,int n)
{
int p,i;
add(s);
vis[s]=1;
p=delete();
if(p!=0)
printf(" %d",p);
while(p!=0)
{
for(i=1;i<=n;i++)
if((a[p][i]!=0)&&(vis[i]==0))
{
add(i);
vis[i]=1;
}
p=delete();
if(p!=0)
printf(" %d ",p);
}
for(i=1;i<=n;i++)
if(vis[i]==0)
bfs(i,n);
}

void add(int item)


{
if(rear==19)
printf("QUEUE FULL");
else
{
if(rear==-1)
{
q[++rear]=item;
front++;
}
else
q[++rear]=item;
}
}
int delete()
{
int k;
if((front>rear)||(front==-1))
return(0);
else
{
k=q[front++];
return(k);
}
}

//***************DFS(depth-first search) code******************//


void dfs(int s,int n)
{
int i,k;
push(s);
vis[s]=1;
k=pop();
if(k!=0)
printf(" %d ",k);
while(k!=0)
{
for(i=1;i<=n;i++)
if((a[k][i]!=0)&&(vis[i]==0))
{
push(i);
vis[i]=1;
}
k=pop();
if(k!=0)
printf(" %d ",k);
}
for(i=1;i<=n;i++)
if(vis[i]==0)
dfs(i,n);
}
void push(int item)
{
if(top==19)
printf("Stack overflow ");
else
stack[++top]=item;
}
int pop()
{
int k;
if(top==-1)
return(0);
else
{
k=stack[top--];
return(k);
}
}
2. Patern Matching:
// C program for Naive Pattern Searching algorithm
#include <stdio.h>
#include <string.h>
void search(char* pat, char* txt)
{
int M = strlen(pat);
int N = strlen(txt);

/* A loop to slide pat[] one by one */


for (int i = 0; i <= N - M; i++) {
int j;

/* For current index i, check for pattern match */


for (j = 0; j < M; j++)
if (txt[i + j] != pat[j])
break;

if (j == M) // if pat[0...M-1] = txt[i, i+1, ...i+M-1]


printf("Pattern found at index %d \n", i);
}
}

/* Driver program to test above function */


int main()
{
char txt[] = "AABAACAADAABAAABAA";
char pat[] = "AABA";
search(pat, txt);
return 0;
}

Priority Queue
In computer science, a priority queue is an abstract data type which is like a regular queue or stack
data structure, but where additionally each element has a "priority" associated with it. In a priority
queue, an element with high priority is served before an element with low priority.

/*
1. * C Program to Implement Priority Queue to Add and Delete Elements

2. */

3. #include <stdio.h>

4. #include <stdlib.h>

5.
6. #define MAX 5

7.

8. void insert_by_priority(int);

9. void delete_by_priority(int);

10. void create();

11. void check(int);

12. void display_pqueue();

13.

14. int pri_que[MAX];

15. int front, rear;

16.

17. void main()

18. {

19. int n, ch;

20.

21. printf("\n1 - Insert an element into queue");

22. printf("\n2 - Delete an element from queue");

23. printf("\n3 - Display queue elements");

24. printf("\n4 - Exit");

25.

26. create();

27.

28. while (1)

29. {

30. printf("\nEnter your choice : ");

31. scanf("%d", &ch);

32.

33. switch (ch)

34. {
35. case 1:

36. printf("\nEnter value to be inserted : ");

37. scanf("%d",&n);

38. insert_by_priority(n);

39. break;

40. case 2:

41. printf("\nEnter value to delete : ");

42. scanf("%d",&n);

43. delete_by_priority(n);

44. break;

45. case 3:

46. display_pqueue();

47. break;

48. case 4:

49. exit(0);

50. default:

51. printf("\nChoice is incorrect, Enter a correct choice");

52. }

53. }

54. }

55.

56. /* Function to create an empty priority queue */

57. void create()

58. {

59. front = rear = -1;

60. }

61.

62. /* Function to insert value into priority queue */

63. void insert_by_priority(int data)


64. {

65. if (rear >= MAX - 1)

66. {

67. printf("\nQueue overflow no more elements can be inserted");

68. return;

69. }

70. if ((front == -1) && (rear == -1))

71. {

72. front++;

73. rear++;

74. pri_que[rear] = data;

75. return;

76. }

77. else

78. check(data);

79. rear++;

80. }

81.

82. /* Function to check priority and place element */

83. void check(int data)

84. {

85. int i,j;

86.

87. for (i = 0; i <= rear; i++)

88. {

89. if (data >= pri_que[i])

90. {

91. for (j = rear + 1; j > i; j--)

92. {
93. pri_que[j] = pri_que[j - 1];

94. }

95. pri_que[i] = data;

96. return;

97. }

98. }

99. pri_que[i] = data;

100. }

101.

102. /* Function to delete an element from queue */

103. void delete_by_priority(int data)

104. {

105. int i;

106.

107. if ((front==-1) && (rear==-1))

108. {

109. printf("\nQueue is empty no elements to delete");

110. return;

111. }

112.

113. for (i = 0; i <= rear; i++)

114. {

115. if (data == pri_que[i])

116. {

117. for (; i < rear; i++)

118. {

119. pri_que[i] = pri_que[i + 1];

120. }

121.
122. pri_que[i] = -99;

123. rear--;

124.

125. if (rear == -1)

126. front = -1;

127. return;

128. }

129. }

130. printf("\n%d not found in queue to delete", data);

131. }

132.

133. /* Function to display queue elements */

134. void display_pqueue()

135. {

136. if ((front == -1) && (rear == -1))

137. {

138. printf("\nQueue is empty");

139. return;

140. }

141.

142. for (; front <= rear; front++)

143. {

144. printf(" %d ", pri_que[front]);

145. }

146.

147. front = 0;

148. }
Palindrome
for number:
#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
printf("palindrome number ");
else
printf("not palindrome");
return 0;
}

Slidig Window Protocol


#include<stdio.h>
int main()
{
int w,i,f,frames[50];

printf("Enter window size: ");


scanf("%d",&w);

printf("\nEnter number of frames to transmit: ");


scanf("%d",&f);

printf("\nEnter %d frames: ",f);

for(i=1;i<=f;i++)
scanf("%d",&frames[i]);
printf("\nWith sliding window protocol the frames will be sent in the following manner
(assuming no corruption of frames)\n\n");
printf("After sending %d frames at each stage sender waits for acknowledgement sent by the
receiver\n\n",w);

for(i=1;i<=f;i++)
{
if(i%w==0)
{
printf("%d\n",frames[i]);
printf("Acknowledgement of above frames sent is received by sender\n\n");
}
else
printf("%d ",frames[i]);
}

if(f%w!=0)
printf("\nAcknowledgement of above frames sent is received by sender\n");

return 0;
}

You might also like