csc 213 large scale programming
Download
Skip this Video
Download Presentation
Lecture 29: Edge-list & Adjacency-List based Graphs

Loading in 2 Seconds...

play fullscreen
1 / 32

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


  • 144 Views
  • Uploaded on

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?

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

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


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.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
today s goals
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
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 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
VertexSuperclass
  • Value stored at each point in the Graph
    • Must hold an element so implements Position
    • Nothing else required for default Vertex class
vertex superclass1
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
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 superclass1
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
Edge List Structure

u

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

a

c

b

d

v

w

z

edge list structure1
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 structure2
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

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

}

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

}

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

}

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

}

graph adt
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 to v
graph adt1
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 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
edge superclass2
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
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 implementation1
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
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?
adjacency list implementation
Adjacency-List Implementation
  • Vertex has Sequence of Edges
    • Edges still refer to Vertex

v

b

a

u

u

w

w

adjacency list implementation1
Adjacency-List Implementation
  • 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

adjacency list implementation2
Adjacency-List Implementation
  • 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

adjacency list implementation3
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?

v

b

a

u

u

w

w

vertices

w

u

v

a

b

edges

adjacency list vertex
Adjacency-List Vertex
  • 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}

adjacency list vertex1
Adjacency-List Vertex
  • 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
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
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?
ad