Demonstrate your understanding of structures

Assignment Help Programming Languages
Reference no: EM133696375

Foundations of Algorithms

Learning Outcomes
In this assignment you will demonstrate your understanding of structures, linked data structures, and algo- rithm time complexity analysis. You will further extend your skills in program design and implementation.

The Story...
In Assignment 1, we have extended your algorithmic searching capability to multidimensional numeric data. In this assignment, we will continue to add text searching to your arsenal. We continue using point of in- terest (POI) search as the background application to minimise context switching. However, the algorithms you will implement are generic to text search problems and serve as fundamental building blocks of search engines such as Google or Bing. If you have missed Assignment 1, you can still compete this assignment - it would be helpful to revisit the first two pages of the Assignment 1 specification to understand the context in this case.

In Assignment 1, we have assumed that all POIs given are relevant to the queries (e.g., they are all cafes), and we only need to filter them based on the POI locations which are numeric properties. In reality, there are POIs of many different categories in the same area, and only few are relevant to a query. See Figure 1 for example. There are not only cafes in the Melbourne CBD but also shops, supermarkets, hotels, post offices, a cathedral, etc. When a user queries for "cafes", none of the POIs in the other categories need to be considered. In this assignment, we will implement an algorithm to quickly filter out POIs of irrelevant categories (or web documents in search engines, users in social network searches, etc.).

Your Task
The input POI and query dataset in this assignment is expanded to include POI category information. The input still contains two sections, with a sample input shown below:
At least 1 and up to 50 lines of POI records. Each line represents a POI, which starts with an unique integer POI ID of up to two digits (the POI IDs are just the line numbers, to simplify the assignment). Each POI record then contains two real numbers representing the POI coordinates in the x- and y- dimensions. After that, each POI record contains at least 1 and up to 5 category keywords ("category " for short hereafter) separated by single whitespace characters. Each category is a string of at least 1 and up to 20 lower-case English letters. At the end of each line, there is a special character ‘#' to indicate the end of the line - this is used to simplify input processing for the assignment; it is not part of the POI categories.

You may assume that there are no repetitions among the categories of a POI. In the sample input below, POI #0 has coordinates (16.4, 69.4), and its categories are petrol, atm, and carwash.

At least 1 and no predefined maximum number of lines of queries. Each line represents a query, which starts with four real numbers representing the query range (xlb, ylb, xub, yub) as in Assignment 1. Recall that xlb and ylb represent the lower bounds in the x- and y-dimensions of the query range, while xub and yub represent the respective upper bounds. Intuitively, each query range is a rectangle whose bottom left corner is at (xlb, ylb) and top right corner is at (xub, yub). You may assume that xlb < xub and ylb < yub always hold. Each query line is then followed by a query category, which is a string of at least 1 and up to 20 lower-case English letters.
All coordinate values are in (0, 100). Note that there is a line with 10 ‘#'s to separate the two input sections.
16.4 69.4 petrol atm carwash #
88.7 13.3 atm #
19.5 78.5 shop supermarket atm coles woolworths #
85.1 96.1 supermarket #
15.4 22.3 cinema movie theatre #
22.2 73.5 gas petrol carwash #
97.3 68.1 petrol carwash #
80.0 32.7 carpark cafe shop #
46.8 43.3 hotel atm restaurant carpark #
87.3 42.3 cafe atm #
87.5 34.6 atm cafe shop #
24.7 4.4 theatre cinema atm carpark movie # ##########

You may assume that the test data always follows the format above. No input validity checking is needed.
You will be given a skeleton code file named program.c for this assignment on LMS. The skeleton code file contains a main function that has been partially completed. There are a few other functions which are incomplete. You need to add code to all the functions including the main function for the following tasks.
Stage 1: Read the POIs (Up to 5 Marks)
Your first task is to add code to the stage_one function to read the POI records. You need to define a struct named poi_t to represent a POI, and an array of this struct type to store all POI records. This stage outputs (1) the number of POI records read, (2) the POI with the largest number of categories, and
(3) the categories of this POI. If there is a tie, print out the POI with the smallest ID among the tied ones.
Hint: Note the newline character ‘\n' at the end of a line if you use getchar to read the categories.
The output for this stage given the above sample input is shown in the next page (where "mac:" is the command prompt).

