981 KB - KFUPM Resources v3

King Fahd University of Petroleum & Minerals
College of Computer Science & Engineering
Information & Computer Science Department
Unit 11
Graphs (2)
Reading Assignment
 “Data Structures and Algorithms in Java”, 3rd Edition,
Adam Drozdek, Cengage Learning, ISBN 978-9814239233
 Chapter 8
 Section
 Section
 Section
 Section
8.3:
8.4:
8.5:
8.6:
Shortest Paths (8.3.1 is not included)
Cycle Detection (8.4.1 is not included)
Spanning Trees
Connectivity
Outline
 Graph Connectedness and Cycles
 Dijkstra’s Shortest Path Algorithm
 Minimum Spanning Tree Algorithms
 Prim’s Algorithm
 Kruskal’s Algorithm
3
Graph Connectedness and Cycles
 Connectedness of an Undirected Graph
 Connectedness of a directed Graph


Algorithm for finding strongly connected components.
Cycle detection algorithm in Directed Graphs.
 Review Questions.
4
Connectedness of an Undirected Graph
 An undirected graph G = (V, E) is connected if there is a path
between every pair of vertices.
 Although the figure below appears to be two graphs, it is actually
a single graph.
 Clearly, G is not connected. e.g. no path between A and D.
 G consists of two unconnected parts, each of which is a connected
sub-graph --- connected components.
V = {A, B, C, D, E, F}
E = {{A, B}, {A, C}, {B, C}, {D, E}, {E, F}}
5
6
Algorithm for Undirected Graphs Connectedness
 A simple way to test for connectedness in an
undirected graph is to use either one method call
for depth-first or breadth-first traversal (i.e., not
the General Traversal Algorithm)
 If all the vertices are visited after that one call, the
graph is connected.
Connectedness of a Directed Graph
 Remember that a directed graph G = (V, E) is strongly
connected if there is a directed path between every pair
of vertices.
 A simple way to test for strong connectedness is to use
|V| traversals - The graph is strongly connected if all the
vertices are visited in each traversal.
 What is the time complexity of this algorithm in O()?
 Weak connectedness is implemented in the Lab.
V = {A, B, C, D, E, F}
E = {(A, B), (B, C), (C, A), (B, E), (D, E), (E, F),
(F, D)
7
8
Strongly Connected Components
 A strongly connected component in a directed graph
DG=<V,E> is a set of vertices C (which is a subset of V)
such that for every pair of vertices x,y in C, there exists
a directed path from x to y and a directed path from y
to x.
 Thus, starting at any vertex in a strongly connected component
C, it is possible to reach every other vertex in C.
 The number of strongly connected components in a strongly
connected directed graph is …..
9
Example
3
7
1
5
8
2
4
6
10
SCC Algorithm
1.
2.
3.
4.
Perform a post-order depth first search on the directed
graph DG.
Number the depth first search tree (or forest) using the
visit method of the postorder traversal.
Form a new graph DGr=<V,Er> by reversing every edge
in E to form Er.
Perform a depth first search on the directed graph DGr,
with vertices ordered in decreasing order of their
numbers generated in 2. Assign a distinct component
number to all visited vertices every time the DFS
procedure is called from within the general traversal
algorithm.
What is the time complexity of the SCC Algorithm in O()?
11
Example
3
7
1
5
8
2
4
6
12
Example
8
7
1
3
3
6
2
7
5
8
7
1
3
3
6
2
2
8
7
5
8
1
1
4
6
4
4
5
5
8
7
1
3
3
6
2
7
5
8
1
4
5
2
6
4
2
6
4
Pop Quiz: Run SCC Algorithm on
1
5
3
2
4
6
13
14
Cycles in a Directed Graph

An easy way to detect the presence of cycles in a directed graph is to
attempt a topological order traversal.



This algorithm visits all the vertices of a directed graph if the graph has no
cycles.
In the following graph, after A is visited and removed, all the remaining
vertices have in-degree of one.
Thus, a topological order traversal cannot complete. This is because of the
presence of the cycle { B, C, D, B}.
• What is the time complexity of this algorithm in O()?
Review Questions
1.
Every tree is a directed, acyclic graph (DAG), but there exist DAGs that
are not trees.
a) How can we tell whether a given DAG is a tree?
b) Devise an algorithm to test whether a given DAG is a tree.
2.
Consider an acyclic, connected, undirected graph G that has n vertices.
How many edges does G have?
3.
In general, an undirected graph contains one or more connected
components.
a) Devise an algorithm that counts the number of connected components
in a graph.
b) Devise an algorithm that labels the vertices of a graph in such a way
that all the vertices in a given connected component get the same label
and vertices in different connected components get different labels.
4.
Devise an algorithm that takes as input a graph, and a pair of vertices, v
and w, and determines whether w is reachable from v.
15
Shortest Path Algorithm
 What is the Shortest Path Problem?
 Is the shortest path problem well defined?
 The Dijkstra's Algorithm for Shortest Path Problem.
 Implementation of Dijkstra's Algorithm
16
What is the shortest path problem?
 In an edge-weighted graph, the weight of an edge measures the
cost of traveling that edge.
 For example, in a graph representing a network of airports, the
weights could represent: distance, cost or time.
 Such a graph could be used to answer any of the following:
 What is the fastest way to get from A to B?
 Which route from A to B is the least expensive?
 What is the shortest possible distance from A to B?
 Each of these questions is an instance of the same problem:
The shortest path problem!
17
Is the shortest path problem well defined?
 If all the edges in a graph have non-negative weights, then it is
possible to find the shortest path from any two vertices.
 For example, in the figure below, the shortest path from B to F is
{ B, A, C, E, F } with a total cost of nine.
 Thus, the problem is well defined for a graph that contains nonnegative weights.
18
19
Is the shortest path problem well defined? - Cont'd
 Things get difficult for a graph with negative weights.
 For example, the path D, A, C, E, F costs 4 even though the edge
(D, A) costs 5 -- the longer the less costly.
 The problem gets even worse if the graph has a negative cost
cycle. e.g. {D, A, C, D}
 A solution can be found even for negative-weight graphs but not
for graphs involving negative cost cycles.
{D, A, C, D, A, C, E, F} = 2
{D, A, C, D, A, C, D, A, C, E, F} = 0
The Dijkstra's Algorithm
 Dijkstra's algorithm solves the single-source shortest
path problem for a non-negative weights graph.
 It finds the shortest path from an initial vertex, say s, to
all the other vertices.
20
The Dijkstra's Algorithm Cont'd
21
// Let V be the set of all vertices in G, and s the start vertex.
for(each vertex v){
currentDistance(s-v) = ∞;
For each vertex, the algorithm keeps
predecessor(v) = undefined;
track of its current distance from the
}
starting vertex and the predecessor on
currentDistance(s-s) = 0;
the current path
T = V;
while(T  ){
v = a vertex in T with minimal currentDistance from s;
T = T – {v};
for(each vertex u adjacent to v and in T){
if(currentDistance(s-u) > currentDistance(s-v) + weight(edge(vu)){
currentDistance(s-u) = currentDistance(s-v) + weight(edge(vu));
predecessor(u) = v;
}
}
}
 How can we implement Dijkstra’s algorithm?
 What is the time complexity of the algorithm?
22
Example
Tracing Dijkstra’s algorithm starting at vertex B:
Active
Vertex
1
2
B
A

B
0
C

D

E

F

The resulting vertex-weighted graph is:
3
4
5
6
Predecesso
r
initially
weight
Pass
23
Example
Tracing Dijkstra’s algorithm starting at vertex B:
Active
Vertex
Predecesso
r
initially
weight
Pass
### ### ### ### ###
3
B
### ### ### ### ### ###
0
-
4
A
6
C
8
C
9
E
1
2
3
4
5
6
B
A
C
D
E
F
A

B
0
C

5
4
D



6
E



8
8
F




11
3
The resulting vertex-weighted graph is:
### ### ### ###
### ### ###
### ###
9
###
Review Questions

Use the graph Gc shown above to trace the execution of Dijkstra's
algorithm as it solves the shortest path problem starting from vertex a.

Dijkstra's algorithm works as long as there are no negative edge weights.
Given a graph that contains negative edge weights, we might be tempted
to eliminate the negative weights by adding a constant weight to all of
the edges. Explain why this does not work.

Dijkstra's algorithm can be modified to deal with negative edge weights
(but not negative cost cycles). Implement this modified algorithm.
24
Minimum Spanning Tree
 What is a Minimum Spanning Tree.
 Constructing Minimum Spanning Trees.
 What is a Minimum-Cost Spanning Tree.
 Applications of Minimum Cost Spanning Trees.
 Prim’s Algorithm.
 Example.
 Implementation.
 Kruskal’s algorithm.
 Example.
 Implementation.
 Review Questions.
25
What is a Spanning Tree.
26
 Let G = (V, E) be a simple, connected, undirected graph that is not edgeweighted.
 A spanning tree of G is a free tree (i.e., a tree with no root) with | V | - 1
edges that connects all the vertices of the graph.
 Thus a spanning tree for G is a graph, T = (V’, E’) with the following
properties:
 V’ = V
 T is connected
 T is acyclic.
 A spanning tree is called a tree because every acyclic undirected graph
can be viewed as a general, unordered tree. Because the edges are
undirected, any vertex may be chosen to serve as the root of the tree.
27
Constructing Spanning Trees

Any traversal of a connected, undirected graph visits
all the vertices in that graph. The set of edges which
are traversed during a traversal forms a spanning tree.
(a) Graph G

For example, Fig:(b) shows the spanning tree obtained
from a breadth-first traversal starting at vertex b.
(b) Breadth-first spanning
tree of G rooted at b

Similarly, Fig:(c) shows the spanning tree obtained
from a depth-first traversal starting at vertex c.
(c) Depth-first spanning
tree of G rooted at c
What is a Minimum-Cost Spanning Tree
28
 For an edge-weighted , connected, undirected graph, G, the total
cost of G is the sum of the weights on all its edges.
 A minimum-cost spanning tree for G is a spanning tree of G that
has the least total cost.
 Example: The graph
Has 16 spanning trees. Some are:
The graph has two minimum-cost spanning trees, each with a cost
of 6:
29
Applications of Minimum-Cost Spanning Trees
Minimum-cost spanning trees have many applications. Some are:
 Building cable networks that join n locations with minimum cost.
 Building a road network that joins n cities with minimum cost.
 Obtaining an independent set of circuit equations for an electrical
network.
 In pattern recognition minimal spanning trees can be used to find
noisy pixels.
Prim’s Algorithm
30
 Prim’s algorithm finds a minimum cost spanning tree by selecting
edges from the graph one-by-one as follows:
 It starts with a tree, T, consisting of the starting vertex, x.
 Then, it adds the shortest edge emanating from x that connects T
to the rest of the graph.
 It then moves to the added vertex and repeats the process.
Consider a graph G=(V, E);
Let T be a tree consisting of only the starting vertex x;
while (T has fewer than IVI vertices)
{
find a smallest edge connecting T to G-T;
add it to T;
}
 How can we implement Prim’s algorithm?
 What is the time complexity of the algorithm?
Example
Trace Prim’s algorithm starting at vertex a:
The resulting minimum-cost spanning tree
is:
31
Kruskal's Algorithm.
32
 Kruskal’s algorithm also finds the minimum cost
spanning tree of a graph by adding edges one-by-one.
enqueue edges of G in a queue in increasing order of cost.
T=;
while(queue is not empty){
dequeue an edge e;
if(e does not create a cycle with edges in T)
add e to T;
}
return T;
 How can we implement Kruskal’s algorithm?
 What is the time complexity of the algorithm?
33
Example for Kruskal’s Algorithm.
Trace Kruskal's algorithm in finding a
minimum-cost spanning tree for the
undirected, weighted graph given below:
edge
weight
Insertion
status
Insertion
order
The minimum cost is
34
Example for Kruskal’s Algorithm.
Trace Kruskal's algorithm in finding a
minimum-cost spanning tree for the
undirected, weighted graph given below:
The minimum cost is: 24
Prim’s and Kruskal’s Algorithms
Note: It is not necessary that Prim's and Kruskal's algorithm generate the same
minimum-cost spanning tree.
For example for the graph:
Kruskal's algorithm (that imposes an ordering on edges with
equal weights) results in the following minimum cost
spanning tree:
The same tree is generated by Prim's algorithm if the start vertex
is any of: A, B, or D; however if the start vertex is C the minimum
cost spanning tree is:
35
36
Review Questions
GB
1. Find the breadth-first spanning tree and depth-first spanning tree of the
graph GA shown above.
2. For the graph GB shown above, trace the execution of Prim's algorithm as it
finds the minimum-cost spanning tree of the graph starting from vertex a.
3. Repeat question 2 above using Kruskal's algorithm.