Vous êtes sur la page 1sur 66

DATA STRUCTURE AND

ALGORITHMS FIT324
Summer 2014
Lecture #11
Nguyen Xuan Thang
nxthang@hanu.edu.vn
1
Graphs
2
Content
Graph definition and terminologies
Graph types
Graph Traversal
Topological Sort
3
Wha !s a "raph# $1%
Definition
! graph G is defined as a pair (V, E) "here
# is the set of nodes (vertices)
$ is the set of edges
%or any nodes u and v& if u and v are connected
'y an edge& such edge is denoted as (u, v)
4
v
u
(u, v)
Wha !s a "raph# $2%
Graphs also represent the relationships
among data items
$ach node (vertex) represents an item
$ach edge represents the relationship 'et"een
t"o items
5
vertex
edge
Wha !s a "raph# $3%
Some examples
*olecular structure& net"or+
,ther examples electrical and communication
net"or+s& airline routes& flo" chart& graphs for
planning pro-ects
6
H
H
C H H
Molecular Structure
Server 1
Server 2
Terminal 1
Terminal 2
Computer Network
Ma!& 'pes () Graph
.ndirected Graph and /irected Graph
0eighted Graph and .n"eight graph
!nd mix of them
7
Terminology $1%
Adjacent
T"o nodes u and v are said to 'e adjacent if
(u, v) E
u and v are ad-acent
v and " are not ad-acent
8
v
w
u
(u, v)
Terminology $2%
! path from v
1
to v
+
is a se1uence of nodes
v
1
& v
2
& 3& v
+
that are connected 'y edges
(v
1
& v
2
)& (v
2
& v
4
)& 3& (v
+51
& v
+
)
! simple path is a path that every node
appears at most once.
$g #
1
#
2
#
4
9
v
1
v
2
v
4
v
3
v
5
Terminology $3%
! cycle is a path that 'egins and ends at
the same node.
! simple cycle is a cycle if every node
appears at most once& except for the first
and the last nodes
$.g #
2
#
4
#
6
#
2
10
v
1
v
2
v
4
v
3
v
5
Terminology $4%
onnected graph
! graph G is connected if there exists path
'et"een every pair of distinct nodes7
other"ise& it is disconnected
11
v
1
v
4
v
3
v
5
v
2
Terminology $*%
$xample of disconnected graph
12
v
1
v
4
v
3
v
5
v
2
v
7
v
6
v
8
v
9
Terminology $+%
onnected component
8f a graph is disconnect& it can 'e partitioned
into a num'er of graphs such that each of
them is connected. $ach such graph is called a
connected component.
13
v
1
v
4
v
3
v
5
v
2
v
7
v
6
v
8
v
9
Terminology $,%
omplete graph
! graph is complete if each pair of distinct
nodes has an edge
14
Complete graph with
3 nodes
Complete graph
with 4 nodes
Terminology $-%
! su!graph of a graph G 9(#& $) is a graph
: 9 (.& %) such that . # and % $.
15
v
1
v
4
v
3
v
5
v
2
G
v
4
v
3
v
5
v
2
H
Terminology $.%
"eighted graph
8f each edge in G is assigned a #eight& it is
called a "eighted graph
16
Houston
Chicago
1
2
35
New York
Term!&(/("' $10%
Directed graph (digraph)
8f each edge in $ has a direction& it is called
a directed edge.
! directed graph is a graph "here every
edges is a directed edge.
17
Term!&(/("' $11%
8f (x& y) is a directed edge& "e say
y is adjacent to x
y is successor of x
x is predecessor of y
8n a directed graph& directed path&
directed cycle can 'e defined similarly.
18
y x
Graph prese&a!(&
$ main #ays to represent a graph
Adjacency matrix
;epresent a graph using a t#o%dimensional
array.
Adjacency list
;epresent a graph using n lin&ed lists #here n
is the num!er of vertices.
19
A01a2e&2' mar!3 )(r 0!re2e0 "raph
1 2 3 4 5
v
1
v
2
v
3
v
4
v
5
1 v
1
0 1 0 0 0
2 v
2
0 0 0 1 0
3 v
3
0 1 0 1 0
4 v
4
0 0 0 0 0
5 v
5
0 0 1 1 0
20
v
1
v
4
v
3
v
5
v
2
G
Matr|x a||]|j] = _
1 if if if if (vi, (vi, (vi, (vi, vj) vj) vj) vj) E EE E
if if if if (vi, (vi, (vi, (vi, vj) vj) vj) vj) E EE E
A01a2e&2' mar!3 )(r 4e!"he0
u&0!re2e0 "raph
21
v
1
v
2
v
3
v
4
v
5
v
1
5
v
2
5 2 4
v
3
2 3 7
v
4
4 3 8
v
5
7 8
v
1
v
4
v
3
v
5
v
2
G
5
2
3
7
8
4
Matr|x a||]|j] = _
w(vi, w(vi, w(vi, w(vi, vj) vj) vj) vj) if if if if (vi, (vi, (vi, (vi, vj) vj) vj) vj) E EE E oi oi oi oi ( (( (vj, vj, vj, vj, vi) vi) vi) vi) E EE E
otheiwise otheiwise otheiwise otheiwise
A01a2e&2' /!s )(r 0!re2e0 "raph
' v
'
v
$
$ v
$
v
(
) v
)
v
$
v
(
( v
(
* v
*
v
)
v
(
22
v
1
v
4
v
3
v
5
v
2
G
A01a2e&2' /!s )(r 4e!"he0 u&0!re2e0
"raph
1 v
1
v
2
(5)
2 v
2
v
1
(5) v
3
(2) v
4
(4)
3 v
3
v
2
(2) v
4
(3) v
5
(7)
4 v
4
v
2
(4) v
3
(3) v
5
(8)
5 v
5
v
3
(7) v
4
(8)
23
v
1
v
4
v
3
v
5
v
2
G
5
2
3
7
8
4
5r(s a&0 C(&s
Adjacency matrix
!llo"s us to determine "hether there is an
edge from node i to node j in ,(1) time
Adjacency list
!llo"s us to find all nodes ad-acent to a given
node j efficiently
8f the graph is sparse& ad-acency list re1uires
less space
24
Graph ADT
Graph
+ addVertex(id, la!el) , adds a ne# vertex to graph- Vertices
are indexed !y ids and assigned la!els-
+ getVertex(id) . Vertex
+ addEdge(sourceVert/d, destinationVert/d, #eight)
+ getEdge"eight(sourceVert/d, destinationVert/d)
+ removeEdge(sourceVert/d, destinationVert/d)
+ areAdjacent(vertex0/d, vertexV/d) .
+ get1um!er2fVertices() . /nteger
25
Imp/eme&a!(& $1%
Vertex
5 int id
5 char la!el, color
+ Vertex(int id, char
color, char la!el)
+ get/d()
+ get3a!el()
+ getolor()
+ set3a!el()
+ setolor()
26
A4Graph
5 int numVertices
5 Vertex 56 v3ist 77an array of vertices
5 int 5656 a
+ A4Graph(int numVertices)
+ setAdj4atrix(int5656 a)
+ addVertex(int id, char la!el)
+ addEdge(int u/d, int v/d)
+ getVertex(int id) . Vertex
+ getEdge"eight(int u/d, int v/d) . int
+ removeEdge(int u/d, int v/d)
+ get1um!er2fVertices() . int
Imp/eme&a!(& $2%
27
A3Graph
5 int numVertices
5 Vertex3ist v3ist 77Asingly%lin&ed list of vertices
+ A3Graph()
+ addVertex(int id, char la!el)
+ addEdge(int u/d, int v/d)
+ getVertex(int id) . Vertex
+ getEdge"eight(int u/d, int v/d) . int
+ removeEdge(int u/d, int v/d)
+ get1um!er2fVertices() . int
+ createAdjacent4atrix() . int5656
Imp/eme&a!(& $3%
28
Vertex3ist
5 A3Vertexhead
+ Vertex3ist()
+ add(A3Vertex ne#Vertex)
+ getVertex8y/d(int id) . A3Vertex
+ isEmpty() . !oolean
+ get3ength() . int
Imp/eme&a!(& $4%
29
A3Vertex
5 int id
5 char la!el, color
5 Adj3ist a3ist 77A singly%lin&ed list of adjacent vertices
5 A3Vertexnext
+ A3Vertex(int id, char color, char la!el)
+ get1ext() . A3Vertex
+ set1ext(A3Vertexvertex)
+ get7set id, la!el, color
+ add9oAdj3ist(A3Vertex vertex, int #eight)
+ remove:romAdj3ist(int id)
+ getEdge"eight8y/d(int id)
Imp/eme&a!(& $*%
30
Adj3ist
5 AdjVertexhead
+ Adj3ist()
+ add(A3Vertex vertex, int #eight)
+ getVertex8y/d(int id) . AdjVertex
+ get"eight8y/d(int id) . int
+ isEmpty()
+ get3ength() . int
AdjVertex
5 A3Vertexvertex
5 int #eight
5 AdjVertexnext
+ AdjVertex(A3Vertexvertex, int
#eight)
+ get1ext() . AdjVertex
+ set1ext(AdjVertexvertex)
+ get"eight() . int
+ isVertex() . A3Vertex
Imp/eme&a!(& $+%
31
Adjacency list Adjacency matrix
Storage 2(;V;+;E;) 2(;V;
$
)
Add vertex 2(')
1um!er of vertices
are fixed- 2(;V;
$
)
Add edge 2(V) 2(')
<emove vertex 2(;E;)
1um!er of vertices
are fixed- 2(;V;
$
)
<emove edge 2(;E;) 2(')
Are vertices u, v
adjacent=
2(;V;
$
) 2(')
5r(6/ems re/ae0 ( Graph
Graph 9raversal
9opological Sort
Spanning Tree
*inimum Spanning Tree
Shortest >ath
32
Graph Tra7ersa/ $1%
! graph traversal algorithmtries to visit all
the nodes it can reach.
8f a graph is disconnected& a graph traversal
that 'egins at a node # "ill visit only a
su'set of nodes& that is& the connected
component containing #.
33
Graph Tra7ersa/ $2%
T"o 'asic graph traversal algorithms
Depth%first%search (D:S)
!fter visit node #& /%S strategy proceeds along a
path from # as deeply into the graph as possi'le
'efore 'ac+ing up.
Supplementary text!oo& , page ?@).
8readth%first%search (8:S)
!fter visit node #& <%S strategy visits every node
ad-acent to # 'efore visiting any other nodes.
Supplementary text!oo& , page *A(.
34
89S: 8rea0h;)!rs;sear2h $1%
Algorithm
Given a starting vertex s
#isit all vertices at increasing distance from s
8n the first stage& "e visit all the vertices that are at the
distance of one edge a#ay (level '). 0hen "e visit
there& "e paint as =visited>.
8n the second stage& "e visit all the ne" vertices "e can
reach at the distance of t#o edges a#ay (level $) from
the source vertex s. These ne" vertices& "hich are
adjacent to level ' vertices and not previously painted.
3
The <%S traversal terminates "hen every connected
vertex has 'een visited.
35
89S: 8rea0h;)!rs;sear2h $2%
Algorithm
To +eep trac+ of progress& <%S colors each vertex.
$ach vertex of the graph is in one of three states
1. .ndiscovered (color <lac+).
2. /iscovered 'ut not fully explored (color ?ello").
4. %ully explored (color ;ed).
! 1ueue @ is used to store all ?ello" vertices.
36
89S: 8rea0h;)!rs;sear2h $3%
37
Algorithm BFS(V,E,s):
Input: A graph with V & E are the set of vertices and
edges, s is the starting vertex.
Output: All connected vertices in V are visited.
For each v in V do Color[v]Black
Color[ s]Yellow
Q new empty queue
Q.Enqueue(s)
while Q is not empty do
u Q.Dequeue()
Visit u
Color[u] Red
For each v adjacent to u do
If Color[v] is Black then
Color[v] Yellow
Q.Enqueue(v)
endwhile
ode
89S: 8rea0h;)!rs;sear2h $4%
Start at !. Aut ! to the 1ueue (mar+ed ?ello")
@ueue !
38
F
A
B
G C
E
D
89S: 8rea0h;)!rs;sear2h $*%
Ta+e ! from @ueue& visit ! (mar+ed ;ed)
%ound t"o vertices < and $& that are ad-acent to !
Aut < B $ to 1ueue (mar+ed <B$ ?ello")
@ueue ! < $
39
A
B
G
C
E
D
F
89S: 8rea0h;)!rs;sear2h $+%
Ta+e < from @ueue& visit <& put G and C to 1ueue
@ueue ! < $ G C
40
A
B
G
C
E
D
F
89S: 8rea0h;)!rs;sear2h $,%
Ta+e $ from 1ueue& visit $. Aut /B% to the 1ueue
@ueue ! < $ G C / %
41
A
B
G
C
E
D
F
89S: 8rea0h;)!rs;sear2h $-%
Ta+e G from the 1ueue& visit G& nothing putted to the
1ueue.
Ta+e C from the 1ueue& visit C. % is an ad-acent node to
C& 'ut % is mar+ed as ?ello" already.
0e "onDt put % to the 1ueue.
@ueue ! < $ G C / %
42
A
B
G
C
E
D
F
89S: 8rea0h;)!rs;sear2h $.%
Ta+e / from the 1ueue& visit /. $ B % are ad-acent
nodes to /& 'ut they are mar+ed as ;ed B ?ello"
already. 0e "onDt put $$B% to the 1ueue.
Ta+e % from the 1ueue& visit %. / B $ are ad-acent
nodes to %& 'ut they are mar+ed as ;ed B?ello"
already.
@ueue ! < $ G C / %
43
A
B
G
C
E
D
F
89S: 8rea0h;)!rs;sear2h $10%
9ime complexity of 8:S
See supplementary text!oo&, page *AB.
Consider the graph GC(V,E)& "here # is the set of
vertices and $ is the set of edges.
<%S has the time complexity
2(;V;+;E;).
44
D9S: Deph;)!rs;sear2h $1%
/%S is a systematic "ay to find all the vertices
reacha'le from a source vertex s
$xplore every edge connected to s.
!s soon as discovering a vertex& /%S starts exploring
from it.
.nli+e <%S& "hich puts a vertex on a 1ueue so that it
explores from it later
Can trac+'ac+ and start over from a vertex as
necessary.
45
D9S: Deph;)!rs;sear2h $2%
D:S algorithm.
0ith a current vertex u
$xamining an edge (u,v) that connects u to v.
8f v is already painted& !ac& do#n to u and examine
another edge (u,v').
8f v is unpainted& pain v& consider v as the current
vertex. ;epeat the process a'ove.
! dead%end (dead%loc&) situation
!ll the edges from our current vertex u ta+es us to
painted vertices.
;epeat 'ac+ing do"n along the edge that 'rought us
here to vertex u. Try "ith another vertex.
46
D9S: Deph;)!rs;sear2h $3%
D:S algorithm.
To +eep trac+ of progress& /%S colors each vertex.
$ach vertex of the graph is in one of three states
1. .ndiscovered (color <lac+).
2. /iscovered 'ut not fully explored (color ?ello").
4. %ully explored (color ;ed).
!t the starting point& all vertices are painted to
<lac+.
47
D9S: Deph;)!rs;sear2h $4%
48
Algorithm DFS(u):
Input: u is the current vertex in a graph G=(V,E).
Output: All connected vertices in V are visited.
Color[u]Yellow
For each v adjacent to u do
If Color[v] is Black then
DFS(v)
Color[u] Red
ode
D9S: Deph;)!rs;sear2h $*%
Start "ith !.
Current vertex is !. ! is painted ?ello".
%ind an ad-acent node <
49
A
B
G
C
E
D
F
D9S: Deph;)!rs;sear2h $+%
Current <. < is painted ?ello".
$xpand !Ds ad-acent vertices. Aic+ one (<).
%ind an ad-acent node G.
50
A
B
G
C
E
D
F
D9S: Deph;)!rs;sear2h $,%
Current G. G is painted ?ello".
G has no ad-acent node. #isit G (painted
;ed) and go 'ac+ <.
%ind an ad-acent node C.
51
A
B
G
C
E
D
F
D9S: Deph;)!rs;sear2h $-%
Current C. C is painted ?ello".
%ind an ad-acent node %.
Current %. % is painted ?ello".
So on& / and $ is painted ?ello".
52
A
B
G
C
E
D
F
D9S: Deph;)!rs;sear2h $.%
Current $. %ind no <lac+ ad-acent node.
#isit $ (painted ;ed).
Go 'ac+ to /.
53
A
B
G
C
E
D
F
D9S: Deph;)!rs;sear2h $10%
/& %& C& <& ! is painted ;ed.
The visiting order is G& $& /& %& C& <& !.
54
A
B
G
C
E
D
F
D9S: Deph;)!rs;sear2h $11%
9ime complexity of D:S
Supplementary text!oo& , page ?@?.
Consider the graph GC(V,E)& "here # is the set of
vertices and $ is the set of edges.
/%S has the time complexity
2(;V;+;E;).
55
T(p(/("!2a/ (r0er $1%
Consider the prere1uisite structure for courses
1. $ach node x represents a course x.
$- (x, y) represents that course x is a prereDuisite to
course y.
4. This graph should 'e a directed graph "ithout
cycles.
56
!
"
e
#
a
T(p(/("!2a/ (r0er $2%
Consider the prere1uisite structure for courses
! linear order to ta+e all E courses "hile
satisfying all prere1uisites is called a topological
order.
$xample
a& c& '& e& d
c& a& '& e& d 57
!
"
e
#
a
T(p(/("!2a/ S(r $1%
Topological sort
2rdering of vertices in a directed graph such that
if there is a path from v
i
to v
j
then v
j
appears
after v
i
in the ordering.
!pplication scheduling -o's
$ach -o' is a vertex in a graph& and there is an
edge from x to y if -o' x must 'e completed
'efore -o' y can 'e done.
Topological sort gives the order in "hich to
perform the -o's.
58
T(p(/("!2a/ S(r $2%
Topological sorts example
F& E& 4& 11& G& 2& 1H& I
E& F& 4& G& 11& 2& I& 1H
E& F& 11& 2& 4& G& I& 1H
72
7 5 3
2
8
11
10 9
T(p(/("!2a/ S(r $3%
60
Algorithm TopoSort1()
Input: A graph G=(V,E).
Output: A topological order.
Do
For each v in V do
If v has no successor (no outgoing edges) then
Add v to the result set
Delete v from V (remove all edges come to v)
While V is not empty
ode
T(p(/("!2a/ S(r $4%
61
b
d
e
c
a
1. d has no
successor!
Choose d!
a
5. Choose a!
The topoog!ca order
!s
a b c e d
2. "oth b and e have
no successor!
Choose e!
b
e
c
a
3. "oth b and c
have no
successor!
Choose c!
b
c
a
4. #ny b has no
successor!
Choose b!
b
a
T(p(/("!2a/ S(r $*%
62
Algorithm TopoSort2()
Input: A graph G=(V,E).
Output: A topological order.
Do
For each v in V do
If v has no ancestor (no incoming edges) then
Add v to the result set
Delete v from V (remove all edges come to v)
While V is not empty
ode
T(p(/("!2a/ S(r $+%
63
b
d
e
c
a
1. a, c has no
ancestors!
Choose a!
d
5. Choose e!
The topoog!ca
order !s
a,b,c,e,d
2. "oth b and c have
no ancestors!
Choose b!
3. #ny c has no
ancestors!
Choose c!
4. #ny e has no
ancestors!
Choose !
b
d
e
c
d
e
c
d
e
T(p(/("!2a/ S(r $,%
0hat happens if graph has a cycleJ
Topological ordering is not possi'le
%or t"o vertices v B "& v precedes " and "
precedes v
Topological sorts can have more than one
ordering
64
1 2
3
Every edge has an
incoming vertex so
topological sort can
not be performed
Tu(r!a/ < &e3 (p!2
>reparing for the tutorial.
Aractice "ith examples and exercises in
"ee+11KtutorialKinstruction
>reparing for next topic.
;ead text'oo+ chapter I (I.1 L I.4) Graph
algorithms.
;ead supplementary 'oo+ chapter 22& 26 and
chapter 2E
65
66
Any
Questions?

Vous aimerez peut-être aussi