Advertisement
Guest User

q1 q2 q3

a guest
Nov 22nd, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.40 KB | None | 0 0
  1. //1st question
  2. class Graph {
  3.  
  4.     int N; // No. of vertices
  5.     LinkedList<Integer> adjListArray[]; // To store a adjacency list
  6.  
  7.     // Initialize a graph
  8.     Graph(int nodes) {
  9.         this.N = nodes;
  10.  
  11.         adjListArray = new LinkedList[nodes];
  12.  
  13.         int i = 0;
  14.         while (i < nodes) {
  15.             adjListArray[i] = new LinkedList<>();
  16.  
  17.             i++;
  18.         }
  19.  
  20.         // YOUR CODE HERE
  21.  
  22.     }
  23.  
  24.     // Adds an edge from a source vertex to a desctination vertex
  25.     void addEdge(int src, int dest) {
  26.         // YOUR CODE HERE
  27.         adjListArray[src].add(dest);
  28.  
  29.     }
  30.  
  31.     // A method to print the adjacency list
  32.     void printGraph(Graph graph) {
  33.         // YOUR CODE HERE
  34.         int j = 0;
  35.         while (j < graph.N) {
  36.             if(adjListArray[j].size() == 0){
  37.                 System.out.print("No vertex found");
  38.             }
  39.  
  40.             else {
  41.                 int i = 0;
  42.                 String vert = "";
  43.                 while(i < graph.adjListArray[j].size()){
  44.                     vert += + graph.adjListArray[j].get(i) + ",";
  45.                     i++;
  46.                 }
  47.                 System.out.println("Adjacency list of vertex "+ j +": " + vert.substring(0, vert.length() -1));
  48.                
  49.             }
  50.                
  51.                    
  52.                
  53.                 System.out.println("");
  54.                 j++;
  55.             }
  56.            
  57.            
  58.        
  59.         }
  60.        
  61.     }
  62. //2nd question
  63. class GraphTraversal {
  64.     private int V; // No. of vertices
  65.     private LinkedList<Integer> adj[]; // To store a adjacency list
  66.  
  67.     /*
  68.      * Initialize a graph
  69.      */
  70.     GraphTraversal(int v) {
  71.         // YOUR CODE HERE
  72.         V = v;
  73.         adj = new LinkedList[v];
  74.         int i = 0;
  75.         while( i < v) {
  76.             adj[i] = new LinkedList();
  77.             i++;
  78.         }
  79.  
  80.     }
  81.  
  82.     /*
  83.      * Add an edge into a graph
  84.      */
  85.     void addEdge(int v, int w) {
  86.         // YOUR CODE HERE
  87.         adj[v].add(w);
  88.     }
  89.  
  90.     /*
  91.      * This mehtod is to calculate and show the shortest path from a given
  92.      * vertex
  93.      */
  94.     void shortestPath(int start) {
  95.         // YOUR CODE HERE
  96.  
  97.         boolean[] visited = new boolean[V];
  98.  
  99.         LinkedList<Integer> q = new LinkedList<>();
  100.         LinkedList<Integer> current = new LinkedList<>();
  101.  
  102.         current.add(start);
  103.         q.add(start);
  104.  
  105.         visited[start] = true;
  106.         for (int parts = 0; !q.isEmpty(); parts++) {
  107.             for (int i = 0; !q.isEmpty(); i++) {
  108.  
  109.                 System.out.println("Distance from vertex 2 to vertex " + q.peek() + " is: " + parts);
  110.                 q.remove();
  111.             }
  112.  
  113.             for (int j = 0; !current.isEmpty(); j++) {
  114.                 int k = 0;
  115.                 while (k < adj[current.getFirst()].size()) {
  116.                     if (visited[adj[current.getFirst()].get(k)] != true) {
  117.                         q.add(adj[current.getFirst()].get(k));
  118.                         visited[adj[current.getFirst()].get(k)] = true;
  119.  
  120.                     }
  121.                     k++;
  122.  
  123.                 }
  124.  
  125.                 current.remove();
  126.  
  127.             }
  128.  
  129.             Iterator<Integer> it = q.iterator();
  130.             for (int s = 0; it.hasNext(); s++) {
  131.                 current.add(it.next());
  132.                
  133.             }
  134.  
  135.         }
  136.     }
  137. }
  138. //3rd question
  139. GraphTraversalDFS(int v) {
  140.         // YOUR CODE HERE
  141.         V = v;
  142.         adj = new LinkedList[v];
  143.         int i = 0;
  144.         while (i < v) {
  145.             adj[i] = new LinkedList();
  146.             i++;
  147.         }
  148.  
  149.     }
  150.  
  151.     /*
  152.      * Add an edge into a graph
  153.      */
  154.     void addEdge(int v, int w) {
  155.         // YOUR CODE HERE
  156.         adj[v].add(w);
  157.  
  158.     }
  159.  
  160.     /*
  161.      * Method to traverse the graph with DFS Suggestion: write recursive DFS
  162.      */
  163.     void DFS(int v, boolean visited[]) {
  164.         // YOUR CODE HERE
  165.         Stack<Integer> stack = new Stack<Integer>();
  166.         visited[v] = true;
  167.  
  168.         int currentVert = v;
  169.         stack.push(v);
  170.  
  171.         int currentNodes = 1;
  172.  
  173.         LinkedList<Integer> firstNodes = new LinkedList<>();
  174.  
  175.         firstNodes.add(v);
  176.         int nodes;
  177.         for (nodes = 1; !stack.isEmpty(); nodes++) {
  178.             stack.pop();
  179.             int i = 0;
  180.             while (i < adj[currentVert].size()) {
  181.                 if (visited[adj[currentVert].get(i)] != true) {
  182.                     stack.push(adj[currentVert].get(i));
  183.                     visited[adj[currentVert].get(i)] = true;
  184.                 }
  185.                 i++;
  186.             }
  187.             if (!stack.isEmpty()) {
  188.                 currentVert = stack.peek();
  189.             } else {
  190.                 int j = 0;
  191.                 while (j < visited.length) {
  192.                     if (visited[j] != true) {
  193.                         stack.push(j);
  194.                         currentVert = j;
  195.                         currentNodes++;
  196.                         firstNodes.add(j);
  197.                         visited[j] = true;
  198.                         break;
  199.                     }
  200.                     j++;
  201.                 }
  202.             }
  203.         }
  204.         System.out.println("You can traverse the entire graph by selecting " + currentNodes + " starting vertices");
  205.         System.out.println("Starting vertices are: " + firstNodes);
  206.     }
  207.  
  208.     /*
  209.      * Method to calculate the number of starting nodes
  210.      */
  211.     void countStartingNodes() {
  212.         // YOUR CODE HERE
  213.         boolean visited[] = new boolean[V];
  214.  
  215.         // Call the recursive helper function to print DFS traversal
  216.         // starting from all vertices one by one
  217.  
  218.         DFS(0, visited);
  219.         // Call DFS HERE
  220.  
  221.         // Call the recursive helper function to print DFS traversal
  222.  
  223.     }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement