Вы находитесь на странице: 1из 10

DATA STRUCTURES FOR

GRAPHS
Edge list
Adjacency lists
Adjacency matrix
E
NW 35

DL 247

BOS

AA 49

LAX

DL 335 AA 1387 AA 523 AA 411 UA 120 AA 903 UA 877

DFW

JFK

MIA

ORD

TW 45

SFO

Data Structures for Graphs

Data Structures for Graphs


A Graph! How can we represent it?
To start with, we store the vertices and the edges into
two containers, and each edge object has references
to the vertices it connects.
TW 45
NW

35

BOS

ORD

A
LAX

DFW

AA 49

AA

47
DL 2

38
1
A

AA 903

UA

DL 33

SFO

0
2
1

UA 8
77

JFK

523
MIA

AA 411

Additional structures can be used to perform


efficiently the methods of the Graph ADT

Data Structures for Graphs

Edge List
The edge list structure simply stores the vertices and
the edges into unsorted sequences.
Easy to implement.
Finding the edges incident on a given vertex is
inefficient since it requires examining the entire
edge sequence

E
NW 35

DL 247

BOS

AA 49

LAX

DL 335 AA 1387 AA 523 AA 411 UA 120 AA 903 UA 877

DFW

JFK

MIA

ORD

TW 45

SFO

Data Structures for Graphs

Performance of the Edge List


Structure
Operation
size, isEmpty, replaceElement, swap
numVertices, numEdges
vertices
edges, directedEdges, undirectedEdges
elements, positions
endVertices, opposite, origin, destination,
isDirected
incidentEdges, inIncidentEdges, outIncidentEdges, adjacentVertices, inAdjacentVertices, outAdjacentVertices,
areAdjacent, degree, inDegree, outDegree
insertVertex, insertEdge, insertDirectedEdge, removeEdge, makeUndirected,
reverseDirection, setDirectionFrom, setDirectionTo
removeVertex

Data Structures for Graphs

Time
O(1)
O(1)
O(n)
O(m)
O(n+m)
O(1)
O(m)

O(1)

O(m)

Adjacency List
(traditional)
adjacency list of a vertex v:
sequence of vertices adjacent to v
represent the graph by the adjacency lists of all the
vertices
a

b
c
e

d
a

Space = (N +

deg(v)) = (N + M)

Data Structures for Graphs

Adjacency List
(modern)
The adjacency list structure extends the edge list
structure by adding incidence containers to each
vertex.
NW 35

DL 247

BOS

in

out

AA 49

DL 335 AA 1387 AA 523 AA 411 UA 120 AA 903 UA 877

LAX

DFW

JFK

in

in

in

out

out

out

MIA

in

out

ORD

in

out

NW 35

AA 49 UA 120

AA1387 DL335

NW 35 AA1387

DL 247 AA523

UA 120 UA 877

DL 247

AA 411

UA 877 AA 49

AA 903

AA 903 AA 411

DL 335

AA 523

TW 45

TW 45

SFO

in

out

TW 45

The space requirement is O(n + m).


Data Structures for Graphs

Performance of the Adjacency


List Structure
Operation
size, isEmpty, replaceElement, swap
numVertices, numEdges
vertices
edges, directedEdges, undirectedEdges
elements, positions
endVertices, opposite, origin, destination, isDirected, degree, inDegree, outDegree
incidentEdges(v), inIncidentEdges(v),
outIncidentEdges(v), adjacentVertices(v), inAdjacentVertices(v), outAdjacentVertices(v)
areAdjacent(u, v)

Time
O(1)
O(1)
O(n)
O(m)
O(n+m)
O(1)

O(deg(v))

O(min(deg(u),
deg(v)))
insertVertex, insertEdge, insertDirected- O(1)
Edge, removeEdge, makeUndirected,
reverseDirection,
removeVertex(v)
O(deg(v))
Data Structures for Graphs

Adjacency Matrix
(traditional)
a

b
a
b
c
d
e

c
d

F
T
T
T
F

T
F
F
F
T

T
F
F
T
T

T
F
T
F
T

F
T
T
T
F

matrix M with entries for all pairs of vertices


M[i,j] = true means that there is an edge (i,j) in the
graph.
M[i,j] = false means that there is no edge (i,j) in the
graph.
There is an entry for every possible edge, therefore:
Space = (N2)

Data Structures for Graphs

Adjacency Matrix
(modern)
The adjacency matrix structures augments the edge
list structure with a matrix where each row and
column corresponds to a vertex.
0

NW
35

DL
247

AA
49

DL
335

AA
1387

AA
903

TW
45

UA
120

AA
523

AA
411

UA
877

BOS DFW JFK LAX MIA ORD SFO


0
1
2
3
4
5
6
The space requirement is O(n2 + m)
Data Structures for Graphs

Performance of the Adjacency


Matrix Structure
Operation
size, isEmpty, replaceElement, swap
numVertices, numEdges
vertices
edges, directedEdges, undirectedEdges
elements, positions
endVertices, opposite, origin, destination,
isDirected, degree, inDegree, outDegree
incidentEdges, inIncidentEdges, outIncidentEdges, adjacentVertices, inAdjacentVertices, outAdjacentVertices,
areAdjacent
insertEdge, insertDirectedEdge, removeEdge, makeUndirected, reverseDirection,
setDirectionFrom, setDirectionTo
insertVertex, removeVertex

Data Structures for Graphs

Time
O(1)
O(1)
O(n)
O(m)
O(n+m)
O(1)
O(n)

O(1)
O(1)

O(n2)

10

Вам также может понравиться