Пример #1
0
 /**
  * Method to initialize a graph 1) Sets the parent of every vertex as null 2) Sets the seen
  * attribute of every vertex as false 3) Sets the distance of every vertex as infinite
  *
  * @param g : Graph - The reference to the graph
  */
 void initialize() {
   int i = 1;
   for (Vertex u : this) {
     u.seen = false;
     u.parent = null;
     u.weight = INFINITY;
     u.mActive = false;
     u.mIndegree = 0;
   }
 }
Пример #2
0
 /**
  * Method to do the DFS traversal - Recursive call
  *
  * @param u: Vertex for which the DFS checks the adjacency list.
  * @param cno: Count variable to keep the number of connected components.
  */
 static void dfsVisit(Vertex u, int cno) {
   u.seen = true;
   u.cno = cno;
   for (Edge e : u.Adj) {
     Vertex v = e.otherEnd(u);
     if (!v.seen) {
       v.parent = u;
       dfsVisit(v, cno);
     }
   }
 }
Пример #3
0
 /**
  * Method to find the DFS traversal for the Graph
  *
  * @param G : Input Graph G - undirected graph
  * @return : Returns the count for checking connected components
  */
 static int dfsUtil(Graph<Vertex> G) {
   for (Vertex u : G) {
     u.seen = false;
     u.parent = null;
   }
   int cno = 0; // Initializing count variable to check for connected components
   for (Vertex u : G) {
     if (!u.seen) dfsVisit(u, ++cno);
   }
   return cno;
 }