It is a vertex v such that all other vertices in G can be reached by a path from v.
Solution:
step1: first we need to do DFS on each vertex and get the value of vertex when all value of visited array true.
step2: need to DFS on selected value if all the vertex visited it means the value is mother vertex.
public class MotherVertex {
public MotherVertex() {
}
void addEdge(ArrayList<ArrayList<Integer>> adj, int v, int u){
adj.get(v).add(u);
}
public static void main(String[] args) {
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
for(int i=0;i<7;i++){
adj.add(new ArrayList<>());
}
MotherVertex g = new MotherVertex();
g.addEdge(adj,0, 1);
g.addEdge(adj,0, 2);
g.addEdge(adj,1, 3);
g.addEdge(adj,4, 1);
g.addEdge(adj,6, 4);
g.addEdge(adj,6, 0);
g.addEdge(adj,5, 2);
g.addEdge(adj,5, 6);
g.motherVertex(adj,7);
}
void motherVertex(ArrayList<ArrayList<Integer>> adj,int V){
public MotherVertex() {
}
void addEdge(ArrayList<ArrayList<Integer>> adj, int v, int u){
adj.get(v).add(u);
}
public static void main(String[] args) {
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
for(int i=0;i<7;i++){
adj.add(new ArrayList<>());
}
MotherVertex g = new MotherVertex();
g.addEdge(adj,0, 1);
g.addEdge(adj,0, 2);
g.addEdge(adj,1, 3);
g.addEdge(adj,4, 1);
g.addEdge(adj,6, 4);
g.addEdge(adj,6, 0);
g.addEdge(adj,5, 2);
g.addEdge(adj,5, 6);
g.motherVertex(adj,7);
}
void motherVertex(ArrayList<ArrayList<Integer>> adj,int V){
//dfs on all vertex
boolean[] visited = new boolean[V];
for(int i=0;i<visited.length;i++){
visited[i]=false;
}
int val= -1;
for(int i=0;i<V;i++){
if(!visited[i]) {
DFS(i, adj, visited);
val = i;
}
}
boolean[] visited = new boolean[V];
for(int i=0;i<visited.length;i++){
visited[i]=false;
}
int val= -1;
for(int i=0;i<V;i++){
if(!visited[i]) {
DFS(i, adj, visited);
val = i;
}
}
//selected value when visited array value get true
System.out.println(val);
//dfs on selected value
visited = new boolean[V];
DFS(val,adj,visited);
for (int i=0;i<7;i++){
if(!visited[i]){
System.out.println("not found mother vertex");
return;
}
}
System.out.println(val);
//dfs on selected value
visited = new boolean[V];
DFS(val,adj,visited);
for (int i=0;i<7;i++){
if(!visited[i]){
System.out.println("not found mother vertex");
return;
}
}
//if all value of visited array is true then its mother vertex
System.out.println("mother vertex"+val);
}
void DFS(int v,ArrayList<ArrayList<Integer>> adj,boolean[] visited){
visited[v]=true;
// System.out.println(v);
ArrayList<Integer> list = adj.get(v);
Iterator<Integer> iterator = list.iterator();
while (iterator.hasNext()){
Integer i = iterator.next();
if(!visited[i]){
DFS(i,adj,visited);
}
}
}
}
System.out.println("mother vertex"+val);
}
void DFS(int v,ArrayList<ArrayList<Integer>> adj,boolean[] visited){
visited[v]=true;
// System.out.println(v);
ArrayList<Integer> list = adj.get(v);
Iterator<Integer> iterator = list.iterator();
while (iterator.hasNext()){
Integer i = iterator.next();
if(!visited[i]){
DFS(i,adj,visited);
}
}
}
}
output:
5
mother vertex 5
No comments:
Post a Comment