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

Slides:



Advertisements
Similar presentations
CSE 373 Data Structures and Algorithms Lecture 20: Graphs II.
Advertisements

Graphs Chapter 30 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Introduction to Design Patterns What is Design Pattern? The Container Pattern. The Visitor Pattern. The SearchableContainer Pattern. The Enumeration Pattern.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
Implementation of Graphs Identification of Classes and Interfaces Concrete Implementations for Graph Review Questions.
Introduction to Stacks What is a Stack Stack implementation using arrays. Application of Stack.
Topological Sort Introduction. Definition of Topological Sort.
Testing for Connectedness and Cycles
1 Hashing Techniques: Implementation Implementing Hash Functions Implementing Hash Tables Implementing Chained Hash Tables Implementing Open Hash Tables.
Introduction to Stacks What are Stacks? Stack implementation using arrays. Stack application.
1 Testing for Connectedness and Cycles Connectedness of Undirected Graphs Implementation of Connectedness detection Algorithm for Undirected Graphs. Implementation.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
What is an Interface? 4 Common in everyday life. –Movie theaters, TVs, VCRs, etc. 4 Java interface definitions are simple: –Method headers. –Field definitions.
Hashing with Separate Chaining Implementation – data members public class SCHashTable implements HashTableInterface { private List [] table; private int.
JAVA Objects & The Comparable Interface The MyComparable Interface. The MyComparable Interface. The AbstractObject class. The AbstractObject class. Wrapper.
Introduction to Stacks What is a Stack Stack implementation using array. Stack implementation using linked list. Applications of Stack.
1 Introduction to Design Patterns What is Design Pattern? The Container Pattern. The Iterator Pattern. The Visitor Pattern. The SearchableContainer Pattern.
Graph Traversals Depth-First Traversal. The Algorithm.
Queues What is a queue? Queue Implementations: –As Array –As Circular Array –As Linked List Applications of Queues. Priority queues.
1 Binary Search Trees (BST) What is a Binary search tree? Why Binary search trees? Binary search tree implementation Insertion in a BST Deletion from a.
1 Introduction to Stacks What is a Stack? Stack implementation using array. Stack implementation using linked list. Applications of Stacks.
Introduction to Design Patterns What is Design Pattern? The Container Pattern. The Visitor Pattern. The SearchableContainer Pattern. The Iterator Pattern.
1 Binary Tree Traversals Binary Tree Traversal classification Tree traversal animations DepthFirst traversal abstraction Accept method of AbstractTree.
1 Topological Sort Introduction. Definition of Topological Sort. Topological Sort is Not Unique. Topological Sort Algorithms. An Example. Implementation.
Minimum Spanning Tree What is a Minimum Spanning Tree. Constructing a Minimum Spanning Tree. What is a Minimum-Cost Spanning Tree. Prim’s Algorithm. –Animated.
Introduction to Design Patterns What is Design Pattern? The Container Pattern. The Iterator Pattern. The Visitor Pattern. The SearchableContainer Pattern.
Testing for Connectedness & Cycles Connectedness of an Undirected Graph Implementation of Connectedness detection Algorithm. Implementation of Strong Connectedness.
Testing for Connectedness and Cycles Connectedness of an Undirected Graph Implementation of Connectedness detection Algorithm. Implementation of Strong.
Topological Sort Introduction. Definition of Topological Sort. Topological Sort is Not Unique. Topological Sort Algorithm. An Example. Implementation.
Queues What is a Queue? Queue Implementations: Queue As Array
Data Structures and Algorithms Graphs. 2Content MotivationMotivation GraphGraph –Directed Graph –Undirected Graph –Representation –Implementation AlgorithmsAlgorithms.
Graph. Data Structures Linear data structures: –Array, linked list, stack, queue Non linear data structures: –Tree, binary tree, graph and digraph.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 13: Graphs Java Software Structures: Designing and Using Data.
Ics202 Data Structures. U n i v e r s i t y o f H a i l 1. Stacks top push (8)push (2)
1 Graphs and Search Trees Instructor: Mainak Chaudhuri
Data Structures and Algorithms Graphs Fall 2006 Major part of this presentation is courtesy of Dr.Bingol.
U n i v e r s i t y o f H a i l ICS 202  2011 spring  Data Structures and Algorithms  1.
Graphs Chapter 12.
GRAPHS. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different implementations.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
Java Programming Persistent Data Types. Persistent Data Structure A persistent data structure is a data structure having an internal state that never.
Minimum Spanning Tree What is a Minimum Spanning Tree.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
abstract data types built on other ADTs
Queues.
Graphs.
Chapter 13: The Graph Abstract Data Type
Queues What is a queue? Queue Implementations: As Array
Java Programming Graphs.
Implementation of Graphs
Graph Traversals Depth-First Traversals. Algorithms. Example.
null, true, and false are also reserved.
Introduction to Design Patterns
Testing for Connectedness and Cycles
Topological Sort Introduction. Definition of Topological Sort.
JavaScript Reserved Words
Implementation of Graphs
Queues What is a Queue? Queue Implementations: As Array
Graphs G = (V, E) V are the vertices; E are the edges.
Chapter 10 The Graph ADT.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Topological Sort Introduction. Definition of Topological Sort.
Graph Operations And Representation
Topological Sort Introduction. Definition of Topological Sort.
Graph Vocabulary.
Topological Sort Introduction. Definition of Topological Sort.
Introduction to Design Patterns
Queues What is a queue? Queue Implementations: As Array
Introduction to Design Patterns
Presentation transcript:

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

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.

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 }

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 }

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 }

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 }

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 }

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 //...

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 //....

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...

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 }

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 } //..

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 //…

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 }//…

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().