Download Graph Data Structures and File Management - Prof. William D. Mcquain and more Study notes Computer Science in PDF only on Docsity!
Computer Science Dept Va Tech June 2004 ©2004 McQuain WD
Graphs^1
Data Structures & File Management
Graphs
A graph G consists of a set V of vertices and a set E of pairs of distinct
vertices from V. These pairs of vertices are called edges.
If the pairs of vertices are unordered, G is an undirected graph. If the pairs of
vertices are ordered, G is a directed graph or digraph.
A tree is a graph.
An undirected graph. (^) A directed graph.
Computer Science Dept Va Tech June 2004 ©2004 McQuain WD
Graphs^2
Undirected Graph Terminology
V = {a, b, c, d, e, f, g, h, i}
a
i
g
f
e
d c
b
h
An undirected graph G, where:
E = { {a, b}, {a, c}, {b, e}, {b, h}, {b, i} ,
{c, d} , {c, e} , {e, f} , {e, g} , {h, i} }
e = {c, d} is an edge, incident upon the vertices c and d
Two vertices, x and y, are adjacent if {x, y} is an edge (in E).
A path in G is a sequence of distinct vertices, each adjacent to the next.
A path is simple if no vertex occurs twice in the path.
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 June 2004 ©2004 McQuain WD
Graphs^3
Data Structures & File Management
Undirected Graph Terminology
i
g
f
e
a
d c
b
h
A graph G is connected if, given any two vertices x and y in G, there is a path in G with first vertex x and last vertex y.
The graph on the previous slide is connected.
If a graph G is not connected, then we say that a maximal connected set of vertices is a component of G.
Computer Science Dept Va Tech June 2004 ©2004 McQuain WD
Graphs^4
Directed Graph Terminology
The terminology for directed graphs
is only slightly different…
e = (c, d) is an edge, from c to d
A directed path in a directed graph G is a sequence of distinct vertices, such that there is an edge from each vertex in the sequence to the next.
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).
g
e
a b
c
d
f
h
i
g
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 June 2004 ©2004 McQuain WD
Graphs^7
Data Structures & File Management
An Adjacency Table Class
class AdjacencyTable { protected: string Name; int numVertices; Array2DT Table; bool* Marker;
public: AdjacencyTable(int V = 0, string N = "Unknown"); AdjacencyTable(const AdjacencyTable& Source); AdjacencyTable& operator=(const AdjacencyTable& Source);
string getName() const; int getnumVertices() const;
bool addEdge(int Source, int Terminus); bool deleteEdge(int Source, int Terminus); bool isEdge(int Source, int Terminus) const;
//... continued...
Array2DT is a template that encapsulates a virtual two-dimensional array.
Marker is a dynamically allocated bool array, used for vertex marking algorithms.
Computer Science Dept Va Tech June 2004 ©2004 McQuain WD
Graphs^8
An Adjacency Table Class
//... continued...
int firstNeighbor(int Source) const; int nextNeighbor(int Source, int prevNeighbor) const;
bool isMarked(int Vertex) const; bool Mark(int Vertex); bool unMark(int Vertex);
void Clear();
virtual void Display(ostream& Out); ~AdjacencyTable(); };
firstNeighbor() returns the first vertex adjacent to Source.
nextNeighbor() returns the next vertex, after prevNeighbor , which is adjacent to Source.
Clear() deletes all the edges from the graph.
Graph: Sample
______________________
Computer Science Dept Va Tech June 2004 ©2004 McQuain WD
Graphs^9
Data Structures & File Management
Adjacency List Representation
A graph may also be represented by
an adjacency list structure:
Array of linked lists, where list nodes store node labels for neighbors.
Computer Science Dept Va Tech June 2004 ©2004 McQuain WD
Graphs^10
Adjacency List Representation
The adjacency list structure:
- Worst case: Θ( |V| ) to determine existence of a specific edge
- Θ( |V| + |E| ) storage cost
- Worst case: Θ( |V| ) for finding all neighbors of a specific vertex
- Worst case: Θ( |V| ) to add or delete an edge
- Still not easy to add or delete a vertex; however, we can use a linked list
in place of the array.
Note, for an undirected graph, the upper bound on the number of edges is:
|E| ≤ |V|(|V|-1) So, the space comparison with the adjacency table scheme is not trivial.*
Computer Science Dept Va Tech June 2004 ©2004 McQuain WD
Graphs^13
Data Structures & File Management
Graph Traversals: Depth-First
Assuming the node labeled a has
been designated as the starting point,
a depth-first traversal would visit the
graph nodes in the order:
a b e c d f g h i
Note that if the edges taken during
the depth-first traversal are marked,
they define a tree (not necessarily
binary) which includes all the nodes
of the graph.
Such a tree is called a spanning tree
for the graph.
a
i
g
f
e
d c
b
h
a
Computer Science Dept Va Tech June 2004 ©2004 McQuain WD
Graphs^14
Implementing a Depth-First Traversal
a
i
g
f
e
d c
b
h
If we modify DFS() to take another
AdjacencyTable object as a
parameter, it is relatively trivial to
have DFS() build a copy of the
spanning tree.
void DFS(AdjacencyTable& G, int Source) {
G.Mark(Source);
for (int w = G.firstNeighbor(Source); G.isEdge(Source, w); w = G.nextNeighbor(Source, w) ) {
if ( !G.isMarked(w) ) DFS(G, w);
} }
Computer Science Dept Va Tech June 2004 ©2004 McQuain WD
Graphs^15
Data Structures & File Management
Graph Traversals: Breadth-First
Assume a particular node has been designated as the starting point.
Let A be the last node visited and suppose A has neighbors N1, N2, …, Nk.
A breadth-first traversal will:
- visit N1, then N2, and so forth through Nk, then
- proceed to traverse all the unvisited immediate neighbors of N1, then
- traverse the immediate neighbors of N2, … Nk in similar fashion.
N
N2 Nk
A
neighbors of N
Computer Science Dept Va Tech June 2004 ©2004 McQuain WD
Graphs^16
Graph Traversals: Breadth-First
Assuming the node labeled a has
been designated as the starting point,
a breadth-first traversal would visit
the graph nodes in the order:
a b c e h i d f g
Note the edges taken during the
breadth-first traversal also define a
spanning tree for the given graph.
As is the case here, the breadth-first
spanning tree is usually different
from the depth-first spanning tree.
a
i
g
f
e
d c
b
h
Computer Science Dept Va Tech June 2004 ©2004 McQuain WD
Graphs^19
Data Structures & File Management
Computing a Topological Ordering
A topological ordering of the
vertices of G may be obtained
by performing a generalized
depth-first traversal.
We build a list L of vertices
of G.
Begin with vertex 0.
Do a depth-first traversal, marking each visited vertex and adding a vertex to L
only if it has no unmarked neighbors.
When the traversal adds its starting vertex to L, pick the first unmarked vertex
as a new starting point and perform another depth-first traversal.
Stop when all vertices have been marked.
Computer Science Dept Va Tech June 2004 ©2004 McQuain WD
Graphs^20
Depth-First Traversal Trace
Initially we probe from
0 to 1 to 7 , which has
no successors.
L: 7
Next the recursion backs out to 1 , which has no unmarked successors.
L: 1 7
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 June 2004 ©2004 McQuain WD
Graphs^21
Data Structures & File Management
Depth-First Traversal Trace
Now we pick the first
unmarked vertex, 2 ,
and continue the
process. 2 has no
successors.
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
The recursion backs out, adding vertices 4 and 3.
L: 3 4 8 2 0 5 1 7
Computer Science Dept Va Tech June 2004 ©2004 McQuain WD
Graphs^22
Depth-First Traversal Trace
Next we pick the unmarked vertex, 6 , which has no unmarked successors.
L: 6 3 4 8 2 0 5 1 7
Next we pick the unmarked vertex, 9 , which has no unmarked successors.
L: 9 6 3 4 8 2 0 5 1 7
At this point, all the vertices have been marked and the algorithm terminates.
Computer Science Dept Va Tech June 2004 ©2004 McQuain WD
Graphs^25
Data Structures & File Management
Weighted Graphs
In many applications, each edge of a
graph has an associated numerical
value, called a weight.
Usually, the edge weights are non-
negative integers.
Weighted graphs may be either
directed or undirected.
a
i
g
f
e
d c
b
h
25
15
10 5
10
20 15
5
25
10
The weight of an edge is often referred to as the "cost" of the edge.
In applications, the weight may be a measure of the length of a route, the
capacity of a line, the energy required to move between locations along a
route, etc.
Computer Science Dept Va Tech June 2004 ©2004 McQuain WD
Graphs^26
Shortest Path Problem*
Given a weighted graph, and a
designated node S, we would like to
find a path of least total weight from
S to each of the other vertices in the
graph.
The total weight of a path is the sum
of the weights of its edges.
a
i
g
f
e
d c
b
h
25
15
10 5
10
20 15
5
25
10
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.
There is a simple, greedy algorithm that will solve this problem.
*single source, all destinations version
Computer Science Dept Va Tech June 2004 ©2004 McQuain WD
Graphs^27
Data Structures & File Management
Dijkstra's Algorithm*
We assume that there is a path from
the source vertex S to every other
vertex in the graph.
Let S be the set of vertices whose
minimum distance from the source
vertex has been found. Initially S
contains only the source vertex.
The algorithm is iterative, adding one
vertex to S on each pass.
a
i
g
f
e
d c
b
h
25
15
10 5
10
20 15
5
25
10
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.
Computer Science Dept Va Tech June 2004 ©2004 McQuain WD
Graphs^28
Dijkstra's Algorithm Trace
Let the source vertex be a.
a
i
g
f
e
d c
b
h
25
15
10 5
10
20 15
5
25
10
S = {a}
D a b c d e f g h i
S = {a,b}
D a b c d e f g h i
Computer Science Dept Va Tech June 2004 ©2004 McQuain WD
Graphs^31
Data Structures & File Management
Instrumenting Dijkstra's Algorithm
The algorithm can be modified to record the paths as well as the table of
minimum distances by building a list of edges for each vertex, modifying it as
the vertex distance is updated.
S = {a}
Ø Ø Ø Ø Ø Ø Ø Ø Ø
a b c d e f g h i
S = {a, b}
Y {a, b} N N N N N N N
a b c d e f g h i
Y {a, b} N N N N N {b, h} N
S = {a, b, h} a b c d e f g h i
Computer Science Dept Va Tech June 2004 ©2004 McQuain WD
Graphs^32
Minimal Spanning Tree
Given a weighted graph, we would
like to find a spanning tree for the
graph that has minimal total weight.
The total weight of a spanning tree is
the sum of the weights of its edges.
We want to find a spanning tree T,
such that if T' is any other spanning
tree for the graph then the total
weight of T is less than or equal to
that of T'.
a
i
g
f
e
d c
b
h
25
15
10 5
10
20 15
5
25
10
Computer Science Dept Va Tech June 2004 ©2004 McQuain WD
Graphs^33
Minimal Spanning Tree Algorithm
By modifying Dijkstra’s Algorithm to build a list of the edges that are used as
vertices are added, and storing the distance from nodes to the current tree (rather
than from nodes to the source) 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.