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

CSE246-Report (1) (1)

The document outlines a project for a Library Book Search System developed by students at East West University, aiming to improve book location efficiency using binary search for title suggestions and Breadth-First Search (BFS) for pathfinding. The system allows users to input a book title and their current location, providing the shortest path to the book's location within a library. Limitations include reliance on a static library layout and potential inefficiencies with large datasets, while the implementation demonstrates practical applications of algorithms and file handling.

Uploaded by

rayhanbd27
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)
77 views

CSE246-Report (1) (1)

The document outlines a project for a Library Book Search System developed by students at East West University, aiming to improve book location efficiency using binary search for title suggestions and Breadth-First Search (BFS) for pathfinding. The system allows users to input a book title and their current location, providing the shortest path to the book's location within a library. Limitations include reliance on a static library layout and potential inefficiencies with large datasets, while the implementation demonstrates practical applications of algorithms and file handling.

Uploaded by

rayhanbd27
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/ 10

East West University

Department of CSE

CSE246
Algorithms

Project Name

“Library Book Search System”

Semester & Year


Fall 2024

Name of Student & Id : Course Instructor :

Amit Mandal
Md. Rayhan Ahamed Lecturer
2021-3-60-197 Department of Computer Science and

Nurtaz Ahmed Engineering, EWU


2020-2-60-062
Sayma Sultana
2022-1-60-405
Moriom Akter Ruby
2022-3-50-026

Date of Report Submitted : 02.02.2025


1. Problem Statement
Libraries often house thousands of books across numerous shelves, making it
difficult for users to quickly locate specific titles. Traditional methods, such as
manually browsing the shelves or using basic catalog systems, can be time-
consuming and inefficient. There's a need for a system that not only offers auto-
complete suggestions for book titles based on user input but also guides users to
the exact location of the desired book within the library.

2. Objective
The objective of this project is to develop a Library Book Search System that:

• Provides auto-complete suggestions for book titles using binary search.


• Finds the shortest path to a book’s location within the library using
Breadth-First Search (BFS).

3. Methodology
Algorithms

• Binary Search: Used to search for book titles efficiently. This algorithm
also supports the auto-complete feature by suggesting book titles based
on a given prefix.
• Breadth-First Search (BFS): Used to find the shortest path from the
user’s current location to the book’s location in the library.

Implementation

• Book Data Storage: Store book titles and their locations (row and
column) in a file.
• Binary Search for Title Lookup: Implement binary search to suggest
books based on user-entered prefixes.
• Shortest Path Calculation: Implement BFS to guide users from their
current location to the book’s location.
User Interaction

• The user is prompted to enter the title of the book and their current
location
• The user selects the exact book title from the book.txt file.
• The system then calculates and displays the shortest path to the book’s
location.

BFS Implementation

• Initialization: A queue is initialized to store nodes to be explored, and


arrays are used to track visited nodes and parent nodes for path
reconstruction.
• Exploration: The algorithm dequeues the front node, checks if it is the
destination, and if not, enqueues its unvisited neighbors.
• Path Construction: If the destination is reached, the path is
reconstructed using the parent array.

4. Limitations
• The system assumes a static library layout. Any changes in the layout
require updating the library map data.
• The efficiency of BFS may decrease if the library is very large or has
many obstacles.
• The binary search for auto-complete suggestions may not handle large
datasets optimally without additional data structures like a trie.
5. Time Complexity
Loading Books:

• Reading from a file: O(n)O(n)O(n), where nnn is the number of books.

Binary Search:

• Searching for a book: O(log⁡n)O(\log n)O(logn), where nnn is the number


of books.

BFS:

• Exploring the grid: O(V+E)O(V + E)O(V+E), where VVV is the number


of vertices (cells in the grid) and EEE is the number of edges
(connections between cells).

Flow Chart :
Start
|
Load Books from
File
|
Prompt User for
Exact Tilte Selection
|
Get User’s Current
Location
|
Find Shortest Path
Using BFS
|
Display Path to User
|

E
n
d
Source Code :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h> // For execution time measurement

#define MAX_ROWS 15
#define MAX_COLS 15
#define MAX_BOOKS 10 // Maximum number of books stored in the array

// Structure to store book details


typedef struct {
char title[50];
int row;
int col;
} Book;

// Book database stored in an array


Book books[MAX_BOOKS] = {
{"Data Structures", 5, 7},
{"C Programming", 3, 4},
{"Algorithm Design", 1, 2},
{"Computer Networks", 6, 5},
{"Operating Systems", 2, 8},
{"Machine Learning", 4, 6},
{"Artificial Intelligence", 7, 3},
{"Database Systems", 8, 9},
{"Cyber Security", 3, 6},
{"Software Engineering", 5, 5}
};

// Structure for BFS queue


typedef struct {
int row, col;
} QueueNode;

typedef struct {
QueueNode nodes[MAX_ROWS * MAX_COLS];
int front, rear;
} Queue;

void initQueue(Queue *q) { q->front = q->rear = -1; }


int isQueueEmpty(Queue *q) { return q->front == -1; }
void enqueue(Queue *q, int row, int col) {
if (q->rear == MAX_ROWS * MAX_COLS - 1) return;
if (isQueueEmpty(q)) q->front = 0;
q->nodes[++q->rear] = (QueueNode){row, col};
}
QueueNode dequeue(Queue *q) {
if (isQueueEmpty(q)) exit(1);
QueueNode node = q->nodes[q->front++];
if (q->front > q->rear) q->front = q->rear = -1;
return node;
}

// Function prototypes
int binarySearch(Book books[], int n, char *title);
void bfs(int startRow, int startCol, int destRow, int destCol, int rows, int cols, int
library[MAX_ROWS][MAX_COLS]);
void printPath(int parent[MAX_ROWS][MAX_COLS][2], int destRow, int destCol);

int main() {
int library[MAX_ROWS][MAX_COLS] = {{0}}; // Library grid (0 = free space, 1 = obstacle)

char title[50];
printf("Enter the Book Title to Search: ");
scanf("%[^\n]%*c", title);

// Measure Binary Search Execution Time


clock_t start = clock();
int index = binarySearch(books, MAX_BOOKS, title);
clock_t end = clock();
double time_taken = ((double)(end - start)) / CLOCKS_PER_SEC;
printf("Binary Search took: %.6f seconds\n", time_taken);

if (index == -1) {
printf("Book not found.\n");
return 0;
}

int userRow, userCol;


printf("Enter Your Current Location (Row & Column): ");
scanf("%d %d", &userRow, &userCol);

printf("Searching for '%s' at (%d, %d)...\n", books[index].title, books[index].row,


books[index].col);

// Measure BFS Execution Time


start = clock();
bfs(userRow, userCol, books[index].row, books[index].col, MAX_ROWS, MAX_COLS,
library);
end = clock();
time_taken = ((double)(end - start)) / CLOCKS_PER_SEC;
printf("BFS took: %.6f seconds\n", time_taken);

return 0;
}

// Binary Search to find book index in the array


int binarySearch(Book books[], int n, char *title) {
int left = 0, right = n - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
int cmp = strcmp(books[mid].title, title);
if (cmp == 0) return mid;
if (cmp < 0) left = mid + 1;
else right = mid - 1;
}
return -1;
}

// Breadth-First Search (BFS) for shortest path to book


void bfs(int startRow, int startCol, int destRow, int destCol, int rows, int cols, int
library[MAX_ROWS][MAX_COLS]) {
int visited[MAX_ROWS][MAX_COLS] = {0}, parent[MAX_ROWS][MAX_COLS][2];
Queue queue;
initQueue(&queue);

int directions[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; // Up, Down, Left, Right

enqueue(&queue, startRow, startCol);


visited[startRow][startCol] = 1;
parent[startRow][startCol][0] = parent[startRow][startCol][1] = -1;

while (!isQueueEmpty(&queue)) {
QueueNode current = dequeue(&queue);
if (current.row == destRow && current.col == destCol) {
printf("Path found: ");
printPath(parent, destRow, destCol);
return;
}

for (int i = 0; i < 4; i++) {


int newRow = current.row + directions[i][0];
int newCol = current.col + directions[i][1];

if (newRow >= 0 && newRow < rows && newCol >= 0 && newCol < cols &&
!visited[newRow][newCol] && library[newRow][newCol] == 0) {
enqueue(&queue, newRow, newCol);
visited[newRow][newCol] = 1;
parent[newRow][newCol][0] = current.row;
parent[newRow][newCol][1] = current.col;
}
}
}

printf("No Path Found to The Destination\n");


}

// Function to print the shortest path


void printPath(int parent[MAX_ROWS][MAX_COLS][2], int destRow, int destCol) {
if (parent[destRow][destCol][0] == -1 && parent[destRow][destCol][1] == -1) {
printf("(%d, %d)\n", destRow, destCol);
return;
}

printPath(parent, parent[destRow][destCol][0], parent[destRow][destCol][1]);


printf(" -> (%d, %d)\n", destRow, destCol);
}

Simulation :
Function BFS

void bfs(int startRow, int startCol, int destRow, int destCol, int rows, int cols,
int library[MAX_ROWS][MAX_COLS]) { int visited[MAX_ROWS]
[MAX_COLS] = {0}; int parent[MAX_ROWS][MAX_COLS][2]; Queue
queue; initQueue(&queue);

• Purpose: This function performs a Breadth-First Search (BFS) to find


the shortest path from a start position to a destination position in a grid-
based library.
• Details:
• int visited[MAX_ROWS][MAX_COLS] = {0};: Initializes a 2D
array to keep track of visited nodes, initially all set to 0
(unvisited).
• int parent[MAX_ROWS][MAX_COLS][2];: Initializes a
3D array to store the parent of each node, which will be used
to reconstruct the path.
• Queue queue;: Declares a queue to be used for BFS.
• initQueue(&queue);: Initializes the queue.

Direction Array

int directions[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

• Purpose: Defines the possible moves (up, down, left, right) in the grid.

Enqueue Start Position

• Purpose: Enqueue the starting position and mark it as visited.

• enqueue(&queue, startRow, startCol);: Adds the starting position


to the queue.
• visited[startRow][startCol] = 1;: Marks the starting position as visited.
• parent[startRow][startCol][0] = -1;: Sets the parent of the
starting position to -1 (indicating no parent).
• parent[startRow][startCol][1] = -1;: Same as above, for column.

BFS Loop

• Purpose: Continue processing nodes until the queue is empty.

• while (!isQueueEmpty(&queue)): Loop until the queue is empty.

• QueueNode current = dequeue(&queue);: Dequeues the front node from the


queue and

assigns it to current.

Check Destination

• Purpose: Check if the current node is the destination.

• if (current.row == destRow && current.col == destCol): Checks if


the current node is the destination.

Explore Neighbors

• Purpose: Explore all neighboring nodes. • Details:

• for (int i = 0; i < 4; i++): Loop through all possible moves.


• int newRow = current.row + directions[i][0];: Calculate the new
row index.
• int newCol = current.col + directions[i][1];: Calculate the new
column index.
Validate Neighbor

• Purpose: Check if the neighbor is within bounds, not visited, and not
an obstacle. If valid, enqueue it.
• Details:
• if (newRow >= 0 && newRow < rows && newCol >= 0
&& newCol < cols): Check if the

neighbor is within the grid bounds.

• && !visited[newRow][newCol]: Check if the neighbor has


not been visited.
• && library[newRow][newCol] == 0): Check if the neighbor
is not an obstacle.

Function Binary Search:

int binarySearch(Book books[], int n, char *title) {

• int binarySearch: This declares that the function binarySearch


returns an integer.
• Book books[]: This parameter is an array of Book structures. Each Book
contains a title and its location in the library grid.
Binary Search Loop

while (left <= right) {

• This while loop continues as long as the left index is less than or equal to the
right index, meaning

there are still elements to consider.

Calculating the Middle Index

int mid = left + (right - left) / 2;


• mid : The mid index is calculated by taking the average of left and right. This
is done to prevent potential overflow issues that could occur with (left +
right) /
2. Comparing the Mid Element with the Target
int cmp = strcmp(books[mid].title, title);

• int cmp: strcmp is used to compare the book title at the mid index with
the target title.

Checking the Comparison Result

if (cmp == 0) return mid;


• If cmp is 0, it means the book title at the mid index matches the target title.
The function returns mid, the index of the found book.
if(cmp<0)left=mid+1;

6. Results

The Library Book Search System effectively:

The program effectively finds and prints the shortest path from the user's
current location to the specified book's location, provided there is a path
available. If no path is found, it informs the user.This helps users quickly find
books and navigate the library efficiently, reducing the time spent searching for
books.

7. Conclusions

The Library Book Search System demonstrates a practical application of file


I/O, binary search, and BFS in solving a real-world problem. It efficiently
locates a book and finds the shortest path in a grid-based library layout.
However, it could be enhanced by making the grid size dynamic, allowing for
real-time updates to the library layout, and supporting multiple book searches.

You might also like