21
LECTURE 30: ADJACENCY- MATRIX BASED GRAPH CSC 213 – Large Scale Programming

Lecture 30: ADJACENCY-Matrix based Graph

  • Upload
    deana

  • View
    74

  • Download
    0

Embed Size (px)

DESCRIPTION

CSC 213 – Large Scale Programming. Lecture 30: ADJACENCY-Matrix based Graph. Today’s Goals. Review first two implementation for Graph ADT What fields & data used in edge-list based approach Operations adjacency-list improves & how it does this - PowerPoint PPT Presentation

Citation preview

Page 1: Lecture 30: ADJACENCY-Matrix based Graph

LECTURE 30:ADJACENCY-MATRIXBASED GRAPH

CSC 213 – Large Scale Programming

Page 2: Lecture 30: ADJACENCY-Matrix based Graph

Today’s Goals

Review first two implementation for Graph ADT What fields & data used in edge-list based

approach Operations adjacency-list improves & how

it does this Consider when Graph used in real-life

problems For these cases, what operations are

important? How can we speed them up to make work

go faster? Could new implementation use arrays O(1)

time? Consider changes needed to enable using

matrices

Page 3: Lecture 30: ADJACENCY-Matrix based Graph

edges

vertices

Edge-List Implementation

Base for all Graph implementations Sequences of

vertices & edges Each instance of Edge refers to end vertices

u w

u v w

a b

u

v

wa b

Page 4: Lecture 30: ADJACENCY-Matrix based Graph

edges

a b

Adjacency-List Implementation Vertex maintains Sequence of Edges Only change

needed Methods which use

incident edges faster Costs some space Improves few

methods to O(1)

vertices

u v w

u wu

v

wa b

Page 5: Lecture 30: ADJACENCY-Matrix based Graph

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 6: Lecture 30: ADJACENCY-Matrix based Graph

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 7: Lecture 30: ADJACENCY-Matrix based Graph

Can This Be Made Faster?

Testing for adjacency is very common Often check how or if vertices are

connected Checking in O(1) time speeds up Graph

algorithms

Page 8: Lecture 30: ADJACENCY-Matrix based Graph

Can This Be Made Faster?

Testing for adjacency is very common Often check how or if vertices are connected Checking in O(1) time speeds up Graph

algorithms Can trade off lots of space to make

faster Unique integer ID assigned to each Vertex Matrix is created as doubly-subscripted array

of Edge matrix[sourceID][targetID] refers to Edge or null

Page 9: Lecture 30: ADJACENCY-Matrix based Graph

edges

vertices

0 1 2

0

1

2

Adjacency Matrix Structure

Edge-List structurestill used as base

u v w

0 1 2

u

v

wa b

ba

Page 10: Lecture 30: ADJACENCY-Matrix based Graph

edges

vertices

0 1 2

0

1

2

Adjacency Matrix Structure

Edge-List structurestill used as base

Vertex stores int Index found in

matrix u v w

0 1 2

u

v

wa b

ba

Page 11: Lecture 30: ADJACENCY-Matrix based Graph

edges

vertices

0 1 2

0

1

2

Adjacency Matrix Structure

Edge-List structurestill used as base

Vertex stores int Index found in

matrix Adjacency matrix

in Graph class

u v w

0 1 2

u

v

wa b

ba

Page 12: Lecture 30: ADJACENCY-Matrix based Graph

edges

vertices

0 1 2

0

1

2

Adjacency Matrix Structure

Edge-List structurestill used as base

Vertex stores int Index found in

matrix Adjacency matrix

in Graph class null if

not adjacent

u v w

0 1 2

u

v

wa b

ba

Page 13: Lecture 30: ADJACENCY-Matrix based Graph

edges

vertices

0 1 2

0

1

2

Adjacency Matrix Structure

Edge-List structurestill used as base

Vertex stores int Index found in matrix

Adjacency matrix in Graph class null if

not adjacent -or-

Edge incidentto both vertices

u v w

0 1 2

u

v

wa b

ba

Page 14: Lecture 30: ADJACENCY-Matrix based Graph

edges

vertices

0 1 2

0

1

2

Adjacency Matrix Structure

Undirected edgesstored in both array locations

u v w

0 1 2

u

v

wa b

ba

Page 15: Lecture 30: ADJACENCY-Matrix based Graph

edges

vertices

0 1 2

0

1

2

Adjacency Matrix Structure

Undirected edgesstored in both array locations

Directed edgesonly in array from source to target

u v w

0 1 2

u

v

wa b

ba

Page 16: Lecture 30: ADJACENCY-Matrix based Graph

0 1 2

0

1

2

edges

vertices

Adjacency Matrix Structure

Undirected edgesstored in both array locations

Directed edgesonly in array from source to target

u v w

0 1 2

u

v

wa b

ba

Page 17: Lecture 30: ADJACENCY-Matrix based Graph

Vertex in the Matrix

Another Vertex implementation Only change is a field for this Vertex

Make subclass of existing Vertex class Have 2 classes, which should we use? Does

it matter?

class AMVertex<V> extends Vertex<V>{-or-

class AMVertex<V,E> extends ALVertex<V,E> {private int rank;

// Also need to define getRank, but not setRank}

Page 18: Lecture 30: ADJACENCY-Matrix based Graph

Inserting/Removing Vertex

Reallocates & copy adjacency matrix Insertion grows array creating locations for

vertex But we have choices when Vertex

removed Resize adjacency-matrix to prevent

“bubbles” Only good when vertices are constant

Page 19: Lecture 30: ADJACENCY-Matrix based Graph

Inserting/Removing Vertex

Reallocates & copy adjacency matrix Insertion grows array creating locations for

vertex But we have choices when Vertex

removed Resize adjacency-matrix to prevent

“bubbles” Only good when vertices are constant

What else could we do & when is it useful?

Page 20: Lecture 30: ADJACENCY-Matrix based Graph

n vertices & m edges no self-loops

Edge-List

Adjacency-List

Adjacency-Matrix

Space n + m n + m n2

incidentEdges(v) m deg(v) n + deg(v)

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

insertVertex(o) 1 1 n2

insertEdge(v,w,o) 1 1 1

removeVertex(v) m deg(v) n2

removeEdge(e) 1 1 1

Asymptotic Performance

Page 21: Lecture 30: ADJACENCY-Matrix based Graph

For Next Lecture

Finish up your coding of program #2; due today Can use virtual extension, if you still have it

Midterm #2 in class week from today Test will include all material through today Lab on graphs & implementations, so get

chance to use