mac: ./program < test0.txt Stage 1
==========
Number of POIs: 12
POI #2 has the largest number of categories:
shop supermarket atm coles woolworths
Like in Assignment 1, we will again use input redirection to feed test data into your program. Thus, you should still use the standard input functions such as scanf or getchar to read the data. You do not need to (and should not ) use any file operation functions such as fopen or fread. Your program should not print anything except for the data requested to be output (as shown in the output example).
You can also modify the stage_one function to read all input in one go. You can (and should) create further functions to complete the tasks when opportunities arise.
Stage 2: Read and Process the Queries (Up to 10 Marks)
Add code to the stage_two function to read the queries. You will need to define another struct named query_t to represent a query, and a linked list to store the input list of queries. You should adapt the linked list structure and code given in the skeleton code for this purpose. Note: If you are not confident with linked lists, you may use an array of the query_t type instead, assuming up to 20 queries. This will cost a 2-mark deduction but will not impact the rest of your assignment implementation.
Then, add code to the process_queries function to go through the list (or array) of queries. For each query, calculate and output the IDs of the POIs that are within the query range and match the query category. You may use linear search to go through all POIs and their categories to process each query, and do not need to use the advanced search algorithms described in Assignment 1.
We say that a POI at (x, y) is within a query range (xlb, ylb, xub, yub) if the following inequalities hold:
xlb ≤ x ≤ xub and ylb ≤ y ≤ yub (1)
Further, we say that a POI matches a query category if one of the categories of the POI is exactly the same
as the query category. The output for this stage given the above sample input should be:
Stage 2
==========
POIs in Q0: 4 11
POIs in Q1: none POIs in Q2: 5 POIs in Q3: none
POIs in Q4: 7 9 10
Here, Qi to refer to the i-th input query. If there is no POI that satisfies a query, we output "none". As an example, for Q2, even though both POIs #2 and #5 are within the query range, only POI #5 matches the query category petrol. Thus, only POI #5 is outputted.
Stage 3: Compute the Unique POI Categories (Up to 15 Marks)
Next, we consider a smarter strategy to search the POIs based on their categories. Stage 3 is a preparation step for this purpose. Add code to the stage_three function to identify and output the list of all unique POI category strings that have appeared in the input POI records. The POI categories should be outputted in the order that they appear in the input. To simplify the assignment, you may assume that there are at most 50 unique POI categories in the input, and you may use an array of strings to store the unique POI categories. Given the sample input above, the output of this stage is shown below (5 categories per line).
Stage 3
==========
15 unique POI categories:
petrol, atm, carwash, shop, supermarket coles, woolworths, cinema, movie, theatre gas, carpark, cafe, hotel, restaurant

Stage 4: Construct an Inverted Index (Up to 20 Marks)
Finally, add code to the stage_four function to construct an inverted index over the POI as follows:
Define a struct type named index_t of three fields:
category, which is a string of up to 20 lower-case English letters;
pois, which is an int array to store the IDs of all POIs that match the category; and
num_matched_pois, which is the number of POIs that match the category.
For example, as shown in the sample output below, category cafe matches with POIs #7, #9, and #10. These three POI IDs are supposed to be stored in the pois array corresponding to category cafe, while num_matched_pois is 3.
Create an array of index_t type. Let us name this array index. This array will have the same size as the array of unique POI categories created in Stage 3. Each element of the index array stores a unique POI category in its category field.
Sort the index array created in Step 2, in ascending alphabetical order (that is, dictionary order) of the POI categories stored in the category field of each array element.
Hint: You may adapt the insertion sort code from Assignment 1 for this step, or use the qsort Go through the array of all POIs constructed in Stage 1, and each POI's categories. For each category (denoted by cat) of a POI, find the element of the index array whose category field matches cat. Once such an element is found, add the ID of the POI to the end of the pois array of this element.
Hint: You may use either linear search or binary search for this step. If you use binary search, you may adapt the binary search code from Assignment 1, or use the bsearch function from stdlib.h.
The output of this stage is the content of the index array, where each array element is printed in one line. Given the sample input above, the output of this stage is as follows.
Stage 4
==========
atm: 0 1 2 8 9 10 11
cafe: 7 9 10
carpark: 7 8 11
carwash: 0 5 6
cinema: 4 11
coles: 2
gas: 5
hotel: 8
movie: 4 11
petrol: 0 5 6
restaurant: 8
shop: 2 7 10
supermarket: 2 3
theatre: 4 11
woolworths: 2
Query algorithm (for analysis and not for implementation). Once the inverted index is constructed, when a query comes, we can run a binary search over the inverted index (that is, the index array) to find the array element whose category field matches the query category. Then, we can perform a linear search over the pois array of the found index array element to identify the POIs in the query range. For example, for query Q4, we only need to examine POIs #7, #9, and #10.
At the end of your submission file, you need to add a comment that states:

