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)
23 views
34 pages
Daa Mod 3
daa
Uploaded by
su2222man
AI-enhanced title
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 DAA MOD 3 For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
0 ratings
0% found this document useful (0 votes)
23 views
34 pages
Daa Mod 3
daa
Uploaded by
su2222man
AI-enhanced title
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 DAA MOD 3 For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
Download
Save DAA MOD 3 For Later
You are on page 1
/ 34
Search
Fullscreen
Definition oP © A graph ¢ is defined as an ordered set (v, £), where v(c) represents the set of vertices and €(6) represents the edges that connect these vertices. O-® Figure 13.1 shows a graph with v(6) = (4, B, C, DandE) and E(6) = ((A, 8), (8, C)» Figure 19.1. Undirected (As 9), (8, 0), (0, £), (C, €)}. Note that there are five vertices or nodes and six graph edges in the graph.13.6 GRAPH TRAVERSAL ALGORITHMS Inthis section, we will discuss how to traverse graphs. By traversing a graph, we mean the method of examining the nodes and edges of the graph. There are two standard methods of graph traversal which we will discuss in this section. These two methods are: pin, 16 “= SD PAg Resuk:- 0 44 2 ye C1925 ey Adjarauk + unvisited vertices o} mode t inbented in the quan 02,9456 5 Randy Shalt ) apaitivg a be posted w aadjastat node Searels STH CMe une Coie Comply Ding ARF ode (9) igen > fp 0 prmesssed wer gUor= 0 0-0 sulle TRG ute ar pressed 5) 0 maiguhoun #7 2007 Sad su (enmuone) 39 reve poets 1, Breadth-first search 2, Depth-first search ataih oF Fy 9) CR Coratue a) ar [loaitg) ef Aap 9 Table 13.1 Value of status and its significance Status| State of the Description node a_| Ready The initial state of the node N 2 [waiting Node Nis placed on the queue on stack and waiting to be processed 3__| Processed Node N has been conpletely processed 13.6.1 Breadth-First Search Algorithm Breadth-first search (BFS) is a graph search algorithm that begins at the root node and explores all the neighbouring nodes. Then for each of those nearest nades, the algorithm (Fig, 13,20) explores their imexplored neighbour nodes, and so on, until it finds the goal, ‘That is, we start examining the node a and then all the SET STATUS = 1 (ready state) for each node in G s Engueue the starting node A and set its STATUS = 2 (waiting state) : Repeat Steps 4 and 5 until QUELE is ompty Dequeue 2 node N. Process it and set its STATUS = 3 (processed state). Enqueue all the neighbours of N that are in the ready state (hose STATUS = 1) and set their STATUS = 2 (waiting state) [EKO OF LooP] EXIT Figure 13.20 Algorithm for breadth-lirst search neighbours of'aare examined, In thenextstep, we examine the neighbours of neighbours of 4, so on and so forth. This means that we need to track the neighbours of the node and guarantee that every node in the graph is processed and no node is processed more than once, This is accomplished by using a queue that will hold the nodes that are waiting, for further processing and a variable status to represent the current state of the nade.Features of Breadth-First Search Algorithm Space complexity nthe breadth-first search algorithm, all the nodes at a particular level must be saved until their child nodes in the next level have been generated. The space complexity is therefore proportional to the number of nodes at the deepest level of the graph. Given graph with branching factor b (number of children at each node) and depth ¢, the asymptotic space complexity is the number of nodes at the deepest level o(b*). Ifthe number of vertices and edges in the graph are known ahead of time, the space complexity can also be expressed as 0. (| €| + | V1), Where | € | is the total number of edges in ¢ and Iv | is the number of nodes or vertices. Time complexity In the worst case, breadth-first search has to traverse through all paths to all possible nodes, thus the time complexity of this algorithm asymptotically approaches o(b*).. However, the time complexity can also be expressed as 0 | € | + | V| ), since every vertex and every edge will be explored in the worst case.13.6.2 Depth-first Search Algorithm The depth-first search algorithm (Fig. 13.22) progresses by expanding the starting node of 6 and then going deeper and decper until the goal node is found, or until a node that has no children is encountered. When a dead-end is reached, the algorithm backtracks, returning to the most recent, node that has not been completely explored In other words, depth-first search begins at a starting node a which becomes the current node. Then, it examines cach node v along a path P which begins at a. That is, we process a neighbour of a, then a neighbour of neighbour of a, and so on. During the execution of the algorithm, if we reach a path that has a node w that has already been processed, then we backtrack to the current node, Otherwise, the unvisited (unprocessed) node becomes the current node, SET STATUS = 1 (ready state) for each node in G Step 2: Push the starting node A on the stack and set its STATUS = 2 (waiting state) Step 3: Repeat Steps 4 and 5 until STACK is enpty Step 4: Pop the top node N. Process it and set its STATUS = 3 (processed state) Step 5: Push on the stack all the neighbours of i that are in the ready state (whose STATUS = 1) and set their STATUS = 2 (waiting state) [END OF Loop) step 6: EXIT Figure 13,22 Algorithm for depth-first search The algorithm proceeds like this until we reach a dead-end (end of path p), On reaching the dead- end, we backtrack to find another path p’, The algorithm terminates when backtracking leads back to the starting node a. In this algorithm, edges that lead to a new vertex are called discovery edges and edges that lead to an already visited vertex are called back edges. Observe that this algorithm is similar to the in-order traversal of a binary tree, Its implementation is similar to that of the breadth-first search algorithm but here we use a stack instead of a queue. Again, we use a variable status to represent the current state of the node.Rant - S Podinr - Ge Ror - A Pour - Posir - F Pont - e Pyink- B Pyink - © Stock - A? Srook- 8 87 Stock - Be Ge” Stock - BC FW Stock - Be FE” Stk - 8 ©? Steck - 8 27 Stok - 8x Stack - 87Features of Depth-First Search Algorithm Space complexity The space complexity of a depth-first search is lower than that of a breadth- first search. Time complexity The time complexity of a depth-first search is proportional to the number of vertices plus the number of edges in the graphs that are traversed. The time complexity can be given as (o( |v] + JE|)).BFS DFS BFS stands for “’Breadth DFS stsnds for Depth First First Search” Search. BFS traverses the tree level | DFS traverses the tree Depth wise. wise. BFS is implemented using DFS is implemented using Stack queue which is FIFP list. which is LIFO list. It’s single step algorithm, It’s two step algorithm. In first wherein the visited vertices | stage, the visited vertices are are removed from the queue | pushed onto the stack and later and then displayed at once. | on when there is no vertex further to visit those are popped out. BFS requires more memory | DFS requires less memory compare to DFS. compare to BFS. Applications of BFS: Applications of DFS 1) To find shortest path. DUseful in Cycle detection. 11) In Spanning tree. iin Connectivity testing. iii) In Connectivity. iii)In finding Spanning trees & forest. Always provides shallowest path solution. No backtracking is required in BFS. Does'nt guarantee shallowest path solution. Backtracking is implemented in DFS. Generally gets trapped into infinite loops. Can never get trapped into finite loops.13.7 TOPOLOGICAL SORTING Topological sort of a directed acyclic graph (DAG) c is defined as a linear ordering of its nodes in which each node comes before all nodes to which it has outbound edges. Every DAG has one or more number of topological sorts, A topological sort of a DAG is an ordering of the vertices of 6 such that if. contains an edge (v, v), then u appears before v in the ordering. Note that topological sort is possible only on Example 13.3 Consider three DAGs shown in Fig. 13.24 and their possible topological sorts. Q ® & @ 1) SO @ © Topological can be aiven as AB.CD.E be poo BEO mom Figure 13.24 Topological sort One main property of a DAG is that more the number of edges in a DAG, fewer the number of topological orders it has. This is because each edge (u, v) forces node u to occur before v, which restricts the number of valid permutations of the nodes. directed acyclic graphs that do not have any cycles. For a DAG that contains cycles, no linear ordering of its vertices is possible. Algorithm The algorithm for the topological sort of a graph (Fig, 13.25) that has no cycles focuses on selecting a node w with zero in-degree, that is, a node that has no predecessor. The two main steps involved in the topological sortalgorithm include: * Selecting a node with zero in-degree © Deleting from the graph along with its edges in the graph FRONT = FRONT + 1 [ED OF LOOP] Step 6: Exit Figure 13.25 Algorithm for topological sort Step 1; Find the in-degree INDEG(N) of every node Step 2: Enqueue all the nodes with a zero in-degree Step 3: Repeat Steps 4 and 5 until the QUEUE is enpty Step 4: Renave the front node N of the QUEUE by sotting Step 5: Repeat for each neighbour M of node N: 2) Delete the edge fron N to H by setting TINDEG(H) = TADEG(H) ~ 2 ) TF IRDEG(H) = 0, then Enqueue M, that is, add M to the rear of the queve [END OF INNER LOOP)Topological Sort or Topological Ordering ¢It is linear ordering of graph vertices such that for every directed edge uv from vertex u to vertex v, u comes before v in the ordering. * Applicable on DAG(Direct Acyclic Graph) * Linear running time complexity. * Topological sort is not unique.Minimum Spanning Tree Before knowing about the minimum spanning tree, we should know about the spanning tree. To understand the concept of spanning tree, consider the below graph:The above graph can be represented as G(V, E), where 'V' is the number of vertices, and 'E' is the number of edges. The spanning tree of the above graph would be represented as G’(V’, E). In this case, V = V means that the number of vertices in the spanning tree would be the same as the number of vertices in the graph, but the number of edges would be different. The number of edges in the spanning tree is the subset of the number of edges in the original graph. Therefore, the number of edges can be written as:B€eE It can also be written as: E=|vi-1 Two conditions exist in the spanning tree, which is as follows: o The number of vertices in the spanning tree would be the same as the number of vertices in the original graph. V=V ° The number of edges in the spanning tree would be equal to the number of edges minus 1. E=|v|/-1 ° The spanning tree should not contain any cycle. o The spanning tree should not be disconnected.Consider the below graph: The above graph contains 5 vertices. As we know, the vertices in the spanning tree would be the same as the graph; therefore, V’ is equal 5. The number of edges in the spanning tree would be equal to (5 - 1), i.e., 4. The following are the possible spanning trees:The minimum spanning tree is a spanning tree whose sum of the edges is minimum. Consider the below graph that contains the edge weight: The following are the spanning trees that we can make from the above graph. o The first spanning tree is a tree in which we have removed the edge between the vertices 1 and 5 shown as below: The sum of the edges of the above tree is (1+44+5+42):12 ° The second spanning tree is a tree in which we have removed the edge between the vertices 1 and 2 shown as below: The sum of the edges of the above tree is (8+24+5+4+4):14o The third spanning tree is a tree in which we have removed the edge between the vertices 2 and 3 shown as below: The sum of the edges of the above treeis(1+3+2+5):11 o The fourth spanning tree is a tree in which we have removed the edge between the vertices 3 and 4 shown as below: The sum of the edges of the above tree is (1+ 3 + 2+ 4): 10. The edge cost 10 is minimum so it is a minimum spanning tree.If we remove any edge from the spanning tree, then it becomes disconnected. Therefore, we cannot remove any edge from the spanning tree. If we add an edge to the spanning tree then it creates a loop. Therefore, we cannot add any edge to the spanning tree. In a graph, each edge has a distinct weight, then there exists only a single and unique minimum spanning tree. If the edge weight is not distinct, then there can be more than one minimum spanning tree. A complete undirected graph can have an n™2 number of spanning trees. Every connected and undirected graph contains atleast one spanning tree. The disconnected graph does not have any spanning tree. In a complete graph, we can remove maximum (e-n+1) edges to construct a spanning tree.The number of spanning trees that can be made from the above complete graph equals to n™2 = 442 = 16. Therefore, 16 spanning trees can be created from the above graph. The maximum number of edges that can be removed to construct a spanning tree equals to e-n+1 =6-4+4+1=3.Spanning tree - A spanning tree is the subgraph of an undirected connected graph. Minimum Spanning tree - Minimum spanning tree can be defined as the spanning tree in which the sum of the weights of the edge is minimum. The weight of the spanning tree is the sum of the weights given to the edges of the spanning tree. Now, let's start the main topic. Prim's Algorithm is a greedy algorithm that is used to find the minimum spanning tree from a graph. Prim's algorithm finds the subset of edges that includes every vertex of the graph such that the sum of the weights of the edges can be minimized. Prim's algorithm starts with the single node and explores all the adjacent nodes with all the connecting edges at every step. The edges with the minimal weights causing no cycles in the graph got selected.Potsm’s Algoatthm ) Stool ay Romove all Loops amd porcatel edges ig past: 3) Choose ony vcditary So : A) Creek oukgoing edger amd aalesk the one with Less esl, amd add ae que mee Lik does mot gore 2 ge] 5 Karp vupesting atep A until we get & aninimurn Spam mig tee:ne = 4 Ae =8 Thm B Bp=6 v Be =3 do net , =8 4 [Re = 8] fonger gS 4 © ma 4 2 (F) Thane : y Cex cAXK © 7-© ee 7 Ck = aw Vien & ECE epee 40 not hen Ee e’elvi-t Then Re =e foamet” Ee x SE-1=5 eEp-=o 7 EF = 2 v7 (omy ous coors) it do vot ForgetWhat is meant by Kruskal algorithm? Kruskal's algorithm is a minimum spanning ATc\M-1e (OVA NAM AEs ME] Coro Me e=) 8) gE n)eL01 Me LaLe| finds the subset of the edges of that graph which. form a tree that includes every vertex. has the minimum sum of weights among all the trees that can be formed from the graph.In Kruskal's algorithm, we start from edges with the lowest weight and keep adding the edges until the goal is reached. The steps to implement Kruskal's algorithm are listed as follows - o First, sort all the edges from low weight to high. o Now, take the edge with the lowest weight and add it to the spanning tree. If the edge to be added creates a cycle, then reject the edge. ° Continue to add the edges until we reach all vertices, and a minimum spanning tree is created.(No GyeLE) +X L- 6 TeIt starts to build the Minimum Spanning Tree from any vertex in the graph. It traverses one node more than one time to get the minimum distance. Prim’s algorithm has a time complexity of O(V*2), V being the number of vertices. Prim’s algorithm gives connected component as well as it works only on connected graph. Prim’s algorithm runs faster in dense graphs. It starts to build the Minimum Spanning Tree from the vertex carrying minimum weight in the graph. It traverses one node only once. Kruskal's algorithm's time complexity is O(logV), V being the number of vertices. Kruskal's algorithm can generate forest(disconnected components) at any instant as well as it can work on disconnected components Kruskal's algorithm runs faster in sparse graphs.Oi kstna’ (terug) A\gosatn- D Shou 2 Mask atl nodes umvisited y i istamee = S Assign to evewy nods a tentative oes! Beene §e* ; eon iP < Zero iS owe i i age ‘ OU ofr nodes . Sek te ital WO A . ) Fox tne ernvwnt ode , considers on eamtve vaignbors and eoleuloli wwe he M4 Austougle tue erent ot men Calewtatrd tentative bist Sate Ae asrianod fo tre naignsour amd os& SMollwe ove + U ALBAN
You might also like
Lecture 2 - Graph Traversal
PDF
No ratings yet
Lecture 2 - Graph Traversal
68 pages
Data Structures and Algorithm: Graphs
PDF
No ratings yet
Data Structures and Algorithm: Graphs
28 pages
COMP20007 Design of Algorithms: Graph Traversal
PDF
No ratings yet
COMP20007 Design of Algorithms: Graph Traversal
22 pages
Session9 DFS
PDF
No ratings yet
Session9 DFS
25 pages
Artificial Intelligence Project Presentation
PDF
No ratings yet
Artificial Intelligence Project Presentation
34 pages
Week8 PDF
PDF
No ratings yet
Week8 PDF
44 pages
22 - Elementary Graph Algorithms
PDF
No ratings yet
22 - Elementary Graph Algorithms
55 pages
Graph Theory, Dfs & Bfs
PDF
No ratings yet
Graph Theory, Dfs & Bfs
24 pages
Advance Analysis of Algorithm: Depth First Search & Breadth First Search
PDF
No ratings yet
Advance Analysis of Algorithm: Depth First Search & Breadth First Search
46 pages
Graph Traversal DFS
PDF
No ratings yet
Graph Traversal DFS
56 pages
Graphs
PDF
No ratings yet
Graphs
39 pages
01-Toturial Week2
PDF
No ratings yet
01-Toturial Week2
77 pages
6.006 Introduction To Algorithms: Mit Opencourseware
PDF
No ratings yet
6.006 Introduction To Algorithms: Mit Opencourseware
6 pages
Basic Search and Traversal Techniques
PDF
No ratings yet
Basic Search and Traversal Techniques
18 pages
CENG205 W-11b
PDF
No ratings yet
CENG205 W-11b
41 pages
COMP 482: Design and Analysis of Algorithms: Spring 2013
PDF
No ratings yet
COMP 482: Design and Analysis of Algorithms: Spring 2013
34 pages
Digital Assignment Theory 18bca0045
PDF
No ratings yet
Digital Assignment Theory 18bca0045
16 pages
Unit 3 - Basic Search and Traversal Techniques
PDF
100% (2)
Unit 3 - Basic Search and Traversal Techniques
113 pages
Graphs Are A Generalization of Trees. Like Trees, Graphs Have Nodes and Edges. (The Nodes Are Sometimes
PDF
No ratings yet
Graphs Are A Generalization of Trees. Like Trees, Graphs Have Nodes and Edges. (The Nodes Are Sometimes
18 pages
Daa Part 3
PDF
No ratings yet
Daa Part 3
24 pages
Unit-6 Exploring Graphs
PDF
No ratings yet
Unit-6 Exploring Graphs
22 pages
BFS, DFS, Best First Search
PDF
No ratings yet
BFS, DFS, Best First Search
15 pages
TIP103 - Unit 7 Session 1 - Graph Algorithm
PDF
No ratings yet
TIP103 - Unit 7 Session 1 - Graph Algorithm
75 pages
Lec 12
PDF
No ratings yet
Lec 12
41 pages
MIT6 006F11 Lec14
PDF
No ratings yet
MIT6 006F11 Lec14
7 pages
DAA (Unit 3)
PDF
No ratings yet
DAA (Unit 3)
38 pages
Strong Components Are Directed Analogs of Connected Components We'll Define Them Later in This Lecture
PDF
No ratings yet
Strong Components Are Directed Analogs of Connected Components We'll Define Them Later in This Lecture
8 pages
Graph
PDF
No ratings yet
Graph
56 pages
Graphs and Algorithms
PDF
No ratings yet
Graphs and Algorithms
7 pages
Graph Traversal Algorithm: Recapitulation
PDF
No ratings yet
Graph Traversal Algorithm: Recapitulation
14 pages
Basic Graph
PDF
No ratings yet
Basic Graph
8 pages
Lec 17
PDF
No ratings yet
Lec 17
60 pages
Unit-5 - Oops
PDF
No ratings yet
Unit-5 - Oops
27 pages
Graph Traversal Tech
PDF
No ratings yet
Graph Traversal Tech
47 pages
3 Notes
PDF
No ratings yet
3 Notes
4 pages
CS 412 - T2311 - CH 22 - Part2
PDF
No ratings yet
CS 412 - T2311 - CH 22 - Part2
72 pages
DFS BFS
PDF
No ratings yet
DFS BFS
23 pages
Graph
PDF
No ratings yet
Graph
56 pages
Graph Traversal Algorithms
PDF
No ratings yet
Graph Traversal Algorithms
10 pages
Graphs
PDF
No ratings yet
Graphs
43 pages
CH 6
PDF
No ratings yet
CH 6
52 pages
S2015 Lecture-5
PDF
No ratings yet
S2015 Lecture-5
31 pages
Lec 3
PDF
No ratings yet
Lec 3
21 pages
Lec03 SearchSort - 2023
PDF
No ratings yet
Lec03 SearchSort - 2023
82 pages
INFO 136 DSA Presentation
PDF
No ratings yet
INFO 136 DSA Presentation
11 pages
AI Unit 2
PDF
No ratings yet
AI Unit 2
24 pages
6th Lecture AI
PDF
No ratings yet
6th Lecture AI
70 pages
Unit 6 Exploring Graphs
PDF
No ratings yet
Unit 6 Exploring Graphs
39 pages
Depth First Search
PDF
No ratings yet
Depth First Search
28 pages
Search Algorithm
PDF
No ratings yet
Search Algorithm
12 pages
April+9 +Augmenting+DFS
PDF
No ratings yet
April+9 +Augmenting+DFS
64 pages
Unit 5
PDF
No ratings yet
Unit 5
78 pages
DS Lecture Week 13
PDF
No ratings yet
DS Lecture Week 13
38 pages
Btech Sem 2 Unit 5
PDF
No ratings yet
Btech Sem 2 Unit 5
21 pages
Lecture 19
PDF
No ratings yet
Lecture 19
62 pages
Traversal in Graph
PDF
No ratings yet
Traversal in Graph
5 pages
Dsdes
PDF
No ratings yet
Dsdes
3 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