Question: I am trying to put this code in apache NetBeans 12. Can you correct the code if there are any mistakes and send me screenshots

I am trying to put this code in apache NetBeans 12. Can you correct the code if there are any mistakes and send me screenshots on how to make it run in Apache NetBeans IDE 12.0. Can you also tell me if I should put in a single Java file or should it be split.

import java.io.*; import java.util.*; // Note: considering characters in the graph as numeric values for easy understanding. class Graph { int vertices; int[][] adjacency_matrix; // constructor to initialize number of vertices and // size of adjacency matrix public Graph(int vertices) { this.vertices = vertices; adjacency_matrix = new int[vertices][vertices]; } public void insert(int source, int destination) { // make adjacency_matrix[i][j] = 1 if there is // an edge from i to j adjacency_matrix[destination-1] = 1; } public boolean isSinkPresent(int i) { for (int j = 0 ; j < vertices ; j++) { // if any element in the row i is 1, it means // that there is an edge emanating from the // vertex, which means it cannot be a sink if (adjacency_matrix[i][j] == 1) return false; // if any element other than i in the column // i is 0, it means that there is no edge from // that vertex to the vertex we are testing // and hence it cannot be a sink if (adjacency_matrix[j][i] == 0 && j != i) return false; } //if none of the checks fails, return true return true; } // we will eliminate n-1 non sink vertices so that // we have to check for only one vertex instead of // all n vertices public int eliminate() { int i = 0, j = 0; while (i < vertices && j < vertices) { // If the index is 1, increment the row we are // checking by 1 // else increment the column if (adjacency_matrix[i][j] == 1) i = i + 1; else j = j + 1; } // If i exceeds the number of vertices, it // means that there is no valid vertex in // the given vertices that can be a sink if (i > vertices) return -1; else if (!isSinkPresent(i)) return -1; else return i; } //Code for Remove vertex public void removeVertex(int x) { // checking if the vertex is present if (x > n) { System.out.println("Vertex not present!"); return; } else { int i; // removing the vertex while (x < n) { // shifting the rows to left side for (i = 0; i < n; ++i) { adjacency_matrix[i][x] = adjacency_matrix[i][x + 1]; } // shifting the columns upwards for (i = 0; i < n; ++i) { adjacency_matrix[x][i] = adjacency_matrix[x + 1][i]; } x++; } // decreasing the number of vertices n--; } } } }
 import java.io.*; import java.util.*; class Parser { public static void main (String[] args) { while (reader.hasNextLine()) { String[] nums = reader.nextLine().split(", "); addEdge(adj, Integer.parseInt(nums[0]), Integer.parseInt(nums[1])); } } static void addEdge(List> adj, int u, int v) { int biggerVertex = (u > v ? u : v) + 1; //Keep increasing the size until it's right while (adj.size() < biggerVertex) adj.add(new ArrayList<>()); adj.get(u).add(v); adj.get(v).add(u); } }
 import java.util.*; public class Main{ static List> adj; // Function to add edge u --> v static void addEdge(char u, char v){ adj.get(u).add(v); } // Helper function to check for cycle. static boolean checkCycleUtil (char node, boolean visited[], boolean inStack[]){ // Check if node exists in the // recursive stack. if (inStack[node]) return true; // Check if node is already visited. if (visited[node]) return false; // Marking node as visited. visited[node] = true; // Marking node to be present in // recursive stack. inStack[node] = true; // Iterate for all adjacent of // 'node'. for (char v : adj.get(node)){ // Recurse for 'v'. if (checkCycleUtil(v, visited, inStack)) return true; } // Mark 'node' to be removed // from the recursive stack. inStack[node] = false; // Return false if no cycle exists. return false; } // Function to check for the cycle. static boolean checkCycle(int V, int E){ // Defining visited and inStack array // to keep track of visited vertices // and vertices in Recursive stack. boolean visited[] = new boolean[V]; boolean inStack[] = new boolean[V]; for (int i = 0; i < V; i++){ // Check if cycle exists. if (checkCycleUtil((char)i, visited, inStack)){ return true; } } // Returning false, if no cycle is found. return false; } public static void main(String args[]){ // Defining the number of Vertices // and the number of edges. int V = 5, E = 9; // Defining Adjacency List adj = new ArrayList<>(); for (int i = 0; i < V; i++) adj.add(new ArrayList<>()); // Building the Graph same as example 1. addEdge(a,b); addEdge(a,c); addEdge(b,d); addEdge(b,e); addEdge(d,e); addEdge(e,c); addEdge(c,f); addEdge(c,b); addEdge(e,f); // If the graph contains cycle // Print YES if(checkCycle(V, E)) System.out.println("YES"); // Otherwise Print NO else System.out.println("NO"); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!