Alg Lab Manual4,5,6
Alg Lab Manual4,5,6
Alg Lab Manual4,5,6
Step 1: start
Step 2: Define the txt and pat strings
Step 3: Define the search function
Step 4: Inside the search function, there will be two loops
Step 5: During each iteration, the pat string is searched in the txt string
Step 6: If the match is found, then print the index of the pattern
Step 7: else the inner loop will break and move to the next iteration
Step 8: The above steps are repeated until the entire txt string is reached
Step 9: stop
PROGRAM:
#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 (inti = 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's code
void main ()
{
char txt [] = "AABAACAADAABAAABAA";
char pat[] = "AABA"; // Function call
search (pat, txt);
}
Output:
RESULT:
Thus the program to print all the occurrences of pattern in the given text was executed successfully
NADARSARASWATHICOLLEGEOFENGINEERINGANDTECHNOLOGY
DEPARTMENTOFCOMPUTER SCIENCE ANDENGINEERING
ALGORITHM:
Step 1:Start
Step 2:Build a max heap from the input data
Step 3:At this point, the maximum element is stored at the root of the heap
Step 4:Replace it with the last item of the heap followed by reducing the size of the heap
Step 5:Finally, heapify the root of the tree
Step 6:Repeat step 3 while the size of the heap is greater than 1
Step 7:stop
PROGRAM:
#include <stdio.h>
void swap (int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void heapify(intarr[], int N, inti)
{
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < N &&arr[left] >arr[largest])
largest = left;
if (right < N &&arr[right] >arr[largest])
largest = right;
if (largest != i) {
swap(&arr[i], &arr[largest]);
heapify (arr, N, largest);
}}
void heapSort(intarr[], int N)
{
for (inti = N / 2 - 1; i>= 0; i--)
heapify (arr, N, i);
for (inti = N - 1; i>= 0; i--) {
swap(&arr[0], &arr[i]);
heapify (arr, i, 0); NADARSARASWATHICOLLEGEOFENGINEERINGANDTECHNOLOGY
}
DEPARTMENTOFCOMPUTER SCIENCE ANDENGINEERING
}
void printArray(intarr[], int N) C S34012-A LGORITHMSLABORATORY
{
for (inti = 0; i< N; i++)
printf ("%d ", arr[i]);
printf("\n");
}
intmain ()
{
intarr[] = { 12, 11, 13, 5, 6, 7 };
int N = sizeof(arr) / sizeof(arr[0]);
heapSort (arr, N);
printf ("Sorted array is\n");
printArray (arr, N);
}
Output:
Sorted array is
5 6 7 11 12 13
RESULT:
Thus the program for Heap Sort was verified and executed successfully
NADARSARASWATHICOLLEGEOFENGINEERINGANDTECHNOLOGY
DEPARTMENTOFCOMPUTER SCIENCE ANDENGINEERING
C S34012-A LGORITHMSLABORATORY
EX.NO.5 Develop a program to implement graph traversal using Breadth-First
Search
AIM:
To Develop a program to implement graph traversal using Breadth-First Search
ALGORITHM:
Step 1:Start
Step 2:Declare a queue and insert the starting vertex
Step 3:Initialize a visited array and mark the starting vertex as visited
Step 4:Follow the below process till the queue becomes empty
Remove the first vertex of the queue
Mark the vertex as visited
Insert all the unvisited neighbours of the vertex into the queue
Step 5: Stop
PROGRAM:
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTICES 50 // This struct represents a directed graph using
// adjacency list representation
typedefstructGraph_t {
int V; // No. of vertices
bool adj[MAX_VERTICES][MAX_VERTICES];
} Graph;
// Constructor
Graph* Graph_create (int V)
{
Graph* g = malloc (sizeof (Graph));
g->V = V;
for (inti = 0; i< V; i++) {
for (int j = 0; j < V; j++) {
g->adj[i][j] = false;
}
}
return g; NADARSARASWATHICOLLEGEOFENGINEERINGANDTECHNOLOGY
}
DEPARTMENTOFCOMPUTER SCIENCE ANDENGINEERING
// Destructor
void Graph_destroy (Graph* g) { C S34012-A
free(g); } LGORITHMSLABORATORY
// function to add an edge to graph
void Graph_addEdge (Graph* g, int v, int w)
{
g->adj[v][w] = true; // Add w to v’s list.
}
// prints BFS traversal from a given source s
void Graph_BFS (Graph* g, int s)
{
// Mark all the vertices as not visited
bool visited[MAX_VERTICES];
for (inti = 0; i< g->V; i++) {
visited[i] = false;
}
// Create a queue for BFS
int queue[MAX_VERTICES];
int front = 0, rear = 0;
// Mark the current node as visited and enqueue it
visited[s] = true;
queue[rear++] = s;
while (front!= rear) {
// Dequeue a vertex from queue and print it
s = queue[front++];
printf ("%d ", s);
// Get all adjacent vertices of the dequeued
// vertex s. If a adjacent has not been visited,
// then mark it visited and enqueue it
for (intadjecent = 0; adjecent< g->V;
adjecent++) {
if (g->adj[s][adjecent] &&! visited[adjecent]) {
visited[adjecent] = true;
queue[rear++] = adjecent;
}
}
}
}
// Driver program to test methods of graph struct
Intmain ()
{
// Create a graph given in the above diagram
Graph* g = Graph_create (4);
Graph_addEdge (g, 0, 1);
Graph_addEdge (g, 0, 2);
Graph_addEdge (g, 1, 2);
Graph_addEdge (g, 2, 0);
Graph_addEdge (g, 2, 3);
Graph_addEdge (g, 3, 3);
NADARSARASWATHICOLLEGEOFENGINEERINGANDTECHNOLOGY
printf ("\nFollowing is Breadth First Traversal "
DEPARTMENTOFCOMPUTER SCIENCE ANDENGINEERING
"(starting from vertex 2) \n");
Graph_BFS (g, 2); C S34012-A LGORITHMSLABORATORY
Graph_destroy(g);
return 0;
}
Output:
Following is Breadth First Traversal (starting from vertex 2)
2031
RESULT:
Thus the program for BFS was executed successfully
AIM:
To write a program to implement graph traversal using Depth
First Search
ALGORITHM: