Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 CSC 213 – Large Scale Programming

2 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

3 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 849 802 1387 1743 1843 1099 1120 1233 337 2555 142

4 Graph Types ObjectString AmherstHumboldt

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

6 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 }

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

8 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() }

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

10 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

11 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

12 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; } }

13 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; } }

14 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; } }

15 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; } }

16 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

17 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

18 Edge Superclass

19 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

20 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

21 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

22 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

23 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?

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

25 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

26 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

27 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

28 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 }

29 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 }

30 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 }

31 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

32 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?


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

Similar presentations


Ads by Google