CS136, Lecture 28

    1. Implementations of Graphs
      1. Adjacency matrix
        1. Representation:
      2. Adjacency lists
        1. Representation:
    2. Applications
      1. Map coloring problem:
Recall that you were to have read about the implementation of the Vertex and Edge classes on your own.

Implementations of Graphs

Classes to represent vertices and edges.

If there are a fixed number of edges from each node then we can have fixed number of edges stored with each node (like a binary tree).

Otherwise we typically use an adjacency matrix or adjacency lists.

Example: Here is an undirected graph of the Northeastern states.

NY, Vt, NH, ME, MA, CN, RI

We will draw a line between capitals if the corresponding states share a common border:

We will represent this graph as both an adjacency matrix and an adjacency list.

Adjacency matrix

Representation:

In an adjacency matrix we fill in the entries with values giving information about the existence or non-existence of edges. Represent no edge with null and existence of edge w/ positive number representing the edge weight.

Labels of vertices are stored in a dictionary, so can look up corresponding index for each vertex label.

NYVTNHMEMACN RI
NYnull2nullnull 11null
VT2null1null1 nullnull
NHnull1null13 nullnull
MEnullnull1nullnull nullnull
MA1 1 3 null null 1 1
CN1 null null null 1 null 1
RI null null null null 1 1 null
Adjacency matrix representation of NE graph

If undirected then we can just keep the lower (or upper) triangular part, since matrix is symmetric.

First define abstract GraphMatrix, then two supclasses, GraphMatrixDirected and GraphMatrixUndirected, which add in missing method bodies for adding, removing, and iterating through edges.

It is simple to add and delete edges.

Addition of or finding node is also simple. (Though there is a clear problem in adding a new node if all rows or columns in the array are already filled.)

Deleting node may require shifting all following nodes over to fill hole (unless just leave hole!).

Solve by keeping a list of available indices for vectors and pull off one when needed.

Clearly with n nodes, this representation requires an array with n2 slots.

abstract public class GraphMatrix implements Graph
{
    protected int size;          // allocation size for graph
    protected Edge data[][];     // matrix - array of arrays
    protected Dictionary dict;   // translate labels ->  
                                              // vertices
    protected List freeList;   // available indices in matrix
    protected boolean directed;  // graph is directed

    protected GraphMatrix(int size, boolean dir)
    // pre: size > 0
    // post: construct an empty graph that may be expanded to
    //     at most size vertices.  Graph directed if dir true
    //     and undirected otherwise
    {
        this.size = size; // set maximum size
        directed = dir;   // fix direction of edges
        // the following constructs a size x size matrix
        data = new Edge[size][size];
        // label to index translation table
        dict = new Hashtable(size);  // come back to later
        // put all indices in the the free list
        freeList = new SinglyLinkedList();
        for (int row = size-1; row >= 0; row--)
          freeList.add(new Integer(row));
    }

    public void add(Object label)
    // pre: label is a non-null label for vertex
    // post: a vertex with label is added to graph.
    //   if vertex with label is already in graph, no action.
    {
        // if there already, do nothing.
        if (dict.containsKey(label)) return;

        Assert.pre(!freeList.isEmpty(), "Matrix not full");
        // allocate a free row and column
        int row = ((Integer) freeList.removeFromHead()).intValue();
        // add vertex to dictionary
        dict.put(label, new GraphMatrixVertex(label, row));
    }

    abstract public void addEdge(Object v1, Object v2, Object label);
    // pre: v1 & v2 are labels of existing vertices
    // post: an edge (possibly directed) inserted btn v1 & v2
    //    if edge new, it is labeled with label (can be null)

    public Object remove(Object label)
    // pre: label is non-null vertex label
    // post: vertex with "equals" label is removed, if found
    {      
        // find and extract vertex
        GraphMatrixVertex vert;
        vert = (GraphMatrixVertex)dict.remove(label);
        if (vert == null) return null;
        // remove vertex from matrix
        int index = vert.index();
        // clear row and column entries
        for (int row=0; row<size; row++) {
          data[row][index] = null;
          data[index][row] = null;
        }
        // add node index to free list
        freeList.add(new Integer(index));
        return vert.label();
    }

    abstract public Object removeEdge(Object vLabel1, Object vLabel2);
    // pre: vLabel1 & vLabel2 are labels of existing vertices
    // post: edge is removed, its label is returned
    
    public Object get(Object label)
    // post: returns actual label of vertex with label "equals" 'label'
    {
        GraphMatrixVertex vert;
        vert = (GraphMatrixVertex) dict.get(label);
        return vert.label();
    }

    public Edge getEdge(Object label1, Object label2)
    // post: returns actual edge between vertices.
    {
        int row,col;
        row = ((GraphMatrixVertex) dict.get(label1)).index();
        col = ((GraphMatrixVertex) dict.get(label2)).index();
        return data[row][col];
    }

    public boolean contains(Object label)
    // post: return true iff vertex w/ "equals" label exists.
    {
        return dict.containsKey(label);
    }

    public boolean containsEdge(Object vLabel1, Object vLabel2)
    // post: returns true iff edge with "equals" label exists
    {
        GraphMatrixVertex vtx1, vtx2;
        vtx1 = (GraphMatrixVertex) dict.get(vLabel1);
        vtx2 = (GraphMatrixVertex) dict.get(vLabel2);
        Assert.condition(vtx1 != null, "Vertex exists");
        Assert.condition(vtx2 != null, "Vertex exists");
        return data[vtx1.index()][vtx2.index()] != null;
    }

    public boolean visit(Object label)
    // post: sets visited flag on vertex, 
     //         returns previous value
    { 
        Vertex vert = (Vertex) dict.get(label);
        return vert.visit();
    }

    public boolean visitEdge(Edge e)
    // pre: sets visited flag on edge; returns previous value
    {
        return e.visit();
    }

    public boolean isVisited(Object label)
    // post: returns visited flag on labelled vertex
    {
        GraphMatrixVertex vert;
        vert = (GraphMatrixVertex) dict.get(label);
        return vert.isVisited();
    }

    public boolean isVisitedEdge(Edge e)
    // post: returns visited flag on edge
    {
        return e.isVisited();
    }

    public void reset()
    // post: resets visited flags to false
    {
        Iterator it = dict.elements();
        for (it.reset(); it.hasMoreElements(); it.nextElement()) 
          ((GraphMatrixVertex)it.value()).reset();
        for (int row=0; row<size; row++)
          for (int col=0; col<size; col++) {
            Edge e = data[row][col];
            if (e != null) e.reset();
          }
    }

    public int size()
    // post: returns the actual number of vertices in graph
    {
        return dict.size();
    }

    public int degree(Object label)
    // pre: label labels an existing vertex
    // post: returns number of vertices adjacent to label
    {
        // get index
        int row = ((GraphMatrixVertex)dict.get(label)).index();
        int col;
        int result = 0;
        // count non-null columns in row
        for (col = 0; col < size; col++)
          if (data[row][col] != null) result++;
        return result;
    }

    abstract public int edgeCount();
    // post: returns the number of edges in graph

    public Iterator elements()
    // post: returns iterator across all vertices of graph
    {
        return dict.keys();
    }

    public Iterator neighbors(Object label)
    // pre: label is label of vertex in graph
    // post: returns iterator vertices adj. to labeled vertex
     {
        GraphMatrixVertex vert;
        vert = (GraphMatrixVertex) dict.get(label);
        List list = new SinglyLinkedList();
        for (int row=size-1; row>=0; row--)
        {
          Edge e = data[vert.index()][row];
          if (e != null) {
            if (e.here().equals(vert.label()))
             list.add(e.there());
            else 
             list.add(e.here());
          }
        }
        return list.elements();
     }
      
    abstract public Iterator edges();
    // post: returns iterator across all edges of graph 
            (returns Edges)

    public void clear()
    // post: removes vertices and edges from graph
    {
        dict.clear();
        for (int row=0; row<size; row++)
          for (int col=0; col<size; col++)
            data[row][col] = null;
        freeList = new SinglyLinkedList();
        for (int row=size-1; row>=0; row--)
          freeList.add(new Integer(row));
    }

    public boolean isEmpty()
    // post: returns true iff graph is empty
    {
      return dict.isEmpty();
    }

    public boolean isDirected()
    // post: returns true iff graph is directed
    {
        return directed;
    }

}

class GraphMatrixVertex extends Vertex 
{
    protected int index;

    public GraphMatrixVertex(Object label, int idx)
    // post: constructs a new augmented vertex
    {
        super(label);
        index = idx;
    }

    public int index()
    // post: returns index associated with vertex
    {
        return index;
    }

    public String toString()
    // post: returns string representation of vertex
    {
        return "<GraphMatrixVertex: "+label()+">";
    }
}

