Question

In: Computer Science

A tree can be considered as a structured graph. Write a C++ function that receives a...

A tree can be considered as a structured graph. Write a C++ function that receives a BST object and returns its corresponding graph representation, implemented via an adjacency list. What will be the impact on the search complexity?

Solutions

Expert Solution

// A utility function to print the adjacenncy list representation of graph
void printGraph(struct Graph* graph)
{
    int v;
    for (v = 0; v < graph->V; ++v)
    {
        struct AdjListNode* pCrawl = graph->array[v].head;
        printf("\n Adjacency list of vertex %d\n head ", v);
        while (pCrawl)
        {
            printf("-> %d", pCrawl->dest);
            pCrawl = pCrawl->next;
        }
        printf("\n");
    }
}

The important thing here is that for the time complexity to be valid, we need to cover every possible situation:

  • The outer loop is executed O(|V|) regardless of the graph structure.
    • Even if we had no edges at all, for every iteration of the outer loop, we would have to do a constant number of operations (O(1))
    • The inner loop is executed once for every edge, thus O(deg(v)) times, where deg(v) is the degree of the current node.
    • Thus the runtime of a single iteration of the outer loop is O(1 + deg(v)). Note that we cannot leave out the 1, because deg(v) might be 0 but we still need to do some work in that iteration
  • Summing it all up, we get a runtime of O(|V| * 1 + deg(v1) + deg(v2) + ...) = O(|V| + |E|).

Related Solutions

C++ Write the implementation of the function concatenateIntArrays. This function receives 4 parameters in the following...
C++ Write the implementation of the function concatenateIntArrays. This function receives 4 parameters in the following order: An array of integer values (array1). An integer representing the size of array1 (size1). An array of integer values (array2). An integer representing the size of array2 (size). The function creates a dynamic array of integers of size size1+size2 to store all the values in array1, followed by all the values in array2. The function returns the pointer used to create the dynamic...
C++ PLEASE Write a new program called Lab15A Write a void function named fillArray that receives...
C++ PLEASE Write a new program called Lab15A Write a void function named fillArray that receives an array of 10 integers as a parameter and fills the array by reading the values from a text file (Lab15A.txt). It should also print the values of the array all on the same line, separated by spaces. Write an int function named findLast that receives an array of 10 integers as a parameter and returns the last negative number in the array. Write...
a. Write a c++ function called IsPalindrome that receives a 3-digit number and returns whether it...
a. Write a c++ function called IsPalindrome that receives a 3-digit number and returns whether it is palindrome or not. [2.5 marks] b. Write a c++ function, called GCD that receives two parameters n, d, and prints their greatest common divisor [2.5 marks] Now after you created the two functions, you have to write a main function that can call the above functions according to user choice. Write a menu that allows the user to select from the following options...
Just need the desktop test for this two function a) Write a function that receives the...
Just need the desktop test for this two function a) Write a function that receives the number of terms (n) and returns a container with the first n TunaPoke numbers. Present a "print screen" of the results and their respective desktop test when the function is invoked with the 16. (b) Write a function that receives a limit number and returns a container with the first numbers TunaPoke that do not exceed the specified limit. Present a "print screen" of...
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree...
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string. The binary tree is sorted by the integer value. The functions include: • Insert into the binary tree. This function will take in as parameters: the root of the tree, the integer value, and the string. Note that this function requires you to create the node. • Find a node by integer value: This function takes in two...
Write a program to create a tree randomly. You can use C++ programming language. The input...
Write a program to create a tree randomly. You can use C++ programming language. The input is the number of vertices in the tree, and the output is an adjacent list of the tree. (Managed to complete this assignment with a binary tree. But, was told I needed a general tree instead)
In PYTHON: Write a function that receives a sentence and returns the last word of that...
In PYTHON: Write a function that receives a sentence and returns the last word of that sentence. You may assume that there is exactly one space between every two words, and that there are no other spaces at the sentence. To make the problem simpler, you may assume that the sentence contains no hyphens, and you may return the word together with punctuation at its end.
Write a method for binary tree in Python that can determine whether a binary tree is...
Write a method for binary tree in Python that can determine whether a binary tree is a binary search tree or not. The input should be a binary tree. The output should be true or false. True= binary tree meets the criteria to be a binary search tree. False= does not meet the criteria to be a binary search tree.
write a function that takes as input the root of a general tree and returns a...
write a function that takes as input the root of a general tree and returns a binary tree generated by the conversion process illustrated in java
"sum_between" function Write a function named "sum_between" that receives 2 parameters - "start" (an int) and...
"sum_between" function Write a function named "sum_between" that receives 2 parameters - "start" (an int) and "end" (an int). It should return the sum (total) of all of the integers between (and including) "start" and "end". If "end" is less than "start", the function should return -1 instead. e.g. if you give the function a start of 10 and an end of 15, it should return 75 (i.e. 10+11+12+13+14+15)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT