SlideShare a Scribd company logo
Graph Traversals
Depth-First Search
• Think Stack
Breadth-First Search
• Think Queue
Overview
 Goal
 To systematically visit the nodes of a graph
 A tree is a directed, acyclic, graph (DAG)
 If the graph is a tree,
 DFS is exhibited by preorder, postorder, and (for binary trees)
inorder traversals
 BFS is exhibited by level-order traversal
Depth-First Search
// recursive, preorder, depth-first search
void dfs (Node v) {
if (v == null)
return;
if (v not yet visited)
visit&mark(v); // visit node before adjacent nodes
for (each w adjacent to v)
if (w has not yet been visited)
dfs(w);
} // dfs
Depth-First Search
// recursive, postorder, depth-first search
void dfs (Node v) {
if (v == null)
return;
tag(v); // mark v as having been considered
for (each w adjacent to v)
if (w has not yet been tagged)
dfs(w);
visit(v); // postorder traversal: visit node after
// adjacent nodes
} // dfs
Depth-First Search
// non-recursive, preorder, depth-first search
void dfs (Node v) {
if (v == null)
return;
push(v);
while (stack is not empty) {
pop(v);
if (v has not yet been visited)
mark&visit(v);
for (each w adjacent to v)
if (w has not yet been visited)
push(w);
} // while
} // dfs
Depth-First Search
// non-recursive, postorder, depth-first search
void dfs (Node v) {
// Lex 20 (Do not need to submit)
} // dfs
Example
0
7
1
5
4
3
2
6
Policy: Visit adjacent nodes in increasing index order
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3 2 7 6 4
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5
Push 5
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5
Pop/Visit/Mark 5
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5
1
2
Push 2, Push 1
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1
2
Pop/Visit/Mark 1
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1
0
2
4
2
Push 4, Push 2,
Push 0
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0
2
4
2
Pop/Visit/Mark 0
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0
3
7
2
4
2
Push 7, Push 3
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3
7
2
4
2
Pop/Visit/Mark 3
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3
2
7
2
4
2
Push 2
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3 2
7
2
4
2
Pop/Mark/Visit 2
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3 2 7
2
4
2
Pop/Mark/Visit 7
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3 2 7
6
2
4
2
Push 6
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3 2 7 6
2
4
2
Pop/Mark/Visit 6
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3 2 7 6
4
2
Pop (don’t visit) 2
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3 2 7 6 4
2
Pop/Mark/Visit 4
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3 2 7 6 4
Pop (don’t visit) 2
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3 2 7 6 4
Done
Preorder DFS: Start with Node 5
Note: edge (0,3) removed
0
7
1
5
4
3
2
6
5 1 0 7 6 2 4 3
Depth-First Search
Policy: Don’t push nodes twice// non-recursive, preorder, depth-first search
void dfs (Node v) {
if (v == null)
return;
push(v);
while (stack is not empty) {
pop(v);
if (v has not yet been visited)
mark&visit(v);
for (each w adjacent to v)
if (w has not yet been visited && not yet stacked)
push(w);
} // while
} // dfs
Preorder DFS (Don’t push nodes
twice). Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3 7 6 4 2
Postorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
2 3 6 7 0 4 1 5
Breadth-first Search
 Ripples in a pond
 Visit designated node
 Then visited unvisited nodes a distance i away, where i = 1,
2, 3, etc.
 For nodes the same distance away, visit nodes in systematic
manner (eg. increasing index order)
Breadth-First Search
// non-recursive, preorder, breadth-first search
void bfs (Node v) {
if (v == null)
return;
enqueue(v);
while (queue is not empty) {
dequeue(v);
if (v has not yet been visited)
mark&visit(v);
for (each w adjacent to v)
if (w has not yet been visited && has not been queued)
enqueue(w);
} // while
} // bfs
BFS: Start with Node 5
7
1
5
4
3
2
6
5 1 2 0 4 3 7 6
0
BFS: Start with Node 5
7
1
5
4
3
2
6
5
0
BFS: Node one-away
7
1
5
4
3
2
6
5
0
BFS: Visit 1 and 2
7
1
5
4
3
2
6
5 1 2
0
BFS: Nodes two-away
7
1
5
4
3
2
6
5 1 2
0
BFS: Visit 0 and 4
7
1
5
4
3
2
6
5 1 2 0 4
0
BFS: Nodes three-away
7
1
5
4
3
2
6
5 1 2 0 4
0
BFS: Visit nodes 3 and 7
7
1
5
4
3
2
6
5 1 2 0 4 3 7
0
BFS: Node four-away
7
1
5
4
3
2
6
5 1 2 0 4 3 7
0
BFS: Visit 6
7
1
5
4
3
2
6
5 1 2 0 4 3 7 6
0
Biconnectivity
A connected undirected graph is biconnected if
there are no vertices whose removal disconnects
the rest of the graph.
Example :
If the nodes are computers and the edges are
links, then if any computer goes down, network
mail is unaffected, except, of course, at the down
computer.
Biconnectivity
 If a graph is not biconnected, the vertices whose removal
would disconnect the graph are known as articulation
points. These nodes are critical in many applications.
Biconnectivity
The graph below is not biconnected: C and D
are articulation points. The removal of C
would disconnect G, and the removal of D
would disconnect E and F, from the rest of the
graph
Biconnectivity
• Depth-first search provides a linear-time algorithm to find all
articulation points in a connected graph.
1.Starting at any vertex, perform a depth-first
search and number the nodes as they are visited:
num(v).
2.For every vertex v in the depth-first spanning
tree, compute the lowest-numbered vertex called
low(v), that is reachable from v by taking zero or
more tree edges and then possibly one back
edge (in that order).
Biconnectivity
 The depth-first spanning tree shows the preorder number
first, and then the lowest-numbered vertex reachable
(For each node num/low is given in the tree)
Biconnectivity
• The lowest-numbered vertex , low(v), can be efficiently
computed by performing a postorder traversal of the
depth-first spanning tree, and it is the minimum of
1)num(v)
2)the lowest num(w) among all back edges (v, w)
3)the lowest low(w) among all tree edges (v, w)
Biconnectivity: How to decide
Articulation Points
The root is an articulation point if and only if it has
more than one child (because if it has two children,
removing the root disconnects nodes in different
subtrees, and if it has only one child, removing the
root merely disconnects the root)
Any other vertex v is an articulation point if and only if
v has some child w such that low(w) ≥ num(v).
Example:
Vertex D is an articulation point
 low(E)= 4 ≥ num(D)=4
Vertex C is an articulation point
 low(G)= 7 ≥ num(C)=3
How many squares can you create in this figure by connecting any 4
dots (the corners of a square must lie upon a grid dot?
TRIANGLES:
How many triangles are located in the image below?
There are 11 squares total; 5 small, 4 medium, and 2 large.
27 triangles. There are 16 one-cell triangles, 7 four-cell triangles, 3 nine-cell
triangles, and 1 sixteen-cell triangle.
GUIDED READING
ASSESSMENT
Do the DFS and BFS for this graph
12
43
57
6
DIFFERENT TRAVERSALS
STARTING FROM VERTEX 1
PRE ORDER TRAVERSAL (ALSO CALLED
DEPTH FIRST SEARCH)
1 2 3 4 5 6 7
POST ORDER TRAVERSAL
6 5 4 7 3 2 1
BREADTH FIRST SEARCH
1 2 4 3 5 6 7
DEPTH FIRST SPANNING TREE
6•1
•4
•2
•5
•3
•6
•7
DEPTH FIRST SPANNING TREE
6•1
•4
•2
•5
•3
•6
•7
1
2
3
6
5
4
7
DEPTH FIRST SPANNING TREE
6•1
•4
•2
•5
•3
•6
•7
1
2
3
6 4
5
4
7
DEPTH FIRST SPANNING TREE
6•1
•4
•2
•5
•3
•6
•7
1
2
3
6 4
5 4
4
7
DEPTH FIRST SPANNING TREE
6•1
•4
•2
•5
•3
•6
•7
1
2
3
6 4
5 4
4 1
7
DEPTH FIRST SPANNING TREE
6•1
•4
•2
•5
•3
•6
•7
1
2
3
6 4
5 4
4 1
7 7
DEPTH FIRST SPANNING TREE
6•1
•4
•2
•5
•3
•6
•7
1
2
3 1
6 4
5 4
4 1
7 7
DEPTH FIRST SPANNING TREE
6•1
•4
•2
•5
•3
•6
•7
1
2 1
3 1
6 4
5 4
4 1
7 7
DEPTH FIRST SPANNING TREE
6•1
•4
•2
•5
•3
•6
•7
1 1
2 1
3 1
6 4
5 4
4 1
7 7
DEPTH FIRST SPANNING TREE
6•1
•4
•2
•5
•3
•6
•7
1 1
2 1
3 1
6 4
5 4
4 1
7 7

More Related Content

What's hot

Graphs bfs dfs
Graphs bfs dfsGraphs bfs dfs
Graphs bfs dfs
Jaya Gautam
 
Tree Traversal
Tree TraversalTree Traversal
Tree Traversal
Md. Israil Fakir
 
Depth first search [dfs]
Depth first search [dfs]Depth first search [dfs]
Depth first search [dfs]
DEEPIKA T
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
ManishPrajapati78
 
Depth First Search ( DFS )
Depth First Search ( DFS )Depth First Search ( DFS )
Depth First Search ( DFS )
Sazzad Hossain
 
Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
Syed Zaid Irshad
 
Bfs & dfs application
Bfs & dfs applicationBfs & dfs application
Bfs & dfs application
Umme habiba
 
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
Kawsar Hamid Sumon
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
Trupti Agrawal
 
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
Masud Parvaze
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data Structure
Meghaj Mallick
 
Graph Basic In Data structure
Graph Basic In Data structureGraph Basic In Data structure
Graph Basic In Data structure
Ikhlas Rahman
 
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
 
Trees
TreesTrees
Bfs and dfs in data structure
Bfs and dfs in  data structure Bfs and dfs in  data structure
Bfs and dfs in data structure
Ankit Kumar Singh
 
Linked list
Linked listLinked list
Linked list
Trupti Agrawal
 
Depth-First Search
Depth-First SearchDepth-First Search
Depth-First Search
Dakshitha Dissanayaka
 
Dijkstra
DijkstraDijkstra
Dijkstra
jagdeeparora86
 
Shortest Path in Graph
Shortest Path in GraphShortest Path in Graph
Shortest Path in Graph
Dr Sandeep Kumar Poonia
 
Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary TreeData Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary Tree
ManishPrajapati78
 

What's hot (20)

Graphs bfs dfs
Graphs bfs dfsGraphs bfs dfs
Graphs bfs dfs
 
Tree Traversal
Tree TraversalTree Traversal
Tree Traversal
 
Depth first search [dfs]
Depth first search [dfs]Depth first search [dfs]
Depth first search [dfs]
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 
Depth First Search ( DFS )
Depth First Search ( DFS )Depth First Search ( DFS )
Depth First Search ( DFS )
 
Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
 
Bfs & dfs application
Bfs & dfs applicationBfs & dfs application
Bfs & dfs application
 
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
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data Structure
 
Graph Basic In Data structure
Graph Basic In Data structureGraph Basic In Data structure
Graph Basic In Data structure
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
 
Trees
TreesTrees
Trees
 
Bfs and dfs in data structure
Bfs and dfs in  data structure Bfs and dfs in  data structure
Bfs and dfs in data structure
 
Linked list
Linked listLinked list
Linked list
 
Depth-First Search
Depth-First SearchDepth-First Search
Depth-First Search
 
Dijkstra
DijkstraDijkstra
Dijkstra
 
Shortest Path in Graph
Shortest Path in GraphShortest Path in Graph
Shortest Path in Graph
 
Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary TreeData Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary Tree
 

Viewers also liked

2.5 dfs & bfs
2.5 dfs & bfs2.5 dfs & bfs
2.5 dfs & bfs
Krish_ver2
 
5.5 back tracking
5.5 back tracking5.5 back tracking
5.5 back tracking
Krish_ver2
 
DFS & BFS Graph
DFS & BFS GraphDFS & BFS Graph
DFS & BFS Graph
Mahfuzul Yamin
 
2.5 graph dfs
2.5 graph dfs2.5 graph dfs
2.5 graph dfs
Krish_ver2
 
Algorithum Analysis
Algorithum AnalysisAlgorithum Analysis
Algorithum Analysis
Ain-ul-Moiz Khawaja
 
Depth First Search, Breadth First Search and Best First Search
Depth First Search, Breadth First Search and Best First SearchDepth First Search, Breadth First Search and Best First Search
Depth First Search, Breadth First Search and Best First Search
Adri Jovin
 
Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3
Traian Rebedea
 
nhận thiết kế clip quảng cáo giá tốt
nhận thiết kế clip quảng cáo giá tốtnhận thiết kế clip quảng cáo giá tốt
nhận thiết kế clip quảng cáo giá tốtraul110
 
Online Trading Concepts
Online Trading ConceptsOnline Trading Concepts
Online Trading Concepts
Fxmoneyworld LTD
 
문제는 한글이 잘 구현되는가?
문제는 한글이 잘 구현되는가?문제는 한글이 잘 구현되는가?
문제는 한글이 잘 구현되는가?
Choi Man Dream
 
1.9 b trees eg 03
1.9 b trees eg 031.9 b trees eg 03
1.9 b trees eg 03
Krish_ver2
 
4.1 webminig
4.1 webminig 4.1 webminig
4.1 webminig
Krish_ver2
 
5.4 randamized algorithm
5.4 randamized algorithm5.4 randamized algorithm
5.4 randamized algorithm
Krish_ver2
 
5.3 dyn algo-i
5.3 dyn algo-i5.3 dyn algo-i
5.3 dyn algo-i
Krish_ver2
 
4.2 bst 03
4.2 bst 034.2 bst 03
4.2 bst 03
Krish_ver2
 
4.2 bst 02
4.2 bst 024.2 bst 02
4.2 bst 02
Krish_ver2
 
РЕКЛАМНЫЕ МОДЕЛИ
РЕКЛАМНЫЕ МОДЕЛИРЕКЛАМНЫЕ МОДЕЛИ
РЕКЛАМНЫЕ МОДЕЛИ
Moscow State University of Printing Arts
 
RESUME-ARITRA BHOWMIK
RESUME-ARITRA BHOWMIKRESUME-ARITRA BHOWMIK
RESUME-ARITRA BHOWMIK
Aritra Bhowmik
 
2.4 mst prim &kruskal demo
2.4 mst  prim &kruskal demo2.4 mst  prim &kruskal demo
2.4 mst prim &kruskal demo
Krish_ver2
 
Top Forex Brokers
Top Forex BrokersTop Forex Brokers
Top Forex Brokers
Fxmoneyworld LTD
 

Viewers also liked (20)

2.5 dfs & bfs
2.5 dfs & bfs2.5 dfs & bfs
2.5 dfs & bfs
 
5.5 back tracking
5.5 back tracking5.5 back tracking
5.5 back tracking
 
DFS & BFS Graph
DFS & BFS GraphDFS & BFS Graph
DFS & BFS Graph
 
2.5 graph dfs
2.5 graph dfs2.5 graph dfs
2.5 graph dfs
 
Algorithum Analysis
Algorithum AnalysisAlgorithum Analysis
Algorithum Analysis
 
Depth First Search, Breadth First Search and Best First Search
Depth First Search, Breadth First Search and Best First SearchDepth First Search, Breadth First Search and Best First Search
Depth First Search, Breadth First Search and Best First Search
 
Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3
 
nhận thiết kế clip quảng cáo giá tốt
nhận thiết kế clip quảng cáo giá tốtnhận thiết kế clip quảng cáo giá tốt
nhận thiết kế clip quảng cáo giá tốt
 
Online Trading Concepts
Online Trading ConceptsOnline Trading Concepts
Online Trading Concepts
 
문제는 한글이 잘 구현되는가?
문제는 한글이 잘 구현되는가?문제는 한글이 잘 구현되는가?
문제는 한글이 잘 구현되는가?
 
1.9 b trees eg 03
1.9 b trees eg 031.9 b trees eg 03
1.9 b trees eg 03
 
4.1 webminig
4.1 webminig 4.1 webminig
4.1 webminig
 
5.4 randamized algorithm
5.4 randamized algorithm5.4 randamized algorithm
5.4 randamized algorithm
 
5.3 dyn algo-i
5.3 dyn algo-i5.3 dyn algo-i
5.3 dyn algo-i
 
4.2 bst 03
4.2 bst 034.2 bst 03
4.2 bst 03
 
4.2 bst 02
4.2 bst 024.2 bst 02
4.2 bst 02
 
РЕКЛАМНЫЕ МОДЕЛИ
РЕКЛАМНЫЕ МОДЕЛИРЕКЛАМНЫЕ МОДЕЛИ
РЕКЛАМНЫЕ МОДЕЛИ
 
RESUME-ARITRA BHOWMIK
RESUME-ARITRA BHOWMIKRESUME-ARITRA BHOWMIK
RESUME-ARITRA BHOWMIK
 
2.4 mst prim &kruskal demo
2.4 mst  prim &kruskal demo2.4 mst  prim &kruskal demo
2.4 mst prim &kruskal demo
 
Top Forex Brokers
Top Forex BrokersTop Forex Brokers
Top Forex Brokers
 

Similar to 2.5 bfs & dfs 02

BFS & DFS in Data Structure
BFS & DFS in Data StructureBFS & DFS in Data Structure
BFS & DFS in Data Structure
Meghaj Mallick
 
Breath first Search and Depth first search
Breath first Search and Depth first searchBreath first Search and Depth first search
Breath first Search and Depth first search
Kirti Verma
 
Graph Traversals
Graph TraversalsGraph Traversals
Graph Traversals
MaryJacob24
 
kumattt).pptx
kumattt).pptxkumattt).pptx
kumattt).pptx
gurukhade1
 
Algorithm
AlgorithmAlgorithm
Algorithm
Shakil Ahmed
 
Data Structures and Algorithm Analysis1. How much memory w.docx
Data Structures and Algorithm Analysis1. How much memory w.docxData Structures and Algorithm Analysis1. How much memory w.docx
Data Structures and Algorithm Analysis1. How much memory w.docx
randyburney60861
 
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
Deepak John
 
articulation points in graph
articulation points in grapharticulation points in graph
articulation points in graph
Savit Chandra
 
Analysis & design of algorithm
Analysis & design of algorithmAnalysis & design of algorithm
Analysis & design of algorithm
rahela bham
 
Graph
GraphGraph
Graph
GraphGraph
Graphs
GraphsGraphs
Analysis of Pathfinding Algorithms
Analysis of Pathfinding AlgorithmsAnalysis of Pathfinding Algorithms
Analysis of Pathfinding Algorithms
SigSegVSquad
 
Data Structures and Algorithm AnalysisSpring 2020 Post Midterm E
Data Structures and Algorithm AnalysisSpring 2020 Post Midterm EData Structures and Algorithm AnalysisSpring 2020 Post Midterm E
Data Structures and Algorithm AnalysisSpring 2020 Post Midterm E
jeniihykdevara
 
Bfs dfs
Bfs dfsBfs dfs
Bfs dfs
Praveen Yadav
 
