You are on page 1of 44

Graph

Tran Ngoc Bao Duy

Graph representation and Graph representation

elementary algorithms Adjacency-list representation


Adjacency-matrix representation
Weighted graphs

Searching a graph
Breadth-first search
Depth-first search
Data Structures and Algorithms
Topological sorting

Tran Ngoc Bao Duy


Faculty of Computer Science and Engineering
Ho Chi Minh University of Technology, VNU-HCM

Graph.1
Graph
Overview
Tran Ngoc Bao Duy

1 Graph representation
Graph representation
Adjacency-list representation Adjacency-list representation

Adjacency-matrix representation Adjacency-matrix representation


Weighted graphs

Weighted graphs Searching a graph


Breadth-first search
Depth-first search

Topological sorting
2 Searching a graph
Breadth-first search
Depth-first search

3 Topological sorting

Graph.2
Graph

Tran Ngoc Bao Duy

Graph representation
Adjacency-list representation
Adjacency-matrix representation

GRAPH Weighted graphs

Searching a graph
Breadth-first search

REPRESENTATION
Depth-first search

Topological sorting

Graph.3
Graph
Graph representation
Tran Ngoc Bao Duy

Two standard ways to represent a graph G = (V, E) apply Graph representation


Adjacency-list representation
to both directed and undirected graphs: Adjacency-matrix representation
Weighted graphs

1 Adjacency-list: provides a compact way to represent Searching a graph


sparse graph (|E|  |V |2 ). Breadth-first search
Depth-first search

Topological sorting

Graph.4
Graph
Graph representation
Tran Ngoc Bao Duy

Two standard ways to represent a graph G = (V, E) apply Graph representation


Adjacency-list representation
to both directed and undirected graphs: Adjacency-matrix representation
Weighted graphs

1 Adjacency-list: provides a compact way to represent Searching a graph


sparse graph (|E|  |V |2 ). Breadth-first search
Depth-first search

Topological sorting

2 Adjacency-matrix: preferred when the graph is dense


(|E| ≈ |V |2 ) or when need to tell quickly if there is an
edge connecting two given vertices.

Graph.4
Graph
Adjacency-list representation
Tran Ngoc Bao Duy

Graph representation
Definition Adjacency-list representation
Adjacency-matrix representation
The adjacency-list representation of a graph G = (V, E): Weighted graphs

• Consists of an array Adj of |V | lists, one for each vertex Searching a graph
Breadth-first search
in V . Depth-first search

Topological sorting

Graph.5
Graph
Adjacency-list representation
Tran Ngoc Bao Duy

Graph representation
Definition Adjacency-list representation
Adjacency-matrix representation
The adjacency-list representation of a graph G = (V, E): Weighted graphs

• Consists of an array Adj of |V | lists, one for each vertex Searching a graph
Breadth-first search
in V . Depth-first search

Topological sorting
• For each u ∈ V , the adjacency list Adj[u] contains all
the vertices such that there is an edge (u, v) ∈ E or all
the vertices adjacent to u in G.

Graph.5
Graph
Adjacency-list representation
Tran Ngoc Bao Duy

Definition
The adjacency-list representation of a graph G = (V, E):
• Consists of an array Adj of |V | lists, one for each vertex Graph representation
Adjacency-list representation
in V . Adjacency-matrix representation
Weighted graphs

• For each u ∈ V , the adjacency list Adj[u] contains all Searching a graph
Breadth-first search

the vertices such that there is an edge (u, v) ∈ E or all Depth-first search

the vertices adjacent to u in G. Topological sorting

(Source: Introduction to algorithms - 3rd edition)

Graph.5
Graph
Adjacency-list representation
Tran Ngoc Bao Duy

Definition
The adjacency-list representation of a graph G = (V, E):
• Consists of an array Adj of |V | lists, one for each vertex Graph representation
in V . Adjacency-list representation
Adjacency-matrix representation
Weighted graphs

• For each u ∈ V , the adjacency list Adj[u] contains all Searching a graph
Breadth-first search
the vertices such that there is an edge (u, v) ∈ E or all Depth-first search

the vertices adjacent to u in G. Topological sorting

(Source: Introduction to algorithms - 3rd edition)

Graph.5
Graph
Adjacency-list: Properties
Tran Ngoc Bao Duy

1 If G is a directed graph, the sum of the lengths of all Graph representation

the adjacency lists is |E|. Adjacency-list representation


Adjacency-matrix representation
Weighted graphs
2 If G is a undirected graph, the sum of the lengths of Searching a graph
all the adjacency lists is 2 |E|. Breadth-first search
Depth-first search

3 The amount of memory required for both cases of graph Topological sorting
is Θ(V + E).

Graph.6
Graph
Adjacency-list: Properties
Tran Ngoc Bao Duy

1 If G is a directed graph, the sum of the lengths of all Graph representation

the adjacency lists is |E|. Adjacency-list representation


Adjacency-matrix representation
Weighted graphs
2 If G is a undirected graph, the sum of the lengths of Searching a graph
all the adjacency lists is 2 |E|. Breadth-first search
Depth-first search

3 The amount of memory required for both cases of graph Topological sorting
is Θ(V + E).
Disadvantages: provides no quicker way to determine whether
a given edge (u, v) is present in the graph than to search
for in the adjacency list Adj[u].

Graph.6
Graph
Adjacency-matrix representation
Tran Ngoc Bao Duy

Definition Graph representation


Adjacency-list representation

Assume that the vertices are numbered 1, 2, . . . , |V | in some Adjacency-matrix representation


Weighted graphs

arbitrary manner, the adjacency-matrix representation of Searching a graph


a graph G = (V, E) consists of a |V |×|V | matrix A = (aij ) Breadth-first search
Depth-first search

such that Topological sorting



1 if (i, j) ∈ E
aij =
0 otherwise

Graph.7
Graph
Adjacency-matrix representation
Tran Ngoc Bao Duy

Definition Graph representation


Adjacency-list representation

Assume that the vertices are numbered 1, 2, . . . , |V | in some Adjacency-matrix representation


Weighted graphs

arbitrary manner, the adjacency-matrix representation of Searching a graph


a graph G = (V, E) consists of a |V |×|V | matrix A = (aij ) Breadth-first search
Depth-first search

such that Topological sorting



1 if (i, j) ∈ E
aij =
0 otherwise

Graph.7
Graph
Adjacency-matrix representation
Tran Ngoc Bao Duy

Definition
Assume that the vertices are numbered 1, 2, . . . , |V | in some
arbitrary manner, the adjacency-matrix representation of
Graph representation
a graph G = (V, E) consists of a |V |×|V | matrix A = (aij ) Adjacency-list representation

such that Adjacency-matrix representation


Weighted graphs
 Searching a graph
1 if (i, j) ∈ E
aij = Breadth-first search
Depth-first search
0 otherwise
Topological sorting

(Source: Introduction to algorithms - 3rd edition)


Graph.7
Graph
Adjacency-matrix representation
Tran Ngoc Bao Duy

Definition
Assume that the vertices are numbered 1, 2, . . . , |V | in some
arbitrary manner, the adjacency-matrix representation of
Graph representation
a graph G = (V, E) consists of a |V |×|V | matrix A = (aij ) Adjacency-list representation

such that Adjacency-matrix representation


Weighted graphs

 Searching a graph
1 if (i, j) ∈ E
aij = Breadth-first search
Depth-first search
0 otherwise
Topological sorting

(Source: Introduction to algorithms - 3rd edition)


Graph.7
Graph
Adjacency-matrix: Properties
Tran Ngoc Bao Duy

Graph representation
Adjacency-list representation

1 Requires Θ(V 2 ) memory, independent of the number Adjacency-matrix representation


Weighted graphs

of edges in the graph. Searching a graph


Breadth-first search
Depth-first search
2 Along the main diagonal of the adjacency matrix, there Topological sorting
is a symmetry such that the adjacency matrix A of an
undirected graph is its own transpose: A = AT .

Graph.8
Graph
Weighted graphs
Tran Ngoc Bao Duy

To represent weighted graphs, that is, graphs for which


Graph representation
each edge has an associated weight, typically given by a Adjacency-list representation
Adjacency-matrix representation
weight function w : E → R. Weighted graphs

• Adjacency-list: store the weight w(u, v) of the edge Searching a graph


Breadth-first search

(u, v) ∈ E with vertex v in u’s adjacency list. Depth-first search

Topological sorting

• Adjacency-matrix: store the weight w(u, v) of the edge


(u, v) ∈ E as the entry in row u and column v of the
adjacency matrix. If an edge does not exist, it stores
NIL, 0 or ∞ up to your convenience.

Graph.9
Graph
Comparision
Tran Ngoc Bao Duy

Graph representation
Adjacency-list representation
Adjacency-matrix representation

1 Adjacency-list: requires less memory, required simply Weighted graphs

Searching a graph
by most of the graph algorithms. Breadth-first search
Depth-first search

2 Adjacency-matrix: simpler and preferred when graphs Topological sorting

are reasonably small, requires only one bit per entry.

Graph.10
Graph

Tran Ngoc Bao Duy

Graph representation
Adjacency-list representation
Adjacency-matrix representation

SEARCHING Weighted graphs

Searching a graph
Breadth-first search

A GRAPH
Depth-first search

Topological sorting

Graph.11
Graph
Searching a graph
Tran Ngoc Bao Duy

Searching a graph means systematically following the edges Graph representation


of the graph so as to visit the vertices of the graph. Adjacency-list representation
Adjacency-matrix representation

• A graph-searching algorithm can discover much about Weighted graphs

Searching a graph
the structure of a graph. Breadth-first search
Depth-first search

Topological sorting

Graph.12
Graph
Searching a graph
Tran Ngoc Bao Duy

Searching a graph means systematically following the edges Graph representation


of the graph so as to visit the vertices of the graph. Adjacency-list representation
Adjacency-matrix representation

• A graph-searching algorithm can discover much about Weighted graphs

Searching a graph
the structure of a graph. Breadth-first search
Depth-first search

• Many algorithms begin by searching their input graph Topological sorting

to obtain this structural information.

Graph.12
Graph
Searching a graph
Tran Ngoc Bao Duy

Searching a graph means systematically following the edges Graph representation


of the graph so as to visit the vertices of the graph. Adjacency-list representation
Adjacency-matrix representation

• A graph-searching algorithm can discover much about Weighted graphs

Searching a graph
the structure of a graph. Breadth-first search
Depth-first search

• Many algorithms begin by searching their input graph Topological sorting

to obtain this structural information.

• Techniques for searching a graph lie at the heart of the


field of graph algorithms.

Graph.12
Graph
Breadth-first search
Tran Ngoc Bao Duy

Definition
• Given a graph G = (V, E) and a distinguished source
vertex s, breadth-first search systematically explores Graph representation

the edges of G to discover every vertex that is reachable Adjacency-list representation


Adjacency-matrix representation

from s by computing the distance (smallest number of Weighted graphs

Searching a graph
edges) from s to each reachable vertex. Breadth-first search

• It also produces a breadth-first tree with root s that Depth-first search

Topological sorting
contains all reachable vertices.

Graph.13
Graph
Breadth-first search
Tran Ngoc Bao Duy

Definition
• Given a graph G = (V, E) and a distinguished source
vertex s, breadth-first search systematically explores Graph representation

the edges of G to discover every vertex that is reachable Adjacency-list representation


Adjacency-matrix representation

from s by computing the distance (smallest number of Weighted graphs

Searching a graph
edges) from s to each reachable vertex. Breadth-first search

• It also produces a breadth-first tree with root s that Depth-first search

Topological sorting
contains all reachable vertices.

Properties:
1 For any vertex reachable from s, the simple path in the
breadth-first tree from s to corresponds to a shortest
path from s to in G, that is, a path containing the
smallest number of edges.

Graph.13
Graph
Breadth-first search
Tran Ngoc Bao Duy

Definition
• Given a graph G = (V, E) and a distinguished source
vertex s, breadth-first search systematically explores Graph representation

the edges of G to discover every vertex that is reachable Adjacency-list representation


Adjacency-matrix representation

from s by computing the distance (smallest number of Weighted graphs

Searching a graph
edges) from s to each reachable vertex. Breadth-first search

• It also produces a breadth-first tree with root s that Depth-first search

Topological sorting
contains all reachable vertices.

Properties:
1 For any vertex reachable from s, the simple path in the
breadth-first tree from s to corresponds to a shortest
path from s to in G, that is, a path containing the
smallest number of edges.
2 Works on both directed and undirected graphs.
Graph.13
Graph
Breadth-first search: Pseudocode
Tran Ngoc Bao Duy

Graph representation
Adjacency-list representation
Adjacency-matrix representation
Weighted graphs

Searching a graph
Breadth-first search
Depth-first search

Topological sorting

Graph.14
Graph
Breadth-first search
Tran Ngoc Bao Duy

Graph representation
Adjacency-list representation
Adjacency-matrix representation
Weighted graphs

Searching a graph
Breadth-first search
Depth-first search

Topological sorting

(Source: Introduction to algorithms - 3rd edition)


Graph.15
Graph
Shortest path between two vertices
Tran Ngoc Bao Duy

Assuming that BFS has already computed, the following Graph representation
Adjacency-list representation

procedure prints out the vertices on a shortest path from s Adjacency-matrix representation
Weighted graphs
to v: Searching a graph
Breadth-first search
Depth-first search

Topological sorting

Graph.16
Graph
Depth-first search
Tran Ngoc Bao Duy

Depth-first search is the strategy to search deeper in the


graph whenever possible.
1 Explores edges out of the most recently discovered ver- Graph representation
tex v that still has unexplored edges leaving it. Adjacency-list representation
Adjacency-matrix representation
Weighted graphs

Searching a graph
Breadth-first search
Depth-first search

Topological sorting

Graph.17
Graph
Depth-first search
Tran Ngoc Bao Duy

Depth-first search is the strategy to search deeper in the


graph whenever possible.
1 Explores edges out of the most recently discovered ver- Graph representation
tex v that still has unexplored edges leaving it. Adjacency-list representation
Adjacency-matrix representation

2 Once all of v’s edges have been explored, the search Weighted graphs

Searching a graph
backtracks to explore edges leaving the vertex from Breadth-first search

which v was discovered. Depth-first search

Topological sorting

Graph.17
Graph
Depth-first search
Tran Ngoc Bao Duy

Depth-first search is the strategy to search deeper in the


graph whenever possible.
1 Explores edges out of the most recently discovered ver- Graph representation
tex v that still has unexplored edges leaving it. Adjacency-list representation
Adjacency-matrix representation

2 Once all of v’s edges have been explored, the search Weighted graphs

Searching a graph
backtracks to explore edges leaving the vertex from Breadth-first search

which v was discovered. Depth-first search

Topological sorting
3 Continues until all the vertices that are reachable from
the original source vertex discovered.

Graph.17
Graph
Depth-first search
Tran Ngoc Bao Duy

Depth-first search is the strategy to search deeper in the


graph whenever possible.
1 Explores edges out of the most recently discovered ver- Graph representation
tex v that still has unexplored edges leaving it. Adjacency-list representation
Adjacency-matrix representation

2 Once all of v’s edges have been explored, the search Weighted graphs

Searching a graph
backtracks to explore edges leaving the vertex from Breadth-first search

which v was discovered. Depth-first search

Topological sorting
3 Continues until all the vertices that are reachable from
the original source vertex discovered.
4 If any undiscovered vertices remain, then depth-first
search selects one of them as a new source, and it
repeats the search from that source.

Graph.17
Graph
Depth-first search
Tran Ngoc Bao Duy

Depth-first search is the strategy to search deeper in the


graph whenever possible.
1 Explores edges out of the most recently discovered ver- Graph representation
tex v that still has unexplored edges leaving it. Adjacency-list representation
Adjacency-matrix representation

2 Once all of v’s edges have been explored, the search Weighted graphs

Searching a graph
backtracks to explore edges leaving the vertex from Breadth-first search

which v was discovered. Depth-first search

Topological sorting
3 Continues until all the vertices that are reachable from
the original source vertex discovered.
4 If any undiscovered vertices remain, then depth-first
search selects one of them as a new source, and it
repeats the search from that source.
5 The algorithm repeats this entire process until it has
discovered every vertex.

Graph.17
Graph
Depth-first search: Pseudocode
Tran Ngoc Bao Duy

Graph representation
Adjacency-list representation
Adjacency-matrix representation
Weighted graphs

Searching a graph
Breadth-first search
Depth-first search

Topological sorting

Graph.18
Graph
Depth-first search
Tran Ngoc Bao Duy

Graph representation
Adjacency-list representation
Adjacency-matrix representation
Weighted graphs

Searching a graph
Breadth-first search
Depth-first search

Topological sorting

Graph.19
Graph

Tran Ngoc Bao Duy

Graph representation
Adjacency-list representation
Adjacency-matrix representation

TOPOLOGICAL Weighted graphs

Searching a graph
Breadth-first search

SORTING
Depth-first search

Topological sorting

Graph.20
Graph
Bumstead dressing
Tran Ngoc Bao Duy

Graph representation
Adjacency-list representation
Adjacency-matrix representation
Weighted graphs

Searching a graph
Breadth-first search
Depth-first search

Topological sorting

Graph.21
Graph
Bumstead dressing
Tran Ngoc Bao Duy

Graph representation
Adjacency-list representation
Adjacency-matrix representation
Weighted graphs

Searching a graph
Breadth-first search
Depth-first search

Topological sorting

Graph.21
Graph
Topological sorting
Tran Ngoc Bao Duy

Definition
Graph representation
A topological sort of a directed acyclic graph G = (V, E) Adjacency-list representation
Adjacency-matrix representation
is a linear ordering of all its vertices such that if G contains Weighted graphs

an edge (u, v) then u appears before in the ordering. If the Searching a graph
Breadth-first search
graph contains a cycle, then no linear ordering is possible. Depth-first search

Topological sorting

Graph.22
Graph
Topological sorting
Tran Ngoc Bao Duy

Definition
Graph representation
A topological sort of a directed acyclic graph G = (V, E) Adjacency-list representation
Adjacency-matrix representation
is a linear ordering of all its vertices such that if G contains Weighted graphs

an edge (u, v) then u appears before in the ordering. If the Searching a graph
Breadth-first search
graph contains a cycle, then no linear ordering is possible. Depth-first search

Topological sorting

Graph.22
Graph
Applications
Tran Ngoc Bao Duy

• A common application of topological sorting is in schedul-


ing a sequence of jobs. Graph representation
Adjacency-list representation
Adjacency-matrix representation
Weighted graphs

Searching a graph
Breadth-first search
Depth-first search

Topological sorting

Graph.23
Graph
Applications
Tran Ngoc Bao Duy

• A common application of topological sorting is in schedul-


ing a sequence of jobs. Graph representation
Adjacency-list representation

• The jobs are represented by vertices, and there is an Adjacency-matrix representation


Weighted graphs

edge from x toy if job x must be completed before job Searching a graph
Breadth-first search
y can be started Depth-first search

Topological sorting
For example, in constructing a building,the basement
must be completed before the first floor, which must
be completed before the second floor and so on.

Graph.23
Graph
Applications
Tran Ngoc Bao Duy

• A common application of topological sorting is in schedul-


ing a sequence of jobs. Graph representation
Adjacency-list representation

• The jobs are represented by vertices, and there is an Adjacency-matrix representation


Weighted graphs

edge from x toy if job x must be completed before job Searching a graph
Breadth-first search
y can be started Depth-first search

Topological sorting
For example, in constructing a building,the basement
must be completed before the first floor, which must
be completed before the second floor and so on.

• A topological sort gives an order in which we should


perform the jobs.

Graph.23
Graph

Tran Ngoc Bao Duy

Graph representation
Adjacency-list representation
Adjacency-matrix representation
Weighted graphs

THANK YOU. Searching a graph


Breadth-first search
Depth-first search

Topological sorting

Graph.24

You might also like