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

1 / 32

# Lecture 29: Edge-list & Adjacency-List based Graphs - PowerPoint PPT Presentation

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?

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'Lecture 29: Edge-list & Adjacency-List based Graphs' - dawson

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 - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
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?
• 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
• 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 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

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

classVertex implements Position {private Velement;publicVertex(Velem){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 implements Position {private Eelement;private Vertex source;private Vertex target;private booleanisDirected;// 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 implements Position {private Eelement;private Vertexsource;private Vertextarget;private booleanisDirected;// Add constructor, getters, setters, & element()

}

Edge List Structure

u

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

a

c

b

d

v

w

z

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

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

EdgeListImplementation

classELGraphimplements Graph {private Sequencevertices;private Sequenceedges;public ELGraph(){vertices = // Instantiate a Sequenceedges =// Instantiate a Sequence}// Add Graph’s methods like:public Iterable vertices() { return vertices;}

}

EdgeListImplementation

classELGraphimplements Graph {private Sequence>vertices;private Sequence>edges;public ELGraph(){vertices = // Instantiate a Sequenceedges = // Instantiate a Sequence}// Add Graph’s methods like:public Iterable> vertices() { return vertices;}

}

EdgeListImplementation

classELGraphimplements Graph {private Sequence>vertices;private Sequence>edges;public ELGraph(){vertices = // Instantiate a Sequenceedges = // Instantiate a Sequence}// Add Graph’s methods like:public Iterable> vertices() { return vertices;}

}

EdgeListImplementation

classELGraphimplements Graph{private Sequence>vertices;private Sequence>edges;public ELGraph(){vertices = // Instantiate a Sequenceedges = // Instantiate a Sequence}// Add Graph’s methods like:public Iterable> 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
• 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
• replace(v,x): makexnew element at vertexv
• replace(e,x): makexnew element at edgee
• Update methods
• insertVertex(x): create vertex storing elementx
• removeVertex(v): remove v(& incident edges)
• removeEdge(e): remove e
• Retrieval methods
• incidentEdges(v): get edges incident tov
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 implements Position {private static final int SOURCE = 0; private static final int TARGET = 1;private Eelement;private Vertex[]endPoints;private booleanisDirected;// Add constructor, getters, setters, & element()}

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

classALVertexextendsVertex{ private Sequenceincidence;// 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

classALVertexextendsVertex{ private Sequence>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

classALEdge extends Edge { private Position>[]incidentEnd;// incidentEnd[SOURCE] is in source’s incident Sequence // incidentEnd[TARGET] is in target’s incident Sequence }

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?