Program to traverse a graph using DFS traversal
class Solution {
private boolean[] visited;
private ArrayList<Integer> res;
// Function to return a list containing the DFS traversal of the graph.
public ArrayList<Integer> dfsOfGraph(int V, ArrayList<ArrayList<Integer>> adj) {
visited = new boolean[V];
res = new ArrayList<>();
dfsTraversal(adj, 0);
return res;
private void dfsTraversal(ArrayList<ArrayList<Integer>> adj, int cv) {
visited[cv] = true;
res.add(cv);
for(int i: adj.get(cv)) {
if(!visited[i])
dfsTraversal(adj, i);
}
}
}
Program to traverse a graph using BFS traversal
class Solution {
private ArrayList<Integer> res;
private boolean[] visited;
private Queue<Integer> myQ;
// Function to return Breadth First Traversal of given graph.
public ArrayList<Integer> bfsOfGraph(int V, ArrayList<ArrayList<Integer>> adj) {
myQ = new LinkedList<>();
visited = new boolean[V];
res = new ArrayList<>();
bfsTraversal(adj);
return res;
}
private void bfsTraversal(ArrayList<ArrayList<Integer>> adj) {
myQ.offer(0);
visited[0] = true;
while(!myQ.isEmpty()) {
int cv = myQ.poll();
res.add(cv);
for(int nv: adj.get(cv)) {
if(!visited[nv]) {
myQ.offer(nv);
visited[nv] = true;
}
}
}
}
}
Program to detect cycle in an undirected graph by DFS traversal
class Solution {
// Function to detect cycle in an undirected graph.
public boolean isCycle(int V, ArrayList<ArrayList<Integer>> adj) {
visited = new boolean[V];
for(int i=0; i<V; i++) {
if(!visited[i] && dfsCycle(adj, i, -1))
return true;
}
return false;
}
private static boolean[] visited;
private boolean dfsCycle(ArrayList<ArrayList<Integer>> adj, int cv, int parent) {
visited[cv] = true;
for(int v: adj.get(cv)) {
if(v == parent)
continue;
if(visited[v] || dfsCycle(adj, v, cv))
return true;
}
return false;
}
}
Program to detect cycle in an undirected graph by BFS traversal
class Solution {
// Function to detect cycle in an undirected graph.
public boolean isCycle(int V, ArrayList<ArrayList<Integer>> adj) {
visited = new boolean[V];
queue = new LinkedList<>();
for(int i=0; i<V; i++) {
if(!visited[i] && BFS_Cycle(adj, i))
return true;
}
return false;
}
private static boolean[] visited;
private static Queue<Pair> queue;
private boolean BFS_Cycle(ArrayList<ArrayList<Integer>> adj, int cv) {
queue.offer(new Pair(cv, -1));
visited[cv] = true;
while(!queue.isEmpty()) {
Pair cvp = queue.poll();
for(int v: adj.get(cvp.vertex)) {
if(v == cvp.parent)
continue;
if(visited[v])
return true;
queue.offer(new Pair(v, cvp.vertex));
visited[v] = true;
}
}
return false;
private class Pair {
int vertex;
int parent;
private Pair (int vertex, int parent) {
this.vertex = vertex;
this.parent = parent;
}
}
}
Program to detect cycle in a directed graph by DFS traversal
class Solution {
// Function to detect cycle in a directed graph.
public boolean isCyclic(int V, ArrayList<ArrayList<Integer>> adj) {
visited = new boolean[V];
inCurrentRecursion = new boolean[V];
for(int i=0; i<V; i++) {
if(!visited[i] && DFS_cycleDetect(adj, i))
return true;
}
return false;
}
private boolean[] visited;
private boolean[] inCurrentRecursion;
private boolean DFS_cycleDetect(ArrayList<ArrayList<Integer>> adj, int cv) {
visited[cv] = true;
inCurrentRecursion[cv] = true;
for(int v: adj.get(cv)) {
if(visited[v] && inCurrentRecursion[v])
return true;
if(!visited[v] && DFS_cycleDetect(adj, v))
return true;
}
inCurrentRecursion[cv] = false;
return false;
}
}
Topological sorting of vertices of a directed acyclic graph by DFS traversal
class Solution {
//Function to return list containing vertices in Topological order.
Public static int[] topoSort(int V, ArrayList<ArrayList<Integer>> adj) {
int[] ans = new int[V];
visited = new boolean[V];
stack = new Stack<>();
for(int i=0; i<V; i++) {
if(!visited[i])
DFS_traversal(adj, i);
}
for(int i=0; i<V; i++)
ans[i] = stack.pop();
return ans;
}
private static boolean[] visited;
private static Stack<Integer> stack;
private static void DFS_traversal(ArrayList<ArrayList<Integer>> adj, int cv) {
visited[cv] = true;
for(int v: adj.get(cv)) {
if(!visited[v])
DFS_traversal(adj, v);
}
stack.push(cv);
}
}
Topological sorting of vertices of a directed acyclic graph by BFS traversal
class Solution {
//Function to return list containing vertices in Topological order.
static int[] topoSort(int V, ArrayList<ArrayList<Integer>> adj) {
queue = new LinkedList<>();
indegree = new int[V];
int[] ans = new int[V];
for(int i=0; i<V; i++) {
for(int v: adj.get(i))
indegree[v]++;
}
BFS_traversal(adj, ans);
return ans;
}
private static int[] indegree;
private static Queue<Integer> queue;
private static void BFS_traversal(ArrayList<ArrayList<Integer>> adj, int[] arr){
for(int i=0; i<indegree.length; i++) {
if(indegree[i] == 0)
queue.offer(i);
}
int p = 0;
while(!queue.isEmpty()) {
int cv = queue.poll();
arr[p++] = cv;
for(int v: adj.get(cv)) {
indegree[v]--;
if(indegree[v] == 0)
queue.offer(v);
}
}
}
}
Program to detect cycle in a directed graph by BFS traversal(topological sort)
class Solution {
private int[] indegree;
private Queue<Integer> queue;
// Function to detect cycle in a directed graph.
public boolean isCyclic(int V, ArrayList<ArrayList<Integer>> adj) {
indegree = new int[V];
for(int i=0; i<V; i++) {
for(int cv: adj.get(i))
indegree[cv]++;
}
BFS_cycleDetect(adj);
for(int d: indegree) {
if(d > 0)
return true;
}
return false;
}
private void BFS_cycleDetect(ArrayList<ArrayList<Integer>> adj) {
queue = new LinkedList<>();
for(int i=0; i<indegree.length; i++) {
if(indegree[i] == 0)
queue.offer(i);
}
while(!queue.isEmpty()) {
int cv = queue.poll();
for(int v: adj.get(cv)) {
indegree[v]--;
if(indegree[v] == 0)
queue.offer(v);
}
}
}
}
Program to detect if an undirected graph is Bipartite or not by DFS traversal
class Solution {
public boolean isBipartite(int V, ArrayList<ArrayList<Integer>> adj) {
colorVisited = new int[V];
Arrays.fill(colorVisited, -1);
for(int i=0; i<V; i++) {
if(colorVisited[i] == -1 && !DFS_traversal(adj, i, 1))
return false;
}
return true;
}
private static int[] colorVisited;
private static boolean DFS_traversal(List<List<Integer>>adj, int cv, int color){
colorVisited[cv] = color;
for(int i: adj.get(cv)) {
if(colorVisited[i] == colorVisited[cv])
return false;
int newColor = 1 - colorVisited[cv];
if(colorVisited[i] == -1 && !DFS_traversal(adj, i, newColor))
return false;
}
return true;
}
}
Program to detect if an undirected graph is Bipartite or not by BFS traversal
class Solution {
public boolean isBipartite(int V, ArrayList<ArrayList<Integer>> adj) {
// Code here
color = new int[V];
Arrays.fill(color, -1);
for(int i=0; i<V; i++) {
if(color[i] == -1 && !BFS_traversal(adj, i))
return false;
}
return true;
}
private static int[] color;
private static boolean BFS_traversal(ArrayList<ArrayList<Integer>> adj, int cv){
Queue<Integer> queue = new LinkedList<>();
queue.offer(cv);
int currColor = 1;
color[cv] = currColor;
while(!queue.isEmpty()) {
int v = queue.poll();
for(int i: adj.get(v)) {
if(color[i] == color[v])
return false;
if(color[i] == -1) {
color[i] = 1 - color[v];
queue.offer(i);
}
}
}
return true;
}
}
Program to detect cycle in an undirected graph by DFS traversal
class Solution {
//Function to detect cycle using DSU in an undirected graph.
public boolean detectCycle(int V, ArrayList<ArrayList<Integer>> adj) {
// Code here
int[] parent = new int[V];
int[] rank = new int[V];
for(int i=0; i<V; i++)
parent[i] = i;
for(int cv=0; cv<V; cv++) {
for(int i: adj.get(cv)) {
if(cv < i) {
if(findParent(cv, parent) == findParent(i, parent))
return true;
union(cv, i, parent, rank);
}
}
}
return false;
}
private static void union(int x, int y, int[] parent, int[] rank) {
int parent_x = findParent(x, parent);
int parent_y = findParent(y, parent);
if(parent_x != parent_y) {
if(rank[parent_x] > rank[parent_y])
parent[parent_y] = parent_x;
else if(rank[parent_y] > rank[parent_x])
parent[parent_x] = parent_y;
else {
parent[parent_x] = parent_y;
rank[parent_y]++;
}
}
}
private static int findParent(int x, int[] parent) {
if(parent[x] == x)
return x;
return parent[x] = findParent(parent[x], parent);
}
}
DIJKATRA’s Algorithm to get the minimum cost in an undirected graph BFS
class Solution {
static int[] dijkstra(int V, List<List<List<Integer>>> adj, int Source) {
distance = new int[V];
Arrays.fill(distance, Integer.MAX_VALUE);
DijkstraAlgo(adj, Source);
return distance;
}
private static int[] distance;
private static void DijkstraAlgo(List<List<List<Integer>>> adj, int Source) {
PriorityQueue<Pair> minHeap = new PriorityQueue<>(new Comparator<Pair>(){
@Override
public int compare(Pair first, Pair second) {
return first.cost - second.cost;
}
});
minHeap.offer(new Pair(Source, 0));
distance[S] = 0;
while(!minHeap.isEmpty()) {
Pair curr = minHeap.poll();
int cv = curr.vertex;
int weight = curr.cost;
for(List<Integer> list: adj.get(cv)) {
int v = list.get(0);
int w = list.get(1) + weight;
if(w < distance[v]) {
distance[v] = w;
minHeap.offer(new Pair(v, w));
}
}
}
}
private static class Pair {
int vertex;
int cost;
private Pair(int vertex, int cost) {
this.vertex = vertex;
this.cost = cost;
}
}
}