Professional Documents
Culture Documents
CSCI2100 08 Graphs
CSCI2100 08 Graphs
Graph Algorithms
Irwin King
king@cse.cuhk.edu.hk
http://www.cse.cuhk.edu.hk/~king
Department of Computer Science & Engineering
The Chinese University of Hong Kong
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Introduction
Show several real-life problems using graphs.
algorithms to solve several common graph
Give
problems.
how the proper choice of data structures can
Show
drastically reduce the running time of these
algorithms.
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Examples
Algorithms to find the minimum path
between two nodes
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Definitions
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Definitions
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Definition
Vertex w is adjacent to v if and only if
(v,w) E.
Path Definition
Path Definition
If the graph contains an edge (v,v) from a
vertex to itself, then the path v, v is
sometimes referred to as a loop.
Cycle Definition
A cycle in a directed graph is a path of
length at least 1 such that w1 = wn; this
cycle is simple if the path is simple.
Why?
Cycle Definition
In a directed graph, these are different
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Connectedness
Definition
Airport Example
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Example
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Example
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Implementation
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Example
For instance, if we were looking for the cheapest
airplane route, we could represent nonexistent
flights with a cost of .
Notes
The space requirement is
2
(|V| ).
This is
unacceptable if the graph does not have
very many edges.
Adjacency List
Example
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Topological
Example
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Example
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Notes
Is topological ordering possible with a cyclic
graph?
Topological Sort
Pseudocode
topsort( graph G )
{
unsigned int counter;
vertex v, w;
for(counter=0; counter<NUM_VERTEX; counter++){
v = find_new_vertex_of_indegree_zero( );
if( v = NOT_A_VERTEX ) {
error("Graph has a cycle"); break;}
top_num[v] = counter;
for each w adjacent to v
indegree[w]--;}}
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Time Complexity
How long will the algorithm take to find
the topological ordering of a graph?
2
O(|V| ).
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
-------------------------------------v1
v2
v3
v4
v5
v6
v7
-------------------------------------enqueue
v1
v2
v5
v4
v3
v7
v6
-------------------------------------dequeue
v1
v2
v5
v4
v3
v7
v6
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Shortest Path
Algorithms
n1
i ,i +1
i =1
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Example
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Example
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Problem
The path from v
and v4 is
Notes
Currently there are no algorithms in which finding
the path from s to one vertex is any faster (by
more than a constant factor) than finding the path
from s to all vertices.
The intermediate nodes in a shortest path must
also be the shortest path node from s.
Notes
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Unweighted Shortest
Paths
some vertex, s, which is an input
Using
parameter, we would like to find the
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Unweighted Shortest
Paths
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Algorithm
Example
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Example
v
Known
dv
pv
-----------------v1
v2
v3
v4
v5
v6
v7
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Example
Initial State
------------v
Known
dv
pv
v3 Dequeued
v1 Dequeued
v6 Dequeued
--------------
-------------
-------------
Known
Known
Known
dv
pv
dv
pv
dv
pv
---------------------------------------------------------------v1
v3
v3
v3
v2
v1
v1
v3
v4
v5
v6
v7
0
0
0
0
0
1
0
0
0
0
0
0
0
v3
0
1
0
0
0
0
0
2
0
v1
0
v3
0
0
v1
0
v3
0
---------------------------------------------------------------Q:
v3
v1,v6
v6,v2,v4
v2,v4
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Example
v2 Dequeued
v4 Dequeued
------------v
Known
dv
--------------
pv
Known
dv
pv
v5 Dequeued
v7 Dequeued
-------------
-------------
Known
Known
dv
pv
dv
pv
---------------------------------------------------------------v1
v3
v3
v3
v3
v2
v1
v1
v1
v1
v3
v4
v1
v1
v1
v1
v5
v2
v2
v2
v2
v6
v3
v3
v3
v3
v7
v4
v4
v4
---------------------------------------------------------------Q:
v4,v5
v5,v7
v7
empty
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Breadth-first Search
strategy for searching a graph is
This
known as breadth-first search.
Notes
each vertex, we will keep track of three pieces
For
of information.
Notes
Dijkstra's Algorithm
If the graph is weighted, the problem
(apparently) becomes harder.
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Outline
Greedy Algorithm
general method to solve the single The
source shortest-path problem is known as
Dijkstra's algorithm.
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Notes
Dijkstra's algorithm proceeds in stages.
each stage, Dijkstra's algorithm selects a
At
vertex v, which has the smallest d among
v
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Initial Configuration
v
Known
dv
pv
------------------v1
v2
v3
v4
v5
v6
v7
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
After v1 Is Known
v
Known
dv
pv
-------------------v1
v2
v1
v3
v4
v1
v5
v6
v7
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
After v4 Is Known
v
Known
dv
pv
-------------------v1
v2
v1
v3
v4
v4
v1
v5
v4
v6
v4
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
After v2 Is Known
v
Known
dv
pv
-------------------v1
v2
v1
v3
v4
v4
v1
v5
v4
v6
v4
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Known
dv
pv
-------------------v1
v2
v1
v3
v4
v4
v1
v5
v4
v6
v3
v7
v4
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
After v7 Is Known
v
Known
dv
pv
------------------v1
v2
v1
v3
v4
v4
v1
v5
v4
v6
v7
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
After v6 Is Known
v
Known
dv
pv
------------------v1
v2
v1
v3
v4
v4
v1
v5
v4
v6
v7
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Acyclic Graphs
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Acyclic Graphs
Why does this selection rule work?
Because when a vertex v is selected, its
distance, dv, can no longer be lowered,
Applications
Downhill skiing problem
Modeling of (nonreversible) chemical reactions.
Critical path analysis
Activity-node Graph
The edges represent
precedence
relationships: An edge
(v, w) means that
activity v must be
completed before
activity w may begin.
Of course, this implies
that the graph must
be acyclic.
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Notes
Model construction projects
What is the earliest completion time for
the project?
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Notes
important question is to determine
Another
which activities can be delayed, and by how
long, without affecting the minimum
completion time.
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Problem
At any vertex, v, that is not either s or t,
the total flow coming in must equal the
total flow going out.
Example
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Example
Example
Example
Example
Problem
t is unreachable from s the algorithm
When
terminates.
resulting flow of 5 happens to be the
The
maximum.
Problem of Not Being Optimal
Example
How To Make It
Optimal
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Example
Example
s
s
2
3
a
b
2
3
t
b
0
3
d
a
0
2
4
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
3
t
Example
s
s
2
3
a
b
2
c
2
b
0
3
d
a
0
d
3
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
3
t
Example
s
2
2
4
3
t
b
2
b
1
1 2
d
a
2
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
3
t
Notes
Notes
If the capacities are all integers and the
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Example
1000000
a
s
1
1000000
1000000
b
1000000
1000000
a
1000000
1000000
b
1000000
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Example
s
1000000
a
s
1000000
1000000
1000000
1
b
1000000
999999
a
b
1 999999
1000000
t
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Example
1000000
a
s
1
1000000
1000000
b
1000000
999999
a
999999
999999
1 1
1
b
1 1 999999
t
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Problem
maximum flow is seen by inspection to be
The
2,000,000 by sending 1,000,000 down each side.
the algorithm, 2,000,000 augmentations
Using
would be required, when we could get by with only
2.
Example
Is the minimum
spanning tree unique?
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Notes
Notice that the number of edges in the
minimum spanning tree is |V| - 1.
Prims Algorithm
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Notes
Example
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Example
v
Known
dv
pv
-------------------v1
v2
v3
v4
v5
v6
v7
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
After V1 is Known
v
Known
dv
pv
-------------------v1
v2
v1
v3
v1
v4
v1
v5
v6
v7
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Example
v
Known
dv
pv
-------------------v1
v2
v1
v3
v4
v4
v1
v5
v4
v6
v4
v7
v4
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Known
dv
pv
-------------------v1
v2
v1
v3
v4
v4
v1
v5
v4
v6
v3
v7
v4
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
After V7 is Known
v
Known
dv
pv
-------------------v1
v2
v1
v3
v4
v4
v1
v5
v7
v6
v7
v7
v4
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Known
dv
pv
-------------------v1
v2
v1
v3
v4
v4
v1
v5
v7
v6
v7
v7
v4
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Notes
aware that Prim's algorithm runs on
Be
undirected graphs, so when coding it,
remember to put every edge in two
adjacency lists.
Kruskal's Algorithm
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Notes
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Notes
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Example
Edge
Weight
Action
---------------------------(v1,v4)
Accepted
(v6,v7)
Accepted
(v1,v2)
Accepted
(v3,v4)
Accepted
(v2,v4)
Rejected
(v1,v3)
Rejected
(v4,v7)
Accepted
(v3,v6)
Rejected
(v5,v7)
Accepted
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Notes
worst-case running time of this
The
algorithm is O(|E| log |E|), which is
dominated by the heap operations.
Depth-First Search
search is a generalization of
Depth-first
preorder traversal.
at some vertex, v, we process v
Starting
and then recursively traverse all vertices
adjacent to v.
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
Depth-First Search
we perform this process on an arbitrary
Ifgraph,
we need to be careful to avoid
cycles.
Undirected Graph
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
DFS Example
A
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.
BFS Example
A
CSC2100 Data Structures, The Chinese University of Hong Kong, Irwin King, All rights reserved.