package javacodes; /** * This class is to represent a Graph and to perform some operation in Graph */ import java.util.*; class WebGraph { private int V; private LinkedList adj[]; private int dist[]; private int par[]; /** * Constructor to create a WebGraph * @param v */ WebGraph(int v) { V = v; adj = new LinkedList[v]; dist=new int[v]; par=new int[v]; for (int i=0; i queue = new LinkedList(); visited[s]=true; dist[s]=0; queue.add(s); while (queue.size() != 0) { s = queue.poll(); Iterator i = adj[s].listIterator(); while (i.hasNext()) { int n = i.next(); if (!visited[n]) { visited[n] = true; dist[n]=dist[s]+1; par[n]=s; queue.add(n); } } } } /** * Thus api prints a path(showing all the vertices in between s and d * vertices) from Source vertices s to Destination vertices d * * @param s * @param d */ void path(int s,int d) { BFS(s); System.out.print(d); int tmp=d; while(tmp!=-1&&tmp!=s){ System.out.print("<-"+par[tmp]); tmp=par[tmp]; } System.out.println("\nDistance form "+s+" to "+d+" is : "+dist[d]); } /** * This api prints the diameter of this WebGraph */ void diameter() { int max=Integer.MIN_VALUE; for(int j=0;j