Lecture 11a- Introduction to Graphs
Lecture 11a- Introduction to Graphs
Graphs Graphs 1
A tree is
a graph.
Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD
a
An undirected graph G, where: b
V = {a, b, c, d, e, f, g, h, i} d
c
h
E = { {a, b}, {a, c}, {b, e}, {b, h}, {b, i} ,
A cycle in G is a path in G, containing at least three vertices, such that the last vertex in
the sequence is adjacent to the first vertex in the sequence.
Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD
d
The graph on the previous slide is c
connected. h
Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD
a
The terminology for directed graphs b
is only slightly different…
d
e = (c, d) is an edge, from c to d c
h
A directed graph G is weakly connected if, the undirected graph obtained by suppressing
the directions on the edges of G is connected (according to the previous definition).
A directed graph G is strongly connected if, given any two vertices x and y in G, there is a
directed path in G from x to y.
Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD
0
A graph may be represented by a 1
two-dimensional adjacency table:
3
If G has n = |V| vertices, let M be 2
7
an n by n matrix whose entries
are defined by 4
5 8
1 if (i, j) is an edge 6
mij =
0 otherwise 0 1 1 0 0 0 0 0 0
0 0 0 0 1 0 0 1 1
1 0 0 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0
M (G ) = 0 1 0 0 0 0 1 0 0
0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD
class AdjacencyTable {
protected: Array2DT is a template
string Name; that encapsulates a virtual
int numVertices; two-dimensional array.
Array2DT<bool> Table;
bool* Marker; Marker is a dynamically
allocated bool array, used
for vertex marking
algorithms.
public:
AdjacencyTable(int V = 0, string N = "Unknown");
AdjacencyTable(const AdjacencyTable& Source);
AdjacencyTable& operator=(const AdjacencyTable& Source);
// . . . continued . . .
Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD
// . . . continued . . .
firstNeighbor() returns
the first vertex adjacent to
Source.
int firstNeighbor(int Source) const;
int nextNeighbor(int Source, int prevNeighbor) const;
0 1 2 3 4 5
______________________
0| 0 0 1 0 1 0
1| 0 0 1 0 0 1
2| 1 1 0 1 0 1
3| 0 0 1 0 0 1
4| 1 0 0 0 0 1
5| 0 1 1 1 1 0
Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD
0
A graph may also be represented by 1
an adjacency list structure:
3
2
4
0 1 2 •
5
1 0 4 5 •
2 0 3 •
3 2 •
4 1 5 •
5 1 4 •
Array of linked lists, where list nodes store node labels for neighbors.
Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD
1 0 4 5 •
Note, for an undirected
graph, the upper bound on 2 0 3 •
the number of edges is:
|E| ≤ |V|*(|V|-1) 3 2 •
Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD
During a traversal we must keep track of which vertices have been visited; the
most common approach is to provide some sort of “marking” support.
Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD
1
2
A
N1 10
5
3 8
4
7
N2 ••• Nk
neighbors (and extended
neighbors) of N1
6
9
Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD
a b e c d f g h i e
f
i
Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD
a
b
Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD
2 A
N1 7
4
9 3
5 6
N2 ••• Nk
neighbors of N1
10
Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD
a b c e h i d f g e
f
i
Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD
Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD
Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD
Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD
Next the recursion backs out to 0, and probes to 5, which has no successors.
L: 5 1 7
Next the recursion backs out to 0 again, which now has no unmarked
successors.
L: 0 5 1 7
Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD
L: 2 0 5 1 7
Next start with vertex 3, and probe to 4 and then to 8, which has no unmarked
successors.
L: 8 2 0 5 1 7
Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD
At this point, all the vertices have been marked and the algorithm terminates.
Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD
Also, a generalized breadth-first traversal can be used instead. For the graph
above, a breadth-first traversal yields the ordering:
Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD
Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD
Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD
e 15
The total weight of a path is the sum 10
f
of the weights of its edges. 5 i
We have seen that performing a DFS or BFS on the graph will produce a
spanning tree, but neither of those algorithms takes edge weights into account.
We maintain a table D such that for each vertex v, D(v) is the minimum
distance from the source vertex to v via vertices that are already in S (aside
possibly from v itself).
Greed: on each iteration, add to S the vertex v not already in S for which
D(v) is minimal.
*1959
Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD
e 15
10
f 5 i
S = {a} D a b c d e f g h i
0 15 25 ∞ ∞ ∞ ∞ ∞ ∞
S = {a,b} D a b c d e f g h i
0 0 25 ∞ 10 ∞ ∞ 5 25
Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD
Continuing:
S = {a, b, h} D a b c d e f g h i
0 0 25 ∞ 10 ∞ ∞ 0 15
S = {a, b, h, e} D a b c d e f g h i
0 0 20 ∞ 0 10 5 0 15
S = {a, b, h, e, g} D a b c d e f g h i
0 0 20 ∞ 0 10 0 0 15
S = {a, b, h, e, g, f} D a b c d e f g h i
0 0 20 ∞ 0 0 0 0 15
S = {a, b, h, e, g, f, i} D a b c d e f g h i
0 0 20 ∞ 0 0 0 0 0
Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD
Continuing:
S = {a, b, h, e, g, f, i, c} D a b c d e f g h i
0 0 0 10 0 0 0 0 0
S = {a, b, h, e, g, f, i, c, d} D a b c d e f g h i
0 0 0 0 0 0 0 0 0
a 15
b
25
5 The corresponding tree is shown
d
10
at left. As described, the
c 10
25 h algorithm does not maintain a
20 record of the edges that were used,
e 15 nor does it produce a table of
10
f minimum distances for the
5 i
shortest paths found.
g
Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD
The algorithm can be modified to record the tree and a table of minimum
distances by:
- list the edges used as nodes are added
- adding a mark field for each vertex to the distance table, or using one
provided by the graph structure and retaining the distance values instead
of setting them to zero
S = {a} D a b c d e f g h i
E = {} 0 15 25 ∞ ∞ ∞ ∞ ∞ ∞
Y N N N N N N N N
S = {a, b} D a b c d e f g h i
E = {{a,b}} 0 15 25 ∞ 10 ∞ ∞ 5 25
Y Y N N N N N N N
Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD
Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD
By modifying Dijkstra’s Algorithm to build a list of the edges that are used as
vertices are added, we obtain Prim’s Algorithm (R C Prim, 1957).
It turns out that this algorithm does, in fact, create a spanning tree of minimal
weight if the graph to which it is applied is connected.
Since the complex steps in Prim’s algorithm are the same as Dijkstra’s, no
detailed example is traced here.
Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD