GraphMatrixTester - v=G.nextAdjacent(u,v); }...

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

View Full Document Right Arrow Icon
public class GraphMatrixTester { private static int gcd(int a,int b) { if (a==0) return b; if (b==0) return a; return gcd(b,a%b); } private static boolean coprime(int a,int b) { return gcd(a,b)==1; } public static void print(GraphMatrix G) { System.out.println("List of edges:"); for (int u=0; u<G.getNumVertices(); u++) { // traverse through neigbors of u int v=G.nextAdjacent(u,-1); while (v>=0) { if (u<v) // print each edge only once System.out.print(" ["+u+","+v+"]");
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: v=G.nextAdjacent(u,v); } System.out.println(); } } public static void main(String args) { GraphMatrix G = new GraphMatrix(10); // graph with 10 vertices // add some edges for (int u=0; u<G.getNumVertices(); u++) for (int v=u+1; v<G.getNumVertices(); v++) if (coprime(u,v)) G.addEdge(u,v); print(G); // remove some edges for (int u=1; u<G.getNumVertices(); u++) G.removeEdge(u-1,u); print(G); } }...
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.

Ask a homework question - tutors are online