Advertisement
And1

Bridges from GeeksForGeeks

Mar 1st, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.35 KB | None | 0 0
  1. // A Java program to find bridges in a given undirected graph
  2. import java.io.*;
  3. import java.util.*;
  4. import java.util.LinkedList;
  5.  
  6. // This class represents a undirected graph using adjacency list
  7. // representation
  8. class Graph
  9. {
  10.     private int V;   // No. of vertices
  11.  
  12.     // Array  of lists for Adjacency List Representation
  13.     private LinkedList<Integer> adj[];
  14.     int time = 0;
  15.     static final int NIL = -1;
  16.  
  17.     // Constructor
  18.     Graph(int v)
  19.     {
  20.         V = v;
  21.         adj = new LinkedList[v];
  22.         for (int i=0; i<v; ++i)
  23.             adj[i] = new LinkedList();
  24.     }
  25.  
  26.     // Function to add an edge into the graph
  27.     void addEdge(int v, int w)
  28.     {
  29.         adj[v].add(w);  // Add w to v's list.
  30.         adj[w].add(v);    //Add v to w's list
  31.     }
  32.  
  33.     // A recursive function that finds and prints bridges
  34.     // using DFS traversal
  35.     // u --> The vertex to be visited next
  36.     // visited[] --> keeps tract of visited vertices
  37.     // disc[] --> Stores discovery times of visited vertices
  38.     // parent[] --> Stores parent vertices in DFS tree
  39.     void bridgeUtil(int u, boolean visited[], int disc[],
  40.                     int low[], int parent[])
  41.     {
  42.  
  43.         // Mark the current node as visited
  44.         visited[u] = true;
  45.  
  46.         // Initialize discovery time and low value
  47.         disc[u] = low[u] = ++time;
  48.  
  49.         // Go through all vertices aadjacent to this
  50.         Iterator<Integer> i = adj[u].iterator();
  51.         while (i.hasNext())
  52.         {
  53.             int v = i.next();  // v is current adjacent of u
  54.  
  55.             // If v is not visited yet, then make it a child
  56.             // of u in DFS tree and recur for it.
  57.             // If v is not visited yet, then recur for it
  58.             if (!visited[v])
  59.             {
  60.                 parent[v] = u;
  61.                 bridgeUtil(v, visited, disc, low, parent);
  62.  
  63.                 // Check if the subtree rooted with v has a
  64.                 // connection to one of the ancestors of u
  65.                 low[u]  = Math.min(low[u], low[v]);
  66.  
  67.                 // If the lowest vertex reachable from subtree
  68.                 // under v is below u in DFS tree, then u-v is
  69.                 // a bridge
  70.                 if (low[v] > disc[u])
  71.                     System.out.println(u+" "+v);
  72.             }
  73.  
  74.             // Update low value of u for parent function calls.
  75.             else if (v != parent[u])
  76.                 low[u]  = Math.min(low[u], disc[v]);
  77.         }
  78.     }
  79.  
  80.  
  81.     // DFS based function to find all bridges. It uses recursive
  82.     // function bridgeUtil()
  83.     void bridge()
  84.     {
  85.         // Mark all the vertices as not visited
  86.         boolean visited[] = new boolean[V];
  87.         int disc[] = new int[V];
  88.         int low[] = new int[V];
  89.         int parent[] = new int[V];
  90.  
  91.  
  92.         // Initialize parent and visited, and ap(articulation point)
  93.         // arrays
  94.         for (int i = 0; i < V; i++)
  95.         {
  96.             parent[i] = NIL;
  97.             visited[i] = false;
  98.         }
  99.  
  100.         // Call the recursive helper function to find Bridges
  101.         // in DFS tree rooted with vertex 'i'
  102.         for (int i = 0; i < V; i++)
  103.             if (visited[i] == false)
  104.                 bridgeUtil(i, visited, disc, low, parent);
  105.     }
  106.  
  107.     public static void main(String args[])
  108.     {
  109.         // Create graphs given in above diagrams
  110.         System.out.println("Bridges in first graph ");
  111.         Graph g1 = new Graph(5);
  112.         g1.addEdge(1, 0);
  113.         g1.addEdge(0, 2);
  114.         g1.addEdge(2, 1);
  115.         g1.addEdge(0, 3);
  116.         g1.addEdge(3, 4);
  117.         g1.bridge();
  118.         System.out.println();
  119.  
  120.         System.out.println("Bridges in Second graph");
  121.         Graph g2 = new Graph(4);
  122.         g2.addEdge(0, 1);
  123.         g2.addEdge(1, 2);
  124.         g2.addEdge(2, 3);
  125.         g2.bridge();
  126.         System.out.println();
  127.  
  128.         System.out.println("Bridges in Third graph ");
  129.         Graph g3 = new Graph(7);
  130.         g3.addEdge(0, 1);
  131.         g3.addEdge(1, 2);
  132.         g3.addEdge(2, 0);
  133.         g3.addEdge(1, 3);
  134.         g3.addEdge(1, 4);
  135.         g3.addEdge(1, 6);
  136.         g3.addEdge(3, 5);
  137.         g3.addEdge(4, 5);
  138.         g3.bridge();
  139.     }
  140. }
  141. // This code is contributed by Aakash Hasija
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement