1 / 17

Lecture 29: ADJACENCY-list based Graph

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.

kamala
Télécharger la présentation

Lecture 29: ADJACENCY-list based Graph

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 213 – Large Scale Programming Lecture 29:ADJACENCY-list based Graph

  2. 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

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

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

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

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

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

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

  9. Adjacency-List Implementation • Vertex has Sequence of Edges • Edges still refer to Vertex v b a u w

  10. 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

  11. 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

  12. 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

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

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

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

  16. 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 • 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?

More Related