CITS2200
Class Graph

java.lang.Object
  extended by CITS2200.Graph

public class Graph
extends Object

Graph.java is a class to assist with graph and network algorithms for CITS3210. It is assumed that the edges of the graph have only positive integer weights (of value 1 if the graph is not weighted).


Method Summary
 int[][] getEdgeMatrix()
           
 int getNumberOfVertices()
           
 int getWeight(int u, int v)
           
 boolean isDirected()
           
 boolean isWeighted()
           
static Graph randomBipartiteGraph(int numberOfVertices, boolean directed, double density)
          Creates a random unweighted bipartite graph
static Graph randomGraph(int numberOfVertices, boolean directed, double density)
          Creates a random unweighted graph
static Graph randomGraph(int numberOfVertices, double density)
          Creates a random unweighted, undirected graph
static Graph randomWeightedBipartiteGraph(int numberOfVertices, boolean directed, double density, int maxWeight)
          Creates a random weighted bipartite graph.
static Graph randomWeightedGraph(int numberOfVertices, boolean directed, double density, int maxWeight)
          Creates a random weighted graph.
static Graph readFile(String filename, boolean weighted, boolean directed)
          reads an adjacency matrix from a file and returns it.
 String toString()
          This method produces a representation of the graph that corresponds to the adjacency matrix used by the readFile method.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Method Detail

getWeight

public int getWeight(int u,
                     int v)
Parameters:
u - the start of the edge
v - the end of the edge
Returns:
the weight of an edge.

getNumberOfVertices

public int getNumberOfVertices()
Returns:
the number of vertices

isDirected

public boolean isDirected()
Returns:
true if the graph is directed

isWeighted

public boolean isWeighted()
Returns:
true if the graph is weighted

getEdgeMatrix

public int[][] getEdgeMatrix()
Returns:
a clone of the edgeMatrix

randomGraph

public static Graph randomGraph(int numberOfVertices,
                                boolean directed,
                                double density)
Creates a random unweighted graph

Parameters:
numberOfVertices - the nukmber of vertices
directed - true if the graph is to be directed
density - between 0 and 1, the probablity of an edge existing.
Returns:
a random graph of the specified density

randomGraph

public static Graph randomGraph(int numberOfVertices,
                                double density)
Creates a random unweighted, undirected graph

Parameters:
numberOfVertices - the nukmber of vertices
density - between 0 and 1, the probablity of an edge existing.
Returns:
a random graph of the specified density

randomWeightedGraph

public static Graph randomWeightedGraph(int numberOfVertices,
                                        boolean directed,
                                        double density,
                                        int maxWeight)
Creates a random weighted graph. The weights will be even distributed between 1 and the maximum edge weight (inclusive).

Parameters:
numberOfVertices - the nuxkmber of vertices
directed - true if the graph is to be directed
maxWeight - the maximum edgeWeight for the Graph.
density - between 0 and 1, the probablity of an edge existing.
Returns:
a random graph of the specified density

randomWeightedBipartiteGraph

public static Graph randomWeightedBipartiteGraph(int numberOfVertices,
                                                 boolean directed,
                                                 double density,
                                                 int maxWeight)
Creates a random weighted bipartite graph. The weights will be even distributed between 1 and the maximum edge weight (inclusive), the vertices will be partitioned into odd and even vertices

Parameters:
numberOfVertices - the nuxkmber of vertices
directed - true if the graph is to be directed
maxWeight - the maximum edgeWeight for the Graph.
density - between 0 and 1, the probablity of an edge existing.
Returns:
a random graph of the specified density

randomBipartiteGraph

public static Graph randomBipartiteGraph(int numberOfVertices,
                                         boolean directed,
                                         double density)
Creates a random unweighted bipartite graph

Parameters:
numberOfVertices - the nukmber of vertices
directed - true if the graph is to be directed
density - between 0 and 1, the probablity of an edge existing.
Returns:
a random graph of the specified density

readFile

public static Graph readFile(String filename,
                             boolean weighted,
                             boolean directed)
                      throws Exception
reads an adjacency matrix from a file and returns it. The adjacency matrix is assumed to have the following format in the file: n 0 6 0 0 ... 0 0 0 0 3 ... 0 ... 1 0 7 0 ... 0 where the first line contains an integer that denotes the dimensions of the square matrix of integers that follow, and there are n integers (tab separated) on each of the subsequent lines.

Parameters:
filename - the name of the file to be read
weighted - true if the Graph is to be weighted
directed - true if the graph is to be directed
Returns:
A graph read fro the file.
Throws:
Exception

toString

public String toString()
This method produces a representation of the graph that corresponds to the adjacency matrix used by the readFile method.

Overrides:
toString in class Object
Returns:
a String representation of the graph,