13-2 - Using nextAdjacent To list all vertices connected to...

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

View Full Document Right Arrow Icon
Using nextAdjacent() To list all vertices connected to a vertex u in graph G we can use the following loop: for ( int v=G.nextAdjacent(u,-1); v>=0; v=G.nextAdjacent(u,v)) { System.out.print( v ); }
Background image of page 1

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

View Full DocumentRight Arrow Icon
Adjacency Lists The edges are recorded in an array of size | V | of linked lists In an unweighted graph a list at index i records the keys of the vertices adjacent to vertex i In a weighted graph a list at index i contains pairs which record vertex keys (of vertices adjacent to i ) and their associated edge weights Looking up an edge requires time proportional to the number of edges adjacent to a node (a degree of a vertex) Finding all vertices adjacent to a given vertex also takes time proportional to the degree of the vertex (which is minimal possible) The list requires O ( | E | ) space
Background image of page 2
Adjacency List Examples A B C D E F G B A C D E G F A B C D E F G B A C D E G F 4 1 5 8 2 2 5 1 1 3 2 3 B 1 B C D F A C E G A B E G A B C G A G B C E F 3 D 5 F 2 E 5 A 2 B 4 C 1 A 2 C 3 G 8 G
Background image of page 3

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

View Full DocumentRight Arrow Icon
Implementation with adjacency lists class Graph { // simple graph (no multiple edges); undirected; unweighted private int numVertices; private int numEdges; private List<Integer> adjList[]; public Graph( 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; } public int getNumEdges() { return numEdges; } public boolean isEdge( int v, int w) {
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the 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 / 17

13-2 - Using nextAdjacent To list all vertices connected to...

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

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