0% found this document useful (0 votes)
7 views

Lecture 10 Introduction To Graph

Uploaded by

An Nguyễn
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lecture 10 Introduction To Graph

Uploaded by

An Nguyễn
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

Introduction to graphs

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

G = (V, E) is a weighted graph if each edge (u,v)∈E is


assigned a weight
➢ Example of weighted graph?
➢ Example of unweighted graph?
8
Graph Presentation

G = (V, E); V = {0, 1,…, n-1}


➢ Present a graph by an adjacency matrix
❖ A[u][v] = 1 if (u,v) ∈ E
❖ A[u][v] = 0 Otherwise
0 1 2 3 4

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

G = (V, E); V = {0, 1,…, n-1}

➢ Present a graph by edge list structure

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

➢ A connected component of Connected graph


a graph G is a maximal
connected subgraph of G

Non connected graph with two


connected components 12
Unrooted trees
An unrooted tree is an
undirected graph T such that
❖ T is connected
❖ T has no cycles Tree

Note: This definition of tree


is different from the one of a
rooted tree

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

➢ Breadth-first search (BFS) is a


general technique for traversing a
graph

➢ if vertex u is visited before


vertex v, then adjacent vertices of
u will be visited before adjacent
vertices of v

➢ A BFS traversal is used to


determine whether G is
connected

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

Take Node 1 as start

19
Breadth-First Search

1
2
5

4 6

Node 2 is visited next as it was unvisited and at distance of one edge

20
Breadth-First Search

1
2
5

4 6

Node 5 is visited next as it was unvisited and at


distance of 1 edge from node 1

21
Breadth-First Search

1
2
5

4 6

Node 3 is visited next as it was unvisited and at


distance of 1 edge from node 2

22
Breadth-First Search

1
2
5

4 6

Node 4 is visited next as it was unvisited and at


distance of 1 edge from node 2

23
Breadth-First Search

1
2
5

4 6

Node 6 is visited next as it was unvisited and at


distance of 1 edge from node 5

24
Breadth-First Search
BreadthFirstSearch (G, s) { 1
2 31 65
(1) Set a queue Q empty;
(3) enqueue s onto Q; 4 82

(2) Mark s as visited;


S 2 54 76
(4) while (Q not empty) {
(5) w = dequeue Q;
(6) for (each u adjacent w) Start with all White vertices
except s
(7) if ( u not visited) {
(8) visit and enqueue u onto Q;
(9) mark u as visited Q= S
}
}

25
Breadth-First Search
BreadthFirstSearch (G, s) { 1
2 31 65
(1) Set a queue Q empty;
(3) enqueue s onto Q; 4 82

(2) ark s as visited;


S 2 54 76
(4) while (Q not empty) {
(5) w = dequeue Q;
(6) for (each u adjacent w) After first time through loop
(7) if ( u not visited) {
(8) visit and enqueue u onto Q;
(9) mark u as visited Q= 1 2
}
}
}
26
Breadth-First Search
BreadthFirstSearch (G, s) { 1
2 31 65
(1) Set a queue Q empty;
(3) enqueue s onto Q; 4 82

(2) ark s as visited;


S 2 54 76
(4) while (Q not empty) {
(5) w = dequeue Q;
(6) for (each u adjacent w) After second time through
loop
(7) if ( u not visited) {
(8) visit and enqueue u onto Q;
(9) mark u as visited Q= 2 3
}
}

27
Breadth-First Search
BreadthFirstSearch (G, s) { 1
2 31 65
(1) Set a queue Q empty;
(3) enqueue s onto Q; 4 82

(2) ark s as visited;


S 2 54 76
(4) while (Q not empty) {
(5) w = dequeue Q;
(6) for (each u adjacent w) After third time through loop
(7) if ( u not visited) {
(8) visit and enqueue u onto Q;
(9) mark u as visited Q= 3 4 5
}
}

28
Breadth-First Search
BreadthFirstSearch (G, s) { 1
2 31 65
(1) Set a queue Q empty;
(3) enqueue s onto Q; 4 82

(2) Mark s as visited;


S 2 54 76
(4) while (Q not empty) {
(5) w = dequeue Q;
(6) for (each u adjacent w) After fourht time through
loop
(7) if ( u not visited) {
(8) visit and enqueue u onto Q;
(9) mark u as visited Q= 4 5 6
}
}
}
29
Breadth-First Search
// Travel on G=(V, E) by BFS
BreadthFirstSearch_traversal (G) {
(10) for (each v ∈V)
(11) mark v as unvisited;
(12) for (each v ∈V)
(13) if (v not visited)
(14) BreadthFirstSearch(v);
}

Complexity: BFS on a graph with n vertices and m edges takes O(n + m )


time

30
Exercises

➢ Use the BFS to determine the number of connected components.

➢ 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

➢ DFS starts from a node and explores 2 7 8


as far as possible along each branch
before backtracking. 1
3 6 9
2
➢ Applications:
❖ Scheduling jobs 1 1
4 5
0 1
❖ 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 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);
}

DFS on a graph with n vertices and m edges takes O(n + m ) time

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

Use the DFS to determine the number of connected components

36

You might also like