CSC 213 – Large Scale Programming. Today’s Goals  Briefly review graphs and vital graph terms  Begin discussion of how to implement Graph  Vertex &

Slides:



Advertisements
Similar presentations
© 2010 Goodrich, Tamassia Graphs1 Graph Data Structures ORD DFW SFO LAX
Advertisements

© 2010 Goodrich, Tamassia Graphs1 Graph Data Structures ORD DFW SFO LAX
CS16: Introduction to Data Structures & Algorithms
CSC 213 – Large Scale Programming Lecture 27: Graph ADT.
CSC 213 Lecture 23: Shortest Path Algorithms. Weighted Graphs Each edge in weighted graph has numerical weight Weights can be distances, building costs,
CSC 213 ORD DFW SFO LAX Lecture 20: Graphs.
© 2004 Goodrich, Tamassia Graphs1 ORD DFW SFO LAX
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
B.Ramamurthy1 Graphs Chapter 12 B.Ramamurthy. 2 Introduction A structure that represents connectivity information. A tree is kind of graph. Applications.
1 Graphs ORD DFW SFO LAX Many slides taken from Goodrich, Tamassia 2004.
1 Graphs: Concepts, Representation, and Traversal CSC401 – Analysis of Algorithms Lecture Notes 13 Graphs: Concepts, Representation, and Traversal Objectives:
Graphs1 Part-H1 Graphs ORD DFW SFO LAX
© 2004 Goodrich, Tamassia Shortest Paths1 Shortest Paths (§ 13.6) Given a weighted graph and two vertices u and v, we want to find a path of minimum total.
Graphs1 ORD DFW SFO LAX Graphs2 Outline and Reading Graphs (§6.1) Definition Applications Terminology Properties ADT Data structures.
Graphs1 ORD DFW SFO LAX Graphs2 Outline and Reading Graphs (§6.1) Definition Applications Terminology Properties ADT Data structures.
CSC311: Data Structures 1 Chapter 13: Graphs I Objectives: Graph ADT: Operations Graph Implementation: Data structures Graph Traversals: DFS and BFS Directed.
ECE669 L10: Graph Applications March 2, 2004 ECE 669 Parallel Computer Architecture Lecture 10 Graph Applications.
CSC 213 – Large Scale Programming. Today’s Goals  Review first two implementation for Graph ADT  What fields & data used in edge-list based approach.
Graphs1 Graphs Chapter 6 ORD DFW SFO LAX
Graphs - according to the mathematicians An undirected graph is 2-tuple: G=(V,E) a set of vertices a set of edges Vertices = {A, B, C, D, E} Edges = {
Weighted Graphs In a weighted graph, each edge has an associated numerical value, called the weight of the edge Edge weights may represent, distances,
Graphs – ADTs and Implementations ORD DFW SFO LAX
Graphs Chapter 12.
CSC 213 – Large Scale Programming. Today’s Goals  Discuss what is NOT meant by term “Graph”  Why no bar charts, scatter plots, & pie charts mentioned.
CSC 213 – Large Scale Programming. Final Exam  Thurs., May 10 from 8:00 – 10:00 in OM 200  Plan on exam taking full 2 hours  If major problem, come.
CSC 213 – Large Scale Programming. Today’s Goals  Discuss what is meant by weighted graphs  Where weights placed within Graph  How to use Graph ’s.
Graphs Part 1. Outline and Reading Graphs (§13.1) – Definition – Applications – Terminology – Properties – ADT Data structures for graphs (§13.2) – Edge.
Graphs. Data Structure for Graphs. Graph Traversals. Directed Graphs. Shortest Paths. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013.
Chapter 6 Graphs ORD DFW SFO LAX
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
1 Data Structures for Graphs Edge list Adjacency lists Adjacency matrix.
Graphs CSE 2011 Winter June Graphs A graph is a pair (V, E), where  V is a set of nodes, called vertices  E is a collection of pairs.
Spring 2007Graphs1 ORD DFW SFO LAX
GRAPHS 1. Outline 2  Undirected Graphs and Directed Graphs  Depth-First Search  Breadth-First Search.
1 prepared from lecture material © 2004 Goodrich & Tamassia COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material.
Lecture 16. Shortest Path Algorithms
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
CSC 213 – Large Scale Programming. Today’s Goals  Discuss what is meant by weighted graphs  Where weights placed within Graph  How to use Graph ’s.
Introduction to Programming
CSC 213 – Large Scale Programming. Graphs  Mathematically, graph is pair (V, E) where  V is collection of nodes, called vertices  Two nodes can be.
CSC401: Analysis of Algorithms 6-1 CSC401 – Analysis of Algorithms Chapter 6 Graphs Objectives: Introduce graphs and data structures Discuss the graph.
Graphs Quebec Toronto Montreal Ottawa 449 km 255 km 200 km 545 km Winnipeg 2075 km 2048 km New York 596 km 790 km 709 km.
CHAPTER 13 GRAPH ALGORITHMS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
1 COMP9024: Data Structures and Algorithms Week Eleven: Graphs (I) Hui Wu Session 1, 2016
Graphs ORD SFO LAX DFW Graphs 1 Graphs Graphs
Graphs 10/24/2017 6:47 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Graphs.
Graphs 5/14/ :46 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Graphs 7/18/2018 7:39 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Graphs ORD SFO LAX DFW Graphs Graphs
COMP9024: Data Structures and Algorithms
Graphs ORD SFO LAX DFW Graphs Graphs
COMP9024: Data Structures and Algorithms
Graphs Part 1.
CMSC 341 Lecture 21 Graphs (Introduction)
CSE 373: Data Structures and Algorithms
Discrete Maths 9. Graphs Objective
Graphs ORD SFO LAX DFW Graphs Graphs
Graphs.
Graphs.
Graphs ORD SFO LAX DFW Graphs Graphs
Graphs CSE 2011 Winter November 2018.
Shortest Paths C B A E D F Shortest Paths
Graphs ORD SFO LAX DFW Graphs Graphs
Graphs ORD SFO LAX DFW Graphs Graphs
Graphs 4/29/15 01:28:20 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Graphs Part 1 ORD SFO LAX DFW
Graphs ORD SFO LAX DFW /15/ :57 AM
Graphs.
CSE 373 Graphs 3: Implementation reading: Weiss Ch. 9
Presentation transcript:

CSC 213 – Large Scale Programming

Today’s Goals  Briefly review graphs and vital graph terms  Begin discussion of how to implement Graph  Vertex & Edge classes will implement which ADT?  How to best go about listing classes’ generic types?  How to implement Graph ? What fields required?  For an implementation, what are performance effects?  Ways to improve performance & their cost examined

Graphs  Mathematically, graph is pair (V, E) where vertices  V is collection of nodes, called vertices edge  Two nodes can be connected by an edge in E  Position implemented by Vertex & Edge classes ORD PVD MIA DFW SFO LAX LGA HNL

Graph Types ObjectString AmherstHumboldt

Vertex Superclass  Value stored at each point in the Graph  Must hold an element so implements Position  Nothing else required for default Vertex class

Vertex Superclass  Value stored at each point in the Graph  Must hold an element so implements Position  Nothing else required for default Vertex class class Vertex implements Position { private V element; public Vertex(V elem){ element = elem; } // Also defines setElement & element }

Edge Superclass  Slightly more complicated than Vertex  Also provides only minimum fields & methods  Inheritance used to allow later specialization class Edge implements Position { private E element; private Vertex source; private Vertex target; private boolean isDirected; // Add constructor, getters, setters, & element() }

Edge Superclass  Slightly more complicated than Vertex  Also provides only minimum fields & methods  Inheritance used to allow later specialization class Edge implements Position { private E element; private Vertex source; private Vertex target; private boolean isDirected; // Add constructor, getters, setters, & element() }

Edge List Structure  Simplest Graph  Space efficient  No change to use with directed or undirected v u w ac b z d

vertices Edge List Structure  Simplest Graph  Space efficient  No change to use with directed or undirected  Fields  Sequence of vertices v u w ac b z d u v w z

edges Edge List Structure  Simplest Graph  Space efficient  No change to use with directed or undirected  Fields  Sequence of vertices  Sequence of edges vw ac b a z d b c d vertices v w z u u

EdgeList Implementation class ELGraph implements Graph { private Sequence vertices; private Sequence edges; public ELGraph() { vertices = // Instantiate a Sequence edges = // Instantiate a Sequence } // Add Graph ’s methods like: public Iterable vertices() { return vertices; } }

EdgeList Implementation class ELGraph implements Graph { private Sequence > vertices; private Sequence > edges; public ELGraph() { vertices = // Instantiate a Sequence edges = // Instantiate a Sequence } // Add Graph ’s methods like: public Iterable > vertices() { return vertices; } }

EdgeList Implementation class ELGraph implements Graph { private Sequence > vertices; private Sequence > edges; public ELGraph() { vertices = // Instantiate a Sequence edges = // Instantiate a Sequence } // Add Graph ’s methods like: public Iterable > vertices() { return vertices; } }

EdgeList Implementation class ELGraph implements Graph { private Sequence > vertices; private Sequence > edges; public ELGraph() { vertices = // Instantiate a Sequence edges = // Instantiate a Sequence } // Add Graph ’s methods like: public Iterable > vertices() { return vertices; } }

Graph ADT  Accessor methods  vertices() : iterable for vertices  edges() : iterable for edges  endVertices(e) : array with endpoints of edge e  opposite(v,e) : e ’s endpoint that is not v  areAdjacent(v,w) : check if v and w are adjacent  replace(v,x) : make x new element at vertex v  replace(e,x) : make x new element at edge e  Update methods  insertVertex(x) : create vertex storing element x  insertEdge(v,w,x) : add edge (v,w) with element x  removeVertex(v) : remove v (& incident edges)  removeEdge(e) : remove e  Retrieval methods  incidentEdges(v) : get edges incident to v

Graph ADT  Accessor methods  vertices() : iterable for vertices  edges() : iterable for edges  endVertices(e) : array with endpoints of edge e  opposite(v,e) : e ’s endpoint that is not v  areAdjacent(v,w) : check if v and w are adjacent  replace(v,x) : make x new element at vertex v  replace(e,x) : make x new element at edge e  Update methods  insertVertex(x) : create vertex storing element x  insertEdge(v,w,x) : add edge (v,w) with element x  removeVertex(v) : remove v (& incident edges)  removeEdge(e) : remove e  Retrieval methods  incidentEdges(v) : get edges incident to v

Edge Superclass

n vertices & m edges no self-loops Edge-List Space n  m incidentEdges (v) m areAdjacent (v,w) m insertVertex (o) 1 insertEdge (v,w,o) 1 removeVertex (v) m removeEdge (e) 1 Asymptotic Performance

n vertices & m edges no self-loops Edge-List Space n  m incidentEdges (v) m areAdjacent (v,w) m insertVertex (o) 1 insertEdge (v,w,o) 1 removeVertex (v) m removeEdge (e) 1 Asymptotic Performance

Ideal Edge-List Implementation  Great when results not needed or RAM is limited  incidentEdges requires scanning all edges  All edges scanned in removeVertex, also  Anyone here really had OutOfMemoryException ?  Optimized for many vertices and few edges  Examining cities with no roads connecting them  Scheduling exams for students taking 1 class  Hermit-based networks searched for terrorists

Ideal Edge-List Implementation  Great when results not needed for a few years  incidentEdges requires scanning all edges  All edges scanned in removeVertex, also  Edge-list is good when memory is limited  How much do you have in your machine?  Optimized for many vertices and few edges  Examining cities with no roads connecting them  Scheduling exams for students taking 1 class  Hermit-based networks searched for terrorists

Real Graph Implementations  Need to consider REAL graphs  Edges outnumber vertices often by a lot  May need multiple edges between vertices  List of incident edges stored in each Vertex  Edges still refer to their endpoints  Why would this be good?

Adjacency-List Implementation  Vertex has Sequence of Edge s  Edges still refer to Vertex uwu v w a b

edges vertices Adjacency-List Implementation  Vertex has Sequence of Edge s  Edges still refer to Vertex  Ideas in Edge-List serve as base uw uv w ab u v w a b

edges vertices Adjacency-List Implementation  Vertex has Sequence of Edge s  Edges still refer to Vertex  Ideas in Edge-List serve as base  Extends Vertex uw uv w ab u v w a b

edges vertices Adjacency-List Implementation  Vertex has Sequence of Edge s  Edges still refer to Vertex  Ideas in Edge-List serve as base  Extends Vertex  Could make edge removal slower  How to speed up? uw uv w ab u v w a b

Adjacency-List Vertex  Extend existing Vertex class  Any code using old classes will continue to work  No need to rewrite all the existing methods  Biggest change is to add field for incident edges class ALVertex extends Vertex { private Sequence incidence; // No getter & setter for incidence, but // add methods to add & remove Edge s }

Adjacency-List Vertex  Extend existing Vertex class  Any code using old classes will continue to work  No need to rewrite all the existing methods  Biggest change is to add field for incident edges class ALVertex extends Vertex { private Sequence > incidence; // No getter & setter for incidence, but // add methods to add & remove Edge s }

Should Edge Class Change?  Ensure that SOURCE & TARGET fields protected  Can be used in subclasses of Edge that we may need  Add references to Position s in incident lists  Not strictly necessary, but can speed some work class ALEdge extends Edge { private Position >[] incidentEnd; // incidentEnd[SOURCE] is in source’s incident Sequence // incidentEnd[TARGET] is in target’s incident Sequence }

n vertices & m edges no self-loops Edge- List Adjacency- List Space n  m incidentEdges (v) m deg(v) areAdjacent (v,w) m min(deg(v), deg(w)) insertVertex (o) 11 insertEdge (v,w,o) 11 removeVertex (v) m deg(v) removeEdge (e) 11 Asymptotic Performance

For Next Lecture  No weekly assignment due this week  Req'd for graduation; keep working program #2  Nearing the end of time – final submission due Wed.  Really good idea to check your JUnit tests work  Another G RAPH implementation reading up next  Why is it better than adjacency list-based Graph?  Are there any situations where adjacency-list better?