You are on page 1of 5

package com.interanshala.

javaApp;

import java.util.*;

public class Main {

public static void addVertex(ArrayList<ArrayList<Integer>> gp,int u,int v){


gp.get(u).add(v);
// gp.get(v).add(u);
}

public static void addVertex(ArrayList<ArrayList<Integer>>


gp,ArrayList<ArrayList<Integer>> gp1,int u,int v){
gp.get(u).add(v);
gp1.get(v).add(u);
}

public static void main(String[] args) {


// write your code here
Scanner in = new Scanner(System.in);
int v = in.nextInt();
ArrayList<ArrayList<Integer>> graph = new ArrayList<ArrayList<Integer>>();
ArrayList<ArrayList<Integer>> graph1 = new
ArrayList<ArrayList<Integer>>();
for(int i=0;i<v;i++){
graph.add(new ArrayList<Integer>());
graph1.add(new ArrayList<Integer>());
}
int b =65;

// addVertex(graph,5,2);
// addVertex(graph,5,0);
// addVertex(graph,4,0);
// addVertex(graph,4,1);
// addVertex(graph,2,3);
// addVertex(graph,3,1);

/*for(int i=0;i<graph.size();i++){
System.out.print((char)(i+65));
for(int j=0;j<graph.get(i).size();j++){
System.out.print(" " +(char)(graph.get(i).get(j)+65));
}
System.out.println();
}*/

//BFS
// findShortestPath(graph,0);
// findCycle(graph,0);
// findConnectedComponent(graph);

//DFS
// dfs(graph);
// checkConnected(graph,graph1);
}

private static void checkConnected(ArrayList<ArrayList<Integer>>


gp,ArrayList<ArrayList<Integer>> gp1) {
addVertex(gp,gp1,0,1);
addVertex(gp,gp1,0,2);
addVertex(gp,gp1,1,2);
addVertex(gp,gp1,3,3);
boolean[] visited1 = new boolean[gp.size()];
boolean[] visited2 = new boolean[gp1.size()];
Arrays.fill(visited1,false);
Arrays.fill(visited2,false);
dfs1(gp,visited1,0);
dfs1(gp1,visited2,0);

for(int i=0;i<gp.size();i++){
if(!visited1[i] && !visited2[i]){
System.out.println("Not a connected Graph");
return;
}
}
System.out.println("Connected Graph");

public static void dfs1(ArrayList<ArrayList<Integer>> graph,boolean[]


visited,int i){
visited[i]=true;
//System.out.println(i+" true");
for(int j=0;j<graph.get(i).size();j++){
if(!visited[graph.get(i).get(j)]){
dfs1(graph,visited,graph.get(i).get(j));
}
}
}
public static void dfs(ArrayList<ArrayList<Integer>> graph){
boolean[] visited = new boolean[graph.size()];
boolean[] backEdge = new boolean[graph.size()];
Arrays.fill(backEdge,false);
Arrays.fill(visited,false);
Stack<Integer> stack = new Stack<>();
boolean cycle = false;
for(int i=0;i<graph.size();i++){
if(!visited[i]){
topologicalSort(graph,stack,i,visited);

// if(findCycleDfs(graph,visited,backEdge,i)) {
// cycle=true;
// }
}
}
// if(cycle){
// System.out.println("Graph contains Cycle");
// }else{
// System.out.println("Not found any Cycle");
// }
while(!stack.empty()){
System.out.print(stack.pop()+" ");
}

// Method to find Cycle using Dfs


private static boolean findCycleDfs(ArrayList<ArrayList<Integer>> graph,
boolean[] visited, boolean[] backEdge, int i) {
if(backEdge[i]){
return true;
}

if(visited[i]){
return false;
}
visited[i]=true;
backEdge[i]=true;
for(int j=0;j<graph.get(i).size();j++){
if(findCycleDfs(graph,visited,backEdge,graph.get(i).get(j))){
return true;
}
}
backEdge[i]=false;
return false;
}

// Method for Topological order


public static void topologicalSort(ArrayList<ArrayList<Integer>>
graph,Stack<Integer> stack,int current,boolean[] visited){
visited[current]=true;
for(int i=0;i<graph.get(current).size();i++){
if(!visited[graph.get(current).get(i)]){
topologicalSort(graph,stack,graph.get(current).get(i),visited);
}
}
stack.push(new Integer(current));
}

// Method for finding the connected component using BFS


private static void findConnectedComponent(ArrayList<ArrayList<Integer>>
graph) {
Queue<Integer> queue = new LinkedList<>();
boolean[] visited = new boolean[graph.size()];
Arrays.fill(visited,false);
for(int i=0;i<visited.length;i++) {
HashSet<Integer> hs = new HashSet<>();
if(!visited[i]) {
queue.add(i);
while (!queue.isEmpty()) {
int current = queue.remove();
hs.add(current);
visited[current] = true;
ArrayList<Integer> list = graph.get(current);
for (int j = 0; j < list.size(); j++) {
if(!visited[list.get(j)]){
queue.add(list.get(j));
}
}
}
Iterator<Integer> j = hs.iterator();
while (j.hasNext())
System.out.print(j.next());
System.out.println();
}
}
}

// Method to find Cycle in a graph using BFS


private static void findCycle(ArrayList<ArrayList<Integer>> graph, int source)
{
Queue<Integer> queue = new LinkedList<>();
boolean[] visited = new boolean[graph.size()];
Arrays.fill(visited,false);
int[] parent = new int[graph.size()];
Arrays.fill(parent,-1);
queue.add(source);
boolean cycle=false;
while (!queue.isEmpty()){
int current = queue.remove();
visited[current]=true;
ArrayList<Integer> list = graph.get(current);
for(int i=0;i<list.size();i++){
if(!visited[list.get(i)]){
queue.add(list.get(i));
parent[list.get(i)]=current;
}else{
if(parent[current]==list.get(i)){
continue;
}
cycle=true;
break;
}
}

}
if(cycle){
System.out.println("Graph has a Cycle");
}else{
System.out.println("No Cycle detected");
}

// Method to find the Shortest Path form source to every vertex


private static void findShortestPath(ArrayList<ArrayList<Integer>> graph, int
source) {
Queue<Integer> queue = new LinkedList<>();
boolean[] visited = new boolean[graph.size()];
Arrays.fill(visited,false);
HashMap<Integer,Integer> path = new HashMap<>();
path.put(source,0);
queue.add(source);
while(!queue.isEmpty()){
int current = queue.remove();
visited[current]=true;
ArrayList<Integer> list = graph.get(current);
for(int i=0;i<list.size();i++){
int vertex = list.get(i);
if(vertex!=current && visited[vertex]==false && !
queue.contains(vertex)){
queue.add(vertex);
path.put(vertex,path.get(current)+1);
}
}
}

for (Map.Entry<Integer,Integer> entry : path.entrySet()){


System.out.println(entry.getKey() +" = "+ entry.getValue());
}
}
}

You might also like