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

32
LECTURE 28: EDGE-LIST & ADJACENCY- LIST BASED GRAPHS CSC 213 – Large Scale Programming

description

CSC 213 – Large Scale Programming. Lecture 28: 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? - PowerPoint PPT Presentation

Transcript of Lecture 28: Edge-list & Adjacency-List based Graphs

Page 1: Lecture 28: Edge-list & Adjacency-List based Graphs

LECTURE 28:EDGE-LIST & ADJACENCY-LIST BASED GRAPHS

CSC 213 – Large Scale Programming

Page 2: Lecture 28: 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? How to best go about listing classes’ generic

types? How to implement Graph? What fields

required? For an implementation, what are

performance affects? Ways to improve performance & their cost

examined

Page 3: Lecture 28: Edge-list & Adjacency-List based Graphs

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 ORD PVD

MIADFW

SFO

LAX

LGAHNL

849

802

13871743

1843

10991120

1233337

2555

142

Page 4: Lecture 28: Edge-list & Adjacency-List based Graphs

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

Page 5: Lecture 28: Edge-list & Adjacency-List based Graphs

Vertex Superclass

Value stored at each point in the Graph Must hold an element so implements Position

Nothing else required for default Vertex class

Page 6: Lecture 28: Edge-list & Adjacency-List based Graphs

Vertex Superclass

Value stored at each point in the Graph Must hold an element so implements Position

Nothing else required for default Vertex class

class Vertex<V> implements Position<V> {private V element;public Vertex(V elem){ element = elem;}// Also defines setElement & element

}

Page 7: Lecture 28: Edge-list & Adjacency-List based Graphs

Edge Superclass

Slightly more complicated than Vertex Also provides only minimum fields &

methods Inheritance used to allow later

specializationclass Edge<E> implements Position<E> {private E element;private Vertex source;private Vertex target;private boolean isDirected;// Add constructor, getters, setters, & element()

}

Page 8: Lecture 28: Edge-list & Adjacency-List based Graphs

Edge Superclass

Slightly more complicated than Vertex Also provides only minimum fields &

methods Inheritance used to allow later

specializationclass Edge<E,V> implements Position<E> {private E element;private Vertex<V> source;private Vertex<V> target;private boolean isDirected;// Add constructor, getters, setters, & element()

}

Page 9: Lecture 28: Edge-list & Adjacency-List based Graphs

Edge List Structure

Simplest Graph Space efficient No change to use with

directed or undirected

v

u

w

a cb zd

Page 10: Lecture 28: Edge-list & Adjacency-List based Graphs

vertices

Edge List Structure

Simplest Graph Space efficient No change to use with

directed or undirected

Fields Sequence of vertices

v

u

w

a cb zd

u v w z

Page 11: Lecture 28: Edge-list & Adjacency-List based Graphs

edges

Edge List Structure

Simplest Graph Space efficient No change to use with

directed or undirected

Fields Sequence of vertices Sequence of edges

v w

a cb

a

zd

b c d

vertices

v w z

u

u

Page 12: Lecture 28: Edge-list & Adjacency-List based Graphs

EdgeList Implementation

