GraphMatrix - if (!isEdge(v,w)) { adjMatrix[v][w] = true;...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
class GraphMatrix { // simple graph (no multiple edges); undirected; unweighted private int numVertices; private int numEdges; private boolean[][] adjMatrix; public GraphMatrix(int n) { numVertices = n; numEdges = 0; adjMatrix = new boolean[n][n]; } // end constructor public int getNumVertices() { return numVertices; } // end getNumVertices public int getNumEdges() { return numEdges; } // end getNumEdges public boolean isEdge(int v, int w) { return adjMatrix[v][w]; } // end getWeight public void addEdge(int v, int w) {
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: if (!isEdge(v,w)) { adjMatrix[v][w] = true; adjMatrix[w][v] = true; numEdges++; } } // end addEdge public void removeEdge(int v, int w) { if (isEdge(v,w)) { adjMatrix[v][w] = false; adjMatrix[w][v] = false; numEdges--; } } // end remove public int nextAdjacent(int v,int w) // if w<0, return the first adjacent vertex // otherwise, return next one after w // if none exist, return -1 { for (int i=w+1; i<numVertices; i++) if (isEdge(v,i)) return i; return -1; } } // end Graph...
View Full Document

Ask a homework question - tutors are online