Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

12 Graph

Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

Design and Analysis of Algorithms

Graph
Dr. Lê Nguyên Khôi
The VNU University of Engineering and Technology
Contents

 Definition
 Terminology
 Representation
 Traversal
 Topological sort

Design and Analysis of Algorithms 1


Definition
Graph consist of
 Set of vertices
 Set of edges
 Undirected graph
 Unordered pair of vertices
 Directed graph
 Ordered pair of vertices
Both cases,
if is connected,

Design and Analysis of Algorithms 2


Terminology

 Directed
 Weighted
 Connected
 Cycle
 Network

Design and Analysis of Algorithms 3


Terminology

undirected directed
Cầu Giấy Cầu Giấy

ĐHQG BX Kim Mã ĐHQG BX Kim Mã

Ngã tư Sở Ngã tư Sở

Design and Analysis of Algorithms 4


Terminology

unweighted weighted
Cầu Giấy 5 Cầu Giấy 7

ĐHQG BX Kim Mã ĐHQG BX Kim Mã

11 15

Ngã tư Sở Ngã tư Sở

Design and Analysis of Algorithms 5


Terminology

connected disconnected

Design and Analysis of Algorithms 6


Terminology

cycle no cycle

Design and Analysis of Algorithms 7


Network

 Transportation
 Communication
 Information
 Social
 Dependency

Design and Analysis of Algorithms 8


Transportation

PVD
ORD
SFO
LGA

HNL
LAX
DFW
MIA

Design and Analysis of Algorithms 9


Communication

Design and Analysis of Algorithms 10


Information

Design and Analysis of Algorithms 11


Social

Design and Analysis of Algorithms 12


Dependency

Design and Analysis of Algorithms 13


Representation

 Adjacency list
 One-dimensional array of lists
 Each list for a vertex consists of
Node such that
 Adjacency matrix
 Two-dimensional array
 Each vertex is numbered
Value of 1 indicates that

Design and Analysis of Algorithms 14


Adjacency List Representation

 An adjacency list of a vertex is the


list of vertices adjacent to

Design and Analysis of Algorithms 15


Adjacency Matrix Representation
 The adjacency matrix of a graph ,
where , is the matrix
given by

Design and Analysis of Algorithms 16


Representation

 Adjacency list
 Sparse graph
 is much smaller than
 Determine edges reached from
 Adjacency matrix
 Dense graph
 is close to
 Determine edge
 For both directed and undirected graphs

Design and Analysis of Algorithms 17


Traversal

 Determine the structure of a graph


 Visit all vertices of
 Each vertex is visited once
 Use all edges in
 02 methods
 Breath First Search – BFS
 Depth First Search – DFS

Design and Analysis of Algorithms 18


Breath First Search

Design and Analysis of Algorithms 19


Breath First Search

 From vertex , visit all vertices adjacent


to vertex which are not visited
 Then, the vertex is visited first, its
adjacent vertices are visited first
 Continue until all vertices are visited

Design and Analysis of Algorithms 20


Breath First Search – Pseudocode
Algorithm BFS(u)
Input: vertex u unvisited
Initialize an empty queue Q
Mark u visited
Q.enqueue(u)
while Q.empty() ≠ TRUE
v  Q.dequeue()
for (each w adjacent to v)
if (w is not visited)
Mark w is visited
Q.enqueue(w)

Design and Analysis of Algorithms 21


Breath First Search – Analysis

 Operation on the queue:


 Vertices are enqueued/dequeued only once
 Operation on the adjacency list:
 When vertices are dequeued
 Only once
 The length of the list
 Initialize:
 Mark all vertices unvisited
 Complexity:

Design and Analysis of Algorithms 22


Breath First Search – Application

 Determine if there is a path from to


 Check the connectivity of the graph
 Determine connected sub-graphs

Design and Analysis of Algorithms 23


Depth First Search

Design and Analysis of Algorithms 24


Depth First Search

 From visit adjacent to


 From visit adjacent to
 Continue until
 Reach which has no vertex to visit
 Go back to previous , visit other of
 Continue until all vertices are visited

Design and Analysis of Algorithms 25


Depth First Search – Pseudocode
Algorithm DFS(u)
Input: Vertex u unvisited
Mark u visited
for (each v adjacent to u)
if (v is not visited)
Mark v visited
DFS(v)

Design and Analysis of Algorithms 26


Depth First Search – Application

 Determine cycles

Design and Analysis of Algorithms 27


Topological Sort

 Relationship of a set of objects could be


represented by directed no cycle graph
 Sequential order relationship
 Timing relationship between tasks in the
scheme
 Timing relationships between subjects
in a curriculum

Design and Analysis of Algorithms 28


Topological Sort

LTNC
Toán
cao
cấp

LTHĐT CTDL
&GT

Trí tuệ LTTT


nhân
tạo

Design and Analysis of Algorithms 29


Sắp Xếp Topo

 là đồ thị định hướng không chu trình


 Sắp xếp các đỉnh đồ thị thành một danh sách
 Sao cho nếu có cung thì cần đứng
trước trong danh sách đó
a b

(a, c, b, d, e, f)
or
c d
(a, b, d, c, e, f)

e f

Design and Analysis of Algorithms 30


Topological Sort
DFS(a)
a b DFS(c)
DFS(e)
L = (e)
L = (e, c)
DFS(d)
c d DFS(f)
L = (e, c, f)
L = (e, c, f, d)
L = (e, c, f, d, a)
DFS(b)
e f L = (e, c, f, d, a, b)
L = (b, a, d, f, c, e)

Design and Analysis of Algorithms 31


Topological Sort

 Topological sort based on depth first


search
 Perform depth first search
 When finish the depth first search on
each vertex , then insert to the end of
the list
 When finish the depth first search on the
graph, reverse that list to obtain the
topological sort

Design and Analysis of Algorithms 32

You might also like