{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

GraphMatrix

# GraphMatrix - if!isEdge(v,w adjMatrix[v[w = true...

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

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

{[ snackBarMessage ]}

Ask a homework question - tutors are online