Question

In: Computer Science

for directed un-weighted graph : Implement the following member function: int MyGraphAss3::findTopV(). This function returns a...

for directed un-weighted graph :

Implement the following member function: int MyGraphAss3::findTopV(). This function returns a vertex that can reach all other vertices in the graph by a path starting from this vertex. There can be more than one top vertex in a graph. Just output one of them.

(in c++)

Solutions

Expert Solution

To implement the given member function: int MyGraphAss3::findTopV(), we will use DFS algorithm while finding a vertex that can reach all other vertices.

Note : If incase there is no top vertex in the given graph, then the following function will return -1.
int MyGraphAss3::findTopV()
{
   vector <bool> visited(V, false); //An array to keep track of visited nodes, here false indicates not visited vertices
   int v = 0; // Here v is a variable to store top vertex
   for (int i = 0; i < V; i++)
   {
       if (visited[i] == false) {//If node is not visited
           DFS(i, visited);
           v = i;
       }
   }
   fill(visited.begin(), visited.end(), false); // At this step we will reset the visited array as false
   DFS(v, visited);
   for (int i=0; i<V; i++)
       if (visited[i] == false)
           return -1;
   return v;
}

Sample output:


Related Solutions

Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted...
Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted in either ascend order or descend order; false otherwise. c++
. Provide a weighted directed graph G = (V, E, c) that includes three vertices a,...
. Provide a weighted directed graph G = (V, E, c) that includes three vertices a, b, and c, and for which the maximum-cost simple path P from a to b includes vertex c, but the subpath from a to c is not the maximum-cost path from a to c
The primary function of the Bank of Utopia is to formulate and implement monetary policy directed...
The primary function of the Bank of Utopia is to formulate and implement monetary policy directed to the economic objective of achieving and maintain stability in the general level of prices. Do you think this statement constitutes a hierarchical or dual mandate for the conduct of monetary policy? (Note: justify your answer with an explanation).
Python 3 question Suppose that a directed and weighted graph represented as vertex-list, how could it...
Python 3 question Suppose that a directed and weighted graph represented as vertex-list, how could it perform uniform cost search by using priority queue?
Write a Haskell function combine :: Int -> Int -> Int -> Int with the following...
Write a Haskell function combine :: Int -> Int -> Int -> Int with the following behavior: • When x, y, and z all correspond to digit values (i.e., integers between 0 and 9, inclusive), combine x y z returns the integer given by the sequence of digits x y z. (That is, x is treated as the digit in the hundreds place, y is treated as the digit in the tens place, and z is treated as the digit...
Using C++: Create a function to search an undirected weighted graph and find the highest weighted...
Using C++: Create a function to search an undirected weighted graph and find the highest weighted edge between two specific values. This should include a class declaration and the definition for all required members that are needed to support the search. NO NEED to code those members. You can assume any other data structures such as stack, heap, or linked list is defined (so you can use their standard functions without declaring or defining them).
In c++ A binary search member function of SortedType has the following prototype: void.SortedType::BinarySearch(int value, bool&...
In c++ A binary search member function of SortedType has the following prototype: void.SortedType::BinarySearch(int value, bool& found); Write the function definition as a recursive search, assuming an array-based implementation. Add a main function as a driver to test the BinarySearch. Test the edge cases.
Consider the following function: int counter( int n) { int s = 0;    for ( int...
Consider the following function: int counter( int n) { int s = 0;    for ( int i = 0; i < n; i++)             for ( int j = i; j > 0; j--)                        s = s + 1;   return s; } Part (a): How many times "s=s+1;" will be executed? Determine a precise count.  Show all your work. Part (b): What is the time complexity of the "counter" function in terms of Big-O notation? Justify and show all your work.
/*Use recursion in the function: void getDigit( int num) /* Try this program and implement the...
/*Use recursion in the function: void getDigit( int num) /* Try this program and implement the function void getDigit( intnum) so that it displays the digits in a given number. For example, the number, 1234 consist of 1, 2, 3 and 4. This is an exercise to demonstate the use of a recursive function and the use of recusrion in C++ */ #include #include using namespace std; int main() {      int num;      cout <<"Enter an integer: ";     ...
implement this search function using CPP Coding language bool binarySearch(vector<int>vec,int low,int high, int search) Test your...
implement this search function using CPP Coding language bool binarySearch(vector<int>vec,int low,int high, int search) Test your code in main
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT