Professional Documents
Culture Documents
Searching a graph
Breadth-first search
Depth-first search
Data Structures and Algorithms
Topological sorting
Graph.1
Graph
Overview
Tran Ngoc Bao Duy
1 Graph representation
Graph representation
Adjacency-list representation Adjacency-list representation
Topological sorting
2 Searching a graph
Breadth-first search
Depth-first search
3 Topological sorting
Graph.2
Graph
Graph representation
Adjacency-list representation
Adjacency-matrix representation
Searching a graph
Breadth-first search
REPRESENTATION
Depth-first search
Topological sorting
Graph.3
Graph
Graph representation
Tran Ngoc Bao Duy
Topological sorting
Graph.4
Graph
Graph representation
Tran Ngoc Bao Duy
Topological sorting
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
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
Graph.5
Graph
Adjacency-list: Properties
Tran Ngoc Bao Duy
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
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
Graph.7
Graph
Adjacency-matrix representation
Tran Ngoc Bao Duy
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
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
Searching a graph
1 if (i, j) ∈ E
aij = Breadth-first search
Depth-first search
0 otherwise
Topological sorting
Graph representation
Adjacency-list representation
Graph.8
Graph
Weighted graphs
Tran Ngoc Bao Duy
Topological sorting
Graph.9
Graph
Comparision
Tran Ngoc Bao Duy
Graph representation
Adjacency-list representation
Adjacency-matrix representation
Searching a graph
by most of the graph algorithms. Breadth-first search
Depth-first search
Graph.10
Graph
Graph representation
Adjacency-list representation
Adjacency-matrix representation
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
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
the structure of a graph. Breadth-first search
Depth-first search
Graph.12
Graph
Searching a graph
Tran Ngoc Bao Duy
Searching a graph
the structure of a graph. Breadth-first search
Depth-first search
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
Searching a graph
edges) from s to each reachable vertex. Breadth-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
Searching a graph
edges) from s to each reachable vertex. Breadth-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
Searching a graph
edges) from s to each reachable vertex. Breadth-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
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
Searching a graph
Breadth-first search
Depth-first search
Topological sorting
Graph.17
Graph
Depth-first search
Tran Ngoc Bao Duy
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
Topological sorting
Graph.17
Graph
Depth-first search
Tran Ngoc Bao Duy
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
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
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
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
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
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
Graph representation
Adjacency-list representation
Adjacency-matrix representation
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
Searching a graph
Breadth-first search
Depth-first search
Topological sorting
Graph.23
Graph
Applications
Tran Ngoc Bao Duy
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
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
Graph representation
Adjacency-list representation
Adjacency-matrix representation
Weighted graphs
Topological sorting
Graph.24