Professional Documents
Culture Documents
ith
abstract containers
last
first
G = (V, E)
networks airline flights road map course prerequisite structure tasks for completing a job flow of control through a program many more
graph variations
undirected
graph (graph)
directed
graph (digraph)
for
a digraph
A B E
storing
each vertex has a unique identifier and, maybe, other information for efficiency, associate each vertex with a number that can be used as an index
storing
the edges
adjacency matrix represent all possible edges adjacency lists represent only the existing edges
graph
operations start by looking up the number associated with a vertex many data structures to use
any of the associative data structures for small graphs a vector can be used
C
2
D
3
0 1 2 3 4
A B C D E
adjacency matrix
A
1 0 4
0 1 2 3 4 0 00 10 20 30 40
A B C D E 1 1 0 1 0 0 2 1 1 0 0 0 3 0 0 1 0 0 4 1 0 0 0 0
C
2
D
3
maximum # edges?
affects
adjacency lists
A
1 0
0 1 2 3 4
A B C D E
C
2
D
3
0 1 2 3 4
1 2 1
2 3
adjacency list
traversing a graph
ny la atl
Where to start? Will all vertices be visited? How to prevent multiple visits?
bos dc chi
readthFirstTraversal (v) put v in Q ny while Q not empty remove w from Q visit w la mark w as visited for each neighbor (u) of w atl if not yet visited put u in Q
depthFirstTraversal (v) visit v mark v as visited for each neighbor (u) of v if not yet visited depthFirstTraversal (u)