Data Structures and Algorithms in Java
Data Structures and Algorithms in Java
6th Edition
ISBN: 9781118771334
Author: Michael T. Goodrich
Publisher: WILEY
bartleby

Concept explainers

Expert Solution & Answer
Book Icon
Chapter 4, Problem 60P

Explanation of Solution

Experimental analysis for prefixAverage1:

With the reference to the textbook, the prefixAverage1 algorithm computes the prefix averages in quadratic time O(n2). Now let us take the given algorithm from the textbook for analysis to draw the visualization representation as follows:

Algorithm: prefixAverage1(X,n)

Input: array x on n integers

Output: Array A of prefix averages of X

//Line1

A<- new array of n integers

//Line2

for i<-0 to n-1 do

  //Line3

  S<-X[0]

  //Line4

  for j<-1 to i do

  //Line5

  S<-S+X[j]

  // Line6

  A[i]<-s/(i+1)

//Line7

return A

Explanation:

In the above algorithm,

  • The Line1 is to initialize the array of “n” integers and it takes O(n) time.
  • Next the Line2 is the outer for loop which act as counter and executed “n” number of times and it takes O(n) times to complete the loop.
  • The Line3 is assigning the initial element of array to variable “s” and this executes in O(n) time.
  • Line4 is the inner body of for loop executes based on the outer for loop and it follows sum up operation such as 1+2+3+…(n-1). So, this line executes in O(n2) time.
  • Similar to Line4 and Line 5 follows same running time O(n2) because it performs sum up operation inside the for loop.
  • On Line6, it calculates the averages for the sum of elements and this is executed in O(n) time because the operation is directly proportional.
  • On Line7, the value is returned and it is executed in constant time O(1).

Visualization representation of algorithm prefixAverage1 in log-log graph:

As the running time of prefixAverage1 is O(1+2++n) and the sum of first “n” integers is n(n+1)2

Blurred answer
Students have asked these similar questions
Perform an experimental analysis on the two algorithms & Visualize their running times as a function of the input size with a log-log chart. 1. prefixAverage1 2. prefixAverage2. I need code for experimental analysis and visualization in java.
Programming with Python:  v Implement Depth First Search (Traversal)  v Implement Breadth First Traversal For the implementation:   1. Create a directed graph with 10 Vertices   2. Add arbitrary edges among vertices to satisfy the directed graph   3. Run your program and show appropriate output (Traversal order)   4. Your implementation should be fully commented Please, submit your source code and output (Screen Capture) too.
Write a Matlab script Fibonacci.m that uses the function input to ask for an integer n, checks if n > 0 and, it is the case, computes the n-th term, an of the Fibonacci's sequence defined by ao = a₁ = 1, ak = ak-1+ak-2. with a for loop. The implementation can be done with: (i) a vector a with n+1 components (you should initialize the vector with a-zeros (1,n) before the loop)

Chapter 4 Solutions

Data Structures and Algorithms in Java

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning