I tried to compile it in java but im getting wrong answer…
public boolean cyclic(T src)
{
Queue q=new LinkedList<>();
Map<T,T> parent=new HashMap<>();
Set vis=new HashSet<>();
q.add(src);
vis.add(src);
parent.put(src,src);
while(!q.isEmpty())
{
T node=q.remove();
Iterator i=h.get(node).iterator();
while(i.hasNext())
{
T j=(T)i.next();
if(vis.contains(j) && !parent.get(j).equals(node))
{
return true;
}
else if(!vis.contains(j))
{
parent.put(j,node);
vis.add(j);
q.add(j);
}
}
}
return false;
}
public static void main(String args[])
{
graph_generictype s=new graph_generictype<>();
s.addedge(1, 0, true);
s.addedge(1, 2, true);
s.addedge(2, 4, true);
s.addedge(2, 3, true);
s.addedge(5, 3, true);
System.out.println(s.cyclic(0));
}
output : true