class ELGraph implements Graph {private Sequence<Vertex> vertices;private Sequence<Edge> edges;public ELGraph() { vertices = // Instantiate a Sequence edges = // Instantiate a Sequence}

// Add Graph’s methods like:public Iterable<Position> vertices() { return vertices;}

}

Page 13: Lecture 28: Edge-list & Adjacency-List based Graphs

EdgeList Implementation

class ELGraph<V,E> implements Graph {private Sequence<Vertex<V>> vertices;private Sequence<Edge<E>> edges;public ELGraph() { vertices = // Instantiate a Sequence edges = // Instantiate a Sequence}

// Add Graph’s methods like:public Iterable<Position<V>> vertices() { return vertices;}

}

Page 14: Lecture 28: Edge-list & Adjacency-List based Graphs

EdgeList Implementation

class ELGraph<V,E> implements Graph {private Sequence<Vertex<V>> vertices;private Sequence<Edge<E,V>> edges;public ELGraph() { vertices = // Instantiate a Sequence edges = // Instantiate a Sequence}

// Add Graph’s methods like:public Iterable<Position<V>> vertices() { return vertices;}

}

Page 15: Lecture 28: Edge-list & Adjacency-List based Graphs

EdgeList Implementation

class ELGraph<V,E> implements Graph<V,E> {private Sequence<Vertex<V>> vertices;private Sequence<Edge<E,V>> edges;public ELGraph() { vertices = // Instantiate a Sequence edges = // Instantiate a Sequence}

// Add Graph’s methods like:public Iterable<Position<V>> vertices() { return vertices;}

}

Page 16: Lecture 28: Edge-list & Adjacency-List based Graphs

Graph ADT

Accessor methods vertices(): iterable for

vertices edges(): iterable for

edges endVertices(e): array

with endpoints of edge e

opposite(v,e): e’s endpoint 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 element x

insertEdge(v,w,x): add edge (v,w) with element x

removeVertex(v): remove v (& incident edges)

removeEdge(e): remove e

Retrieval methods incidentEdges(v): get

edges incident to v

Page 17: Lecture 28: Edge-list & Adjacency-List based Graphs

Graph ADT

Accessor methods vertices(): iterable for

vertices edges(): iterable for

edges endVertices(e): array

with endpoints of edge e

opposite(v,e): e’s endpoint 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 element x

insertEdge(v,w,x): add edge (v,w) with element x

removeVertex(v): remove v (& incident edges)

removeEdge(e): remove e

Retrieval methods incidentEdges(v): get

edges incident to v

Page 18: Lecture 28: Edge-list & Adjacency-List based Graphs

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 E element; private Vertex<V>[] endPoints; private boolean isDirected; // Add constructor, getters, setters, & element() }

Page 19: Lecture 28: Edge-list & Adjacency-List based Graphs

n vertices & m edges no self-loops Edge-List

Space n + m

incidentEdges(v) m

areAdjacent(v,w) m

insertVertex(o) 1

insertEdge(v,w,o) 1

removeVertex(v) m

removeEdge(e) 1

Asymptotic Performance

Page 20: Lecture 28: Edge-list & Adjacency-List based Graphs

n vertices & m edges no self-loops Edge-List

Space n + m

incidentEdges(v) m

areAdjacent(v,w) m

insertVertex(o) 1

insertEdge(v,w,o) 1

removeVertex(v) m

removeEdge(e) 1

Asymptotic Performance

Page 21: Lecture 28: Edge-list & Adjacency-List based Graphs

Ideal Edge-List Implementation Great when results not needed or RAM

is limited incidentEdges requires scanning 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 networks searched for

terrorists

Page 22: Lecture 28: Edge-list & Adjacency-List based Graphs

Ideal Edge-List Implementation Great when results not needed for a few

years incidentEdges requires scanning 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 networks searched for

terrorists

Page 23: Lecture 28: Edge-list & Adjacency-List based Graphs

Real Graph Implementations

Need to consider REAL graphs 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?

Page 24: Lecture 28: Edge-list & Adjacency-List based Graphs

Adjacency-List Implementation Vertex has Sequence of Edges Edges still refer

to Vertex

u wu

v

wa b

Page 25: Lecture 28: Edge-list & Adjacency-List based Graphs

edges

vertices

Adjacency-List Implementation Vertex has Sequence of Edges Edges still refer

to Vertex Ideas in Edge-List

serve as base

u w

u v w

a b

u

v

wa b

Page 26: Lecture 28: Edge-list & Adjacency-List based Graphs

edges

vertices

Adjacency-List Implementation Vertex has Sequence of Edges Edges still refer

to Vertex Ideas in Edge-List

serve as base Extends Vertex

u w

u v w

a b

u

v

wa b

Page 27: Lecture 28: Edge-list & Adjacency-List based Graphs

edges

vertices

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?

u w

u v w

a b

u

v

wa b

Page 28: Lecture 28: Edge-list & Adjacency-List based Graphs

Adjacency-List Vertex

Extend existing Vertex class 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

class ALVertex<V> extends Vertex<V>{ private Sequence<Edge> incidence;

// No getter & setter for incidence, but // add methods to add & remove Edges

}

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

Adjacency-List Vertex

Extend existing Vertex class 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

class ALVertex<V,E> extends Vertex<V>{ private Sequence<ALEdge<E,V>> incidence;

// No getter & setter for incidence, but // add methods to add & remove Edges

}

Page 30: Lecture 28: Edge-list & Adjacency-List based Graphs

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

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

Page 31: Lecture 28: Edge-list & Adjacency-List based Graphs

n vertices & m edges no self-loops

Edge-List

Adjacency-List

Space n + m n + m

incidentEdges(v) m deg(v)

areAdjacent(v,w) m min(deg(v), deg(w))

insertVertex(o) 1 1

insertEdge(v,w,o) 1 1

removeVertex(v) m deg(v)

removeEdge(e) 1 1

Asymptotic Performance

Page 32: Lecture 28: Edge-list & Adjacency-List based Graphs

For Next Lecture

Weekly assignment due at regular time If question seems hard, you should ask me

questions Req'd for graduation; keep working

program #2 Today is due date for 2nd preliminary

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