- 71 Views
- Uploaded on
- Presentation posted in: General

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

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

CSC 213 – Large Scale Programming

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

- 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

- 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

- 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

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

- 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

}

- 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()

}

- 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()

}

u

- Simplest Graph
- Space efficient
- No change to use withdirected or undirected

a

c

b

d

v

w

z

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

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

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

}

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

}

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

}

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

}

- 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

- 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

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

- 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

- 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

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

- Vertex has Sequence of Edges
- Edges still refer to Vertex

v

b

a

u

u

w

w

- 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

- 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

- 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

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

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

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

- Not strictly necessary, but can speed some work

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