Question

In: Computer Science

Suppose that you have a program that can read through all posts within a given time...

  1. Suppose that you have a program that can read through all posts within a given time period (e.g. the last 7 days) on a social network web site (e.g., Facebook), and generate a list of pairs (ui, pi) representing texts in all posts found, where i=1, 2, ..., and ui and pi represent the author username and the text content of the i-th post found, respectively. Describe an algorithm that outputs the list of usernames in the decreasing order of their total numbers of words posted among the posts described above. Analyze the running time of your algorithm. You may assume that the input of your algorithm is two arrays U and P that store the pairs (ui, pi) as defined above.

Please answer differently than solution already on chegg, I would like explanations to steps. Thank you.

Solutions

Expert Solution

Algorithm for above will be :-

1) Funtion which takes two input , one is the author array , and other is the posts array.

2)This functions returns the authors array , in the decreasing order of their words in the post.

3)The function will contain :-

  • Call another function , passing the posts array.
    • In this function , start a loop over the posts array.
    • In each iteration calculate the number of words in each post[i] ( i being the iterator).
    • You can easily calculate the nmber of words using the stringstream (this is present in C++).
    • Create another array postlength , which will contain the number of words in each post[i].
    • Return the postlength array from this function.
  • Accept the postlength array returned from above funtion.
  • Now, you will have two arrays , author array(containing the authors name) and secondly postlength array(contianing the number of words in each posts).
  • Now, initialise a map , which will contain author's name as key and number of words as value.
  • Now start a loop over the author's array. In each itereation you will do :-
    • If the author[i] ( i being the iterator) is present in the map's key , just fetch the value from the map , corresponding to the author[i] and add the postlenght[i] value to the value retrieved. Now insert new value with the author[i] in the map.
    • If the author[i] is not present in the map's key, insert the pair(author[i],postlength[i]) in the map.
  • Now sort the map according to the values, and return the map's key by storing them in an array.
  • For sorting the map you can use the insertion sort , where just create a new array and insert the map's key according to its value in the map.
  • The time complexity willl be O(n^2) because of the time complexity of insertion sort which will be maximum and for other loops it will be O(n) as we are iterating over the array once only in each loops . So time complexity is max of all time complexity which will be O(n^2).

This solution is according to me , and if you need any help , just comment , I will be happy to help.


Related Solutions

Write a JAVA program read a number within the range of 1 through 1000 to display...
Write a JAVA program read a number within the range of 1 through 1000 to display the Roman numeral version of that number. Include screenshot of the output
Read the input one line at a time until you have read all lines. Now output...
Read the input one line at a time until you have read all lines. Now output these lines in the opposite order from which they were read.. import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; public class Part12 {       /**    * Your code goes here - see Part0 for an example    * @param r the reader to read from    * @param w the writer to write to    * @throws IOException...
Draw the cache tables and the state of all bits within them. Suppose you have a...
Draw the cache tables and the state of all bits within them. Suppose you have a 16 byte cache with 2 byte long cachelines that is 2-way set associative and write-back. Further assume that prior to processing any read/write requests the state of memory is M[a] = a, or another words, the byte at address 0 is 0, address 1 is 1, address 2 is 2, and so on. Assume an *8 bit* long address. Along with depicting the cache...
How can you apply some of the concepts regarding team work this week through writing posts?...
How can you apply some of the concepts regarding team work this week through writing posts? How can other students apply team building and critical thinking when posting? Minimum 100 Words
Suppose you have to specify the moment in time when a given event occurred, a "zero...
Suppose you have to specify the moment in time when a given event occurred, a "zero time". The record must be accurate to the minute, and be obtainable even after thousands of years. All the measures of time we currently have are relative to a well defined zero, but the zero is not easy to backtrack exactly. One possibility would be to take a sample of Carbon with a well defined, very accurate amount of 14C, and say: the event...
Suppose you have to specify the moment in time when a given event occurred, a "zero...
Suppose you have to specify the moment in time when a given event occurred, a "zero time". The record must be accurate to the minute, and be obtainable even after thousands of years. All the measures of time we currently have are relative to a well defined zero, but the zero is not easy to backtrack exactly. One possibility would be to take a sample of Carbon with a well defined, very accurate amount of 14C, and say: the event...
I believe you have all been through the D.A.R.E. program in high school and college orientations...
I believe you have all been through the D.A.R.E. program in high school and college orientations with the warnings of alcohol, what 2 things stood out
Have you ever signed a contract that you may not have, had you really read through...
Have you ever signed a contract that you may not have, had you really read through the entire document and understood all of the terms (e.g. a gym membership, magazine subscription, or cell phone service?) What happened and what might you do differently next time?
Suppose you have a production technology that can be characterized by a learning curve. Every time...
Suppose you have a production technology that can be characterized by a learning curve. Every time you increase production by 1 unit, your costs decrease by $6. The first unit costs you $64 to produce. 1)You estimate you can win another project for two more units. Have your returns diminished? Why or why not? 2)If you receive a request for 4 units, what is your break-even price?
Write a java program that can create, read, and append a file. Assume that all data...
Write a java program that can create, read, and append a file. Assume that all data written to the file are string type. One driver class and a class that contains the methods. The program should have three methods as follows: CreateFile - only creating a file. Once it create a file successfully, it notifies to the user that it creates a file successfully. ReadingFile - It reads a contents of the file. This method only allow to read a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT