1 / 21

Lecture 30: ADJACENCY-Matrix based Graph

CSC 213 – Large Scale Programming. Lecture 30: ADJACENCY-Matrix based Graph. Today’s Goals. Review first two implementation for Graph ADT What fields & data used in edge-list based approach Operations adjacency-list improves & how it does this

deana
Download Presentation

Lecture 30: ADJACENCY-Matrix 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 30:ADJACENCY-Matrixbased Graph

  2. Today’s Goals • Review first two implementation for Graph ADT • What fields & data used in edge-list based approach • Operationsadjacency-list improves & how it does this • Consider when Graph used in real-life problems • For these cases, what operations are important? • How can we speed them up to make work go faster? • Could new implementation use arrays O(1) time? • Consider changes needed to enable using matrices

  3. Edge-List Implementation • Base for all Graphimplementations • Sequences of vertices & edges • Each instance ofEdge refers to end vertices v b a u u w w vertices u v w a b edges

  4. Adjacency-List Implementation • Vertex maintains Sequence of Edges • Only change needed • Methods which use incident edges faster • Costs some space • Improves few methods to O(1) v b a u u w w vertices w u v a b edges

  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. Graph ADT • Accessor methods • vertices(): iterable for vertices • edges(): iterable for edges • endVertices(e): array with endpoints of edgee • opposite(v,e):e’sendpoint that is not v • areAdjacent(v,w): check ifvand w are 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

  7. Can This Be Made Faster? • Testing for adjacency is very common • Often check how or if vertices are connected • Checking in O(1) time speeds up Graph algorithms

  8. Can This Be Made Faster? • Testing for adjacency is very common • Often check how or if vertices are connected • Checking in O(1) time speeds up Graph algorithms • Can trade off lots of space to make faster • Unique integer ID assigned to each Vertex • Matrix is created as doubly-subscripted array of Edge • matrix[sourceID][targetID] refers to Edge or null

  9. Adjacency Matrix Structure v b a • Edge-List structurestill used as base u w vertices 0 1 2 w v u a b edges

  10. Adjacency Matrix Structure v b a • Edge-List structurestill used as base • Vertex stores int • Index found in matrix u w vertices 0 1 2 w v u a b edges

  11. Adjacency Matrix Structure v b a • Edge-List structurestill used as base • Vertex stores int • Index found in matrix • Adjacency matrix in Graph class u w vertices 0 1 2 w v u a b edges

  12. Adjacency Matrix Structure v b a • Edge-List structurestill used as base • Vertex stores int • Index found in matrix • Adjacency matrix in Graph class • nullif not adjacent u w vertices 0 1 2 w v u a b edges

  13. Adjacency Matrix Structure v b a • Edge-List structurestill used as base • Vertex stores int • Index found in matrix • Adjacency matrix in Graph class • nullif not adjacent -or- • Edgeincidentto both vertices u w vertices 0 1 2 w v u a b edges

  14. Adjacency Matrix Structure v b a • Undirected edgesstored in both array locations u w vertices 0 1 2 w v u a b edges

  15. Adjacency Matrix Structure v b a • Undirected edgesstored in both array locations • Directed edgesonly in array from source to target u w vertices 0 1 2 w v u a b edges

  16. Adjacency Matrix Structure v b a • Undirected edgesstored in both array locations • Directed edgesonly in array from source to target u w vertices 0 1 2 w v u a b edges

  17. Vertex in the Matrix • Another Verteximplementation • Only change is a field for this Vertex • Make subclass of existing Vertex class • Have 2 classes, which should we use? Does it matter? class AMVertex<V>extends Vertex<V>{ -or- class AMVertex<V,E>extends ALVertex<V,E> {private intrank;// Also need to define getRank, but not setRank }

  18. Inserting/Removing Vertex • Reallocates & copy adjacency matrix • Insertion grows array creating locations for vertex • But we have choices when Vertex removed • Resize adjacency-matrix to prevent “bubbles” • Only good when vertices are constant

  19. Inserting/Removing Vertex • Reallocates & copy adjacency matrix • Insertion grows array creating locations for vertex • But we have choices when Vertex removed • Resize adjacency-matrix to prevent “bubbles” • Only good when vertices are constant • What else could we do & when is it useful?

  20. Asymptotic Performance

  21. For Next Lecture • Finish up your coding of program #2; due today • Can use virtual extension, if you still have it • Midterm #2 in class week from today • Test will include all material through today • Lab on graphs & implementations, so get chance to use

More Related