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

Graphs Bfs Dfs

Uploaded by

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

Graphs Bfs Dfs

Uploaded by

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

B.

TECH V SEM CSE


ACADEMIC YEAR: 2023-2024

Course Name: Design and Analysis of Algorithm

Topic: Graphs Traversal (BFS and DFS)

Course code : CS 3102


Credits : 4
Mode of delivery : Physical
Faculty : Mr. Sunil Kumar Patel
Email-id : [email protected]
CS3102 (DAA), Dept. of CSE 1
Assessment criteria’s
Assignment
quiz
Mid term examination
End term Examination

CS3102 (DAA), Dept. of CSE 2


Course Information

• Course Handout
• Communicate through eMail
• Office hours
• To be communicated
• Grading policy
• Will be communicated as per university guidelines

CS3102 (DAA), Dept. of CSE 3


Syllabus
• Introduction: Fundamentals of Algorithms, Important Problem Types, Analysis
of algorithm efficiency. Analysis Framework: Asymptotic Notations and Basic
Efficiency Classes. Mathematical Analysis of Nonrecursive and Recursive
Algorithms: Brute force Techniques, Divide and Conquer. Decrease and
Conquer: Insertion Sort, Depth First Search, Breadth First Search, Topological
Sorting. Transform and Conquer: Presorting, BST, Heapsort. Space and Time
tradeoffs: Input Enhancement in String Matching. Dynamic Programming:
Warshall's and Floyd's Algorithms, The Knapsack Problem. Greedy Techniques:
Prim's, Kruskal's and Dijkstra's Algorithm, Huffman Trees. Coping with
limitations of algorithmic power. Backtracking: nQueens problem, Hamiltonian
Circuit Problem, Subset Sum Problem. Branch and Bound: Assignment
Problem, Knapsack Problem, TSP. P, NP, and NP-complete Problems.

CS3102 (DAA), Dept. of CSE 4


More Information

• Textbook
•Introduction to Algorithms 3rd ,Cormen, Leiserson, Rivest
and Stein, The MIT Press,
•Fundamentals of Computer Algorithms, 2nd, Sartaj Sahni,
Ellis Horowitz, Sanguthevar Rajasekaran

• Others
•Introduction to Design & Analysis Computer Algorithm 3rd,
Sara Baase, Allen Van Gelder, Adison-Wesley, 2000.
•Algorithms, Richard Johnsonbaugh, Marcus Schaefer, Prentice
Hall, 2004.
•Introduction to The Design and Analysis of Algorithms 2nd
Edition, Anany Levitin, Adison-Wesley, 2007.

CS3102 (DAA), Dept. of CSE 5


Course Objectives

• CS1501.1 Analyse the running times of algorithms using asymptotic analysis.


• CS1501.2 Demonstrate and Design algorithms using
divide-and-conquer paradigm to solve business problems hence enhance
skills.

• CS1501.3 Illustrate the concept of greedy and dynamic-programming


approach to solve real life problems to enhance entrepreneurship capabilities.
• CS1501.4 Demonstrate the concept of backtracking
and branch & bound algorithms.
• CS1501.5 Synthesize and analyse various advanced algorithms concept such as
graphs, string matching, approximation algorithms and complexity classes to
enhance employability.

CS3102 (DAA), Dept. of CSE 6


Graphs Traversal

Breadth First Search


&
Depth First Search

CS3102 (DAA), Dept. of CSE 7


Contents
◼ Overview of Graph terminology.

◼ Graph representation.

◼ Breadth first search.

◼ Depth first search.

◼ Applications of BFS and DFS.

CS3102 (DAA), Dept. of CSE 8


Graph terminology - overview
◼ A graph consists of
❑ set of vertices V = {v1, v2, ….. vn}
❑ set of edges that connect the vertices E ={e1, e2, …. em}
◼ Two vertices in a graph are adjacent if there is an
edge connecting the vertices.
◼ Two vertices are on a path if there is a sequences
of vertices beginning with the first one and ending
with the second one
◼ Graphs with ordered edges are directed. For
directed graphs, vertices have in and out degrees.
◼ Weighted Graphs have values associated with
edges.

CS3102 (DAA), Dept. of CSE 9


Graph representation – undirected

graph Adjacency list Adjacency matrix

CS3102 (DAA), Dept. of CSE 10


Graph representation – directed

graph Adjacency list Adjacency matrix

CS3102 (DAA), Dept. of CSE 11


Some notes

◼ Adjacency list representation is usually


preferred since it is more efficient in
representing sparse graphs.
❑ Graphs for which |E| is much less than |V|2
◼ Adjacency list requires memory of the order
of θ(V+E)

◼ Searching a graph means systematically


following the edges of the graph so as to visit
the vertices.
CS3102 (DAA), Dept. of CSE 12
Breadth first search
◼ Given
❑ a graph G=(V,E) – set of vertices and edges
❑ a distinguished source vertex s
◼ Breadth first search systematically explores the
edges of G to discover every vertex that is
reachable from s.
◼ It also produces a ‘breadth first tree’ with root s that
contains all the vertices reachable from s.
◼ For any vertex v reachable from s, the path in the
breadth first tree corresponds to the shortest path in
graph G from s to v.
◼ It works on both directed and undirected graphs.
However, we will explore only undirected graphs.

CS3102 (DAA), Dept. of CSE 13


Breadth first search
It is so named because
It discovers all vertices at distance k from s before
discovering vertices at distance k+1.

CS3102 (DAA), Dept. of CSE 14


Breadth-First Search (BFS)

CS3102 (DAA), Dept. of CSE 15


Breadth first search - concepts
◼ To keep track of progress, it colors each
vertex - white, gray or black.
◼ All vertices start white.
◼ A vertex discovered first time during the
search becomes nonwhite.
◼ All vertices adjacent to black ones are
discovered. Whereas, gray ones may have
some white adjacent vertices.
◼ Gray represent the frontier between
discovered and undiscovered vertices.

CS3102 (DAA), Dept. of CSE 16


BFS – How it produces a Breadth first tree

◼ The tree initially contains only root. – s


◼ Whenever a vertex v is discovered in
scanning adjacency list of vertex u
❑ Vertex v and edge (u,v) are added to the tree.

CS3102 (DAA), Dept. of CSE 17


BFS - algorithm white: undiscovered
gray: discovered
black: finished
BFS(G, s) // G is the graph and s is the starting node
1 for each vertex u ∈ V [G] - {s}
2 do color[u] ← WHITE // color of vertex u Q: a queue of discovered
3 d[u] ← ∞ // distance from source s to vertex u vertices
4 π[u] ← NIL // predecessor of u color[v]: color of v
5 color[s] ← GRAY d[v]: distance from s to v
6 d[s] ← 0 [u]: predecessor of v
7 π[s] ← NIL
8 Q←Ø // Q is a FIFO - queue
9 ENQUEUE(Q, s)
10 while Q ≠ Ø // iterates as long as there are gray vertices. Lines 10-18
11 do u ← DEQUEUE(Q)
12 for each v ∈ Adj[u]
13 do if color[v] = WHITE // discover the undiscovered adjacent vertices
14 then color[v] ← GRAY // enqueued whenever painted gray
15 d[v] ← d[u] + 1
16 π[v] ← u
17 ENQUEUE(Q, v)
18 color[u] ← BLACK // painted black whenever dequeued

CS3102 (DAA), Dept. of CSE 18


Breadth first search - analysis
◼ Enqueue and Dequeue happen only once for
each node. - O(V).
◼ Scanning adjacent vertices: O(|E|)
◼ Initialization overhead O(V)

Total runtime O(V+E)

CS3102 (DAA), Dept. of CSE 19


Example (BFS)

r s t u
 0  

   
v w x y

Q: s
0
CS3102 (DAA), Dept. of CSE 20
Example (BFS)

r s t u
1 0  

 1  
v w x y

Q: w r
1 1
CS3102 (DAA), Dept. of CSE 21
Example (BFS)

r s t u
1 0 2 

 1 2 
v w x y

Q: r t x
1 2 2
CS3102 (DAA), Dept. of CSE 22
Example (BFS)

r s t u
1 0 2 

2 1 2 
v w x y

Q: t x v
2 2 2
CS3102 (DAA), Dept. of CSE 23
Example (BFS)

r s t u
1 0 2 3

2 1 2 
v w x y

Q: x v u
2 2 3
CS3102 (DAA), Dept. of CSE 24
Example (BFS)

r s t u
1 0 2 3

2 1 2 3
v w x y

Q: v u y
2 3 3
CS3102 (DAA), Dept. of CSE 25
Example (BFS)

r s t u
1 0 2 3

2 1 2 3
v w x y

Q: u y
3 3
CS3102 (DAA), Dept. of CSE 26
Example (BFS)

r s t u
1 0 2 3

2 1 2 3
v w x y

Q: y
3
CS3102 (DAA), Dept. of CSE 27
Example (BFS)

r s t u
1 0 2 3

2 1 2 3
v w x y

Q: 

CS3102 (DAA), Dept. of CSE 28


Example (BFS)

r s t u
1 0 2 3

2 1 2 3
v w x y

BFS Tree

CS3102 (DAA), Dept. of CSE 29


Depth first search
◼ It searches ‘deeper’ the graph when possible.
◼ Starts at the selected node and explores as far as
possible along each branch before backtracking.
◼ Vertices go through white, gray and black stages of
color.
❑ White – initially
❑ Gray – when discovered first
❑ Black – when finished i.e. the adjacency list of the vertex is
completely examined.
◼ Also records timestamps for each vertex
❑ d[v] when the vertex is first discovered
❑ f[v] when the vertex is finished

CS3102 (DAA), Dept. of CSE 30


Depth first search

CS3102 (DAA), Dept. of CSE 31


Depth first search - algorithm
DFS(G)
1 for each vertex u ∈ V [G]
2 do color[u] ← WHITE // color all vertices white, set their parents NIL
3 π[u] ← NIL
4 time ← 0 // zero out time
5 for each vertex u ∈ V [G] // call only for unexplored vertices
6 do if color[u] = WHITE // this may result in multiple sources
7 then DFS-VISIT(u)

DFS-VISIT(u)
1 color[u] ← GRAY ▹White vertex u has just been discovered.
2 time ← time +1
3 d[u] = time // record the discovery time
4 for each v ∈ Adj[u] ▹Explore edge(u, v).
5 do if color[v] = WHITE
6 then π[v] ← u // set the parent value Can Uses a global
7 DFS-VISIT(v) // recursive call timestamp time.
8 color[u] = BLACK ▹ Blacken u; it is finished.
9 f [u] ▹ time ← time +1

CS3102 (DAA), Dept. of CSE 32


Depth first search - analysis
◼ Lines 1-3, initialization take time Θ(V).

◼ Lines 5-7 take time Θ(V), excluding the time to call the DFS-VISIT.

◼ DFS-VISIT is called only once for each node (since it’s called only for
white nodes and the first step in it is to paint the node gray).

◼ Loop on line 4-7 is executed |Adj(v)| times. Since, ∑vєV |Adj(v)| = Ө (E),
the total cost of DFS-VISIT is θ(E)

The total cost of DFS is θ(V+E)

CS3102 (DAA), Dept. of CSE 33


Example (DFS)

u v w
1/

x y z

CS3102 (DAA), Dept. of CSE 34


Example (DFS)

u v w
1/ 2/

x y z

CS3102 (DAA), Dept. of CSE 35


Example (DFS)

u v w
1/ 2/

3/
x y z

CS3102 (DAA), Dept. of CSE 36


Example (DFS)

u v w
1/ 2/

4/ 3/
x y z

CS3102 (DAA), Dept. of CSE 37


Example (DFS)

u v w
1/ 2/
B

4/ 3/
x y z

CS3102 (DAA), Dept. of CSE 38


Example (DFS)

u v w
1/ 2/
B

4/5 3/
x y z

CS3102 (DAA), Dept. of CSE 39


Example (DFS)

u v w
1/ 2/
B

4/5 3/6
x y z

CS3102 (DAA), Dept. of CSE 40


Example (DFS)

u v w
1/ 2/7
B

4/5 3/6
x y z

CS3102 (DAA), Dept. of CSE 41


Example (DFS)

u v w
1/ 2/7

F B

4/5 3/6
x y z

CS3102 (DAA), Dept. of CSE 42


Example (DFS)

u v w
1/8 2/7

F B

4/5 3/6
x y z

CS3102 (DAA), Dept. of CSE 43


Example (DFS)

u v w
1/8 2/7 9/

F B

4/5 3/6
x y z

CS3102 (DAA), Dept. of CSE 44


Example (DFS)

u v w
1/8 2/7 9/

F B C

4/5 3/6
x y z

CS3102 (DAA), Dept. of CSE 45


Example (DFS)

u v w
1/8 2/7 9/

F B C

4/5 3/6 10/


x y z

CS3102 (DAA), Dept. of CSE 46


Example (DFS)

u v w
1/8 2/7 9/

F B C

4/5 3/6 10/ B


x y z

CS3102 (DAA), Dept. of CSE 47


Example (DFS)

u v w
1/8 2/7 9/

F B C

4/5 3/6 10/11 B


x y z

CS3102 (DAA), Dept. of CSE 48


Example (DFS)

u v w
1/8 2/7 9/12

F B C

4/5 3/6 10/11 B


x y z

CS3102 (DAA), Dept. of CSE 49


BFS and DFS - comparison

◼ Space complexity of DFS is lower than that of BFS.


◼ Time complexity of both is same – O(|V|+|E|).
◼ The behavior differs for graphs where not all the
vertices can be reached from the given vertex s.
◼ Predecessor subgraphs produced by DFS may be
different than those produced by BFS. The BFS
product is just one tree whereas the DFS product
may be multiple trees.

CS3102 (DAA), Dept. of CSE 50


BFS and DFS – possible applications
◼ Exploration algorithms in Artificial Intelligence
◼ Possible to use in routing / exploration wherever travel is
involved. E.g.,
❑ I want to explore all the nearest pizza places and want to go to
the nearest one with only two intersections.
❑ Find distance from my factory to every delivery center.

❑ Most of the mapping software (GOOGLE maps, YAHOO(?)


maps) should be using these algorithms.
❑ Companies like Waste Management, UPS and FedEx?

◼ Applications of DFS
❑ Topologically sorting a directed acyclic graph.
◼ List the graph elements in such an order that all the nodes are listed
before nodes to which they have outgoing edges.
❑ Finding the strongly connected components of a directed graph.
◼ List all the subgraphs of a strongly connected graph which
themselves are strongly connected.

CS3102 (DAA), Dept. of CSE 51

You might also like