Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 213 – Large Scale Programming. Graphs  Mathematically, graph is pair (V, E) where  V is collection of nodes, called vertices  Two nodes can be.

Similar presentations


Presentation on theme: "CSC 213 – Large Scale Programming. Graphs  Mathematically, graph is pair (V, E) where  V is collection of nodes, called vertices  Two nodes can be."— Presentation transcript:

1 CSC 213 – Large Scale Programming

2 Graphs  Mathematically, graph is pair (V, E) where  V is collection of nodes, called vertices  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

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

4 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

5 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

6 Using 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 RAM does your machine have?  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

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

8 Better 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?

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

10 edges vertices Adjacency-List Implementation  Vertex has Sequence of Edge s  Edges still refer to Vertex  Edge-List base for implementation uw uv w ab u v w a b

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

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

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

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

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

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

17 For Next Lecture  Weekly assignment due tomorrow, as usual  "Prof. the Moron" using same deadline for lab #10  Work on programming assignment #2  2 nd preliminary deadline is today  Check your JUnit tests & make sure they work  Reading on implementing Graph for Wednesday  Can we make some checks even faster?  Why would we care about this? And what is cost?


Download ppt "CSC 213 – Large Scale Programming. Graphs  Mathematically, graph is pair (V, E) where  V is collection of nodes, called vertices  Two nodes can be."

Similar presentations


Ads by Google