Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help needed C++ programming. please read the Assignment Overview carefully as it instruct what is allowed to be used for this code in bold. Assignment

Help needed C++ programming. please read the Assignment Overview carefully as it instruct what is allowed to be used for this code in bold.

Assignment Overview

This assignment focuses on the design, implementation and testing of an object oriented C++ program that use a user-defined class, file processing, built-in arrays and pointers. It is an extensive program that needs to be developed and tested incrementally.

Assignment Specifications

Have you ever wondered how social media sites suggest friends to you? Well, most of the sites do indeed use highly sophisticated algorithms, but for this project you will implement a naive method to recommend the most likely new friend to users of a social network.

Background

A social network such as Facebook consists of a set of users and connections between the users (i.e., there exists a connection between you and everyone who you have befriended). An interesting problem is to write a computer program that can automatically suggest possible new connections (i.e., friends) for each user. For this project you will implement a system that will, for each user, suggest the most probable user to befriend based upon the intersection of your common friends. In other words, the user that you will suggest to Person A is the person who has the most friends in common with Person A, but who currently is not friends with Person A.

The algorithm begins with: for each user go through all the other users (another for) and calculate the number of friends they have in common. Then for a given user the friend you will suggest to them is the user in the social network who they are currently not friends with, but have the most friends in common. Intuitively, it makes sense why they might want to be connected as friends. (Note that when you look for your most common friend in this scheme it will be you, i.e. you will have to remember to remove yourself from consideration.)

Project Description / Specification

Two files have been provided for you to run your program with. One is a small test file containing a made-up set of users and connections between them (that file is networkdata.txt).

https://s2.smu.edu/~etchison/cse1342/networkdata.txt

The other is a subset of a real Facebook dataset (facebook_1000_data.txt) .

https://s2.smu.edu/~etchison/cse1342/facebook_1000_data.txt

The format of both files is the same:

The first line of the file is an integer representing the number of users in the given network.

The following lines are of the form: user_u user_v

where user_u and user_v are the IDs of two users who are friends.

For example, here is a very small file that has 5 users in the social network:

5

0 1

1 2

1 4

2 3

The above is a representation of a social network that contains 5 users.

User ID=0 is friends with User IDs = 1

User ID=1 is friends with User IDs = 0, 2, 4

User ID=2 is friends with User IDs = 1, 3

User ID=3 is friends with User IDs = 2

User ID=4 is friends with User IDs = 1

2) You will create a class called SocialNetwork. Data members of the class should be a

ifstream infile file object for reading the file

vector * network_data a pointer to a vector of ints

int **similarity_matrix a pointer to a pointer to an int to use in the creation of a two dimensional array

int size; the number of friend ids in the file

Methods within your class should be as follows:

A constructor that receives nothing and prompts the user to enter a file name. The constructor will try to open one of the network data files entered by the user. An appropriate error message should be shown if the data file cannot be opened. This function will loop until it receives proper input and successfully opens the file. It will then read the first line of the file (the number of users in the social network) and dynamically create an array of vectors of integers, the size equal to the data in the first line of the file.

network_data = new vector[size];// this creates an array of size number of vectors

Additionally, the constructor should call the init_matrix() method supplied below that creates a size by size two dimensional similarity_matrix and initializes it to zeroes.

getSize method

returns the size attribute

readFile method

