Graphs

ADTs So Far…

Types of Graphs and Graph Applications

Sampling of graph terminology:

Graph ADT

Since there is so much variability in types of graphs and how they are used, most standard collection libraries don’t include a Graph implementation. Here is a sample ADT described as a Java interface:

/**
 * Minimal Unweighted Graph Interface. 
 * This interface assumes that the graph size is known at initialization time. 
 * Each node has an integer identifier. 
 * No self-edges are allowed.
 */
public interface Graph<T> {

  void init(int numNodes);

  int numNodes();

  int numEdges();

  void addEdge(int from, int to);

  boolean hasEdge(int from, int to);

  void removeEdge(int from, int to);

  Iterable<Integer> neighbors(int id);

}

Graph Implementations: Adjacency Matrix

Graph Implementations: Adjacency Matrix

Graph Implementations: Adjacency List

Graph Implementations: Adjacency List

Graph Implementation: Hashing

Depth-First Traversal

Breadth-First Traversal

BFS(startV) {
   Enqueue startV in frontierQueue
   Add startV to discoveredSet

   while ( frontierQueue is not empty ) {
      currentV = Dequeue from frontierQueue
      "Visit" currentV
      for each vertex adjV adjacent to currentV {
         if ( adjV is not in discoveredSet ) {
            Enqueue adjV in frontierQueue
            Add adjV to discoveredSet
         }
      }
   }
}

Example:

https://www.cs.usfca.edu/~galles/visualization/BFS.html