0% found this document useful (0 votes)
6 views

Lecture 11a- Introduction to Graphs

Uploaded by

oscarfx94
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lecture 11a- Introduction to Graphs

Uploaded by

oscarfx94
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

CS 2604 Spring 2004

Graphs Graphs 1

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 January 2004 Data Structures & File Management ©2004 McQuain WD

Undirected Graph Terminology Graphs 2

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} ,

{c, d} , {c, e} , {e, f} , {e, g} , {h, i} }


e
f
e = {c, d} is an edge, incident upon the i
vertices c and d
g
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 January 2004 Data Structures & File Management ©2004 McQuain WD

©William D McQuain January 2004 1


CS 2604 Spring 2004

Undirected Graph Terminology Graphs 3

A graph G is connected if, given any two a


vertices x and y in G, there is a path in G b
with first vertex x and last vertex y.

d
The graph on the previous slide is c
connected. h

If a graph G is not connected, then we say e


that a maximal connected set of vertices is a f
component of G. i

Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD

Directed Graph Terminology Graphs 4

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 path in a directed graph G is a e


sequence of distinct vertices, such that
there is an edge from each vertex in the f
i
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).

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

©William D McQuain January 2004 2


CS 2604 Spring 2004

Adjacency Table Representation Graphs 5

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

Adjacency Table Representation Graphs 6

The adjacency table:


- Θ(1) to determine existence of a specific edge
- Θ( |V|2 ) storage cost (cut cost by 75% by using bool instead of int)
- Θ( |V| ) for finding all neighbors of a specific vertex
- Θ(1) to add or delete an edge
- Not easy to add or delete a vertex; better for static graph structure.
- Symmetric matrix for undirected graph; so half is redundant then.
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

©William D McQuain January 2004 3


CS 2604 Spring 2004

An Adjacency Table Class Graphs 7

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);

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 . . .

Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD

An Adjacency Table Class Graphs 8

// . . . continued . . .
firstNeighbor() returns
the first vertex adjacent to
Source.
int firstNeighbor(int Source) const;
int nextNeighbor(int Source, int prevNeighbor) const;

bool isMarked(int Vertex) const; nextNeighbor() returns


bool Mark(int Vertex); the next vertex, after
bool unMark(int Vertex); prevNeighbor, which is
adjacent to Source.

void Clear(); Clear() deletes all the


edges from the graph.
virtual void Display(ostream& Out);
~AdjacencyTable();
}; Graph: Sample07

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

©William D McQuain January 2004 4


CS 2604 Spring 2004

Adjacency List Representation Graphs 9

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

Adjacency List Representation Graphs 10

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.
0 1 2 •

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 •

So, the space comparison 4 1 5 •


with the adjacency table
scheme is not trivial. 5 1 4 •

Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD

©William D McQuain January 2004 5


CS 2604 Spring 2004

Graph Traversals Graphs 11

Some algorithms require that every a


vertex of a graph be visited exactly b
once.
d
The order in which the vertices are
c
visited may be important, and may h
depend upon the particular algorithm.
e
The two common traversals:
f
i
- depth-first
g
- breadth-first
- topological sort

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

Graph Traversals: Depth-First Graphs 12

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 depth-first traversal will:
- visit N1, then
- proceed to traverse all the unvisited neighbors of N1, then
- proceed to traverse the remaining neighbors of A in similar fashion.

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

©William D McQuain January 2004 6


CS 2604 Spring 2004

Graph Traversals: Depth-First Graphs 13

Assuming the node labeled a has a


b
been designated as the starting point,
a depth-first traversal would visit the
d
graph nodes in the order:
c
h

a b e c d f g h i e
f
i

Note that if the edges taken during g


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.

Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD

Implementing a Depth-First Traversal Graphs 14

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, DFSTree, w);
}
}

a
b

If we modify DFS() to take another d


AdjacencyTable object as a c
h
parameter, it is relatively trivial to
have DFS() build a copy of the e
spanning tree. f i

Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD

©William D McQuain January 2004 7


CS 2604 Spring 2004

Graph Traversals: Breadth-First Graphs 15

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.
8
1

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

Graph Traversals: Breadth-First Graphs 16

Assuming the node labeled a has a


b
been designated as the starting point,
a breadth-first traversal would visit
d
the graph nodes in the order:
c
h

a b c e h i d f g e
f
i

Note the edges taken during the g


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.

Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD

©William D McQuain January 2004 8


CS 2604 Spring 2004

Implementing a Breadth-First Traversal Graphs 17


a
The breadth-first traversal uses a local b
queue to organize the graph nodes into
the proper order: d
c
void BFS(AdjacencyTable& G, int Source) { h

QueueT<int> toVisit; // schedule nodes here


e
toVisit.Enqueue(Source);
G.Mark(Source); f i
while ( !toVisit.isEmpty() ) {
g
int VisitNow = toVisit.Dequeue();

for (int w = G.firstNeighbor(VisitNow);


G.isEdge(VisitNow, w); w = G.nextNeighbor(VisitNow, w) ) {
if ( !G.isMarked(w) ) {
toVisit.Enqueue(w);
G.Mark(w);
}
} The for loop schedules all
} the unvisited neighbors of
} the current node for future
visits.

Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD

Topological Ordering Graphs 18

Suppose that G is a directed


graph which contains no
directed cycles:

Then, a topological ordering of the vertices in G is a sequential listing of the


vertices such that for any pair of vertices, v and w in G, if (v,w) is an edge in G
then v precedes w in the sequential listing.

Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD

©William D McQuain January 2004 9


CS 2604 Spring 2004

Computing a Topological Ordering Graphs 19

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 January 2004 Data Structures & File Management ©2004 McQuain WD

Depth-First Traversal Trace Graphs 20

Initially we probe from 0 1


0 to 1 to 7, which has
no successors.
L: 7
5 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 January 2004 Data Structures & File Management ©2004 McQuain WD

©William D McQuain January 2004 10


CS 2604 Spring 2004

Depth-First Traversal Trace Graphs 21

Now we pick the first 2 3 7


unmarked vertex, 2,
and continue the
process. 2 has no
successors. 8

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 January 2004 Data Structures & File Management ©2004 McQuain WD

Depth-First Traversal Trace Graphs 22

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 January 2004 Data Structures & File Management ©2004 McQuain WD

©William D McQuain January 2004 11


CS 2604 Spring 2004

Multiple Topological Orderings Graphs 23

There is usually more than


one topological ordering for a
graph. Choosing a different
rule for picking the starting
vertices may yield a different
ordering.

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

Applications of Topological Orderings Graphs 24

Applications of topological orderings are relatively common…

- prerequisite relationships among courses


- glossary of technical terms whose definitions involve dependencies
- organization of topics in a book or a course

Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD

©William D McQuain January 2004 12


CS 2604 Spring 2004

Weighted Graphs Graphs 25

In many applications, each edge of a a 15


b
graph has an associated numerical
25
value, called a weight. 5
10 d
Usually, the edge weights are non- c 10
25 h
negative integers.
20

Weighted graphs may be either e 15


10
directed or undirected. f 5 i

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 January 2004 Data Structures & File Management ©2004 McQuain WD

Shortest Path Problem* Graphs 26

Given a weighted graph, and a a 15


designated node S, we would like to b
find a path of least total weight from 25
5
S to each of the other vertices in the 10 d
graph. c 10
25 h
20

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.

There is a simple, greedy algorithm that will solve this problem.

*single source, all destinations version


Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD

©William D McQuain January 2004 13


CS 2604 Spring 2004

Dijkstra's Algorithm* Graphs 27

We assume that there is a path from a 15


the source vertex S to every other b
vertex in the graph. 25
5
10 d
Let S be the set of vertices whose
c 10
minimum distance from the source 25 h
vertex has been found. Initially S 20

contains only the source vertex. e 15


10
f
The algorithm is iterative, adding one 5 i
vertex to S on each pass.
g

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

Dijkstra's Algorithm Trace Graphs 28


a 15
Let the source vertex be a. b
25
5
10 d
c 10
25 h
20

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

©William D McQuain January 2004 14


CS 2604 Spring 2004

Dijkstra's Algorithm Trace Graphs 29

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

Dijkstra's Algorithm Trace Graphs 30

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

©William D McQuain January 2004 15


CS 2604 Spring 2004

Instrumenting Dijkstra's Algorithm Graphs 31

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

Minimal Spanning Tree Graphs 32

Given a weighted graph, we would a 15


b
like to find a spanning tree for the
25
graph that has minimal total weight. 5
10 d
c 10
25 h
The total weight of a spanning tree is 20

the sum of the weights of its edges. e 15


10
f 5 i

We want to find a spanning tree T, g


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'.

Computer Science Dept Va Tech January 2004 Data Structures & File Management ©2004 McQuain WD

©William D McQuain January 2004 16


CS 2604 Spring 2004

Minimal Spanning Tree Algorithm Graphs 33

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

©William D McQuain January 2004 17

You might also like