Lecture 10 Introduction To Graph
Lecture 10 Introduction To Graph
Lê Sỹ Vinh
Computational Science and Engineering
Email: [email protected]
1
Example
➢ Computer network
➢ Facebook friend network
➢ Road map (Google map)
➢ Airlines routes
➢ Family trees
2
Graphs
➢ A graph is a pair (V, E), where
❖ V is a set of nodes, called vertices
❖ E is a collection of pairs of vertices, called edges
❖ Vertices and edges are positions and store elements
➢ Example:
❖ A vertex represents an airport and stores the three-letter airport code
❖ An edge represents a flight route between two airports and stores the mileage
of the route
OR 84 PVD
1 8 4
SFO D 9 14
3 80 2
4 LGA
17
33
2
7
HNL 255 3 1 38 9
10
9
5 LAX
1 23 7 11
DF 20
3 W
MIA 3
Edge Types
➢ Directed edge OR flight
❖ ordered pair of vertices (u,v) PVD
first vertex u is the origin
D AA 1206
❖
❖ second vertex v is the destination
❖ e.g., a flight
➢ Undirected edge OR 849
PVD
❖ unordered pair of vertices (u,v) D mile
❖ e.g., a flight route s
➢ Directed graph
❖ all the edges are directed
❖ e.g., route network
➢ Undirected graph
❖ all the edges are undirected
❖ e.g., flight network
4
Terminology
➢ End vertices (or endpoints) of an
edge V
❖ U and V are the endpoints of a
a b
j
➢ Edges incident on a vertex h
❖ a, d, and b are incident on V U d X Z
➢ Adjacent vertices c e
❖ U and V are adjacent
➢ Degree of a vertex W g
❖ X has degree 4
➢ Self-loop f
❖ j is a self-loop Y
5
Terminology (cont.)
➢ Path
❖ sequence of alternating vertices V
and edges a b
❖ begins with a vertex P
❖ ends with a vertex
U
d X
1 Z
❖ each edge is preceded and P h
followed by its endpoints c e
➢ Simple path 2
❖ path such that all its vertices and W g
edges are distinct
➢ Examples f
❖ P1=(V,b,X,h,Z) is a simple path Y
❖ P2=(U,c,W,e,X,g,Y,f,W,d,V) is a
path that is not simple
6
Terminology (cont.)
➢ Cycle
❖ circular sequence of alternating V
vertices and edges a b
❖ each edge is preceded and
followed by its endpoints
U
d X Z
➢ Simple cycle C h
❖ cycle such that all its vertices and e C
edges are distinct c 2
W g1
➢ Examples
❖ C1=(V,b,X,g,Y,f,W,c,U,a,↵) is a
f
simple cycle Y
❖ C2=(U,c,W,e,X,g,Y,f,W,d,V,a,↵) is
a cycle that is not simple
7
Weighted and unweighted graphs
5
7
2
3
9 3
5 4 7
12
0 1 0 0 1 1 0 0
1 0 0 1 0 1
2
2 0 0 0 1 1
3 1 0 0 0 0
3 4
4 0 0 0 1 0
9
Graph presentation
0 1 3 .
1 2 4 .
2 0 3 4 .
3
4 3 .
10
Subgraphs
➢ A subgraph S of a graph G
is a graph such that
❖ The vertices of S are a subset
of the vertices of G
❖ The edges of S are a subset of Subgraph
the edges of G
➢ A spanning subgraph of G is
a subgraph that contains all
the vertices of G
Spanning subgraph
11
Connectivity
➢ A graph is connected if
there is a path between
every pair of vertices
Forest
13
Spanning Trees and Forests
➢ A spanning tree of a connected
graph is a spanning subgraph
that is a tree
➢ A spanning tree is not unique
unless the graph is a tree
➢ Spanning trees have
applications to the design of Graph
communication networks
Spanning tree
14
Exercise
➢Represent the following graph by matrix
and linked lists. Determine the number of
connected components
G2
G1 G3
15
Breadth-First Search
L0
A
L1
B C D
L2
E F
16
Breadth-First Search
17
Breadth-First Search
3 3
➢ A BFS traversal is used to 3
determine whether G is connected 3
2
➢ BFS can be used to find a path 2 2
with the minimum number of 3
3 4
edges between two given vertices 4 3 2 1 2
➢ Applications:
3 3 4
❖ Crawlers in search engines 2
2
❖ Social network website 2 3
3
3
3 3
4 Shortest path to goal
Goal
18
Breadth-First Search
1
2
5
4 6
19
Breadth-First Search
1
2
5
4 6
20
Breadth-First Search
1
2
5
4 6
21
Breadth-First Search
1
2
5
4 6
22
Breadth-First Search
1
2
5
4 6
23
Breadth-First Search
1
2
5
4 6
24
Breadth-First Search
BreadthFirstSearch (G, s) { 1
2 31 65
(1) Set a queue Q empty;
(3) enqueue s onto Q; 4 82
25
Breadth-First Search
BreadthFirstSearch (G, s) { 1
2 31 65
(1) Set a queue Q empty;
(3) enqueue s onto Q; 4 82
27
Breadth-First Search
BreadthFirstSearch (G, s) { 1
2 31 65
(1) Set a queue Q empty;
(3) enqueue s onto Q; 4 82
28
Breadth-First Search
BreadthFirstSearch (G, s) { 1
2 31 65
(1) Set a queue Q empty;
(3) enqueue s onto Q; 4 82
30
Exercises
➢ Use the BFS to find the path with minimum number of edges
between the vertices a and b. Consider that the set of vertices is
alphabetically ordered.
31
Depth-First Search
➢ Depth-first search (DFS) is a general 1
technique for traversing a graph
32
Depth-First Search
1
//Depth first search from vertex v
DepthFirstSearch (v) {
for (each u adjacent v) 2 7 8
if (u not visited) {
visit and mark u as 1
3 6 9
visited; 2
DepthFirstSearch (u);
1 1
} 4 5
0 1
}
33
Depth-First Search
//Travel on G=(V, E) by DFS
DepthFirstSearch_traversal (G) {
(10) for (each v ∈V)
(11) mark v as unvisted;
(12) for (each v ∈V)
(13) if (v not visited)
(14) DepthFirstSearch(v);
}
34
Travel on graphs
BFS DFS
1 1
2 3 4 2 7 8
1
5 6 7 8 3 6 9
2
1 1 1 1 1
9 4 5
0 1 2 0 1
35
Exercises
36