170 likes | 279 Views
CSC 213 – Large Scale Programming. Lecture 29: ADJACENCY-list based Graph. 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. PVD. 849. ORD.
E N D
CSC 213 – Large Scale Programming Lecture 29:ADJACENCY-list based Graph
Graphs • Mathematically, graph is pair (V, E) where • Vis collection of nodes, called vertices • Two nodes can be connected by an edge in E • Positionimplemented by Vertex & Edge classes PVD 849 ORD 1843 142 SFO 802 LGA 1743 337 1387 HNL 2555 1099 LAX 1233 DFW 1120 MIA
EdgeListImplementation classELGraph<V,E>implements Graph<V,E>{private Sequence<Vertex<V>>vertices;private Sequence<Edge<E,V>>edges;public ELGraph(){vertices = // Instantiate a Sequenceedges = // Instantiate a Sequence}// Add Graph’s methods like:public Iterable<Position<V>> 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’sendpoint 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 elementx • insertEdge(v,w,x): add edge (v,w) with elementx • removeVertex(v): remove v(& incident edges) • removeEdge(e):remove e • Retrieval methods • incidentEdges(v): get edges incident tov
Graph ADT • Accessor methods • vertices(): iterable for vertices • edges(): iterable for edges • endVertices(e): array with endpoints of edge e • opposite(v,e): e’sendpoint 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 elementx • insertEdge(v,w,x): add edge (v,w) with elementx • removeVertex(v): remove v(& incident edges) • removeEdge(e):remove e • Retrieval methods • incidentEdges(v): get edges incident tov
Using Edge-List Implementation • Great when results not needed for a few years • incidentEdgesrequiresscanning 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 networkssearched for terrorists
Using Edge-List Implementation • Great when results not needed for a few years • incidentEdgesrequiresscanning 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 networkssearched for terrorists
Better Graph Implementations • Need to consider REALgraphs • 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 Edges • Edges still refer to Vertex v b a u w
Adjacency-List Implementation • Vertex has Sequence of Edges • Edges still refer to Vertex • Edge-List base forimplementation v b a u u w w vertices w u v edges a b
Adjacency-List Implementation • Vertex has Sequence of Edges • Edges still refer to Vertex • Ideas in Edge-List serve as base • Adds to Vertex v b a u u w w vertices w u v edges a b
Adjacency-List Implementation • Vertex has Sequence of Edges • Edges still refer to Vertex • Ideas in Edge-List serve as base • Adds to Vertex • Could make edgeremoval slower • How to speed up? v b a u u w w vertices w u v edges a b
Adjacency-List Vertex • Extend existing Vertexclass • 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 classALVertex<V>extendsVertex<V>{ private Sequence<Edge>incidence;// No getter & setter for incidence, but // add methods to add & remove Edges}
Adjacency-List Vertex • Extend existing Vertexclass • 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 classALVertex<V,E>extendsVertex<V>{ private Sequence<ALEdge<E,V>>incidence;// No getter & setter for incidence, but // add methods to add & remove Edges}
Should Edge Class Change? • Ensure that SOURCE & TARGET fields protected • Can be used in subclasses of Edge that we may need • Add references to Positions in incident lists • Not strictly necessary, but can speed some work classALEdge<E,V> extends Edge<E,V> { private Position<ALEdge<E,V>>[]incidentEnd;// incidentEnd[SOURCE] is in source’s incident Sequence // incidentEnd[TARGET] is in target’s incident Sequence }
For Next Lecture • Weekly assignment due tomorrow, as usual • "Prof. the Moron" using same deadline for lab #10 • Work on programming assignment #2 • 2nd 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?