Graph implementation using adjacency lists.

public class Graph {
    // instance variables go here:

    // constructor: n is the number of vertices, 
    // directed is a boolean which is true if the graph is directed,
    // false otherwise
    public Graph(int n, boolean directed) {

    }

    // startPoints are the starting points of edges, endPoints are the
    // ending points
    public void setEdges(int [] startPoints, int [] endPoints) {

    }

    public String toString() {
        return "";
    }
}

Testing code for a graph:

public class TestGraphs {
    public static void main(String [] args) {
	Graph g = new Graph(10,false); // undirected graph
	int [] starts = {0, 6, 3, 4, 2, 3, 5, 7, 8, 0, 9, 1};
	int [] ends = {1, 2, 0, 7, 3, 1, 2, 5, 6, 8, 4, 9};
	g.setEdges(starts, ends);
	System.out.println(g);
    }
}

This is an example from CSci 2101 course.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.