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

ICT208 Algorithms and Data Structures

this is the assignment file

Uploaded by

arun neupane
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

ICT208 Algorithms and Data Structures

this is the assignment file

Uploaded by

arun neupane
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Table of Contents

1. Part A.....................................................................................................................................................2
1.1 Maze Exploration Using Recursive Function Code.........................................................................2
1.2 Snapshot...........................................................................................................................................4
1.2.1 Declaration Of 2d Array............................................................................................................4
1.2.2 Backtracking of Path.................................................................................................................4
1.2.3 Exploration Path........................................................................................................................5
1.2.4 Output.......................................................................................................................................5
2. Part B.....................................................................................................................................................6
2.1 Huffman Decoding Using Recursive Function................................................................................6
2.1.1 Code..............................................................................................................................................6
2.1.2 Snapshot..................................................................................................................................10
Figure 1 2D array.......................................................................................................................................3
Figure 2 path backtracking.........................................................................................................................3
Figure 3 Display of path............................................................................................................................4
Figure 4 Dimension Entry..........................................................................................................................4
Figure 5 Path Entry....................................................................................................................................4
Figure 6 Path Display.................................................................................................................................5
Figure 7character input..............................................................................................................................9
Figure 8output of Huffman decoding.........................................................................................................9
1. Part A
1.1 Maze Exploration Using Recursive Function Code

Here is the code that has been developed for the completion of the given task.
#include <iostream>
using namespace std;
// boolean for the rat to determine whether it is safe or not
bool isSafe(int **arr, int x, int y, int n)
{
if (x < n && y < n && arr[x][y] == 1)
{
return true;
}
return false;
}
// for the maze problem
bool inMaze(int **arr, int x, int y, int n, int **slArr)
{
if (x == n - 1 && y == n - 1)
{
slArr[x][y] = 1;
return true;
}
if (isSafe(arr, x, y, n))
{
slArr[x][y] = 1;
if (inMaze(arr, x + 1, y, n, slArr))
{
return true;
}
if (inMaze(arr, x, y + 1, n, slArr))
{
return true;
}
slArr[x][y] = 0;
return false;
}
return false;
}
// main function
int main()
{
int n;
cout << "Enter dimensions of the maze ";
cin >> n;
int **arr = new int *[n];

1
for (int i = 0; i < n; i++)
{
arr[i] = new int[n];
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> arr[i][j];
}
}
int **slArr = new int *[n];
for (int i = 0; i < n; i++)
{
slArr[i] = new int[n];
for (int j = 0; j < n; j++)
{
slArr[i][j] = 0;
}
}
if (inMaze(arr, 0, 0, n, slArr))
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << slArr[i][j];
}
cout << endl;
}
}
return 0;
}

2
1.2 Snapshot
1.2.1 Declaration Of 2d Array

Figure 1 2D array

1.2.2 Backtracking of Path

Figure 2 path backtracking

3
1.2.3 Exploration Path

Fi
gure 3 Display of path

1.2.4 Output

Figure 4 Dimension Entry

Figure 5 Path Entry

4
Figure 6 Path Display

This is the way of performing the maze concept for the rat by using the recursive function and here are
the output and the code of this concept.

2. Part B
2.1 Huffman Decoding Using Recursive Function
2.1.1 Code
#include <iostream>
#include <cstdlib>
using namespace std;
#define MAX_TREE_HT 100
struct MinHeapNode {
char data;
unsigned freq;
struct MinHeapNode *left, *right;
};
struct MinHeap {
unsigned size;
unsigned capacity;
struct MinHeapNode** array;
};
struct MinHeapNode* newNode(char data, unsigned freq)
{
struct MinHeapNode* temp = (struct MinHeapNode*)malloc (sizeof(struct MinHeapNode));
temp->left = temp->right = NULL;
temp->data = data;
temp->freq = freq;
return temp;
}
struct MinHeap* createMinHeap(unsigned capacity)
{
struct MinHeap* minHeap= (struct MinHeap*)malloc(sizeof(struct MinHeap));
minHeap->size = 0;
minHeap->capacity = capacity;
minHeap->array = (struct MinHeapNode**)malloc(minHeap->
capacity * sizeof(struct MinHeapNode*));
return minHeap;
}

