GraphList - adjList[w.add(adjList[w.size 1,new Integer(v...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
class GraphList { // simple graph (no multiple edges); undirected; unweighted private int numVertices; private int numEdges; private List<Integer> adjList[]; public GraphList(int n) { numVertices = n; numEdges = 0; adjList = new List[n]; for (int i=0; i<n; i++) adjList[i] = new List<Integer>(); } // end constructor public int getNumVertices() { return numVertices; } // end getNumVertices public int getNumEdges() { return numEdges; } // end getNumEdges public boolean isEdge(int v, int w) { List<Integer> L = adjList[v]; for (int i=1; i<=L.size(); i++) if (L.get(i).intValue() == w) return true; return false; } // end getWeight public void addEdge(int v, int w) { if (!isEdge(v,w)) { adjList[v].add(adjList[v].size()+1,new Integer(w));
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: adjList[w].add(adjList[w].size()+1,new Integer(v)); numEdges++; } } // end addEdge public void removeEdge(int v, int w) { for (int i=1; i<=adjList[v].size(); i++) if (adjList[v].get(i).intValue() == w) { adjList[v].remove(i); break; } for (int i=1; i<=adjList[w].size(); i++) if (adjList[w].get(i).intValue() == v) { adjList[w].remove(i); numEdges--; break; } } // end remove public int kth_Adjacent(int v,int k) // return the k-th adjacent vertex to v // or -1 if degree of v is smaller than k { List<Integer> L = adjList[v]; if (k>=1 && k<=L.size()) return L.get(k).intValue(); else return -1; } } // end Graph...
View Full Document

This note was uploaded on 04/26/2010 for the course CMPT 225 taught by Professor Annelavergne during the Spring '07 term at Simon Fraser.

Page1 / 2

GraphList - adjList[w.add(adjList[w.size 1,new Integer(v...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online