Question

1. explain what the worst case for the algorithm is 2.explain how you computed the time...

1. explain what the worst case for the algorithm is

2.explain how you computed the time complexity

3.give the order of the time complexity of the algorithm

Code:

public class Degree {
    int degree; 
    public int maxDegree(Node node) {
        degree = 0; 
        if(node == null)
            return degree;
        findMaxDegree(node);
        return degree;
    }
     private void findMaxDegree(Node node) {
        if(node == null || node.numChildren() == 0)
            return;
        degree = Math.max(degree, node.numChildren());
        Node[] children = node.getChildren();
        for(int i=0; i<children.length; i++) {
            findMaxDegree(children[i]);
        }
    }
}

Homework Answers

Answer #1
  • The worst case for this algorithm is also the average case for it where we will travel through all the nodes in the tree.
  • Explantion of how we computed the time complexity​​​​​​​
    • This piece of code does the following
      • For every node, it calculates its degree and updates the max degree variable if the new degree found is greater.
    • Since, we travel through all the node and all of its children in recursion, the time complexity for this will be the number of nodes in the tree.
  • Hence the time complexity for this algorithm is Big-O( n ) where n is the number of nodes in the tree.

Example

say we have this tree with 10 nodes labeled 1 through 10

On executing the above function following will be the recursion flow

  • maxDegree(1)
    • findMaxDegree(1)
      • findMaxDegree(2)
        • findMaxDegree(6)
      • findMaxDegree(3)
        • findMaxDegree(7)
        • findMaxDegree(8)
      • findMaxDegree(4)
        • findMaxDegree(9)
        • findMaxDegree(10)
      • findMaxDegree(5)

We can see that the function findMaxDegree was called with all the nodes once, Hence order is O(number of nodes)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm that is executed most frequently. Express the number of times it is executed as a function of N. Convert this expression into the Big-O notation. A. For each of the three fragments of code, what is its worst-case time complexity, in the form "O(…)". (Use the given solution to the first problem as a model)                 //----------------- This is a sample problem – solved ------...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue class . Call your class Queue. it must be a template class. public class Queue { } I have put a driver program in the module . It is called CircularQueue.java This driver program should then run with your Queue class (no modifications allowed to the driver program). Your Queue class should have at least the following methods: one or more constructors, enqueue, dequeue,...
Analyze the worst case running time of the following code in “big-Oh” notation in terms of...
Analyze the worst case running time of the following code in “big-Oh” notation in terms of the variable n. a. void fdisp1(int n) { for(int i=0; i < n; i++) { for(int j=0; j < n; j++) { for(int k=0; k < n; k++) { for(int m=0; m < n; m++) { System.out.println("Hi I am Here"); } } } } } b. voidprintAllPossibleOrderedPairs(intarr[], int size) { for (int i = 0; i < size; i++) { for (int j =...
How do I implement this method BalancedByNodeCount() ? public class BinarySearchTree { private Node root; private...
How do I implement this method BalancedByNodeCount() ? public class BinarySearchTree { private Node root; private boolean isBalancedByNodeCount() { /**************************************************************************** Implement this method and replace the return statement below with your code. * Definition of Balance tree (On page 372 of book): * An unbalanced tree is created when most of the nodes are on one side of the root or the other. ****************************************************************************/    return false; }       public static void main(String args[]) { int[] values1 = {50,...
Finish the code wherever it says TODO /**    * Node type for this list. Each...
Finish the code wherever it says TODO /**    * Node type for this list. Each node holds a maximum of nodeSize elements in an    * array. Empty slots are null.    */    private class Node {        /**        * Array of actual data elements.        */        // Unchecked warning unavoidable.        public E[] data = (E[]) new Comparable[nodeSize];        /**        * Link to next node.       ...
You are given a reference to the root node of a binary search tree, that implements...
You are given a reference to the root node of a binary search tree, that implements a dictionary data structure. Please print all the elements in depths 500 through 510, all in sorted order. A node in a binary search tree is at depth x, if it takes x hops to get from the root. So the root is at depth 0, the children of the root are at depth 1, and so on. The class TreeNode defines a single...
Use Java: Also: Please include screenshots if possible. Create a class called AbstractStackTest. Write an abstract...
Use Java: Also: Please include screenshots if possible. Create a class called AbstractStackTest. Write an abstract method called makeStack that returns a Stack of Strings. Use the Stack interface as the return type, not a specific implementation! Write a class named NodeStackTest that extends your AbstractStackTest class. Implement the makeStack method to return a NodeStack. Repeat parts 1 and 2 for the Queue interface and the NodeQueue implementation. Write a new stack implementation, ArrayStack. It should be generic and use...
Copy the file SimplerBST.java from the week 1 examples package and rename the file and class...
Copy the file SimplerBST.java from the week 1 examples package and rename the file and class A3BST. Add two public methods: one named countTwins that takes no parameters and that, when called, traverses every node of the tree to count the number of nodes having two children. one named lessThanValueCount that takes a paremeter of generic type V, and that, when called, traverses every node of the tree to count the number of nodes whose value (not its key) is...
1.) the following code fragments give running time analysis (Big Oh). Explain your answer: sum2 =...
1.) the following code fragments give running time analysis (Big Oh). Explain your answer: sum2 = 0; sum5 = 0; for(i=1; i<=n/2; i++) { sum2 = sum + 2; } for(j=1; j<=n*n; j++) { sum5 = sum + 5; } i think it is O(n^2) since big oh finds the order of worst case which is the the second loop being n^2 complexity. but im not sure. 2.) Give an efficient algorithm along with running time analysis to find the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT