SlideShare une entreprise Scribd logo
1  sur  48
Graph Algorithms-
BFS & DFS
Graph Search (traversal)
 How do we search a graph?
– At a particular vertices, where shall we go next?
 Two common framework:
 the depth-first search (DFS)
 the breadth-first search (BFS) and
 In DFS, go as far as possible along a single path until reach
a dead end (a vertex with no edge out or no neighbor
unexplored) then backtrack
 In BFS, one explore a graph level by level away (explore
all neighbors first and then move on)
Breadth First Search
• Given a graph G=(V, E) and a distinguished source vertex
s. Breadth first search systematically explores the edges
of graph G to discover every vertex that is reachable
from s.
• The algorithm finds the distance of each vertex from
the source vertex. If finds the minimum distance
(smallest number of edges) from source s to each
reachable vertex.
• It also produces a breadth first tree with root s that
contains all reachable vertices.
• Breadth first search is so named because the algorithm
discovers all vertices at distance k from s before
discovering any vertices at distance k+1
Breadth First Search
• It works for both directed and undirected graphs
• To keep track of progress BFS colors each vertex white,
gray or black. All vertices start with white and may later
become gray and then black. White color denote
undiscovered vertex. Gray and black denote discovered
vertex. While gray denote discovered vertex which is still
under process or in the queue. And black denote
completely processed and discovered vertex.
• d[u] denotes the distance of vertex u from source s
• (u) denotes the parent of vertexᴨ u
• Adj[u] denotes the adjacent vertices of u
BFS Algorithm
BFS Algorithm
BFS: Example
∞
∞
∞
∞
∞
∞
∞
∞
r s t u
v w x y
∞
∞
0
∞
∞
∞
∞
∞
r s t u
v w x y
sQ:
BFS: Example
1
∞
0
1
∞
∞
∞
∞
r s t u
v w x y
wQ: r
BFS: Example
BFS: Example
1
∞
0
1
2
2
∞
∞
r s t u
v w x y
rQ: t x
BFS: Example
1
2
0
1
2
2
∞
∞
r s t u
v w x y
Q: t x v
BFS: Example
1
2
0
1
2
2
3
∞
r s t u
v w x y
Q: x v u
BFS: Example
1
2
0
1
2
2
3
3
r s t u
v w x y
Q: v u y
BFS: Example
1
2
0
1
2
2
3
3
r s t u
v w x y
Q: u y
BFS: Example
1
2
0
1
2
2
3
3
r s t u
v w x y
Q: y
BFS: Example
1
2
0
1
2
2
3
3
r s t u
v w x y
Q: Ø
BFS: Time Complexity
 Queuing time is O(V) and scanning all edges
requires O(E)
 Overhead for initialization is O (V)
 So, total running time is O(V+E)
Analysis of Time Complexity
 BFS takes O(V) time for initialization
 The operations of enqueuing and dequeuing take O(1) time
for each vertex. So the total time devoted to queue
operation for v vertices is O(V).
 Now the adjacency list of each vertex is scanned only when
the vertex is dequeued. Then the sum of the lengths of all
the adjacency lists is (E). So the total time spent inɵ
scanning adjacency lists is O(E).
 So the total time complexity of BFS is O(V+E)
BFS: Application
• Shortest path problem
Depth First Search
 DFS, go as far as possible along a single path until it reaches
a dead end (that is a vertex with no edge out or no neighbor
unexplored) then backtrack
 As the name implies the DFS search deeper in the graph
whenever possible. DFS explores edges out of the most
recently discovered vertex v that still has unexplored edges
leaving it. Once all of v’s edges have been explored, the
search backtracks to explore edges leaving the vertex from
which v was discovered.
Depth First Search
 To keep track of progress DFS colors each vertex white, gray or
black. Initially all the vertices are colored white. Then they are
colored gray when discovered. Finally colored black when
finished.
 Besides creating depth first forest DFS also timestamps each
vertex. Each vertex goes through two time stamps:
 Discover time d[u]: when u is first discovered
 Finish time f[u]: when backtrack from u or finished u
 f[u] > d[u]
DFS: Algorithm
 DFS(G)
1. for each vertex u in G
2. color[u]=white
3. [u]=NILᴨ
4. time=0
5. for each vertex u in G
6. if (color[u]==white)
7. DFS-VISIT(G,u)
DFS-VISIT(u)
1. time = time + 1
2. d[u] = time
3. color[u]=gray
4. for each v € Adj(u) in G do
5. if (color[v] = =white)
6. [v] = u;ᴨ
7. DFS-VISIT(G,v);
8. color[u] = black
9. time = time + 1;
10. f[u]= time;
DFS: Algorithm (Cont.)
source
vertex
DFS Example
source
vertex
DFS Example
1 | | |
|||
| |
source
vertex
d | f
DFS Example
1 | | |
|||
2 | |
source
vertex
d | f
DFS Example
1 | | |
||3 |
2 | |
source
vertex
d | f
DFS Example
1 | | |
||3|4
2 | |
source
vertex
d | f
DFS Example
1 | | |
|5 |3|4
2 | |
source
vertex
d | f
DFS Example
1 | | |
|5|63|4
2 | |
source
vertex
d | f
DFS Example
1 | | |
|5|63|4
2|7 |
source
vertex
d | f
DFS Example
1 | 8 | |
|5|63|4
2|7 |
source
vertex
d | f
DFS Example
1 | 8 | |
|5|63|4
2|7 9 |
source
vertex
d | f
DFS Example
1 | 8 | |
|5|63|4
2|7 9|10
source
vertex
d | f
DFS Example
1 | 8|11 |
|5|63|4
2|7 9|10
source
vertex
d | f
DFS Example
1|12 8|11 |
|5|63|4
2|7 9|10
source
vertex
d | f
DFS Example
1|12 8|11 13|
|5|63|4
2|7 9|10
source
vertex
d | f
DFS Example
1|12 8|11 13|
14|5|63|4
2|7 9|10
source
vertex
d | f
DFS Example
1|12 8|11 13|
14|155|63|4
2|7 9|10
source
vertex
d | f
DFS Example
1|12 8|11 13|16
14|155|63|4
2|7 9|10
source
vertex
d | f
DFS: Complexity Analysis
 Initialization complexity is O(V)
 DFS_VISIT is called exactly once for each vertex
 And DFS_VISIT scans all the edges which causes
cost of O(E)
 Thus overall complexity is O(V + E)
DFS: Application
• Topological Sort
• Strongly Connected Component
Classification of Edges
 Another interesting property of DFS is that the DFS can be
used to classify the edges of the input graph G=(V,E). The DFS
creates depth first forest which can have four types of edges:
 Tree edge: Edge (u,v) is a tree edge if v was first discovered
by exploring edge (u,v). White color indicates tree edge.
 Back edge: Edge (u,v) is a back edge if it connects a vertex u
to a ancestor v in a depth first tree. Gray color indicates back
edge.
 Forward edge: Edge (u,v) is a forward edge if it is non-tree
edge and it connects a vertex u to a descendant v in a depth
first tree. Black color indicates forward edge.
 Cross edge: rest all other edges are called the cross edge.
Black color indicates forward edge.
Example of Edges
Theorem Derived from DFS
 Theorem 1: In a depth first search of an undirected
graph G, every edge of G is either a tree edge or
back edge.
 Theorem 2: A directed graph G is acyclic if and only
if a depth-first search of G yields no back edges.
Container of BFS and DFS
 Choice of container used to store discovered
vertices while graph search…
 If a queue is used as the container, we get
breadth first search.
 If a stack is used as the container, we get depth
first search.
Problem to Solve
Mawa
Dhaka
Majir
Ghat
Jajira
Shariat
pur
Naria
Bheder
Gong
University
MD. Shakhawat Hossain
Student of Computer Science & Engineering Dept.
University of Rajshahi

Contenu connexe

Tendances

Graph traversal-BFS & DFS
Graph traversal-BFS & DFSGraph traversal-BFS & DFS
Graph traversal-BFS & DFSRajandeep Gill
 
Depth first search and breadth first searching
Depth first search and breadth first searchingDepth first search and breadth first searching
Depth first search and breadth first searchingKawsar Hamid Sumon
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - GraphMadhu Bala
 
Depth first search [dfs]
Depth first search [dfs]Depth first search [dfs]
Depth first search [dfs]DEEPIKA T
 
SINGLE SOURCE SHORTEST PATH.ppt
SINGLE SOURCE SHORTEST PATH.pptSINGLE SOURCE SHORTEST PATH.ppt
SINGLE SOURCE SHORTEST PATH.pptshanthishyam
 
A presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithmA presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithmGaurav Kolekar
 
Depth First Search ( DFS )
Depth First Search ( DFS )Depth First Search ( DFS )
Depth First Search ( DFS )Sazzad Hossain
 
Insertion sort bubble sort selection sort
Insertion sort bubble sort  selection sortInsertion sort bubble sort  selection sort
Insertion sort bubble sort selection sortUmmar Hayat
 
Graph representation
Graph representationGraph representation
Graph representationTech_MX
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structureAbrish06
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Shuvongkor Barman
 

Tendances (20)

DFS & BFS Graph
DFS & BFS GraphDFS & BFS Graph
DFS & BFS Graph
 
Depth-First Search
Depth-First SearchDepth-First Search
Depth-First Search
 
Graph traversal-BFS & DFS
Graph traversal-BFS & DFSGraph traversal-BFS & DFS
Graph traversal-BFS & DFS
 
Depth first search and breadth first searching
Depth first search and breadth first searchingDepth first search and breadth first searching
Depth first search and breadth first searching
 
Depth-First Search
Depth-First SearchDepth-First Search
Depth-First Search
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
 
Depth first search [dfs]
Depth first search [dfs]Depth first search [dfs]
Depth first search [dfs]
 
SINGLE SOURCE SHORTEST PATH.ppt
SINGLE SOURCE SHORTEST PATH.pptSINGLE SOURCE SHORTEST PATH.ppt
SINGLE SOURCE SHORTEST PATH.ppt
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
A presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithmA presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithm
 
Shortest Path in Graph
Shortest Path in GraphShortest Path in Graph
Shortest Path in Graph
 
Dfs
DfsDfs
Dfs
 
Depth First Search ( DFS )
Depth First Search ( DFS )Depth First Search ( DFS )
Depth First Search ( DFS )
 
Insertion sort bubble sort selection sort
Insertion sort bubble sort  selection sortInsertion sort bubble sort  selection sort
Insertion sort bubble sort selection sort
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
Graph representation
Graph representationGraph representation
Graph representation
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS
 
Spanning trees
Spanning treesSpanning trees
Spanning trees
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
 

En vedette

Ch2 3-informed (heuristic) search
Ch2 3-informed (heuristic) searchCh2 3-informed (heuristic) search
Ch2 3-informed (heuristic) searchchandsek666
 
Algorithm Analysis and Design Class Notes
Algorithm Analysis and Design Class NotesAlgorithm Analysis and Design Class Notes
Algorithm Analysis and Design Class NotesKumar Avinash
 
Search algorithms master
Search algorithms masterSearch algorithms master
Search algorithms masterHossam Hassan
 
Linear and Binary Search Algorithms.(Discrete Mathematics)
Linear and Binary Search Algorithms.(Discrete Mathematics)Linear and Binary Search Algorithms.(Discrete Mathematics)
Linear and Binary Search Algorithms.(Discrete Mathematics)Shanawaz Ahamed
 
Heuristic Search
Heuristic SearchHeuristic Search
Heuristic Searchbutest
 
Hillclimbing search algorthim #introduction
Hillclimbing search algorthim #introductionHillclimbing search algorthim #introduction
Hillclimbing search algorthim #introductionMohamed Gad
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of AlgorithmsArvind Krishnaa
 
Heuristic Search Techniques {Artificial Intelligence}
Heuristic Search Techniques {Artificial Intelligence}Heuristic Search Techniques {Artificial Intelligence}
Heuristic Search Techniques {Artificial Intelligence}FellowBuddy.com
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and GraphsIntro C# Book
 

En vedette (13)

Ch2 3-informed (heuristic) search
Ch2 3-informed (heuristic) searchCh2 3-informed (heuristic) search
Ch2 3-informed (heuristic) search
 
Application of dfs
Application of dfsApplication of dfs
Application of dfs
 
Algorithm Analysis and Design Class Notes
Algorithm Analysis and Design Class NotesAlgorithm Analysis and Design Class Notes
Algorithm Analysis and Design Class Notes
 
ADA complete notes
ADA complete notesADA complete notes
ADA complete notes
 
Search algorithms master
Search algorithms masterSearch algorithms master
Search algorithms master
 
130210107039 2130702
130210107039 2130702130210107039 2130702
130210107039 2130702
 
Linear and Binary Search Algorithms.(Discrete Mathematics)
Linear and Binary Search Algorithms.(Discrete Mathematics)Linear and Binary Search Algorithms.(Discrete Mathematics)
Linear and Binary Search Algorithms.(Discrete Mathematics)
 
Heuristic Search
Heuristic SearchHeuristic Search
Heuristic Search
 
Hillclimbing search algorthim #introduction
Hillclimbing search algorthim #introductionHillclimbing search algorthim #introduction
Hillclimbing search algorthim #introduction
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of Algorithms
 
Hill climbing
Hill climbingHill climbing
Hill climbing
 
Heuristic Search Techniques {Artificial Intelligence}
Heuristic Search Techniques {Artificial Intelligence}Heuristic Search Techniques {Artificial Intelligence}
Heuristic Search Techniques {Artificial Intelligence}
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and Graphs
 

Similaire à Breadth first search and depth first search

DFS ppt.pdf
DFS ppt.pdfDFS ppt.pdf
DFS ppt.pdfRajkk5
 
Analysis and design of algorithms part 3
Analysis and design of algorithms part 3Analysis and design of algorithms part 3
Analysis and design of algorithms part 3Deepak John
 
Breadth first search (Bfs)
Breadth first search (Bfs)Breadth first search (Bfs)
Breadth first search (Bfs)Ishucs
 
lecture 18
lecture 18lecture 18
lecture 18sajinsc
 
Graph Traversal Algorithm
Graph Traversal AlgorithmGraph Traversal Algorithm
Graph Traversal Algorithmjyothimonc
 
Depth firstsearchalgorithm
Depth firstsearchalgorithmDepth firstsearchalgorithm
Depth firstsearchalgorithmhansa khan
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in indiaEdhole.com
 
Graph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalGraph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalAmrinder Arora
 
22-graphs1-dfs-bfs.ppt
22-graphs1-dfs-bfs.ppt22-graphs1-dfs-bfs.ppt
22-graphs1-dfs-bfs.pptKarunaBiswas3
 
BFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal AlgorithmBFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal AlgorithmMSA Technosoft
 
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gpchandrashekarr799
 
ada-191015145338.pdf
ada-191015145338.pdfada-191015145338.pdf
ada-191015145338.pdfAshwin180668
 
Elementary Graph Algo.ppt
Elementary Graph Algo.pptElementary Graph Algo.ppt
Elementary Graph Algo.pptSazidHossain9
 
Breadth First Search (BFS)
Breadth First Search (BFS)Breadth First Search (BFS)
Breadth First Search (BFS)Dhrumil Panchal
 

Similaire à Breadth first search and depth first search (20)

Bfs dfs
Bfs dfsBfs dfs
Bfs dfs
 
DFS ppt.pdf
DFS ppt.pdfDFS ppt.pdf
DFS ppt.pdf
 
Analysis and design of algorithms part 3
Analysis and design of algorithms part 3Analysis and design of algorithms part 3
Analysis and design of algorithms part 3
 
Breadth first search (Bfs)
Breadth first search (Bfs)Breadth first search (Bfs)
Breadth first search (Bfs)
 
kumattt).pptx
kumattt).pptxkumattt).pptx
kumattt).pptx
 
lecture 18
lecture 18lecture 18
lecture 18
 
Graph Traversal Algorithm
Graph Traversal AlgorithmGraph Traversal Algorithm
Graph Traversal Algorithm
 
BFS
BFSBFS
BFS
 
Depth firstsearchalgorithm
Depth firstsearchalgorithmDepth firstsearchalgorithm
Depth firstsearchalgorithm
 
Chapter 23 aoa
Chapter 23 aoaChapter 23 aoa
Chapter 23 aoa
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in india
 
U1 L5 DAA.pdf
U1 L5 DAA.pdfU1 L5 DAA.pdf
U1 L5 DAA.pdf
 
Graph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalGraph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search Traversal
 
22-graphs1-dfs-bfs.ppt
22-graphs1-dfs-bfs.ppt22-graphs1-dfs-bfs.ppt
22-graphs1-dfs-bfs.ppt
 
BFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal AlgorithmBFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal Algorithm
 
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
 
ada-191015145338.pdf
ada-191015145338.pdfada-191015145338.pdf
ada-191015145338.pdf
 
19-graph1 (1).ppt
19-graph1 (1).ppt19-graph1 (1).ppt
19-graph1 (1).ppt
 
Elementary Graph Algo.ppt
Elementary Graph Algo.pptElementary Graph Algo.ppt
Elementary Graph Algo.ppt
 
Breadth First Search (BFS)
Breadth First Search (BFS)Breadth First Search (BFS)
Breadth First Search (BFS)
 

Plus de Hossain Md Shakhawat (20)

Recipe for the effective presentaion
Recipe for the effective presentaionRecipe for the effective presentaion
Recipe for the effective presentaion
 
The Road to Higher study in Japan
The Road to Higher study in JapanThe Road to Higher study in Japan
The Road to Higher study in Japan
 
Islamic jurisprudence
Islamic jurisprudenceIslamic jurisprudence
Islamic jurisprudence
 
Introduction to Medical Imaging
Introduction to Medical ImagingIntroduction to Medical Imaging
Introduction to Medical Imaging
 
Jpeg compression
Jpeg compressionJpeg compression
Jpeg compression
 
Surah Fatiha
Surah FatihaSurah Fatiha
Surah Fatiha
 
Hashing
HashingHashing
Hashing
 
Decision making and looping
Decision making and loopingDecision making and looping
Decision making and looping
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
Digital signature
Digital signatureDigital signature
Digital signature
 
Caesar cipher
Caesar cipherCaesar cipher
Caesar cipher
 
Rsa rivest shamir adleman
Rsa rivest shamir adlemanRsa rivest shamir adleman
Rsa rivest shamir adleman
 
Fundamentals of cryptography
Fundamentals of cryptographyFundamentals of cryptography
Fundamentals of cryptography
 
Introduction to programming with c,
Introduction to programming with c,Introduction to programming with c,
Introduction to programming with c,
 
Introduction to digital image processing
Introduction to digital image processingIntroduction to digital image processing
Introduction to digital image processing
 
History of computing
History of computingHistory of computing
History of computing
 
Introduction to Printers
Introduction to PrintersIntroduction to Printers
Introduction to Printers
 
Input devices_(Mouse and Keyboard)
Input devices_(Mouse and Keyboard)Input devices_(Mouse and Keyboard)
Input devices_(Mouse and Keyboard)
 
Introduction to computer
Introduction to computerIntroduction to computer
Introduction to computer
 
Bucket sort
Bucket sortBucket sort
Bucket sort
 

Dernier

ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 

Dernier (20)

ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 

Breadth first search and depth first search

  • 2. Graph Search (traversal)  How do we search a graph? – At a particular vertices, where shall we go next?  Two common framework:  the depth-first search (DFS)  the breadth-first search (BFS) and  In DFS, go as far as possible along a single path until reach a dead end (a vertex with no edge out or no neighbor unexplored) then backtrack  In BFS, one explore a graph level by level away (explore all neighbors first and then move on)
  • 3. Breadth First Search • Given a graph G=(V, E) and a distinguished source vertex s. Breadth first search systematically explores the edges of graph G to discover every vertex that is reachable from s. • The algorithm finds the distance of each vertex from the source vertex. If finds the minimum distance (smallest number of edges) from source s to each reachable vertex. • It also produces a breadth first tree with root s that contains all reachable vertices. • Breadth first search is so named because the algorithm discovers all vertices at distance k from s before discovering any vertices at distance k+1
  • 4. Breadth First Search • It works for both directed and undirected graphs • To keep track of progress BFS colors each vertex white, gray or black. All vertices start with white and may later become gray and then black. White color denote undiscovered vertex. Gray and black denote discovered vertex. While gray denote discovered vertex which is still under process or in the queue. And black denote completely processed and discovered vertex. • d[u] denotes the distance of vertex u from source s • (u) denotes the parent of vertexᴨ u • Adj[u] denotes the adjacent vertices of u
  • 8. ∞ ∞ 0 ∞ ∞ ∞ ∞ ∞ r s t u v w x y sQ: BFS: Example
  • 9. 1 ∞ 0 1 ∞ ∞ ∞ ∞ r s t u v w x y wQ: r BFS: Example
  • 11. BFS: Example 1 2 0 1 2 2 ∞ ∞ r s t u v w x y Q: t x v
  • 12. BFS: Example 1 2 0 1 2 2 3 ∞ r s t u v w x y Q: x v u
  • 13. BFS: Example 1 2 0 1 2 2 3 3 r s t u v w x y Q: v u y
  • 14. BFS: Example 1 2 0 1 2 2 3 3 r s t u v w x y Q: u y
  • 16. BFS: Example 1 2 0 1 2 2 3 3 r s t u v w x y Q: Ø
  • 17. BFS: Time Complexity  Queuing time is O(V) and scanning all edges requires O(E)  Overhead for initialization is O (V)  So, total running time is O(V+E)
  • 18. Analysis of Time Complexity  BFS takes O(V) time for initialization  The operations of enqueuing and dequeuing take O(1) time for each vertex. So the total time devoted to queue operation for v vertices is O(V).  Now the adjacency list of each vertex is scanned only when the vertex is dequeued. Then the sum of the lengths of all the adjacency lists is (E). So the total time spent inɵ scanning adjacency lists is O(E).  So the total time complexity of BFS is O(V+E)
  • 20. Depth First Search  DFS, go as far as possible along a single path until it reaches a dead end (that is a vertex with no edge out or no neighbor unexplored) then backtrack  As the name implies the DFS search deeper in the graph whenever possible. DFS explores edges out of the most recently discovered vertex v that still has unexplored edges leaving it. Once all of v’s edges have been explored, the search backtracks to explore edges leaving the vertex from which v was discovered.
  • 21. Depth First Search  To keep track of progress DFS colors each vertex white, gray or black. Initially all the vertices are colored white. Then they are colored gray when discovered. Finally colored black when finished.  Besides creating depth first forest DFS also timestamps each vertex. Each vertex goes through two time stamps:  Discover time d[u]: when u is first discovered  Finish time f[u]: when backtrack from u or finished u  f[u] > d[u]
  • 22. DFS: Algorithm  DFS(G) 1. for each vertex u in G 2. color[u]=white 3. [u]=NILᴨ 4. time=0 5. for each vertex u in G 6. if (color[u]==white) 7. DFS-VISIT(G,u)
  • 23. DFS-VISIT(u) 1. time = time + 1 2. d[u] = time 3. color[u]=gray 4. for each v € Adj(u) in G do 5. if (color[v] = =white) 6. [v] = u;ᴨ 7. DFS-VISIT(G,v); 8. color[u] = black 9. time = time + 1; 10. f[u]= time; DFS: Algorithm (Cont.) source vertex
  • 25. DFS Example 1 | | | ||| | | source vertex d | f
  • 26. DFS Example 1 | | | ||| 2 | | source vertex d | f
  • 27. DFS Example 1 | | | ||3 | 2 | | source vertex d | f
  • 28. DFS Example 1 | | | ||3|4 2 | | source vertex d | f
  • 29. DFS Example 1 | | | |5 |3|4 2 | | source vertex d | f
  • 30. DFS Example 1 | | | |5|63|4 2 | | source vertex d | f
  • 31. DFS Example 1 | | | |5|63|4 2|7 | source vertex d | f
  • 32. DFS Example 1 | 8 | | |5|63|4 2|7 | source vertex d | f
  • 33. DFS Example 1 | 8 | | |5|63|4 2|7 9 | source vertex d | f
  • 34. DFS Example 1 | 8 | | |5|63|4 2|7 9|10 source vertex d | f
  • 35. DFS Example 1 | 8|11 | |5|63|4 2|7 9|10 source vertex d | f
  • 36. DFS Example 1|12 8|11 | |5|63|4 2|7 9|10 source vertex d | f
  • 37. DFS Example 1|12 8|11 13| |5|63|4 2|7 9|10 source vertex d | f
  • 38. DFS Example 1|12 8|11 13| 14|5|63|4 2|7 9|10 source vertex d | f
  • 39. DFS Example 1|12 8|11 13| 14|155|63|4 2|7 9|10 source vertex d | f
  • 40. DFS Example 1|12 8|11 13|16 14|155|63|4 2|7 9|10 source vertex d | f
  • 41. DFS: Complexity Analysis  Initialization complexity is O(V)  DFS_VISIT is called exactly once for each vertex  And DFS_VISIT scans all the edges which causes cost of O(E)  Thus overall complexity is O(V + E)
  • 42. DFS: Application • Topological Sort • Strongly Connected Component
  • 43. Classification of Edges  Another interesting property of DFS is that the DFS can be used to classify the edges of the input graph G=(V,E). The DFS creates depth first forest which can have four types of edges:  Tree edge: Edge (u,v) is a tree edge if v was first discovered by exploring edge (u,v). White color indicates tree edge.  Back edge: Edge (u,v) is a back edge if it connects a vertex u to a ancestor v in a depth first tree. Gray color indicates back edge.  Forward edge: Edge (u,v) is a forward edge if it is non-tree edge and it connects a vertex u to a descendant v in a depth first tree. Black color indicates forward edge.  Cross edge: rest all other edges are called the cross edge. Black color indicates forward edge.
  • 45. Theorem Derived from DFS  Theorem 1: In a depth first search of an undirected graph G, every edge of G is either a tree edge or back edge.  Theorem 2: A directed graph G is acyclic if and only if a depth-first search of G yields no back edges.
  • 46. Container of BFS and DFS  Choice of container used to store discovered vertices while graph search…  If a queue is used as the container, we get breadth first search.  If a stack is used as the container, we get depth first search.
  • 48. MD. Shakhawat Hossain Student of Computer Science & Engineering Dept. University of Rajshahi