0

Really stumped on this one, don't know where to begin. I thought about making a separate method which calculates the paths between two different vertices, but I don't know how I would implement that.

I thought that finding the minimum degree of the graph and then adding one would give me the girth, but that is assuming there is a cycle in the graph. So I would need the program to scan through the adjacency matrix, somehow uses the true values to find out if there is a cycle or not and then calculate the distance of that cycle.

Looking for a step in the right direction.

Also I'm looking to not use the ArrayList approach, hence why I'm stumped

This is what I have so far:

    import java.util.Scanner;
    import java.util.*;

    public class Graph {

// Setup privately modified variables which will define the graph

// These two parameters are storage variables for edges and vertices
    //These variables were changed from Vertex and Edge to numVertices and numEdges.
private int numVertices;
private int numEdges;

// This will be the adjacency matrix to represent our graph, this will
// represent edges.
    // adj_Matrix_Edges was previously static meaning it did not have access to multiple graphs, onyl one graph.
private boolean[][] adj_Matrix_Edges;

// first step will be to setup the graph, using this constructor
public Graph(int vertices) {

    numVertices = vertices;

    if (numVertices < 0) {
        throw new RuntimeException(
                "Number of vertices cannot be a nonnegative value");
    }

    System.out.println("There are now " + numVertices
            + " vertices in the graph.");

    // A graph is created based on the specifications, N X N or (n^2)
    // graph.
    adj_Matrix_Edges = new boolean[vertices][vertices];
}

// This method validates whether or not two vertices are adjacent, returns
// true if adjacent false otherwise.
public boolean adjacent(int i, int j) {

    if (adj_Matrix_Edges[i][j] == true) {
        return true;
    } else {
        return false;
    }
}

// I needed a review of this class so I had to read about ArrayList class, but it allows
// you to iterate the columns in the adjacency matrix.
// It also allows you to print out integers values instead of booleans
// The for loop, loops over a column you would select, and then the if
// statement checks for an incident in that column.
public List<Integer> neighbors(int vertex) {
    List<Integer> neighbors = new ArrayList<Integer>();
    for (int i = 0; i < adj_Matrix_Edges.length; i++) {

        // The if statement here does not need an equality sign since
        // booleans are in the 2-d matrix.
        if (adj_Matrix_Edges[vertex][i]) {
            // adds that vertex i to the list
            neighbors.add(i);
        }
    }
    System.out.println("The neighbors of vertex " + vertex + " are " + neighbors);
    return neighbors;
}

//This method will count the number of neighbors for a specific vertex.
public double averageDegree(){

    //create a variable for the count, and initialize the counter to 0.
    double neighborCount = 0;

    for (int i = 0; i < adj_Matrix_Edges.length; i++){
        for (int j = 0; j < adj_Matrix_Edges[i].length; j++){
            //this if statement scans the specific vertex for true statements in the boolean array
            if (adj_Matrix_Edges[i][j]){
                // this logical expression adds up the count of the true statements, in graph theory this is adding up the
                // degree of that specific vertex.
                neighborCount++;
            }
        }
    }
    neighborCount= neighborCount / numVertices;
    System.out.println("The average degree of the graph is " + neighborCount);
    return neighborCount;
}

public boolean[][] addVertex() {

    // add an extra vertex to the graph.
    numVertices++;

    // secondly  we have to copy over the contents of the old array into a new array.

    // Initialize a new array

    boolean[][] new_adj_Matrix_Edges = adj_Matrix_Edges;

    // setup a for loop which sets up new values for 

    for (int i = 0; i < adj_Matrix_Edges.length; i++){
        for (int j = 0; j < adj_Matrix_Edges.length; j++){
            adj_Matrix_Edges[i][j] = new_adj_Matrix_Edges[i + 1][j + 1];
        }
    }
    return new_adj_Matrix_Edges;

}

public boolean[][] removeVertex(int vertex){

    // set a local variable.
    int vertex_Selected = vertex;

    // create a new 2-d array where you can copy the old one over.
    boolean[][] new_adj_Matrix_Edges = adj_Matrix_Edges;

    //create a for loop setup to copy over all data from old array to the new array.
    for (int g = 0; g < adj_Matrix_Edges.length; g++){
        for (int h = 0; h < adj_Matrix_Edges[g].length; h++){
            adj_Matrix_Edges[g][h] = new_adj_Matrix_Edges[g][h];
        }
    }

    // now that a new array has been created, and all information is copied over we can then set 
    // all values of the selected vertex to false.

    for (int i = 0; i < new_adj_Matrix_Edges.length; i++){
        for (int j = 0; j < new_adj_Matrix_Edges[i].length; j++){
            if (new_adj_Matrix_Edges[vertex_Selected][j] == true){
                new_adj_Matrix_Edges[vertex_Selected][j] = false;
            }
            if (new_adj_Matrix_Edges[i][vertex_Selected] == true){
                new_adj_Matrix_Edges[i][vertex_Selected] = false;
            }
        }
    }

    return new_adj_Matrix_Edges;
}

public void addEdge(int vertex_add_1, int vertex_add_2) {

    if (adj_Matrix_Edges[vertex_add_1][vertex_add_2] == false) {
        adj_Matrix_Edges[vertex_add_1][vertex_add_2] = true;
        adj_Matrix_Edges[vertex_add_2][vertex_add_1] = true;
    } else {
        System.out.println("There is already an edge between vertex "
                + vertex_add_1 + " and vertex " + vertex_add_2 + ".");
    }
}

// This method removes an edge if the two int values in the 2-d boolean
// array are true, converts to false, otherwise it stays false if no edge
// present
public void removeEdge(int vertex_remove_1, int vertex_remove_2) {

    if (adj_Matrix_Edges[vertex_remove_1][vertex_remove_2] == true) {
        adj_Matrix_Edges[vertex_remove_1][vertex_remove_2] = false;
        adj_Matrix_Edges[vertex_remove_1][vertex_remove_2] = false;
    } else {
        System.out.println("There is no edge between vertex "
                + vertex_remove_1 + " and vertex " + vertex_remove_2);
    }
}

// setup a method which finds the shortest cycle in the graph.
// We want to set the method to return an int value, because the girth of the graph will represent an integer value 
// which cannot be a negative value.

// if we find the diameter of a graph then the shortest cycle will be the girth, which will be the 
// 2diam(G) + 1. 
public int girth(){

    // Set the  

}

public int pathVertices(int vertex_1, int vertex_2){

    if (vertex_1 != vertex_2){

    }

}



public static void main(String[] args) {


    // Make an arbritary graph with 5 vertices. 
    Graph graph = new Graph(10);

    graph.addEdge(1, 2);
    graph.removeEdge(0, 1);
    graph.adjacent(1, 2);
    graph.adjacent(2, 1);
    graph.neighbors(1);
    graph.neighbors(4);
    graph.addVertex();
    graph.removeVertex(0);
    graph.averageDegree();



    //      for (int i = 0; i < adj_Matrix_Edges.length; i++) {
    //          for (int j = 0; j < adj_Matrix_Edges[i].length; j++) {
    //              System.out.println(adj_Matrix_Edges[i][j] + " ");
    //          }
    //          System.out.println("-----");
    //      }

        }
    }

0 Answers0