11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

382 Chap. 11 Graphs}/** Set the weight for an edge */public void setEdge(int i, int j, int weight) {assert weight != 0 : "May not set weight to 0";Edge currEdge = new Edge(j, weight);if (isEdge(i, j)) { // Edge already exists in graphvertex[i].remove();vertex[i].insert(currEdge);}else { // Keep neighbors sorted by vertex indexnumEdge++;for (vertex[i].moveToStart();vertex[i].currPos() < vertex[i].length();vertex[i].next())if (vertex[i].getValue().vertex() > j) break;vertex[i].insert(currEdge);}}/** Delete an edge */public void delEdge(int i, int j){ if (isEdge(i, j)) { vertex[i].remove(); numEdge--; } }/** Determine if an edge is in the graph */public boolean isEdge(int v, int w) {Edge it = vertex[v].getValue();// Check if j is the current neighbor in the listif ((it != null) && (it.vertex() == w)) return true;for (vertex[v].moveToStart();vertex[v].currPos() < vertex[v].length();vertex[v].next())// Check whole listif (vertex[v].getValue().vertex() == w) return true;return false;}/** @return an edge’s weight */public int weight(int i, int j) {if (isEdge(i, j)) return vertex[i].getValue().weight();return 0;}/** Set/Get the mark value for a vertex */public void setMark(int v, int val) { Mark[v] = val; }public int getMark(int v) { return Mark[v]; }Figure 11.7 (continued)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!