Professional Documents
Culture Documents
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
18 4
SFO D 9 14
3 80 2
4 LGA
17
33
2
7
5
Terminology (cont.)
➢ Path
❖ sequence of alternating V
vertices and edges a b
❖ begins with a vertex P
❖ ends with a vertex
U
d X
1 Z
❖ each edge is preceded and
followed by its endpoints
P h
c e
➢ Simple path 2
❖ path such that all its W g
vertices and edges are
distinct f
➢ Examples Y
❖ P1=(V,b,X,h,Z) is a simple
path
❖ P2=(U,c,W,e,X,g,Y,f,W,d,V) 6
is a path that is not simple
Terminology (cont.)
➢ Cycle
❖ circular sequence of V
alternating vertices and a b
edges
❖ each edge is preceded and
U
d X Z
followed by its endpoints C h
➢ Simple cycle e
c 2 C
❖ cycle such that all its W
vertices and edges are g1
distinct
➢ Examples f
Y
❖ C1=(V,b,X,g,Y,f,W,c,U,a,↵)
is a simple cycle
❖ C2=(U,c,W,e,X,g,Y,f,W,d,V,a, 7
↵) is a cycle that is not
Weighted and unweighted
graphs
5
7
2
3
9 3
5 4 7
1
2
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 Subgraph
G
❖ The edges of S are a
subset of the edges of
G
➢ A spanning subgraph
of G is a subgraph that
contains all the Spanning subgraph
vertices of G 11
Connectivity
➢ A graph is connected
if there is a path
between every pair
of vertices
Connected graph
➢ A connected
component of a
graph G is a maximal
connected subgraph
of G
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 Graph
➢ Spanning trees have
applications to the
design of 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
18
Breadth-First Search
1
2
5
4 6
Take Node 1 as
start
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 3
1 6
5
(1) Set a queue Q empty;
(3) enqueue s onto Q; 4 8
2
25
Breadth-First Search
BreadthFirstSearch (G, s) { 1
2 3
1 6
5
(1) Set a queue Q empty;
(3) enqueue s onto Q; 4 8
2
27
Breadth-First Search
BreadthFirstSearch (G, s) { 1
2 3
1 6
5
(1) Set a queue Q empty;
(3) enqueue s onto Q; 4 8
2
28
Breadth-First Search
BreadthFirstSearch (G, s) { 1
2 3
1 6
5
(1) Set a queue Q empty;
(3) enqueue s onto Q; 4 8
2
30
Exercises
31
Depth-First Search
➢ Depth-first search (DFS) is a 1
general technique for
traversing a graph
2 7 8
➢ DFS starts from a node and
explores as far as possible 1
along each branch before 3 6 9
2
backtracking.
1 1
4 5
➢ Applications: 0 1
❖ Scheduling jobs
❖ Check if a network is
connected
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 visited; 1
3 6 9
DepthFirstSearch (u); 2
} 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