For the remaining lines in the file, you will want to obtain the two user id values on that linewe denote those user ids as u and v. You need to place v into us vector and also place u into vs vector, since they are mutual friends (hint: use u and v as indexes to the correct position of the array of vectors and the push_back method to place each user id into the appropriate vector.

display method

Displays in a nicely formatted and readable style all the network data within the dynamically allocated array of vectors (see an example in the output below).

init_matrix method

The method will dynamically allocate a matrix (n x n) where n is the number of friends in the file and set all elements to zero.

void SocialNetwork::init_matrix()

{ similarity_matrix = new int*[size];

for (int i=0; i < size; i++)

similarity_matrix[i]= new int[size];

int j;

for (int i=0; i < size; i++)

{ for ( j =0; j < size; j++);

{ similarity_matrix[i][j] = 0;}

}

}

calc_similarity_scores method

creates a similarity matrix for all the users in the social network. Here the definition of similarity is the number of friends that any pair of users have in common

Use two nested for loops to iterate over all pairs of users in the network_data. For each pair of users you can obtain their vector of friends from the network_data, and then call the function num_in_common_between_vectors (vector1,vector2), which will return an integer, num_in_common, representing the number of friends those two users have in common. The int num_in_common will need to be stored in locations [user1][user2] and [user2][user1] of the similarity_matrix.

Suggestion, to iterate over all pairs of users use the following:

for i in range(n):

for j in range(n):

# call num_in_common_between_vectors using user is vector and user js vector as arguments

num_in_common_between_vectors(vector1, vector2) is a function that calculates the number of common items between two vectors. To do this you can have a for loop that iterates over the items of vector1 and then an if statement that checks whether or not that item is also in vector2. This function is to return an integer, which is the number of items that the two vectors had in common (i.e., the number of items that were found in both of the vectors).

(e.g., if vector1 = [1,3,5,6] and vector2 = [1, 2, 3], then the value 2 would be returned since they have two items in common (the values 1 and 3)).

recommend(user_id) This user_id will be used as an index into the similarity_matrix which will give you access to the row of similarity scores for that given user (user_id) for all other users. You should then determine the largest value in this row and return its index as the most similar. Of course, you dont want to recommend someone who is already a friend and it also doesnt make sense to recommend the person as his or her own friend. It is the most similar because by having the largest value in the row it means that it is the user who had the most friends in common with user_id. (Hint: while developing this function using the small network file you should print out similarity_matrix[user_id] and network_data[user_id] so you can check that the correct recommendation was calculated.)

Main Driver (p4.cpp)

main() In the main program you will need to first create a SocialNetwork object, then on that object call open_file(), followed by calling read_file(f), and then calc_similarity_scores(). After obtaining the similarity_matrix you will need to have a while loop that will repeatedly ask the user for a user_id that should be in the range of [0,n-1] (i.e., from 0 to n-1, inclusive). Call recommend to get a recommendation.

Note that when prompting the user for the user_id you should validate that the input is in the correct range, which is 0 to n-1. Display an error message if invalid and ask for the id again.

After displaying the suggested friend for the user that had their id as user_id you should prompt the user if they want to see the suggestion for another user input or if they want to exit. To exit the program you should accept all forms of the string no (i.e., NO, No, nO, and no).

Sample Outputs

Function Test 1

(Tests the readFile function; reads the file networkdata.txt)

network_data printed nicely:

0 : [1, 2, 3]

1 : [0, 4, 6, 7, 9]

2 : [0, 3, 6, 8, 9]

3 : [0, 2, 8, 9]

4 : [1, 6, 7, 8]

5 : [9]

6 : [1, 2, 4, 8]

7 : [1, 4, 8]

8 : [2, 3, 4, 6, 7]

9 : [1, 2, 3, 5]

Function Test 2

(Tests the num_in_common_between_vectors function; uses network_data[1] and network_data[2] from Test 1 for first test)

should return a 3

Function Test 3

(Tests the calc_similarity_scores function)

Similarity_matrix printed nicely:

0 : [3, 0, 1, 1, 1, 0, 2, 1, 2, 3]

1 : [0, 5, 3, 2, 2, 1, 1, 1, 3, 0]

2 : [1, 3, 5, 3, 2, 1, 1, 1, 2, 1]

3 : [1, 2, 3, 4, 1, 1, 2, 1, 1, 1]

4 : [1, 2, 2, 1, 4, 0, 2, 2, 2, 1]

5 : [0, 1, 1, 1, 0, 1, 0, 0, 0, 0]

6 : [2, 1, 1, 2, 2, 0, 4, 3, 2, 2]

7 : [1, 1, 1, 1, 2, 0, 3, 3, 1, 1]

8 : [2, 3, 2, 1, 2, 0, 2, 1, 5, 2]

9 : [3, 0, 1, 1, 1, 0, 2, 1, 2, 4]

Function Test 4

(Tests the recommend function; uses similarity_matrix from Test3)

person,friend: 3 1

person,friend: 0 9

person,friend: 1 2;

(note that an alternative correct answer for 3 is 6 there was a tie)

Facebook friend recommendation.

Enter a filename: small_network_data.txt

Enter an integer in the range 0 to 9 : 0

The suggested friend for 0 is 9

Do you want to continue (yes/no)? yes

Enter an integer in the range 0 to 9 : 3

The suggested friend for 3 is 1

Do you want to continue (yes/no)? yes

Enter an integer in the range 0 to 9 : 8

The suggested friend for 8 is 1

Do you want to continue (yes/no)? No

Test 2

Facebook friend recommendation.

Enter a filename: xxxxxx

Error in filename.

Enter a filename: small_network_data.txt

Enter an integer in the range 0 to 9 : abc

Error: input must be an int between 0 and 9

Enter an integer in the range 0 to 9 : -1

Error: input must be an int between 0 and 9

Enter an integer in the range 0 to 9 : 15

Error: input must be an int between 0 and 9

Enter an integer in the range 0 to 9 : 10

Error: input must be an int between 0 and 9

Enter an integer in the range 0 to 9 : 0

The suggested friend for 0 is 9

Do you want to continue (yes/no)? nO

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions