SlideShare uma empresa Scribd logo
1 de 65
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

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Graphs bfs dfs
Graphs bfs dfsGraphs bfs dfs
Graphs bfs dfs
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First Search
 
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
 
Depth-First Search
Depth-First SearchDepth-First Search
Depth-First Search
 
Tree-In Data Structure
Tree-In Data StructureTree-In Data Structure
Tree-In Data Structure
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
 
Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
 
Binary search tree in data structures
Binary search tree in  data structuresBinary search tree in  data structures
Binary search tree in data structures
 
Singly link list
Singly link listSingly link list
Singly link list
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and Graphs
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
 
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptxPolynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptx
 
AVL Tree in Data Structure
AVL Tree in Data Structure AVL Tree in Data Structure
AVL Tree in Data Structure
 
Depth-First Search
Depth-First SearchDepth-First Search
Depth-First Search
 
Data structures using c
Data structures using cData structures using c
Data structures using c
 
Depth First Search ( DFS )
Depth First Search ( DFS )Depth First Search ( DFS )
Depth First Search ( DFS )
 
Tree
TreeTree
Tree
 
Spanning trees
Spanning treesSpanning trees
Spanning trees
 

Destaque

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
 
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
 
5.5 back tracking
5.5 back tracking5.5 back tracking
5.5 back trackingKrish_ver2
 
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 SearchAdri Jovin
 
Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3Traian 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
 
문제는 한글이 잘 구현되는가?
문제는 한글이 잘 구현되는가?문제는 한글이 잘 구현되는가?
문제는 한글이 잘 구현되는가?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 03Krish_ver2
 
5.4 randamized algorithm
5.4 randamized algorithm5.4 randamized algorithm
5.4 randamized algorithmKrish_ver2
 
5.3 dyn algo-i
5.3 dyn algo-i5.3 dyn algo-i
5.3 dyn algo-iKrish_ver2
 

Destaque (20)

Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
 
Bfs and dfs in data structure
Bfs and dfs in  data structure Bfs and dfs in  data structure
Bfs and dfs in data structure
 
2.5 dfs & bfs
2.5 dfs & bfs2.5 dfs & bfs
2.5 dfs & bfs
 
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
 
5.5 back tracking
5.5 back tracking5.5 back tracking
5.5 back tracking
 
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
 

Semelhante a 2.5 bfs & dfs 02

BFS & DFS in Data Structure
BFS & DFS in Data StructureBFS & DFS in Data Structure
BFS & DFS in Data StructureMeghaj 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 searchKirti Verma
 
Graph Traversals
Graph TraversalsGraph Traversals
Graph TraversalsMaryJacob24
 
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.docxrandyburney60861
 
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
 
articulation points in graph
articulation points in grapharticulation points in graph
articulation points in graphSavit Chandra
 
Analysis & design of algorithm
Analysis & design of algorithmAnalysis & design of algorithm
Analysis & design of algorithmrahela bham
 
Analysis of Pathfinding Algorithms
Analysis of Pathfinding AlgorithmsAnalysis of Pathfinding Algorithms
Analysis of Pathfinding AlgorithmsSigSegVSquad
 
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 Ejeniihykdevara
 
Bfs & dfs application
Bfs & dfs applicationBfs & dfs application
Bfs & dfs applicationUmme habiba
 
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 connectivityzukun
 
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
 

Semelhante a 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
 
Bfs & dfs application
Bfs & dfs applicationBfs & dfs application
Bfs & dfs application
 
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
 

Mais de Krish_ver2

5.5 back track
5.5 back track5.5 back track
5.5 back trackKrish_ver2
 
5.5 back tracking 02
5.5 back tracking 025.5 back tracking 02
5.5 back tracking 02Krish_ver2
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructuresKrish_ver2
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructuresKrish_ver2
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03Krish_ver2
 
5.3 dynamic programming
5.3 dynamic programming5.3 dynamic programming
5.3 dynamic programmingKrish_ver2
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03Krish_ver2
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquerKrish_ver2
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03Krish_ver2
 
5.1 greedyyy 02
5.1 greedyyy 025.1 greedyyy 02
5.1 greedyyy 02Krish_ver2
 
4.4 hashing ext
4.4 hashing  ext4.4 hashing  ext
4.4 hashing extKrish_ver2
 
4.4 external hashing
4.4 external hashing4.4 external hashing
4.4 external hashingKrish_ver2
 
4.1 sequentioal search
4.1 sequentioal search4.1 sequentioal search
4.1 sequentioal searchKrish_ver2
 
3.9 external sorting
3.9 external sorting3.9 external sorting
3.9 external sortingKrish_ver2
 

Mais de 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
 

Último

mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
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
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 

Último (20)

mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
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
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 

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