Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How to position this code so it runs without any errors as I always get errors and do not know how to run this code.

How to position this code so it runs without any errors as I always get errors and do not know how to run this code.

 I am trying to run this code in NetBeans ide. Can you send me screenshots on exactly how to position this code so it runs without any errors as I always get errors and do not know how to run this code. Also let me know if I'm doing anything wrong. Is the code wrong.



This is all the code


 


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--;

        }

    }

}


}

 


3.

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);

 }

}

 


4.


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");

    }

   

}


These are my screenshots.

    

O JavaApplication6 - Apache NetBeans IDE 12.0 File Edit View Navigate Source Refactor Run Debug Profile Team Tools Window Help Proj... X Files Services Java Application4.java Test Packages Libraries Test Libraries JavaApplication5 Source Packages javaapplication5 JavaApplication5.java Libraries JavaApplication6 Source Packages LLL0 77788 javaapplication6 JavaApplication6.java JavaApplication6 - Navigat... X Members 4500 JavaApplication6 JavaApplication6() main(String[] args) : > : > : > Main Parser 2 3 Start Page x Source History 1 / To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools Templates and open the template in the editor. package javaapplication6; 4 6 7 8 9 10 11 12 13 14 Q 15 16 17 B 21 22 23 0- 24 25 26 27 . NC JavaApplication4.java x Java Application5.java x JavaApplication6.java @author talha "/ public class JavaApplication6 [ 413.2/4650MB import java.io.*; import java.util. * @param args the command line arguments */ public static void main(String[] args) { 2. // 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 javaapplication6.JavaApplication6 Main I Q- Search (Ctrl+1) 250:2 0 X > 88 LIET T INS Windows (CR..

Step by Step Solution

3.30 Rating (150 Votes )

There are 3 Steps involved in it

Step: 1

Youll need to organize it into a cohesive program Heres how you can do it step by step 1 Import Statements and Graph Class Place the import statements ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial Accounting and Reporting a Global Perspective

Authors: Michel Lebas, Herve Stolowy, Yuan Ding

4th edition

978-1408066621, 1408066629, 1408076861, 978-1408076866

More Books

Students also viewed these Programming questions

Question

What are the alternatives? P-369

Answered: 1 week ago

Question

Case : Karl and June Monroe

Answered: 1 week ago