Amazon Interview Question
SDE-2sInterview Type: Phone Interview
//Directed graph
boolean isCyclic(int i){
visited[i]=true;
for(int x:res[i]){
if(!visited[x]){
if(isCyclic(x)) return true;
}
else if(!explored[x]) return true;
}
explored[i]=true;
return false;
}
//Undirected Graph
boolean dfs(int src,int prev,Set<Integer> set){
if(set.contains(src)) return false;
set.add(src);
for(int x:list[src]){
if(x==prev) continue;
if(!dfs(x,src,set)) return false;
}
return true;
}
}
Topological Sorting
- qlmw August 07, 2022