the worst-case time complexity to process a single query using the linear search algorithm of Stage 2,
the worst-case time complexity to process a single query using the query algorithm described in the paragraph above (without actually implementing it), and (continued on next page)

the reason why the two algorithms have those time complexities.

In your analysis, use N to denote the number of POIs, C to denote the maximum number of categories per POI, L to denote the maximum length of a category, and U to denote the number of all unique POI categories.

Reference no: EM133696375

Questions Cloud

Analyze the most significant impact of implementing : Analyze the most significant impact of implementing your costing system choice on company operations. Justify choice of a cost accounting system for a business.
Develop good clinical judgmental intuitions : To what two factors does Kahneman attribute the fact that anesthesiologists tend to develop good clinical judgmental intuitions
Should you notify your clients about software vulnerabilty : Should you notify your clients about the software vulnerability? Why or why not? What would you do to resolve this issue in the most ethical responsible way?
Refusing vaccines or are very apprehensive to consent : You have been seeing quite a few pediatric patients whose parents are refusing vaccines or are very apprehensive to consent.
Demonstrate your understanding of structures : Demonstrate your understanding of structures, linked data structures, and algo- rithm time complexity analysis. You will further extend your skills in program
Telemarketer fraud or physical and emotional abuse : How can we address elderly abuse, such as financial abuse through telemarketer fraud or physical and emotional abuse through a broken bone in a nursing home
How can trend analysis and analyzing ratios help businesses : How trend analysis and analyzing ratios help business identify area of improvement and make informed decision about resource allocation and strategic planning?
Different assessment tools used in nursing education : Examine the different assessment tools used in nursing education. Provide a response that demonstrates a thorough comparison of two types of assessment methods
What is role of artificial intelligence in telehealth : What is the role of Artificial intelligence in telehealth? How can Artificial intelligence powered technologies improve the efficiency of healthcare systems

Reviews

Write a Review

Programming Languages Questions & Answers

  Write a haskell program to calculates a balanced partition

Write a program in Haskell which calculates a balanced partition of N items where each item has a value between 0 and K such that the difference b/w the sum of the values of first partition,

  Create an application to run in the amazon ec2 service

In this project you will create an application to run in the Amazon EC2 service and you will also create a client that can run on local machine and access your application.

  Explain the process to develop a web page locally

Explain the process to develop a Web page locally

  Write functions

These 14 questions covers java class, Array, link list , generic class.

  Programming assignment

If the user wants to read the input from a file, then the output will also go into a different file . If the user wants to read the input interactively, then the output will go to the screen .

  Write a prolog program using swi proglog

Write a Prolog program using swi proglog

  Create a custom application using eclipse

Create a custom Application Using Eclipse Android Development

  Create a application using the mvc architecture

create a application using the MVC architecture. No scripting elements are allowed in JSP pages.

  Develops bespoke solutions for the rubber industry

Develops bespoke solutions for the rubber industry

  Design a program that models the worms behavior

Design a program that models the worm's behavior.

  Writing a class

Build a class for a type called Fraction

  Design a program that assigns seats on an airplane

Write a program that allows an instructor to keep a grade book and also design and implement a program that assigns seats on an airplane.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd