Chapter 13: The Graph Abstract Data Type

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Weighted graphs Example Consider the following graph, where nodes represent cities, and edges show if there is a direct flight between each pair of cities.
Data Structures Using C++
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs Chapter Chapter Contents Some Examples and Terminology Road Maps Airline Routes Mazes Course Prerequisites Trees Traversals Breadth-First.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Graphs Chapter 30 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Data Structures Using C++
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 12 Graphs.
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
Data Structures Using Java1 Chapter 11 Graphs. Data Structures Using Java2 Chapter Objectives Learn about graphs Become familiar with the basic terminology.
Graph II MST, Shortest Path. Graph Terminology Node (vertex) Edge (arc) Directed graph, undirected graph Degree, in-degree, out-degree Subgraph Simple.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
ITEC200 – Week 12 Graphs. 2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Graphs Chapter 28 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Graph Operations And Representation. Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
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.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
Graphs Chapter Chapter Contents Some Examples and Terminology Road Maps Airline Routes Mazes Course Prerequisites Trees Traversals Breadth-First.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
– Graphs 1 Graph Categories Strong Components Example of Digraph
Graphs Upon completion you will be able to:
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
CSE 373, Copyright S. Tanimoto, 2002 Graphs 2 -
Graphs Chapter 20.
Graphs Chapter 15 introduces graphs which are probably the most general and commonly-used data structure. This lecture introduces heaps, which are used.
A vertex u is reachable from vertex v iff there is a path from v to u.
Graphs.
Graphs Representation, BFS, DFS
Data Structures 13th Week
Data Structures and Algorithms I
Csc 2720 Instructor: Zhuojun Duan
CS120 Graphs.
Data Structures and Algorithms for Information Processing
CMSC 341 Lecture 21 Graphs (Introduction)
Graph Operations And Representation
Graphs Graph transversals.
Graphs Representation, BFS, DFS
Graphs Chapter 13.
Java Software Structures: John Lewis & Joseph Chase
Graphs Chapter 11 Objectives Upon completion you will be able to:
Elementary Graph Algorithms
Graphs.
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
A vertex u is reachable from vertex v iff there is a path from v to u.
Chapter 16 1 – Graphs Graph Categories Strong Components
Graph Operations And Representation
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Elementary Graph Algorithms
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

Chapter 13: The Graph Abstract Data Type Data Structures in Java: From Abstract Data Types to the Java Collections Framework by Simon Gray

Graph Terminology A graph G = (V, E) is an ordered pair of finite sets V and E, where V(G) is the set of vertices in G, and E(G) is the set of edges in G. The edge connecting vertices vi and vj is represented as (vi , vj). The number of vertices in G is given as |V| and the number of edges in G is given as |E|.

Sample Graphs: undirected, directed, & weighted. (a) Graph G1 – an undirected graph. (b) Graph G2—a directed graph. (c) Graph G3 – a directed version of graph G1. (d) Graph G4 – an undirected graph with weighted edges.

Graph Terminology adjacency of vertices in-degree & out-degree weighted edges path, simple path & cycle connected & reachable connected graph complete graph subgraph

Connected and Complete Graphs (a) A connected graph. (b) A disconnected graph. (b) A complete graph.

Subgraphs (a) Graph G5. (b) Some subgraphs of G5.

Adjacency Matrix Representation (a) Adjacency matrix for an undirected graph. (b) Adjacency matrix for a directed graph. (c) Adjacency matrix for an undirected weighted graph.

Adjacency List Representation (a) Adjacency list for an undirected graph. (b) Adjacency list for a directed graph. (c) Adjacency list for a directed weighted graph.

Depth First Search // Perform a depth first search of a Graph g // originating from Vertex v Pseudocode DFS ( Graph g, Vertex v ) 1. mark v as visited // visit a vertex only once! 2. for each Vertex w adjacent to v 3. if w has not been visited 4. DFS( w )

Depth First Search Example

