Advertisement
Aldin_SXR

GraphSearch.java

May 26th, 2020
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.97 KB | None | 0 0
  1. package ds.graph.search;
  2.  
  3. import ds.queue.regular.Queue;
  4. import ds.undirected.graph.Graph;
  5.  
  6. public class GraphSearch {
  7.    
  8.     private boolean[] marked;   // keep track of explored vertices
  9.     private int count;          // count the explored vertices
  10.     private Graph G;            // the graph to be searched
  11.    
  12.     /* Construct the graph search */
  13.     public GraphSearch(Graph G) {
  14.         marked = new boolean[G.V()];    // create an array for marking of size V
  15.         this.G = G;                     // store the graph to be searched
  16.     }
  17.    
  18.     /* Depth-first search (DFS) */
  19.     public void dfs(int v) {
  20.         marked[v] = true;               // mark the source vertex as traversed
  21.         count++;                        // increment the number of explored vertices
  22.         System.out.print(v + " ");      // print out the current vertex
  23.         for (int w: G.adj(v)) {         // iterate over adjacent vertices of v
  24.             if (!marked[w]) {           // if the vertex was not explored already,
  25.                 dfs(w);                 // recursively call DFS on it
  26.             }
  27.         }
  28.     }
  29.    
  30.     /* Breadth-first search */
  31.     public void bfs(int v) {
  32.         Queue<Integer> queue = new Queue<Integer>();    // create an empty queue
  33.         marked[v] = true;                               // marked the source vertex as explored
  34.         queue.enqueue(v);                               // add the vertex to the queue
  35.         while (!queue.isEmpty()) {                      // as long as the queue is not empty,
  36.             int s = queue.dequeue();                    // dequeue the vertex
  37.             System.out.print(s + " ");                  // print out the current vertex
  38.             for (int w: G.adj(s)) {                     // iterate over adjacent vertices of s
  39.                 if (!marked[w]) {                       // if the vertex was not explored already,
  40.                     marked[w] = true;                   // mark it
  41.                     queue.enqueue(w);                   // and add it to the queue
  42.                 }
  43.             }
  44.         }
  45.     }
  46.    
  47.     /* Reset the searc2h */
  48.     public void reset() {
  49.         marked = new boolean[G.V()];    // reset the marked array
  50.         count = 0;                      // reset the explored vertex count
  51.     }
  52.    
  53.     /* Check if a node w has been marked */
  54.     public boolean marked(int w) {
  55.         return marked[w];
  56.     }
  57.    
  58.     /* Get the number of explored vertices */
  59.     public int count() {
  60.         return count;
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement