Presentation is loading. Please wait.

Presentation is loading. Please wait.

Implementation of Graphs Identification of Classes. The Vertex Interface. The Edge Interface. Graph and Digraph Interfaces. Two concrete Implementations.

Similar presentations


Presentation on theme: "Implementation of Graphs Identification of Classes. The Vertex Interface. The Edge Interface. Graph and Digraph Interfaces. Two concrete Implementations."— Presentation transcript:

1 Implementation of Graphs Identification of Classes. The Vertex Interface. The Edge Interface. Graph and Digraph Interfaces. Two concrete Implementations. –GraphAsMatrix. –GraphAsLists. Review Questions.

2 Identification of Classes A graph can be viewed as a container with two sections, one which holds vertices and one which holds edges. Thus, we can identify four kinds of objects: vertices, edges, directed graphs, and undirected graphs. Accordingly, we define four interfaces: Vertex, Edge, Graph, and Digraph.

3 The Vertex Interface Each vertex must be distinguishable from other vertices. Thus, each vertex should have a unique ID number. Also, some applications require vertex-weighted graphs. The following code defines the Vertex interface. 1 public interface Vertex 2 extends MyComparable 3 { 4 int getNumber (); 5 Object getWeight (); 6 Enumeration getIncidentEdges (); 7 Enumeration getEmanatingEdges (); 8 Enumeration getPredecessors (); 9 Enumeration getSuccessors (); 10 }

4 The Edge Interface An edge in a directed graph is an ordered pair of vertices; while in an undirected graph it is a set of two vertices. We use the same class for both--the context determines whether it is directed or undirected. The following code defines the Edge interface. 1 public interface Edge 2 extends MyComparable 3 { 4 Vertex getV0 (); 5 Vertex getV1 (); 6 Object getWeight (); 7 boolean isDirected (); 8 Vertex getMate (Vertex vertex); 9 }

5 The Graph Interface We define the Graph interface to represent undirected graphs and then use it to derive the Digraph interface. 1 public interface Graph extends Container { 2 int getNumberOfEdges (); 3 int getNumberOfVertices (); 4 boolean isDirected (); 5 void addVertex (int v); 6 void addVertex (int v, Object weight); 7 Vertex getVertex (int v); 8 void addEdge (int v, int w); 9 void addEdge (int v, int w, Object weight); 10 Edge getEdge (int v, int w); 11 boolean isEdge (int v, int w); 12 Enumeration getVertices (); 13 Enumeration getEdges (); 14 boolean isConnected (); 15 boolean isCyclic (); 16 void depthFirstTraversal(PrePostVisitor visitor,int start); 17 void breadthFirstTraversal (Visitor visitor, int start); 18 }

6 The Digraph Interface The following defines the Digraph interface as an extention of the Graph interface. 1 public interface Digraph 2 extends Graph 3 { 4 boolean isStronglyConnected (); 5 void topologicalOrderTraversal (Visitor visitor); 6 }

7 The AbstractGraph class The following introduces the AbstractGraph class. 1 public abstract class AbstractGraph extends 2 AbstractContainer implements Graph { 3 protected int numberOfVertices; 4 protected int numberOfEdges; 5 protected Vertex vertex[]; 6 public AbstractGraph(int size) { 7 vertex = new Vertex[size]; 8 } 9 public int getNumberOfVertices() { 10 return numberOfVertices; 11 } 12 public int getNumberOfEdges() { 13 return numberOfEdges; 14 } 15 public Vertex getVertex(int v) { 16 if (v>=0 && v<numberOfVertices) 17 return vertex[v]; 18 else 19 throw new NoSuchElementException(); 20 }

8 The AbstractGraph class- Cont'd 21 public Enumeration getVertices() { 22 return new Enumeration() { 23 protected int v; 24 public boolean hasMoreElements(){ 25 return v < numberOfVertices; 26 } 27 public Object nextElement() { 28 if(v >= numberOfVertices) 29 throw new NoSuchElementException(); 30 else 31 return vertex[v++]; 32 } 33 }; 34 } 35 public abstract Enumeration getIncidentEdges(int i); 36 public abstract Enumeration getEmanatingEdges(int i); 37 public abstract void addEdge(Edge edge); 38 public abstract Edge getEdge(int i, int j); 39 public abstract boolean isEdge(int i, int j); 40 public abstract Enumeration getEdges(); 41 //...

9 The GraphVertex class The GraphVertex is implemented as an inner class. 1 public abstract class AbstractGraph extends 2 AbstractContainer implements Graph { 3 protected final class GraphVertex extends AbstractObject 4 implements Vertex { 5 protected int number; 6 protected Object weight; 7 public GraphVertex(int i, Object obj) { 8 number = i; weight = obj; 9 } 10 public int getNumber() { 11 return number; 12 } 14 public Object getWeight() { 15 return weight; 16 } 17 public Enumeration getIncidentEdges() { 18 return AbstractGraph.this.getIncidentEdges(number); 19 } 20 //....

10 The GraphEdge class The GraphEdge is also implemented as an inner class. 1 public abstract class AbstractGraph extends 2 AbstractContainer implements Graph { 3 protected final class GraphEdge extends AbstractObject 4 implements Edge { 5 protected int v0, v1; 6 protected Object weight; 7 public GraphEdge(int v, int w, Object obj) 8 v0 = v; v1 = w; weight = obj; 9 } 10 public Vertex getV0() { 11 return vertex[v0]; 12 } 13 public Vertex getMate(Vertex vertex1) { 14 if(vertex1.getNumber() == v0) 15 return vertex[v1]; 16 if(vertex1.getNumber() == v1) 17 return vertex[v0]; 18 else 19 throw new IllegalArgumentException("Bad Vertex"); 20 } 21...

11 Implementing GraphAsMatrix The following describes the concrete class, GraphAsMatrix. 1 public class GraphAsMatrix extends AbstractGraph { 2 protected Edge matrix[][]; 3 public GraphAsMatrix(int size) { 4 super(size); 5 matrix = new Edge[size][size]; 6 } 7 protected void addEdge(Edge edge) { 8 int i = edge.getV0().getNumber(); 9 int j = edge.getV1().getNumber(); 10 if(matrix[i][j] != null) 11 throw new IllegalArgumentException("duplicate edge); 12 if(i == j) { 13 throw new IllegalArgumentException("not simple"); 14 } 15 else { 16 matrix[i][j] = edge; 17 matrix[j][i] = edge; 18 super.numberOfEdges++; 19 } 20 }

12 Implementing GraphAsMatrix - Cont'd 21 public Enumeration getEdges() { 22 return new Enumeration() { 23 protected int v, w; 24 { 25 for(v = 0; v < numberOfVertices; v++) 26 for(w = v + 1; w < numberOfVertices; w++) 27 if(matrix[v][w] != null) break; 28 } 29 public boolean hasMoreElements() { 30 return v < numberOfVertices && w < numberOfVertices; 31 } 32 public Object nextElement() { 33 if(v >= numberOfVertices || w >= numberOfVertices) 34 throw new NoSuchElementException(); 35 Edge edge = matrix[v][w]; 36 for(w++; w < numberOfVertices; w++) 37 if(matrix[v][w] != null) return edge; 38 for(v++; v < numberOfVertices; v++) 39 for(w = v + 1; w < numberOfVertices; w++) 40 if(matrix[v][w] != null) return edge; 41 return edge; 42 }}; 43 } //..

13 Implementing GraphAsLists The following describes the concrete class, GraphAsLists. 1 public class GraphAsLists extends AbstractGraph { 2 protected LinkedList adjacencyList[]; 3 public GraphAsLists(int i) { 4 super(i); 5 adjacencyList = new LinkedList[i]; 6 for(int j = 0; j < i; j++) 7 adjacencyList[j] = new LinkedList(); 8 } 9 protected void addEdge(Edge edge) { 10 int i = edge.getV0().getNumber(); 11 adjacencyList[i].append(edge); 12 super.numberOfEdges++; 13 } 14 //…

14 Implementing GraphAsLists - Cont'd 15 protected Enumeration getEmanatingEdges(int i) { 16 final Vertex final_vertex = getVertex(i); 17 return new Enumeration() { 18 protected int v; 19 protected LinkedList.Element ptr; 20 { 21 v = final_vertex.getNumber(); 22 ptr = adjacencyList[v].getHead(); 23 } 24 public boolean hasMoreElements() { 25 return ptr != null; 26 } 27 public Object nextElement() { 28 if(ptr == null) { 29 throw new NoSuchElementException(); 30 } else { 31 Object obj = ptr.getDatum(); 32 ptr = ptr.getNext(); 33 return obj; 34} 35 }}; 36 }//…

15 Review Questions 1.Complete the implementation of the GraphVertex class by implementing the following methods: getEmanatingEdges(), getPredecessors(), getSuccessors(), toString() and compareTo(). 2.Complete the implementation of the GraphEdge class by implementing the following methods: getV1(), getWeight(), isDirected(), toString() and compareTo(). 3.Complete the implementation of the AbstractGraph class by implementing the following methods: the two addEdge methods, the three addVertex methods, isDirected(), getEnumeration(), accept(), purge() and toString(). 4.Complete the implementation of the GraphAsMatrix class by implementing the following methods: getEdge(int v, int w), isEdge(int v, int w), getEmanatingEdges(), getIncidentEdges(), purge() and compareTo(). 5.Complete the implementation of the GraphAsLists class by implementing the following methods: getEdge(int v, int w), isEdge(int v, int w), getEdges(), getIncidentEdges(), purge() and compareTo().


Download ppt "Implementation of Graphs Identification of Classes. The Vertex Interface. The Edge Interface. Graph and Digraph Interfaces. Two concrete Implementations."

Similar presentations


Ads by Google