130210107039 2130702
130210107039 2130702130210107039 2130702
130210107039 2130702
Ketaki_Pattani
 
Graph Theory
Graph TheoryGraph Theory
Graph Theory
Andrew Ferlitsch
 
Skiena algorithm 2007 lecture12 topological sort connectivity
Skiena algorithm 2007 lecture12 topological sort connectivitySkiena algorithm 2007 lecture12 topological sort connectivity
Skiena algorithm 2007 lecture12 topological sort connectivity
zukun
 
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
Amrinder Arora
 
lecture 18
lecture 18lecture 18
lecture 18
sajinsc
 

Similar to 2.5 bfs & dfs 02 (20)

BFS & DFS in Data Structure
BFS & DFS in Data StructureBFS & DFS in Data Structure
BFS & DFS in Data Structure
 
Breath first Search and Depth first search
Breath first Search and Depth first searchBreath first Search and Depth first search
Breath first Search and Depth first search
 
Graph Traversals
Graph TraversalsGraph Traversals
Graph Traversals
 
kumattt).pptx
kumattt).pptxkumattt).pptx
kumattt).pptx
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
Data Structures and Algorithm Analysis1. How much memory w.docx
Data Structures and Algorithm Analysis1. How much memory w.docxData Structures and Algorithm Analysis1. How much memory w.docx
Data Structures and Algorithm Analysis1. How much memory w.docx
 
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
 
articulation points in graph
articulation points in grapharticulation points in graph
articulation points in graph
 
Analysis & design of algorithm
Analysis & design of algorithmAnalysis & design of algorithm
Analysis & design of algorithm
 
Graph
GraphGraph
Graph
 
Graph
GraphGraph
Graph
 
Graphs
GraphsGraphs
Graphs
 
Analysis of Pathfinding Algorithms
Analysis of Pathfinding AlgorithmsAnalysis of Pathfinding Algorithms
Analysis of Pathfinding Algorithms
 
Data Structures and Algorithm AnalysisSpring 2020 Post Midterm E
Data Structures and Algorithm AnalysisSpring 2020 Post Midterm EData Structures and Algorithm AnalysisSpring 2020 Post Midterm E
Data Structures and Algorithm AnalysisSpring 2020 Post Midterm E
 
Bfs dfs
Bfs dfsBfs dfs
Bfs dfs
 
130210107039 2130702
130210107039 2130702130210107039 2130702
130210107039 2130702
 
Graph Theory
Graph TheoryGraph Theory
Graph Theory
 
Skiena algorithm 2007 lecture12 topological sort connectivity
Skiena algorithm 2007 lecture12 topological sort connectivitySkiena algorithm 2007 lecture12 topological sort connectivity
Skiena algorithm 2007 lecture12 topological sort connectivity
 
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
 
lecture 18
lecture 18lecture 18
lecture 18
 

More from Krish_ver2

5.5 back track
5.5 back track5.5 back track
5.5 back track
Krish_ver2
 
5.5 back tracking 02
5.5 back tracking 025.5 back tracking 02
5.5 back tracking 02
Krish_ver2
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructures
Krish_ver2
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructures
Krish_ver2
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03
Krish_ver2
 
5.3 dynamic programming
5.3 dynamic programming5.3 dynamic programming
5.3 dynamic programming
Krish_ver2
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
Krish_ver2
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquer
Krish_ver2
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
Krish_ver2
 
5.1 greedyyy 02
5.1 greedyyy 025.1 greedyyy 02
5.1 greedyyy 02
Krish_ver2
 
5.1 greedy
5.1 greedy5.1 greedy
5.1 greedy
Krish_ver2
 
5.1 greedy 03
5.1 greedy 035.1 greedy 03
5.1 greedy 03
Krish_ver2
 
4.4 hashing02
4.4 hashing024.4 hashing02
4.4 hashing02
Krish_ver2
 
4.4 hashing
4.4 hashing4.4 hashing
4.4 hashing
Krish_ver2
 
4.4 hashing ext
4.4 hashing  ext4.4 hashing  ext
4.4 hashing ext
Krish_ver2
 
4.4 external hashing
4.4 external hashing4.4 external hashing
4.4 external hashing
Krish_ver2
 
4.2 bst
4.2 bst4.2 bst
4.2 bst
Krish_ver2
 
4.1 sequentioal search
4.1 sequentioal search4.1 sequentioal search
4.1 sequentioal search
Krish_ver2
 
3.9 external sorting
3.9 external sorting3.9 external sorting
3.9 external sorting
Krish_ver2
 
3.8 quicksort
3.8 quicksort3.8 quicksort
3.8 quicksort
Krish_ver2
 

More from Krish_ver2 (20)

5.5 back track
5.5 back track5.5 back track
5.5 back track
 
5.5 back tracking 02
5.5 back tracking 025.5 back tracking 02
5.5 back tracking 02
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructures
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructures
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03
 
5.3 dynamic programming
5.3 dynamic programming5.3 dynamic programming
5.3 dynamic programming
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquer
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
 
5.1 greedyyy 02
5.1 greedyyy 025.1 greedyyy 02
5.1 greedyyy 02
 
5.1 greedy
5.1 greedy5.1 greedy
5.1 greedy
 
5.1 greedy 03
5.1 greedy 035.1 greedy 03
5.1 greedy 03
 
4.4 hashing02
4.4 hashing024.4 hashing02
4.4 hashing02
 
4.4 hashing
4.4 hashing4.4 hashing
4.4 hashing
 
4.4 hashing ext
4.4 hashing  ext4.4 hashing  ext
4.4 hashing ext
 
4.4 external hashing
4.4 external hashing4.4 external hashing
4.4 external hashing
 
4.2 bst
4.2 bst4.2 bst
4.2 bst
 
4.1 sequentioal search
4.1 sequentioal search4.1 sequentioal search
4.1 sequentioal search
 
3.9 external sorting
3.9 external sorting3.9 external sorting
3.9 external sorting
 
3.8 quicksort
3.8 quicksort3.8 quicksort
3.8 quicksort
 

Recently uploaded

বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
Nguyen Thanh Tu Collection
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
Nguyen Thanh Tu Collection
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Diana Rendina
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
imrankhan141184
 

Recently uploaded (20)

বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
 

2.5 bfs & dfs 02

  • 1. Graph Traversals Depth-First Search • Think Stack Breadth-First Search • Think Queue
  • 2. Overview  Goal  To systematically visit the nodes of a graph  A tree is a directed, acyclic, graph (DAG)  If the graph is a tree,  DFS is exhibited by preorder, postorder, and (for binary trees) inorder traversals  BFS is exhibited by level-order traversal
  • 3. Depth-First Search // recursive, preorder, depth-first search void dfs (Node v) { if (v == null) return; if (v not yet visited) visit&mark(v); // visit node before adjacent nodes for (each w adjacent to v) if (w has not yet been visited) dfs(w); } // dfs
  • 4. Depth-First Search // recursive, postorder, depth-first search void dfs (Node v) { if (v == null) return; tag(v); // mark v as having been considered for (each w adjacent to v) if (w has not yet been tagged) dfs(w); visit(v); // postorder traversal: visit node after // adjacent nodes } // dfs
  • 5. Depth-First Search // non-recursive, preorder, depth-first search void dfs (Node v) { if (v == null) return; push(v); while (stack is not empty) { pop(v); if (v has not yet been visited) mark&visit(v); for (each w adjacent to v) if (w has not yet been visited) push(w); } // while } // dfs
  • 6. Depth-First Search // non-recursive, postorder, depth-first search void dfs (Node v) { // Lex 20 (Do not need to submit) } // dfs
  • 7. Example 0 7 1 5 4 3 2 6 Policy: Visit adjacent nodes in increasing index order
  • 8. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 2 7 6 4
  • 9. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 Push 5
  • 10. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 Pop/Visit/Mark 5
  • 11. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 2 Push 2, Push 1
  • 12. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 2 Pop/Visit/Mark 1
  • 13. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 2 4 2 Push 4, Push 2, Push 0
  • 14. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 2 4 2 Pop/Visit/Mark 0
  • 15. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 7 2 4 2 Push 7, Push 3
  • 16. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 7 2 4 2 Pop/Visit/Mark 3
  • 17. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 2 7 2 4 2 Push 2
  • 18. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 2 7 2 4 2 Pop/Mark/Visit 2
  • 19. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 2 7 2 4 2 Pop/Mark/Visit 7
  • 20. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 2 7 6 2 4 2 Push 6
  • 21. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 2 7 6 2 4 2 Pop/Mark/Visit 6
  • 22. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 2 7 6 4 2 Pop (don’t visit) 2
  • 23. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 2 7 6 4 2 Pop/Mark/Visit 4
  • 24. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 2 7 6 4 Pop (don’t visit) 2
  • 25. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 2 7 6 4 Done
  • 26. Preorder DFS: Start with Node 5 Note: edge (0,3) removed 0 7 1 5 4 3 2 6 5 1 0 7 6 2 4 3
  • 27. Depth-First Search Policy: Don’t push nodes twice// non-recursive, preorder, depth-first search void dfs (Node v) { if (v == null) return; push(v); while (stack is not empty) { pop(v); if (v has not yet been visited) mark&visit(v); for (each w adjacent to v) if (w has not yet been visited && not yet stacked) push(w); } // while } // dfs
  • 28. Preorder DFS (Don’t push nodes twice). Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 7 6 4 2
  • 29. Postorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 2 3 6 7 0 4 1 5
  • 30. Breadth-first Search  Ripples in a pond  Visit designated node  Then visited unvisited nodes a distance i away, where i = 1, 2, 3, etc.  For nodes the same distance away, visit nodes in systematic manner (eg. increasing index order)
  • 31. Breadth-First Search // non-recursive, preorder, breadth-first search void bfs (Node v) { if (v == null) return; enqueue(v); while (queue is not empty) { dequeue(v); if (v has not yet been visited) mark&visit(v); for (each w adjacent to v) if (w has not yet been visited && has not been queued) enqueue(w); } // while } // bfs
  • 32. BFS: Start with Node 5 7 1 5 4 3 2 6 5 1 2 0 4 3 7 6 0
  • 33. BFS: Start with Node 5 7 1 5 4 3 2 6 5 0
  • 35. BFS: Visit 1 and 2 7 1 5 4 3 2 6 5 1 2 0
  • 37. BFS: Visit 0 and 4 7 1 5 4 3 2 6 5 1 2 0 4 0
  • 39. BFS: Visit nodes 3 and 7 7 1 5 4 3 2 6 5 1 2 0 4 3 7 0
  • 41. BFS: Visit 6 7 1 5 4 3 2 6 5 1 2 0 4 3 7 6 0
  • 42. Biconnectivity A connected undirected graph is biconnected if there are no vertices whose removal disconnects the rest of the graph. Example : If the nodes are computers and the edges are links, then if any computer goes down, network mail is unaffected, except, of course, at the down computer.
  • 43. Biconnectivity  If a graph is not biconnected, the vertices whose removal would disconnect the graph are known as articulation points. These nodes are critical in many applications.
  • 44. Biconnectivity The graph below is not biconnected: C and D are articulation points. The removal of C would disconnect G, and the removal of D would disconnect E and F, from the rest of the graph
  • 45. Biconnectivity • Depth-first search provides a linear-time algorithm to find all articulation points in a connected graph. 1.Starting at any vertex, perform a depth-first search and number the nodes as they are visited: num(v). 2.For every vertex v in the depth-first spanning tree, compute the lowest-numbered vertex called low(v), that is reachable from v by taking zero or more tree edges and then possibly one back edge (in that order).
  • 46. Biconnectivity  The depth-first spanning tree shows the preorder number first, and then the lowest-numbered vertex reachable (For each node num/low is given in the tree)
  • 47. Biconnectivity • The lowest-numbered vertex , low(v), can be efficiently computed by performing a postorder traversal of the depth-first spanning tree, and it is the minimum of 1)num(v) 2)the lowest num(w) among all back edges (v, w) 3)the lowest low(w) among all tree edges (v, w)
  • 48. Biconnectivity: How to decide Articulation Points The root is an articulation point if and only if it has more than one child (because if it has two children, removing the root disconnects nodes in different subtrees, and if it has only one child, removing the root merely disconnects the root) Any other vertex v is an articulation point if and only if v has some child w such that low(w) ≥ num(v). Example: Vertex D is an articulation point  low(E)= 4 ≥ num(D)=4 Vertex C is an articulation point  low(G)= 7 ≥ num(C)=3
  • 49. How many squares can you create in this figure by connecting any 4 dots (the corners of a square must lie upon a grid dot? TRIANGLES: How many triangles are located in the image below?
  • 50. There are 11 squares total; 5 small, 4 medium, and 2 large. 27 triangles. There are 16 one-cell triangles, 7 four-cell triangles, 3 nine-cell triangles, and 1 sixteen-cell triangle.
  • 52.
  • 54. Do the DFS and BFS for this graph 12 43 57 6
  • 55. DIFFERENT TRAVERSALS STARTING FROM VERTEX 1 PRE ORDER TRAVERSAL (ALSO CALLED DEPTH FIRST SEARCH) 1 2 3 4 5 6 7 POST ORDER TRAVERSAL 6 5 4 7 3 2 1 BREADTH FIRST SEARCH 1 2 4 3 5 6 7
  • 56. DEPTH FIRST SPANNING TREE 6•1 •4 •2 •5 •3 •6 •7
  • 57. DEPTH FIRST SPANNING TREE 6•1 •4 •2 •5 •3 •6 •7 1 2 3 6 5 4 7
  • 58. DEPTH FIRST SPANNING TREE 6•1 •4 •2 •5 •3 •6 •7 1 2 3 6 4 5 4 7
  • 59. DEPTH FIRST SPANNING TREE 6•1 •4 •2 •5 •3 •6 •7 1 2 3 6 4 5 4 4 7
  • 60. DEPTH FIRST SPANNING TREE 6•1 •4 •2 •5 •3 •6 •7 1 2 3 6 4 5 4 4 1 7
  • 61. DEPTH FIRST SPANNING TREE 6•1 •4 •2 •5 •3 •6 •7 1 2 3 6 4 5 4 4 1 7 7
  • 62. DEPTH FIRST SPANNING TREE 6•1 •4 •2 •5 •3 •6 •7 1 2 3 1 6 4 5 4 4 1 7 7
  • 63. DEPTH FIRST SPANNING TREE 6•1 •4 •2 •5 •3 •6 •7 1 2 1 3 1 6 4 5 4 4 1 7 7
  • 64. DEPTH FIRST SPANNING TREE 6•1 •4 •2 •5 •3 •6 •7 1 1 2 1 3 1 6 4 5 4 4 1 7 7
  • 65. DEPTH FIRST SPANNING TREE 6•1 •4 •2 •5 •3 •6 •7 1 1 2 1 3 1 6 4 5 4 4 1 7 7