0% found this document useful (0 votes)
14 views29 pages

Lecture-11-Graphs Algo Dps Bfs

Uploaded by

rg709pk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views29 pages

Lecture-11-Graphs Algo Dps Bfs

Uploaded by

rg709pk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

GRAPH TRAVERSAL ALGORITHMS

LECTURE-10

Discrette Structures
GRAPHS SEARCH ALGORITHMS

 Systematically search every edge and every


vertex of graph.
 Graph G(V,E) is either directed or undirected.

 Applications:
 Routing, Searching, Clustering.
GRAPH TRAVERSAL

Many problems require processing all graph


vertices (and edges) in systematic fashion

Graph traversal algorithms:

 Depth-first search (DFS)

 Breadth-first search (BFS)


GRAPHS SEARCH ALGORITHMS

 Depth First Search (DFS):


 DFS starts the traversal from the root

node and explore the search as far as


possible from the root node i.e. depth
wise.

 Breath First Search (BFS):


BFS starts traversal from the root node
and then explore the search in the level by
level manner i.e. as close as possible from the
root node.
DEPTH-FIRST SEARCH (DFS)
DFS is an algorithm for searching a graph or tree
data structure.
The algorithm starts at the root (top) node of a tree
and goes as far as it can down a given branch
(path), then backtracks until it finds an unexplored
path, and then explores it.
The algorithm does this until the entire graph has
been explored.
Many problems in computer science can be thought
of in terms of graphs.
For example, analyzing networks, mapping routes,
scheduling, and finding spanning trees are graph
problems. To analyze these problems,
graph-search algorithms like depth-first search are
DEPTH-FIRST SEARCH (DFS)
 Visits graph’s vertices by always moving away from
last
visited vertex to an unvisited one, backtracks if no
adjacent unvisited vertex is available.

 Recurisve or it uses a stack


 a vertex is pushed onto the stack when it’s
reached for the first time
 a vertex is popped off the stack when it becomes
a dead end, i.e., when there is no adjacent
unvisited vertex.
 “Redraws” graph in tree-like fashion (with tree

edges and back edges for undirected graph)


EXAMPLE: DFS TRAVERSAL OF UNDIRECTED GRAPH

a b c d

e f g h

DFS traversal stack: DFS tree:

1 2 6 7
a b c d
abgcdh
abgcd
abgcd
abgc
abfe

abg
abf
abf

e f g h
ab

ab


a

4 3 5 8
Red edges are tree edges
and white edges are back
edges.
GRAPH ALGORITHMS : DEPTH FIRST SEARCH (DFS)
GRAPH ALGORITHMS : DEPTH FIRST SEARCH (DFS)
GRAPH ALGORITHMS : DEPTH FIRST SEARCH (DFS)
GRAPH ALGORITHMS : DEPTH FIRST SEARCH (DFS)

 Now G,E will be popped.


GRAPH ALGORITHMS : DEPTH FIRST SEARCH (DFS)

 N

 Now H and C will be popped.


GRAPH ALGORITHMS : DEPTH FIRST SEARCH (DFS)
DEPTH FIRST SEARCH (DFS)
EXAMPLE
DEPTH FIRST SEARCH (DFS)
EXAMPLE
DEPTH FIRST SEARCH (DFS)
EXAMPLE
BREADTH-FIRST SEARCH
 Breadth-first search (BFS) is an important
graph search algorithm that is used to solve
many problems including finding the shortest
path in a graph and solving puzzle games
(such as Rubik's Cubes).
 Many problems in computer science can be

thought of in terms of graphs.


 For example, analyzing networks, mapping

routes, and scheduling are graph problems.


Graph search algorithms like breadth-first
search are useful for analyzing and solving
graph problems.
BREADTH-FIRST SEARCH (BFS)
 Visits graph vertices by moving across to all
the neighbors of the last visited vertex

 Instead of a stack, BFS uses a queue

 Similar to level-by-level tree traversal

 “Redraws” graph in tree-like fashion (with


tree edges and cross edges for undirected
graph)
EXAMPLE OF BFS TRAVERSAL OF UNDIRECTED GRAPH

a b c d

e f g h

BFS traversal BFS tree:


queue:
1 2 6 8
a b c d
efg
bef

hd
ch
fg

e f g h
a

3 4 5 7
Red edges are tree edges
and white edges are cross
edges.
GRAPH ALGORITHMS :
BREATH FIRST SEARCH (BFS)
GRAPH ALGORITHMS :
BREATH FIRST SEARCH (BFS)
GRAPH ALGORITHMS :
BREATH FIRST SEARCH (BFS)
GRAPH ALGORITHMS :
BREATH FIRST SEARCH (BFS)
GRAPH ALGORITHMS :
BREATH FIRST SEARCH (BFS)
GRAPH ALGORITHMS :
BREADTH FIRST SEARCH (BFS)
BREADTH FIRST SEARCH (BFS)
EXERCISE
BREADTH FIRST SEARCH (BFS)
EXERCISE
BREADTH FIRST SEARCH (BFS)
EXERCISE

You might also like