Specialize for either directed or undirected graphs. E.g.,

public class GraphMatrixUndirected extends GraphMatrix
{
    public GraphMatrixUndirected(int size)
    // pre: size > 0
    // post: constructs an empty graph that may be expanded 
    //     to at most size vertices.  Graph is directed if 
    //     dir true and undirected otherwise
    {
        super(size,false);
    }
    public void addEdge(Object vLabel1, Object vLabel2, Object label)
    // pre: vLabel1 & vLabel2 are labels of existing 
    //      vertices, v1 & v2
    // post: edge (possibly directed) is inserted btn v1 & v2 
    //    if edge is new, it is labeled w/ label (can be null)
    {
        GraphMatrixVertex vtx1,vtx2;
        // get vertices
        vtx1 = (GraphMatrixVertex) dict.get(vLabel1);
        vtx2 = (GraphMatrixVertex) dict.get(vLabel2);
        // update matrix with new edge
        Edge e = new Edge(vtx1.label(), vtx2.label(), label, directed);
        data[vtx1.index()][vtx2.index()] = e;
        data[vtx2.index()][vtx1.index()] = e;
    }
    ...
}

Directed graph is similar.

Adjacency lists

Representation:

An adjacency list is composed of a list of nodes or vertices. Hanging off of each node or vertex is a linked list of edges which are adjacent to that node.
        Vertices                  Edges

Labels of the node list may contain different kinds of information than the labels of the edge lists. For instance, the edge nodes will typically contain the value of the edge.

How are vertices held? Could be vector or linked list or ? See later on how to build dictionaries which allow us to look up names fast. Think of as table for now. Each vertex holds collection of edges that are adjacent to it.

Similarly the list of edges could be any kind of collection (class containing add, remove, contains, and elements) including all kinds of lists and binary search tree. We'll use singly-linked list here.

If Directed Graph then not as symmetric (in either representation).

If undirected then put each edge on on 2 lists (of course, same reference in both lists - therefore changes to one impact the other).

Vertices contain adjacencies collection. Most operations passed on to adjacencies.

class GraphListVertex extends Vertex {
    protected Collection adjacencies; // adjacent edges

    public GraphListVertex(Object key)
    // post: constructs new vertex, not incident to any edge
    {
        super(key); // init Vertex fields
        // new adjacency list
        adjacencies = new SinglyLinkedList();
    }

    public void addEdge(Edge e)
    // pre: e is an edge that mentions this vertex
    // post: adds edge to this vertex's adjacency list
    {
        if (!containsEdge(e)) adjacencies.add(e);
    }

    public boolean containsEdge(Edge e)
    // post: returns true if e appears on adjacency list
    {
        return adjacencies.contains(e);
    }

    public Edge removeEdge(Edge e)
    // post: removes and returns adjacent edge "equal" to e
    {
        return (Edge)adjacencies.remove(e);
    }

    public Edge getEdge(Edge e)
    // post: returns the edge that "equals" e, or null
    {
        Iterator edges = adjacencies.elements();
        while (edges.hasMoreElements())
        {
            Edge adjE = (Edge)edges.nextElement();
            if (e.equals(adjE)) return adjE;
        }
        return null;
  }

    public int degree()
    // post: returns the degree of this node
    { 
        return adjacencies.size(); 
    }

    ...
}

Recall that adding edge to singly-linked list always done at beginning of list (constant time, once find vertex).

Edges connected to a given vertex can be held in order by key. Why might this be good or bad?

abstract public class GraphList implements Graph
{
    protected Dictionary dict;  // label to vertex dictionary
    protected boolean directed; // is graph directed?

    protected GraphList(boolean dir)
    // post: constructs an empty graph.
    //     graph is directed iff dir is true.
    {
        dict = new Hashtable();
        directed = dir;
    }
    public void add(Object label)
    // pre: label is a non-null label for vertex
    // post: a vertex with label is added to graph.
    //    if vertex with label is already in graph, no action.
    {
        if (dict.containsKey(label)) return; // vertex exists
        GraphListVertex v = new GraphListVertex(label);
        dict.put(label,v);
    }

    public Object get(Object label)
    // pre: label labels a valid vertex
    // post: return label of vertex w/ label "equals" 'label'
    {
        Assert.condition(dict.containsKey(label), "Vertex exists");
        return ((GraphListVertex) dict.get(label)).label();
    }

    public boolean contains(Object label)
    // post: returns true iff vertex with "equals" label exits.
    {
        return dict.containsKey(label);   
    }

