Skip to content

Added graph algorithm #5517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import java.util.*;
public class SCC_Naive
{
//Implementing the conventional DFS
public boolean isPath(HashMap<Integer,List<Integer>> adj_list,int N,int visited[],int current_node,int destination)
{
if(current_node == destination)
{
return true;
}
List<Integer> lists = adj_list.get(current_node);
int lists_size = lists.size();
visited[current_node] = 1;
for(int i = 0;i < lists_size;i++)
{
if(visited[lists.get(i)] == 0)
{
if(isPath(adj_list,N,visited,lists.get(i),destination))
{
return true;
}

}
}
return false;
}
public Set<List<Integer>> getOutput(HashMap<Integer,List<Integer>> adj_list,int N)
{
Set<List<Integer>> SCC_lists = new HashSet<>();
for(int i = 1;i <= N;i++)
{
List<Integer> neighbour_vertices = new ArrayList<>();
for(int j = 1;j <= N;j++)
{
if(i != j)
{
int visited[] = new int[N+1];
if(isPath(adj_list, N,visited,i,j))
{
visited = new int[N+1];
if(isPath(adj_list, N, visited, j, i))
{
neighbour_vertices.add(j);
}
}
}
else
{
neighbour_vertices.add(i);

}
}
SCC_lists.add(new ArrayList<>(neighbour_vertices));
}
return SCC_lists;
}
public static void main(String[] args) {
Scanner rs = new Scanner(System.in);
HashMap<Integer,List<Integer>> adj_list = new HashMap<>();
int N = rs.nextInt();
for(int i = 1;i <= N;i++)
{
adj_list.put(i,new ArrayList<>());
}
for(int i = 1;i <= N;i++)
{
System.out.println("Number of neighbour vertices");
int n_vertices = rs.nextInt();
for(int j = 0;j < n_vertices;j++)
{
int vertices = rs.nextInt();
List<Integer> lists = adj_list.get(i);
lists.add(vertices);
}
}
System.out.println(adj_list);
SCC_Naive obj = new SCC_Naive();
System.out.print(obj.getOutput(adj_list,N));
rs.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import java.util.*;
public class SCC_Optimized
{

public void btrack(HashMap<Integer,List<Integer>> adj_list,int visited[],Stack<Integer> dfs_calls_nodes,int current_node)
{
visited[current_node] = 1;
for(int i = 0;i < adj_list.get(current_node).size();i++)
{
if(visited[adj_list.get(current_node).get(i)] == -1)
{
btrack(adj_list, visited, dfs_calls_nodes, current_node);
}
}
dfs_calls_nodes.add(current_node);
}
public void btrack2(HashMap<Integer,List<Integer>> adj_rev_list,int visited[],int current_node,List<Integer> new_scc)
{
visited[current_node] = 1;
for(int i = 0;i < adj_rev_list.get(current_node).size();i++)
{
if(visited[adj_rev_list.get(current_node).get(i)] == -1)
{
btrack2(adj_rev_list, visited, current_node,new_scc);
}
}
}
public int getOutput(HashMap<Integer,List<Integer>> adj_list,int N)
{
int visited[] = new int[N+1];
Arrays.fill(visited,-1);
Stack<Integer> dfs_calls_nodes = new Stack<Integer>();
for(int i = 0;i < N;i++)
{
if(visited[i]==-1)
{
btrack(adj_list, visited,dfs_calls_nodes,i);
}
}
System.out.println(dfs_calls_nodes);
HashMap<Integer,List<Integer>> adj_rev_list = new HashMap<>();
for(int i = 0;i < N;i++)
{
visited[i] = -1;
adj_rev_list.put(i,new ArrayList<Integer>());
}
//Reversing the adjacency lists
for(int i = 0;i < N;i++)
{
List<Integer> lists = adj_list.get(i);
for(Integer num: lists)
{
List<Integer> list1 = adj_rev_list.get(num);
list1.add(i);
adj_rev_list.put(num,list1);
}
}
//Popping out the stack in the descending order and counting the number of SCC
int strongly_connected_components = 0;
List<List<Integer>> sccs = new ArrayList<>();
while(!dfs_calls_nodes.isEmpty())
{
int node = dfs_calls_nodes.pop();
if(visited[node] == -1)
{
++strongly_connected_components;
List<Integer> new_scc = new ArrayList<>();
new_scc.add(node);
btrack2(adj_rev_list, visited, node,new_scc);
sccs.add(new_scc);
}
}
System.out.println(sccs.get(sccs.size()-1).size());
return strongly_connected_components;
}
public static void main(String[] args) {
Scanner rs = new Scanner(System.in);
HashMap<Integer,List<Integer>> adj_list = new HashMap<>();
int N = rs.nextInt();
for(int i = 0;i < N;i++)
{
adj_list.put(i,new ArrayList<>());
}
for(int i = 0;i < N;i++)
{
System.out.println("Number of neighbour vertices");
int n_vertices = rs.nextInt();
for(int j = 0;j < n_vertices;j++)
{
int vertices = rs.nextInt();
List<Integer> lists = adj_list.get(i);
lists.add(vertices);
}
}
System.out.println(adj_list);
SCC_Optimized obj = new SCC_Optimized();
System.out.print(obj.getOutput(adj_list,N));
rs.close();
}
}