5
void swapMinHeapNode(struct MinHeapNode** a, struct MinHeapNode** b)
{
struct MinHeapNode* t = *a;
*a = *b;
*b = t;
}
void minHeapify(struct MinHeap* minHeap, int idx)
{
int smallest = idx;
int left = 2 * idx + 1;
int right = 2 * idx + 2;
if (left < minHeap->size && minHeap->array[left]-> freq < minHeap->array[smallest]->freq)
smallest = left;
if (right < minHeap->size && minHeap->array[right]->
freq < minHeap->array[smallest]->freq)
smallest = right;
if (smallest != idx) {
swapMinHeapNode(&minHeap->array[smallest],
&minHeap->array[idx]);
minHeapify(minHeap, smallest);
}
}
int isSizeOne(struct MinHeap* minHeap)
{
return (minHeap->size == 1);
}
struct MinHeapNode* extractMin(struct MinHeap* minHeap)
{
struct MinHeapNode* temp = minHeap->array[0];
minHeap->array[0]
= minHeap->array[minHeap->size - 1];
--minHeap->size;
minHeapify(minHeap, 0);
return temp;
}
void insertMinHeap(struct MinHeap* minHeap,
struct MinHeapNode* minHeapNode)
{
++minHeap->size;
int i = minHeap->size - 1;
while (i && minHeapNode->freq < minHeap->array[(i - 1) / 2]->freq) {
minHeap->array[i] = minHeap->array[(i - 1) / 2];
i = (i - 1) / 2;
}
minHeap->array[i] = minHeapNode;
}
void buildMinHeap(struct MinHeap* minHeap)

6
int n = minHeap->size - 1;
int i;
for (i = (n - 1) / 2; i >= 0; --i)
minHeapify(minHeap, i);
}
void printArr(int arr[], int n)
{
int i;
for (i = 0; i < n; ++i)
cout<< arr[i];
cout<<"\n";
}
int isLeaf(struct MinHeapNode* root)
{
return !(root->left) && !(root->right);
}
struct MinHeap* createAndBuildMinHeap(char data[], int freq[], int size)
{
struct MinHeap* minHeap = createMinHeap(size);
for (int i = 0; i < size; ++i)
minHeap->array[i] = newNode(data[i], freq[i]);
minHeap->size = size;
buildMinHeap(minHeap);
return minHeap;
}
struct MinHeapNode* buildHuffmanTree(char data[], int freq[], int size)
{
struct MinHeapNode *left, *right, *top;

struct MinHeap* minHeap = createAndBuildMinHeap(data, freq, size);


while (!isSizeOne(minHeap)) {
left = extractMin(minHeap);
right = extractMin(minHeap);
top = newNode('$', left->freq + right->freq);
top->left = left;
top->right = right;
insertMinHeap(minHeap, top);
}
return extractMin(minHeap);
}
void printCodes(struct MinHeapNode* root, int arr[], int top)
{
if (root->left) {
arr[top] = 0;
printCodes(root->left, arr, top + 1);
}
if (root->right) {
arr[top] = 1;

7
printCodes(root->right, arr, top + 1);
}
if (isLeaf(root)) {
cout<< root->data <<": ";
printArr(arr, top);
}
}
void HuffmanCodes(char data[], int freq[], int size)
{

struct MinHeapNode* root


= buildHuffmanTree(data, freq, size);
int arr[MAX_TREE_HT], top = 0;
printCodes(root, arr, top);
}
int main()
{
char arr[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
int freq[] = { 5, 9, 12, 13, 16, 45 };
int size = sizeof(arr) / sizeof(arr[0]);
HuffmanCodes(arr, freq, size);
return 0;
}

8
2.1.2 Snapshot

Figure 7character input

Figure 8output of Huffman decoding

This is the process for performing the Huffman decoding for any character given. There is also the code
and the snapshot of the output after performing the running of the code.

You might also like