    public Edge getEdge(Object label1, Object label2)
    // post: returns actual label of edge between vertices.
    {   
        Assert.condition(dict.containsKey(label1), "Vertex exists");
        Edge e = new Edge(get(label1),get(label2),null,directed); 
        return ((GraphListVertex) dict.get(label1)).getEdge(e);
    }

    public boolean containsEdge(Object vLabel1, Object vLabel2)
    // post: returns true iff edge with "equals" label exists
    {
        Assert.condition(dict.containsKey(vLabel1), "Vertex exists");
        Edge e = new Edge(vLabel1, vLabel2, null, directed); 
        return ((GraphListVertex) dict.get(vLabel1)).containsEdge(e);
    }

    public int size()
    // post: returns the number of vertices in graph
    { 
        return dict.size(); 
    }

    public int degree(Object label)
    // pre: label is a label of a vertex
    // post: returns the degree of vertex
    {
        Assert.condition(dict.containsKey(label), "Vertex exists.");
        return ((GraphListVertex) dict.get(label)).degree();
    }

    abstract public int edgeCount();
    // post: returns the number of edges in graph

    public Iterator elements()
    // post: returns iterator across all vertices of graph
    {
        return dict.keys();
    }

    public Iterator neighbors(Object label)
    // pre: label labels an existing vertex
    // post: returns an iterator traversing neighbor vertices
    {
    // return towns adjacent to vertex labeled lable
        Assert.condition(dict.containsKey(label),  "Vertex exists");
        return ((GraphListVertex) dict.get(label)).adjacentVertices();
    }


    public void clear()
    // post: removes all vertices from graph
    {
      dict.clear();
    }

    ...
}

Adding nodes handled by dictionary (discuss later). Deleting is more complex since must remove vertex for all edge lists (see below).

Operations on edges depend on whether directed or undirected, e.g.,


public class GraphListUndirected extends GraphList
{
    public GraphListUndirected()
    // post: constructs an undirected graph
    {
        super(false);
    }

    public void addEdge(Object vLabel1, Object vLabel2, Object label)
    // pre: vLabel1 & vLabel2 are labels of existing vertices, v1 & v2
    // post: edge (possibly directed) is inserted btn v1 & v2
    //     if edge new, it is labeled with label (can be null)
    {
      GraphListVertex v1 = (GraphListVertex) dict.get(vLabel1);
      GraphListVertex v2 = (GraphListVertex) dict.get(vLabel2);
      Edge e = new Edge(v1.label(), v2.label(), label, false);
      v1.addEdge(e);
      v2.addEdge(e);
    }

    public Object remove(Object label)
    // pre: label is non-null vertex label
    // post: vertex with "equals" label is removed, if found
    {
        GraphListVertex v = (GraphListVertex)dict.get(label);

    // we need to remove each of the reverse edges:
        Iterator vi = neighbors(label);
        while (vi.hasMoreElements())
        {
            // list of adjacent labels
            Object v2 = vi.nextElement();
            // this will remove both edges:
            removeEdge(label,v2);
        }
        dict.remove(label);
        return v.label();
    }

    public Object removeEdge(Object vLabel1, Object vLabel2)  
    // pre: vLabel1 and vLabel2 are labels of existing vertices
    // post: edge is removed, its label is returned
    {
      GraphListVertex v1 = (GraphListVertex) dict.get(vLabel1);
      GraphListVertex v2 = (GraphListVertex) dict.get(vLabel2);
      Edge e = new Edge(v1.label(), v2.label(), null, false);
      v2.removeEdge(e);
      return (v1.removeEdge(e)).label();
    }
    ...
}
Notice how deleting a vertex is expensive since must delete all adjacent edges.

Adding edges is relatively straightforward: just add it in the adjacency lists of vertices if it is not already there.

Deleting an edge requires a search of the appropriate vertex edge list(s).

Applications

Map coloring problem:

The map coloring problem is to color the states in a map in as few colors as possible so that no two adjoining states are of the same color.

An equivalent problem is to color the nodes of the corresponding graph so every edge has nodes of different colors. (See the graph obtained from the Northeast states - imagine making sure all capitals of adjacent states are colored different colors.)

We can color the following graph with only three colors:

Some however, require 4 colors:

An important mathematical result from the late 1970's shows that 4 colors are always enough for planar graphs (satisfying some very reasonable conditions).

Why should this remind you of course scheduling?