1 / 32

Lecture 29: Edge-list & Adjacency-List based Graphs

CSC 213 – Large Scale Programming. Lecture 29: Edge-list & Adjacency-List based Graphs. Today’s Goals. Briefly review graphs and vital graph terms Begin discussion of how to implement Graph Vertex & Edge classes will implement which ADT?

dawson
Download Presentation

Lecture 29: Edge-list & Adjacency-List based Graphs

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:Edge-list & Adjacency-List based Graphs

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

  4. Graph Types • Graph is directed if all edges directed • All edges in graph must be directed • Examples include object hierarchies & CSC courses • Any edge allowed in undirected graphs • Can have only undirected or mix of both edges • Roadways & airline travel are examples of this Object String Amherst Humboldt

  5. VertexSuperclass • Value stored at each point in the Graph • Must hold an element so implements Position • Nothing else required for default Vertex class

  6. VertexSuperclass • Value stored at each point in the Graph • Must hold an element so implements Position • Nothing else required for default Vertex class classVertex<V> implements Position<V> {private Velement;publicVertex(Velem){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<E> implements Position<E> {private Eelement;private Vertex source;private Vertex target;private booleanisDirected;// 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<E,V> implements Position<E> {private Eelement;private Vertex<V>source;private Vertex<V>target;private booleanisDirected;// Add constructor, getters, setters, & element() }

  9. Edge List Structure u • Simplest Graph • Space efficient • No change to use withdirected or undirected a c b d v w z

  10. Edge List Structure u • Simplest Graph • Space efficient • No change to use withdirected or undirected • Fields • Sequenceof vertices a c b d v w z vertices z w v u

  11. Edge List Structure u • Simplest Graph • Space efficient • No change to use withdirected or undirected • Fields • Sequenceof vertices • Sequenceof edges a c b d v w z vertices z w v u edges c a b d

  12. EdgeListImplementation classELGraphimplements Graph {private Sequence<Vertex>vertices;private Sequence<Edge>edges;public ELGraph(){vertices = // Instantiate a Sequenceedges =// Instantiate a Sequence}// Add Graph’s methods like:public Iterable<Position> vertices() { return vertices;} }

  13. EdgeListImplementation classELGraph<V,E>implements Graph {private Sequence<Vertex<V>>vertices;private Sequence<Edge<E>>edges;public ELGraph(){vertices = // Instantiate a Sequenceedges = // Instantiate a Sequence}// Add Graph’s methods like:public Iterable<Position<V>> vertices() { return vertices;} }

  14. EdgeListImplementation classELGraph<V,E>implements Graph {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;} }

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

  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’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 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’sendpoint that is not v • areAdjacent(v,w): check if vandware adjacent • replace(v,x): makexnew element at vertexv • replace(e,x): makexnew element at edgee • 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

  18. Edge Superclass • Much easier if array used to store endpoints • Need to track index of source &target endpoint • Never hardcode constants into code • What do the numbers 0 or 1 mean to you? • static final fields should instead be used class Edge<E,V> implements Position<E> {private static final int SOURCE = 0; private static final int TARGET = 1;private Eelement;private Vertex<V>[]endPoints;private booleanisDirected;// Add constructor, getters, setters, & element()}

  19. Asymptotic Performance

  20. Asymptotic Performance

  21. Ideal Edge-List Implementation • Great when results not needed or RAM is limited • incidentEdgesrequiresscanning 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 networkssearched for terrorists

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

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

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

  25. Adjacency-List Implementation • Vertex has Sequence of Edges • Edges still refer to Vertex • Ideas in Edge-List serve as base v b a u u w w vertices w u v a b edges

  26. Adjacency-List Implementation • Vertex has Sequence of Edges • Edges still refer to Vertex • Ideas in Edge-List serve as base • Extends Vertex v b a u u w w vertices w u v a b edges

  27. Adjacency-List Implementation • Vertex has Sequence of Edges • Edges still refer to Vertex • Ideas in Edge-List serve as base • Extends Vertex • Could make edgeremoval slower • How to speed up? v b a u u w w vertices w u v a b edges

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

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

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

  31. 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 Graph implementation reading up next • Why is it better than adjacency list-based Graph? • Are there any situations where adjacency-list better?

More Related