Breadth First Search // Perform a breadth first search of a Graph g // originating from Vertex v Pseudocode BFS ( Graph g, Vertex v ) vertexQueue – a Queue of Vertices 1. mark v as visited 2. enqueue v in vertexQueue 3. while vertexQueue is not empty 4. let v be the element removed from the front of vertexQueue 5. for all Vertices w adjacent to v 6. if w has not been visited 7. enqueue w in vertexQueue 8. mark w as visited

Breadth First Search Example

Shortest Path Algorithm List shortestPath( Graph g, Vertex src, Vertex dest ) // Find a shortest path from src to dest in Graph g. Return an empty // List if no path // exists, or the vertices in the path from src to dest otherwise. If all edges have the same weight, then BFS will find shortest path.

Spanning Tree Algorithm A spanning tree of a graph G is a connected acyclic subgraph, G, containing all the vertices of G; that is, G( V) = G(V). Graph spanningTree( Graph g, Vertex src ) // Return a spanning tree of g using src as the root of the tree. // If the returned // graph is empty, g is not connected. DFS will visit each of the vertices in the graph once; keep track of the vertices and edges connecting them to identify a spanning tree.

Minimal Path Algorithm // Return the cost of a minimal cost path in // Graph g from Vertex src to dest Pseudocode Minimal Path( Graph g, Vertex src, Vertex dest ) priorityQueue – a priority queue holding Vertex , minCostToVertex pairs verticesInSomePathToDest – a collection of vertices in some possible path from src to dest 1. place src, 0 in priorityQueue 2. while priorityQueue is not empty // get the least expensive path seen so far 3. pair = the vertex, minCostToVertex  pair removed from priorityQueue 4. v = pair.vertex // extract the vertex field from pair 5. minCostToV = pair. minCostToVertex // extract the cost field from pair 6. if v == dest 7. return minCostToV // success; return cost from src to v // haven’t found the target yet, so continue the search 8. add v to verticesInSomePathToDest 9. for each Vertex w adjacent to v 10. if w is not in verticesInSomePathToDest // avoid revisiting a visited vertex 11. let minCostToW be minCostToV + weight(v, w) // get total cost from src to w 12. add the pair w, minCostToW to priorityQueue 13. return failure // there is no path from src to dest

Minimal Path Algorithm Example (1) Searching for a path from A to E

Minimal Path Algorithm Example (2)

Minimal Path Algorithm Example (3)

Minimal Path Algorithm Example (4)

Minimal Spanning Tree Algorithm // Identify a Minimal Spanning Tree for a weighted Graph g with Vertex v as its root. Pseudocode minimalSpanningTree ( Graph g, Vertex v ) MST – a collection of edges (v, w) in the Minimal Spanning Tree; initially this collection is empty mstCost – the total cost of all edges in MST; initially 0 visitedVertices – a collection of all vertices that are in MST; initially this collection stores a vertex from G that will be the root of the MST 1. allVerticesVisited = false 2. put the starting vertex v in visitedVertices // while the MST is not complete and there are vertices to visit 3. while MST.size() < |V| - 1 and not allVerticesVisited // select the least cost candidate edge 4. get the least cost edge (v, w) such that v is in visitedVertices 5. and w is not in visitedVertices 6. if no such edge exists then 7. allVerticesVisited is true 8. else 9. add (v, w) to MST 10. add the cost of edge (v, w) to mstCost 11. add w to visitedVertices // loop invariant: MST contains the edges of a minimal spanning // tree for all vertices from G that are in visitedVertices 12. if MST.size() != |V| - 1 then 13. return failure 14. return MST // success

Minimal Spanning Tree Example (1)

Minimal Spanning Tree Example (2)

Minimal Spanning Tree Example (3)

Minimal Spanning Tree Example (4)

The Graph ADTs What needs to be explicitly represented in a graph? Do we need a type to represent vertices? What about edges? Our solution: explicitly represent vertices and implicitly represent edges via the vertices they connect Graph ADT 13.1 specifies an unweighted graph that could be directed or undirected Weighted Graph ADT 13.2 is as an extension of the Graph ADT. The only significant difference is the addition of operations to support edge weights This design decision will be reflected in the implementation

The Graph Implementation Let the Graph interface reflect the Graph ADT Questions: Can a directed graph class and an undirected graph class be directly related? Should a digraph be a subclass of an undirected graph class, or vice versa? How do weighted graphs affect this design?

The Graph Implementation How to handle the addition and deletion of edges? The addEdge(v1, v2) operation will produce an edge from v1 to v2 in a directed graph, and an edge from v2 to v1 in an undirected graph AdjMatrixDiGraph will implement the Graph interface for directed graphs using an adjacency matrix to store the graph To represent undirected graphs, AdjMatrixGraph will be a subclass of AdjMatrixDiGraph. When dealing with edges, its methods will invoke their superclass (directed graph) counterparts, and then will handle the bidirectionality of an undirected graph locally

The Graph Hierarchy The methods shown in a concrete class are either implemented in that class or are overridden there to provide a specialization of the method’s behavior.

AdjMatrixDiGraph Note the use of protected here so subclasses can have package gray.adts.graph; import java.util.*; /** * An implementation of the <tt>Graph</tt> interface for a * directed graph using an adjacency matrix to indicate the * presence/absence of edges connecting vertices in the graph. */ public class AdjMatrixDiGraph<T> implements Graph<T> { protected int numberOfVertices; protected int numberOfEdges; protected int[][] adjMatrix; protected Vertex<T>[] vertices; protected int v1Pos, v2Pos; protected static int SIZE = 10; Note the use of protected here so subclasses can have direct access.

Method addEdge() // directed graph! public void addEdge( Vertex<T> v1, Vertex<T> v2 ) { v1Pos = getVerticesIndexFor( v1 ); v2Pos = getVerticesIndexFor( v2 ); if ( v1Pos == -1 || v2Pos == -1 ) { throw new IllegalArgumentException( "vertex not found" ); } // avoid adding duplicate edges if ( this.adjMatrix[v1Pos][v2Pos] == 0 ) { this.adjMatrix[v1Pos][v2Pos] = 1; this.numberOfEdges++; else { throw new IllegalArgumentException( "duplicate edge " + v1 + " " + v2 );

Method addEdge() // undirected graph! /** * Adjacency Matrix implementation of an undirected * Graph. */ public class AdjMatrixGraph<T> extends AdjMatrixDiGraph<T> { public AdjMatrixGraph() { super(); } public void addEdge( Vertex<T> v1, Vertex<T> v2 ) { super.addEdge( v1, v2 ); // if we get here, the superclass method completed // successfully and we can set edge from v2 to v1 this.adjMatrix[v2Pos][v1Pos] = 1; let superclass create the edge from v1 to v2 now create the v2 to v1 edge

WeightedAdjMatrixGraph /** * A weighted, undirected graph stored in an adjacency * matrix. The weights must be >= 0. */ public class WeightedAdjMatrixGraph<T> extends AdjMatrixGraph<T> implements WeightedGraph<T> { * The default weight for an edge in a weighted graph. public double DEFAULT_WEIGHT = 1.0; * Store weight edges. The adjacency matrix storing * edges is in an ancestor class. protected double[][] weights;

addEdge() // weighted undirected graph! public void addEdge( Vertex<T> v1, double weight, Vertex<T> v2 ) { if ( weight < 0.0 ) { throw new IllegalArgumentException( "Edge weight " + " must be >= 0.0" ); } super.addEdge( v1, v2 ); // if we get here, method in superclass didn't throw // an exception and method preconditions are met this.setEdgeWeight( v1, weight, v2 );

overridden addEdge() Overridden addEdge() inherited from AdjMatrixGraph. Need to provide a weight for the edge; use the default value. public void addEdge( Vertex<T> v1, Vertex<T> v2 ) { this.addEdge( v1, DEFAULT_WEIGHT, v2 ); } More code reuse: Use the addEdge() from the previous slide and just supply a DEFAULT_WEIGHT.