Question

In: Computer Science

Someone says "All roads lead to Rome"! Given a directed graph Q (you can assume that...

Someone says "All roads lead to Rome"! Given a directed graph Q (you can assume that Q has no self-loops), define a Rome node RN to be a node m in Q such that: there is an edge from x to m for every node x != m in Q and m has no outgoing edges.

n is the number of nodes in our graph Q, assume that the graph structure is stored in an adjacency matrix.

what is a O(n) or O(n^2) algorithm for finding whether or not Q has a Rome node N? Explain why this algorithm has complexity that it does.

What other info is needed? This is all that is provided


Again, there is no other info, so "more info" does not tell me what else needs to be provided, I need to know what else is needed, this is all I have

Solutions

Expert Solution

As directly stated in the question : m is a Rome Node iff  there is an edge from x to m for every node x != m in Q and m has no outgoing edges.

We can use the above condition to check all possible Nodes and see if the fill up the criterion of a Rome Node or not. Let the abobe requirements be broken into 2 conditions.

Condition 1 : All nodes i (except m) has a directed edge to m.

Condition 2 : There are no directed edge coming out of m.

Since , we are given the Graph Q in the form of an adjacency matrix , a simple row and column check algorithm for all possible nodes would suffice the need.

Property of an Adjacency Matrix : -

A[i][j] = 1 => Edge from i to j.

A[i][j] = 0 => No Edge from i to j.

Using the above 2 conditions and the Property of Adjacency matrix , the checking can be done in the following manner.

Satisfying Condition 1 :   A Node m satisfies condition 1 when

A[1][m] = A[2][m] = A[3][m] = ..... =A[m-1][m] = A[m+1][m] = ........=A[N-1][m] = A[N][m] = 1

=>

Satisfying Condition 2 : A Node m satisfies condition 2 when

A[m][1] = A[m][2] = A[m][3] = ..... = ........=A[m][N-1] = A[m][N] = 0

=>

Using the above 2 condition satisfactions , this can be applied on each node starting from 1 to N , and return the solution when any such Node is found.

Here is the pseudocode for the same.

Input - Graph Q , Adj. Matrix A[N][N]
function findRomeNode()
     /* Initialise with -1 when No such Nodes exist */
     int node = -1;
     /* Traverse through each currentNode from 1 to N */ 
     for(int currNode : 1 to N)
     begin for
          /* Initialise Sum of Condn1 and Condn2 as 0 */
          int sum1 = 0 , sum2 = 0
          /* Traverse the currentNode column and check if there are N-1 edges incoming*/
          for(int i : 1 to N)
          begin for
               sum1 = sum1 + Adj[i][currNode];
          end for
           /* Traverse the currentNode row and check if there are 0 edges outgoing*/
          for(int i : 1 to N)
          begin for
               sum2 = sum2 + Adj[currNode][i];
          end for
          /* Return the answer if both the cond1 and condn 2 are satisfied */
          if(sum1 equals N-1 AND sum2 equals 0)
          begin if
               return currNode;
          end if
     end for
     /* When no such node exist */
     return node;
end function

The above algorithm has a O(N2) time complexity . The complexity is like this because in the worst case when no solution exists , we traversed 1 entire row and 1 entire column for each node.

Traversing 1 entire row and 1 entire column = 2N operations.

This was done for all N Nodes , hence 2N*N = 2N2 operations.

Therefore, the time complexity is O(N2)


Related Solutions

1.      If you don’t know where you are going ‘’all roads lead to your destination’’ Discuss...
1.      If you don’t know where you are going ‘’all roads lead to your destination’’ Discuss the forgone assertion in relation to the maintenance of a healthy organisational climate. ​Subject: Organisational Behavior & Management
Assume the input graph is directed but may or may not have cycle in it, that...
Assume the input graph is directed but may or may not have cycle in it, that is, may or may not be a directed acyclic graph. Consider the recursive topological sorting algorithm shown below. TopologicalSort(G) { If G has only one node v then return v. Find a node v with no incoming edge and remove v from G. Return v followed by the order returned by TopologicalSort(G). } (a) Extend the recursive algorithm shown below so it can detect...
Given a directed graph, prove that there exists an Eulerian cycle that is also a hamiltonian...
Given a directed graph, prove that there exists an Eulerian cycle that is also a hamiltonian cycle if and only if the graph is a single cycle.
You are given a directed graph G(V,E) with n vertices and m edges. Let S be...
You are given a directed graph G(V,E) with n vertices and m edges. Let S be the subset of vertices in G that are able to reach some cycle in G. Design an O(n + m) time algorithm to compute the set S. You can assume that G is given to you in the adjacency-list representation.
Question 5: Say that we are given a directed graph with costs 1 or 2 on...
Question 5: Say that we are given a directed graph with costs 1 or 2 on every edge and a vertex s. Give the fastest algorithm you can to find the distance from s to all the vertices in V − s. Please explain the algorithm in words, not pseudocode. Thanks.
can someone explain how weight training can be modeled in a graph?,
can someone explain how weight training can be modeled in a graph?,
In this program you will read a file specifying a directed graph G as a list...
In this program you will read a file specifying a directed graph G as a list of edges. Each edge u →v is listed on one line as u v. The input file simply lists the edges in arbitrary order as pairs of vertices, with each edge on a separate line. The vertices are numbered in order from 1 to the total number of vertices. The program outputs the out-degree sequence for GSCC in increasing order. For example for the...
In this program you will read a file specifying a directed graph G as a list...
In this program you will read a file specifying a directed graph G as a list of edges. Each edge u →v is listed on one line as u v. The input file simply lists the edges in arbitrary order as pairs of vertices, with each edge on a separate line. The vertices are numbered in order from 1 to the total number of vertices. The program outputs the out-degree sequence for GSCC in increasing order. For example for the...
Consider a relation from daily life that can be represented in a directed acyclic graph (DAG)....
Consider a relation from daily life that can be represented in a directed acyclic graph (DAG). Describe the relation in words and draw the directed acyclic graph. Give a topological sort of the directed acyclic graph.
# Problem Description Given a directed graph G = (V,E) with edge length l(e) > 0...
# Problem Description Given a directed graph G = (V,E) with edge length l(e) > 0 for any e in E, and a source vertex s. Use Dijkstra’s algorithm to calculate distance(s,v) for all of the vertices v in V. (You can implement your own priority queue or use the build-in function for C++/Python) # Input The graph has `n` vertices and `m` edges. There are m + 1 lines, the first line gives three numbers `n`,`m` and `s`(1 <=...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT