Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
53 views
18 pages
Basic Search and Traversal Techniques
Uploaded by
Hrik Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download
Save
Save Basic Search and Traversal Techniques For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
53 views
18 pages
Basic Search and Traversal Techniques
Uploaded by
Hrik Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Carousel Previous
Carousel Next
Download
Save
Save Basic Search and Traversal Techniques For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 18
Search
Fullscreen
Chapter-5 Basic Traversal and Search Techniques 5.1 Techniques for Binary Trees Binary Tree A binary tree is a finite set of nodes which is either empty or consists of a root and two disjoint binary trees called the left sub tree and the right sub tree. In a traversal of a binary tree, each element of the binary tree is visited exactly at once. During the visiting of an element, all actions like clone, display, evaluate the operator ete is taken with respect to the element, When traversing a binary tree, we need to follow linear order ie. L, D, R where L>Moving left D->printing the data R->moving right We have three traversal techniques on binary tree, They are* Inorder © Post order * Pre order Examples For fig: 1 S © In order: A-B-C-D-E-F-G-H-I Post order Pre order: Preorder, post order and in order algorithms Algorithm preorder(x) Input: x is the root of a sub tree. 1.1fx #NULL. 2. Then output key(x); 3. Preorder (lefi(x)); 4, Preorder (right(x)); o 2 O»O Algorithm postorder(x) Input: x is the root of a subtree L.Ifx #NULL,2. Then postorder(lefi(x));; 3. Postorder(right(x)); 4, Outputkey(x); ©, Oe» Algorithm inorder(x) Input: x is the root of a subtree 1. If x¢ null 2. Then inorder(left(x)); 3. Outputkey(x); 4, Inorder(right(x)); Qa o © Exercises Graph A G5)cet yo oN fo aN S a » » oD a oy) D 5.2 Techniques for Graphs Graph: The sequence of edges that connected two vertices. ‘A graph is a pair (V, E), where Vis a set of nodes, called vertices Eig collection (can be duplicated) of pairs of vertices, called edges Vertices and edges are data structures and store elements. Types of graphs: Graphs are of three types. a. Directed/Undirected: In a directed graph the direction of the edges must be considered Fig 5.1 Fig 5.2 b. Weighted/ Unweighted: A weighted graph has values on its edge. Ge vON @? iS Fig 5.3 Fig 5.4 (~) On eae ©)¢, Cyelic/Acyelic: A eyele is a path that begins and ends at same vertex and A graph with no cycles is aeyelie, Representation of graphs Graphs can be represented in three ways (i) Adjaceney Matrix: A V x V array, with matrix[/][j] storing whether there is an edge between the ith vertex and the jth vertex. This matrix is also called as “Bit matrix” or “Boolean Matrix” __a_b e ayo. 1 10 b 1 ° ° 1 °o <]1 © © © 0 dJ 1 1 0 0 4 elo 0 0 1 © abc d_e afo 10 710 © bl] 0 0 © 1 0 c]1 © 0 0 0 d] 1 0 0 0 4 elo 0 0 0 0 (ii) Adjacency list: One linked list per vertex, each storing directly reachable verticesofolo}ol» e}ofalo|}» (iii) Linked List or Edge list: 32 Qo i 335 Graph traversal techniques “The process of traversing all the nodes or vertices on a graph is called graph traversal”. ‘We have two traversal techniques on graphs DFS BES Depth First Search The DFS explore each possible path to its conclusion before another path is tried. In other words go as a far as you can (if u don’t have a node to visit), otherwise, go back and try another way. Simply it can be called as “backtracking” Steps for DFS (i Select an unvisited node ‘v’ visits it and treats it as the current node. (ii) Find an unvisited neighbor of current node, visit it and make it new current node(iii) If the current node has no unvisited neighbors, backtrack to its parent and make it as a new current node (iv) Repeat steps 2 and 3 until no more nodes can be visited (v) Repeat from step | for remaining nodes also. Implementation of DFS DFS (Vertex) { Mark was visiting For each vertex V directly reachable from u Ifvis unvisited DFS (¥) } unexplored vertex @® visited vertex unexplored edge » discovery edge _ + backedge Unexplored vertex: The node or vertex which is not yet visited. Visited vertex: The node or vertex which is visited is called ‘visited vertex’ i.e, can be called as “current node”. Unexplored edge: The edge or path which is not yet traversed. Discovery edge: It is opposite to unexplored edge, the path which is already traversed is known as discovery edge Back edge: If the current node has no unvisited neighbors we need to backtrack to its parent node. The path used in back tracking is called back edge. For the following graph the steps for tracing are as follows:Properties of DFS i) DFS G, v) visits all the vertices and edges in the connected component of v. ii) The discovery edges labeled by DFS (G, v) form a spanning tree of the connected component of v.‘Tracing of graph using Depth First Search @ @— To © a rj<—_) topVertex nextNeighbor Visited vertex vertexStack —traversalOrder (top to bottom) (front to back) A A A A A B B BA AB B BA E E EBA ABE E EBA F F FEBA ABEF F FEBA c ic CFEBA ABEFC FEBA FEBA H H HFEBA ABEFCH HFEBA I I IHFEBA, ABEFCHI HFEBA FEBA EBA BA A A D D DA ABEFCHID D DA GDA ABEFCHIDG DA A empty ABEFCHIDG m0 a poem >90Exercise @Q Depth: W-U-V-Y-X-Z 2, Depth: A-B-C-E-D 3 Depth: 1-2-3-4-5-6-7-8-9-10-11-12,5.3 Breadth First Search It is one of the simplest algorithms for searching or visiting cach vertex in a graph, In this ‘method each node on the same level is checked before the search proceeds to the next level. BFS makes use of a queue to store visited vertices, expanding the path from the earliest visited vertices Breadth: a- Steps for BFS: 1, Mark all the vertices as unvisited, 2. Choose any vertex say ‘v’, mark it as visited and put it on the end of the queue. 3. Now, for each vertex on the list, examine in same order all the vertices adjacent to ‘v" 4, When all the unvisited vertices adjacent to v have been marked as visited and put it on the end (rear of the queue) of the list. 5. Remove a vertex from the front of the queue and repeat this procedure. 6. Continue this procedure until the list is empty. Breadth-first search Implementation of BFS While queue Q not empty De queue the first vertex u from Q For each vertex v directly reachable from u Ifv is unvisited En queue v to Q Mark vas visited Initially all vertices except the start vertex are marked as unvisited and the queue contains the start vertex only. Explored vertex: A vertex is said to be explored if all the adjacent vertices of v are visited. Example 1: Breadth first search for the following graph:unexplored vertex visited vertex unexplored edge discovery edge crossedge ayProperties of BFS Notation: Gs (connected component of s) i) BFS (G, s) visits all the vertices and edges of Gs ii) The discovery edges labeled by BFS (G, s) form a spanning tree Ts of G ili) For each vertex v in Li a. The path of Ts from s to v has i edges b. Every path from s to v in Gs has at least i edges. Complexity of BFS Step: read a node from the queue O (v) times, Step2: examine all neighbors, i.e. we examine all edges of the currently read node. Not oriented graph: 2*B edges to examine Hence the complexity of BFS is O (V + 2*E)Tracing of graph using Breadth first search: IST g iS F FroatVertox | mextNelghborg Vited vata vertexQecue A A iN cone B B 8 AB > D ap an t E apr ARE 5 DE > E 6 £G ARDEG ke G F F or Amprer H " orn ABDEGEH Ic Fu F 4 c c uc ABDEGFHC hi c 1 cl AmpEGruct kc 1 f ew xd mie WW t Lie é tye ef Oe ee x {h ) of BFS: 7-11-8-2-9-10-5-3BFS: A-B-C-D-E-F-G-H 5.4 Connected Components and Spanning Trees Connected component: If G is connected undirected graph, then we can visit all the vertices of the graph in the first call to BFS. The sub graph which we obtain after traversing the graph using BFS represents the connected component of the graph. ORY Thus BES can be used to determine whether G is connected. All the newly visited vertices on call to BFS represent the vertices in connected component of graph G. The sub graph formed by theses vertices make the connected component. Spanning tree of a graph: Consider the set of all edges (u, w) where all vertices w are adjacent to u and are not visited. According to BFS algorithm it is established that this set of edges give the spanning tree of G, if G is connected. We obtain depth first search spanning tree similarly These are the BFS and DFS spanning trees of the graph G‘onneeted Components connected if it remains connected after removal A connected undirected graph is said to be bi of any one vertex and the edges that are incident upon that vertex. In this we have two components. i. Articulation point: Let G= (V, E) be a connected undirected graph. Then an articulation point of graph ‘G’ is a vertex whose articulation point of graph is a vertex whose removal disconnects the graph ‘G’. It is also known a ii. Bi-connected graph: A graph ‘G’ is said to be bi-connected if it contains no-articulation point. So8 Articulation points for the above undirected graph are B, E, F “cut point”, i) After del components ing vertex B and incident edges of B, the given graph is divided into twoii) After deleting the vertex E and incident edges of E, the resulting components are iii) After deleting vertex F and incident edges of F, the given graph is divided into teo components.Note: If there exists any articulation point, it is an undesirable feature in communication network where joint point between two networks failure in case of joint node fails. Algorithm to construct the Bi- Connected graph 1. For each articulation point ‘a’ do 2. Let BI, B2, B3 ....Bk are the Bi-connected components 3. Containing the articulation point ‘a’ 4, Let ViE Bi, Vi fa i<-i<-k 5, Add (Vi,Vi+1) to Graph G. Vi-vertex belong Bi Bi-Bi-connected component i- Vertex number I to k a+ articulation point Exercise cmt
You might also like
Graph Treversal (BFS-DFS) Unit 5
PDF
No ratings yet
Graph Treversal (BFS-DFS) Unit 5
16 pages
Presentation of Artificial Intligence
PDF
No ratings yet
Presentation of Artificial Intligence
42 pages
UNIT 4-Graph-1
PDF
No ratings yet
UNIT 4-Graph-1
59 pages
BCA Semester IV Design & Analysis of Algorithms Module 5
PDF
No ratings yet
BCA Semester IV Design & Analysis of Algorithms Module 5
24 pages
Breadth First Search-BFS
PDF
No ratings yet
Breadth First Search-BFS
24 pages
GRAPHS
PDF
No ratings yet
GRAPHS
43 pages
Graph New
PDF
No ratings yet
Graph New
31 pages
6 Chapter 5 Basic Traversal and Search Techniques
PDF
No ratings yet
6 Chapter 5 Basic Traversal and Search Techniques
19 pages
Ad. Algo
PDF
No ratings yet
Ad. Algo
9 pages
2 1graph
PDF
No ratings yet
2 1graph
70 pages
Graph 2
PDF
No ratings yet
Graph 2
22 pages
DAA Unit2
PDF
No ratings yet
DAA Unit2
53 pages
Data Structure 2vb Imp
PDF
No ratings yet
Data Structure 2vb Imp
3 pages
Traversing Graphs:: A Journey Through Connected Data
PDF
No ratings yet
Traversing Graphs:: A Journey Through Connected Data
19 pages
L21 Graphs Clean
PDF
No ratings yet
L21 Graphs Clean
166 pages
Best Graph Notes
PDF
No ratings yet
Best Graph Notes
8 pages
Unit II Notes
PDF
No ratings yet
Unit II Notes
25 pages
design & Analysis of Algorithms Graph Traversal Algorithms PCC-CSBS601
PDF
No ratings yet
design & Analysis of Algorithms Graph Traversal Algorithms PCC-CSBS601
13 pages
Ch8 Graph
PDF
No ratings yet
Ch8 Graph
22 pages
Graph Traversal Techniques
PDF
No ratings yet
Graph Traversal Techniques
31 pages
BFS and DFS 29
PDF
No ratings yet
BFS and DFS 29
95 pages
Graph Traversal Tech
PDF
No ratings yet
Graph Traversal Tech
47 pages
Data Structures & Algorithms PPT 4
PDF
No ratings yet
Data Structures & Algorithms PPT 4
36 pages
Graphs
PDF
No ratings yet
Graphs
28 pages
8 Graph
PDF
No ratings yet
8 Graph
49 pages
DS Graphs
PDF
No ratings yet
DS Graphs
88 pages
Unit 4 Graphs
PDF
No ratings yet
Unit 4 Graphs
76 pages
Unit 5
PDF
No ratings yet
Unit 5
78 pages
Graph Traversal - DFS & BFS
PDF
100% (1)
Graph Traversal - DFS & BFS
42 pages
Graphs
PDF
No ratings yet
Graphs
16 pages
Graphalgorithms-Bfs and Dfs
PDF
No ratings yet
Graphalgorithms-Bfs and Dfs
14 pages
Unit 5 Graphs
PDF
No ratings yet
Unit 5 Graphs
11 pages
DSA MK Lect4 PDF
PDF
No ratings yet
DSA MK Lect4 PDF
73 pages
Unit 5
PDF
No ratings yet
Unit 5
19 pages
CS3401 ALG UNIT 2 NOTES EduEngg
PDF
No ratings yet
CS3401 ALG UNIT 2 NOTES EduEngg
25 pages
Graphs
PDF
No ratings yet
Graphs
8 pages
Mathematical Foundations of Computer Science - UNIT-5
PDF
50% (2)
Mathematical Foundations of Computer Science - UNIT-5
17 pages
Unit 4 Ds (Cs303) 7 Ques With Ans
PDF
No ratings yet
Unit 4 Ds (Cs303) 7 Ques With Ans
18 pages
Checkpoint Week 2
PDF
No ratings yet
Checkpoint Week 2
13 pages
Graph
PDF
No ratings yet
Graph
53 pages
Unit 5 DS
PDF
No ratings yet
Unit 5 DS
30 pages
EXPERIMENT2
PDF
No ratings yet
EXPERIMENT2
6 pages
Breadth First Search
PDF
No ratings yet
Breadth First Search
8 pages
Graphs
PDF
No ratings yet
Graphs
49 pages
DFS BFS
PDF
No ratings yet
DFS BFS
23 pages
Unit-5 21CSC201J
PDF
No ratings yet
Unit-5 21CSC201J
24 pages
Unit3 5
PDF
No ratings yet
Unit3 5
12 pages
Class ppt1 Graphs
PDF
No ratings yet
Class ppt1 Graphs
22 pages
Exp 12
PDF
No ratings yet
Exp 12
7 pages
Httpslms Ou Edu Vn241pluginfile Php217729mod - resourcecontent1DsA-04-Graph PDF
PDF
No ratings yet
Httpslms Ou Edu Vn241pluginfile Php217729mod - resourcecontent1DsA-04-Graph PDF
36 pages
s20 Bfs Dfs Apps
PDF
No ratings yet
s20 Bfs Dfs Apps
13 pages
DSA Unit 5
PDF
No ratings yet
DSA Unit 5
36 pages
Graph Algorithms: BFS, Dfs
PDF
No ratings yet
Graph Algorithms: BFS, Dfs
8 pages
Graph
PDF
No ratings yet
Graph
26 pages
Unit - 5
PDF
No ratings yet
Unit - 5
34 pages
Unit 5ds
PDF
No ratings yet
Unit 5ds
16 pages
Digital Assignment Theory 18bca0045
PDF
No ratings yet
Digital Assignment Theory 18bca0045
16 pages
Basic Traversal and Search Techniques
PDF
No ratings yet
Basic Traversal and Search Techniques
10 pages