csc 213 – large scale programming. today’s goals briefly review graphs and vital graph terms ...

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

Upload: aubrey-morris

Post on 01-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

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

CSC 213 – Large Scale Programming

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

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

LGA

HNL

849

802

13871743

1843

10991120

1233337

2555

142

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

Vertex Superclass

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

Nothing else required for default Vertex class

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

}

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 E element;private Vertex source;private Vertex target;private boolean isDirected;// Add constructor, getters, setters, & element()

}

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

}

Edge List Structure

Simplest Graph Space efficient No change to use with

directed or undirected

v

u

w

a c

b zd

vertices

Edge List Structure

Simplest Graph Space efficient No change to use with

directed or undirected

Fields Sequence of vertices

v

u

w

a c

b zd

u v w z

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 c

b

a

zd

b c d

vertices

v w z

u

u

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

}

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

}

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

}

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

}

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

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

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

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

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

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

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

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?

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

to Vertex

u wu

v

wa b

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

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

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

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

}

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

}

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 }

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

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?