import java.util.Scanner; import java.util.Random; /** * Graph objects can be used to work with undirected graphs. *
Graphs are internally represented using adjacency matrices. *
This class provides facilities to record visits of vertices and edges. *
ONLY MODIFY METHODS DFSVISIT AND ISCONNECTED IN THIS CLASS * @author Sophie Quigley * */ public class Graph { /** * Used to generate edges randomly */ static Random rand = new Random(999); //----------------------------------------------------------------------- // The following instance variables are private and immutable /** * Total number of vertices in graph */ private int totalV = 0; /** * Total number of edges in graph */ private int totalE = 0; /** * Adjacency matrix of graph. *
edges[x][y] is the number of edges from vertex x to vertex y. */ private int[][] edges = null; /** * Degree of each vertex. */ private int[] degrees = null; //----------------------------------------------------------------------- // The following instance variables are public and can be used directly // by external graph visitors, or indirectly using methods provided. /** * Used by graph visitors to keep track of visited vertices. */ public boolean[] visitedV = null; /** * Used by graph visitors to keep track of the degree of each vertex * if unvisited edges are counted. */ public int[] unvisitedVDegree = null; /** * Used by graph visitors to keep track of number of visited edges * as an alternative to using unvisitedE. */ public int[][] visitedE = null; /** * Used by graph visitors to keep track of number of unvisited edges * as an alternative to using visitedE. */ public int[][] unvisitedE = null; /** * * Creates a new undirected Graph whose content will be read from the Scanner. *
Input format consists of non-negative integers separated by white space as follows: * * @throws InstantiationException if incorrect data is read * @param in Scanner used to read graph description */ public Graph(Scanner in) throws InstantiationException { // Read number of totalV and handle empty graph totalV = in.nextInt(); if (totalV < 0) { throw new InstantiationException("Number of vertices must be positive"); } if (totalV == 0) return; // Read adjacency matrix // If mistakes are found in edges, the entire matrix is read // before the exception is thrown, int i, j; boolean inputMistake = false; edges = new int[totalV][totalV]; for (i=0; i0) { int randmax = maxParallelEdges+1; for (int i=0; i=0 && sourceV=0 && destVSide-effect: visitedV is modified. * @param vertex vertex being visited */ public void visitV(int vertex) { if (vertex<0 || vertex>=totalV) return; visitedV[vertex] = true; } /** * Unvisit vertex. *
Side-effect: visitedV is modified. * @param vertex vertex being unvisited */ public void unvisitV(int vertex) { if (vertex<0 || vertex>=totalV) return; visitedV[vertex] = false; } /** * Visit edge between two vertices. *
Side-effect: visitedE and unvisitedE are are modified. * @param v1 Vertex incident on edge being visited * @param v2 Vertex incident on edge being visited */ public void visitE(int v1, int v2) { if (v1<0 || v1>=totalV || v2<0 || v2>=totalV) return; visitedE[v1][v2] += 1; unvisitedE[v1][v2] -= 1; if (v1!=v2) { visitedE[v2][v1] += 1; unvisitedE[v2][v1] -= 1; } } /** * Unvisit edge between two vertices. *
Side-effect: visitedE and unvisitedE are are modified. * @param v1 Vertex incident on edge being unvisited * @param v2 Vertex incident on edge being unvisited */ public void unvisitE(int v1, int v2) { if (v1<0 || v1>=totalV || v2<0 || v2>=totalV) return; visitedE[v1][v2] -= 1; unvisitedE[v1][v2] += 1; if (v1!=v2) { visitedE[v2][v1] -= 1; unvisitedE[v2][v1] += 1; } } //======================================================================================== // LAB6: MODIFY THE TWO METHODS UNDERNEATH AND SUBMIT YOUR MODIFIED GRAPH.JAVA // ASSIGNMENT: USE GRAPH.JAVA YOU MODIFIED IN LAB6 BUT DO NOT SUBMIT IT // IN BOTH CASES: DO NOT MODIFY THE RETURN TYPE AND METHOD SIGNATURE OF ISCONNECTED // DFSVISIT IS A HELPER FUNCTION FOR ISCONNECTED AND YOU CAN DO WHATEVER YOU WANT WITH IT. /** * Verifies whether graph is connected. * @return True iff graph is connected */ public boolean isConnected() { } /** * Conducts a Depth First Search visit of the unvisited vertices * of the graph starting at vertex. *
Ties between vertices are broken in numeric order. *
Side-effect: visitedV is modified. * @param vertex First vertex to be visited */ private void DFSvisit(int vertex) { } }