0% found this document useful (0 votes)
14 views

Dsa

Dsa notes for bca
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
0% found this document useful (0 votes)
14 views

Dsa

Dsa notes for bca
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
You are on page 1/ 20
an ammenne | ss 1 tier "Nat we EDO Uy \ ul eleven ‘ WSL AE tae nt HAY phoanten aoqqennn t han AN HES Bertinggs eM Mette tint aeagtioniee Sines Ft SAP AURIS Hy Math Aviator sapunsnncs anand (EN Traded tt . . \ Ht atl eal it OHA SBHEER, fg 1 wl We Woes ation Lanmat be ine tnlerl Now the gaivationn dc toettnceat tee fri falttny COSht suboas UKs thon forthe vaat neat aT bE seeitue ‘ e ’ 4 a e ’ 5 4.8 Huffman’s Algorithms Huttnan Coding is a Huttinan Coding tsa famous Greedy Algorithm to Dian an oprmal solution toa problem, The Hallinan coding. is peneralty used for lossless data compression mechanisnts. Sometimes, (is also called AWa Compression encoding, [makes sure that there is no ambiguity white Jocoding the output bibstrean {Cuses variable length encoding. Hassigns vartable length code to all the characters, The code length ot a character depends on how frequently it occurs inthe given text, Lhe character whieh occurs most frequently pels the smallest code, The character whieh occurs least frequently pets the largest code. [eis also Known as Huttinan Eacoding, Huthman Coding implements rule Known as a prefix rule, This is to. prevent the ambiguities while decoding. Hensures that the code assipned lo any character is not a prefix of the code assigned to any other ehi Major Steps in Huffman Coding: There are two major steps in Huflinan Coding, eter 150 | Data Structures and Algorithms * Building a Huffman Tree from the input characters. * Assigning code to the characters by traversing the Huff MAN Tree | Algorithm: | The method that is used to construct optimal prefix code is called Hutt | coding. ay This algorithm builds a tree in bottom up manner. | We can denote this tree by T Let, |e| be number of leaves : | |e] -1 are number of operations required to merge the nodes, | Q be the priority queue which can be used while constructing binary heap Algorithm Huffman (c) | ‘ | n= |e] Q=c for i<-1 to n-1 do { temp <- get node () left (temp] Get_min (Q) right [temp] Get Min (Q) a= left [temp] b = right [temp] F [temp]<- f[a] + [b] insert (Q, temp) t return Get_min (0) 5 Steps to build Huffman Tree The steps involved in the Construction of Huffman Tree are as follows Step-01: Create a leaf node for each character of the text. Leaf node of? character contains the Occurring frequency of that character. Step-02: Arrange ail the nodes in increasing order of their frequency valu’ Step-03: Considering the first two nodes having minimum frequen®, Create a new internal node. The frequency of this new node is the sum ° frequency of those two nodes. Mz ° | © the first node as a left child and tt other node as a right child of the newly created node. Step-04: Keep repeating Step-02 and Step-03 until all the nodes {oT single tree. The tree finally obtained is the desired Huffman Tree. fetus understand the algorithm with an example: file contains the following characters with the frequencies as shown. I Huffman Coding is used for data compression, determine- , Huffman Code for each character , Average code length + Length of Huffman encoded message (in bits) Tree Structure | 151 [Characters | A |e i | o u s t Frequencies | 10 i [| 2 [3 | 4 B L Huffman Tree is constructed in the following steps- OO OOOO © Step-1 ' ° u a 8 6 Step-2 Step-3 ql ° @ @2@O® i a @) @e) a | 4) 44 Step-4 SL \ u Qa @ t ° we © ® Gn GS) Gay a i - . OSG Step-5 wR 2) GG) € ) 152 | Data Structures and Algorithms oe a Step-7 Tree Structure | 153 sfiman’s Tree er we have constructed the Huffman tree. we will assign weight to all edg: usassign weight °0" to the left edges and weight “I” to the right edges. I pues: . : y Ifyouassien weight “0° to the left edges. then assign weight "I" to the right edges. y Ifyou assign weight “1” to the left edges. then assign weight °0" to the right edges. Y Any of the above two conventions may be followed. J But follow the same convention at the time of decoding that is adopted at the time of encoding. Afier assigning weight to ali the edges, the modified Huffman Tree is- ao AS) 4 33 4 o/ NI a. i s : (8 y (ay of \1 a © t ° Huffman Code For Characters To write Huffman Code for any character, traverse the Huffman Tree from root node to the leaf node of that character. Following this rule. the Huffman Code for each character is: Oa =10 00 * o= 11001 * u=1101 * s=01 * t= 11000 From here, we ean observ * Characters occurring less frequently in the textare a ned the larger code. * Characters occurring more frequently in the text are smaller code. Just a minute ” Consider the following S Ques Stig, 8 Ce t of X (i) What is the depth of the tree? (ii) Which nodes are children of node B? } (iii), Which node is the parent of node F? I (iv) What is the level of node E? (v)_ Which nodes are the siblings of node H? (vi) Which nodes are siblings of node D? (vii) Which nodes are the leaf nodes? Answer: @ 4 (ii) DandE (iii) C (iv) 2 (v) _H does not have any siblings (vi) The only sibling of D is E (vii) F, GH and 1 7 Maximum number of nodes in a binary tree of depth d can be— Answer: 2¢— | ine traversal method, the root is processed before traversing left and right subtrees. Answer: Preorder 7 Inorder traversal of a binary search tree gives youa Answer: sorted list. i » InaBST, all values in the left subtree ofa node are ... than the val the node. Answer: smaller, , jy used in graph drawing aj... C - < arly he ie Orthogor - Thev are particu arly helpful wher ‘ and graph a ex hs. as they provide . ied and One baw a ces it easier 10 analvze the graph's structure and relaticg ‘out that esi 0 bs al 7 jag that achieving a pertect orth gona! FEPFeseNtatiog s witha =a cro’ ) is not always possible. espectally for, vianar vaph In such cases. certain graph drawing alge MILAM S May intr, ae ae 9 to find an orthogonal representatic, edge cr g E optimizes other criteria. such as edge length or node placement 5.6 Graph Traversals Graph traversals are techniques used to visit all the nodes (vertices) » graph data structure systematically. In a graph. there is no special ver. designated as starting vertex. Therefore. traversal of the graph may from any vertex. There are two main graph traversal algorithms: 1. Depth-First Search (DFS) and 2. Breadth-First Search (BFS). Both algorithms can be applied to both directed and undirected grapts Let’s explore each of these graph traversal algorithms in detail: 1. Depth-First Search (DFS): DFS is a recursive algorithm that explores as far as possi hora ; . possible along each 072 tetore b acking. Muses a stack (usually implemented using recurso an explicit stack data structure) to keep track of nodes to be visited. DFS Algorthm: 7 Start from a source node. Graph Structure | 171 ; cor each unvisi i f , DES eee : the current node. recursively apply 1s pseudocode (recursive): junction DFS(node): trode is NOLS isited: mark node as visited for each neighbour in node’s neighbours: if neighbour is not visited: DFS(neighbour) DES Pseudocode (iterative using an explicit stack): function DFS(start_node): create an empty stack push start_node to the stack while stack is not empty: node = pop from the stack if node is not visited: mark node as visited for each neighbour in node’s neighbours: if neighbour is not visited: push neighbour to the stack Steps to implement Depth First Search Traversal We use the following steps to implement DFS traversal... Step 1 - Define a Stack of size total number of vertices in the graph. Step 2 - Select any vertex as starting point for traversal. Visit that vertex and push it on to the Stack. Step 3 - Visit any one of the non-visited adjacent vertices ofa vertex which isat the top of stack and push it on to the stack. i Step 4 - Repeat step 3 until there is no new vertex to be visited from the Vertex which is at the top of the stack. Step 5 - When there is nO new vertex to visit then use back tracking and Pop one vertex from the stack- ; 5 until stack becomes Empty. Step 6 - 3, 4 an OO eed es Empty, then produce final spanning tree by Step 7 - When stack becom Temoving unused edges fro™ the graph ms 472 Data Structures 24 aigorith} Example: Step-! Initialize the stack Step-2 Mark 12 as visited and push Now Explore any unvisited a In our example We have three nodes. We can pick any of them. Here we are going © PI into stack djacent node from |2. Step-3 Mark § as visited and put it onto the stack. Explore any unvisited adjacent node from 5 Both 12 and 2S are adjacent to $ but we are concerned for unvisited nodes only. Step-+ Visit 2S and mark it as visited and put onto the sta Here. we have 23 and 3 nodes. which are adjacent to 25 and both are unvisited. Step-S We choose 3. mark it as visited and put onto the stack. Here 3 does not have any unvisited adjacent node. So. we pop 3 from the stack « the stack top for retum to the previous heck if it has any unvisited nodes nd 25 C f 125 to be on the top of the stack Step-7 Only unvisited adjacent node is from D is 25 4 So we visit 23, mark it as visited and put it onto the stack. . oa TEL “CE Graph Stractuee | 1 ouirtt a “nip ime complexity: OW # EP) where Vis the number of vertices and bbs nab of ed ase € ‘omplexity: OCV i" Analy sis gine a0 extra Visited array is needed of size V pple ation of DES » Finda path from the source vertex to other vertices » Find the connected components ina g graph «Topological Sorting «Find bridges and articulation points ina graph Find LCA of two nodes ina graph «Find cycles ina directed and undireeted graph Breadth-First Search (BES): isan algorithm that explores the graph level by level. [uses a queue to keep track of nodes to be visited. The nodes at the current level are explored before moving on to the next level. BFS Algorithm: » Start from a source node. > Mark the source node as visited and enqueue it » While the queue is not empty, dequeue the front node, and for each unvisited neighbour of the current node. mark it as visited and enqueue it. BFS Pseudocode: function BF: start_node): create an empty queue mark start_node as visited and enqueue it While queue is not empty: node = dequeue from the queue for e: +h neighbour in node’s neighbours: 5 not visited: if neighbour mark neighbour as v isited and enqueue it Steps to implement Breadth First Seareh Traversal We use the following steps to implement BES (raversal Step Define : a Queue of size tot number of vertices inthe graph ep 2 - Select any vertex as starting, pout for traversal, Visit that vertex “Nd insert it into the Queue 174 | Data Structures and Algorithms Step all the non 5 front of the Queue and insert them into the Queue. Step 4 - When there is no new vertex to be visited trom the v, is at front of the Queue then delete that vertex. Step 5 - Repeat steps 3 and 4 until queue becomes empty. Step 6 - When queue becomes empty. then produce final Spanning tree removing unused edges from the graph {jacent vertices of the vert Example: Let us understand the working of the algorithm with the help of th following example. . Step1: Initially queue and visited arrays are empty. | Visited oa | e Sg yy Queu 7 Step2: Push node 0 into queue and mark it visited. o & |. © e eo”: Step 3: Remove node 0 from the front of queue and visit the unvisite! neighbours and push them into queue. ro) | Visited | 0 | a Queue 1 | 2 o : Step 4: Remove node | from the front of queue and visit the un’ isle neighbours and push them into queue. Visited oO 1 2 3 Go | | Queue 2 3] © t 176 | Data Structures and Algorithms inear with depth. torage requir asily programmable. Disadvantages of Breadth First Search: "The main drawback of BFS is its memory requirement, in level of the graph must be saved in order to generate the nes and the amount of memory is proportional to the number ae le, stored the space complexity of BFS is O(b* ), Where j 1° branching factor(the number of children at each Real outdegree) and d is the depth. As a result. BFS IS Severely a bound in practice so will exhaust the memory available onty computers in a matter of minutes. oD Applications: * Shortest Path and Minimum Spanning Tree for unweighteq grap, * Minimum Spanning Tree for weighted graphs "Peer-to-Peer Networks = Crawlers in Search Engines = Social Networking Websites = GPS Navigation systems = Broadcasting in Network = In Garbage Collection = Path Finding » Al = Network Security = Topological sorting = Image processing C Program: Implementation of BFS for Graph using Adjacency List: // C Program: graph traversal using BFS #include #include #include #define MAX VERTICES 50 // This struct represents a directed graph using typedef struct Graph t | int V: bool adj[MAX VERT ICES |[MAX VERTICES]: } Graph; Graph* Graph create(int V) F i ssentati adjaceney list repres™ Graph Structure | 177 malloc(sizeof(Graph)): for (int i= 0: 1< Vi i++) for (int | = 0: j g->adj[ilfi] = false: } return g: void Graph_destroy(Graph* g) { free(g): } void Graph_addEdge(Graph* g. int v, int w) // Add w to v’s list. g->adj[v][w] = true: Prints BFS traversal from a given source s void Graph_BFS(Graph* g, int s) /| Mark all the vertices as not visited bool visited[MAX_VERTICES]; for (int i= 0; i < g->Vs i++) { visited[i] = false: 5 // Create a queue for BFS int queue[MAX_VERTICES]: int front = 0, rear = 0; // Mark the current node as visited and enqueue it visited[s] = true: queue[reart++] while (front != rear) { // Dequcue a vertex from queue and print it Ss; s queue| front | I: printi(“%d sys) 178 | Data Structures and Algorithms // Get all adjacent vertices of the dequeued vert // If an adjacent has not been visited, then Mark ; and enqueue it vig, for (int adjacent = 0: adjacent < g->V; adjacent++) { if (g->adj[s][adjacent] && 'visited|, visited[adjacent] = true: queue[rear++] = adjacent: Adjacent), ' } } int main() t // Create a graph Graph* g = Graph_create(4): Graph_addEdge(g, 0, 1); Graph_addEdge(g, 0, 2); Graph_addEdge(g, 1, 2); Graph_addEdge(g, 2, 0); Graph_addEdge(g, 2, 3); Graph_addEdge(g, 3, 3); printf(“Following is Breadth First Traversal “ “(starting from vertex 2)\n”); Graph_BFS(g, 2); Graph_destroy(g): return 0; 5 Output: Following is Breadth First Traversal (starting from vertex 2) 203 I 5.7 Floyd Warshall Algorithm = Floyd Warshall Algorithm is a famous algorithm. * Itis used to solve All Pairs Shortest Path Problem. ees ofl = It computes the shortest path between every pair of vertices © given weighted graph. Graph Structure | 179 yd Warshall algorithm is an incredibly powerful tool that can be wy swive a variety of problems. Floyd Warshall Algorithm has the ning main advantages: one of the biggest advantages of the Floyd Warshall algorithm is its versatility. The algorithm can be used to solve a wide range of problems. including finding the shortest path between two nodes inagraph, calculating the transitive closure ofa graph. and detecting negativ les in a graph. , Another advantage is its simplicity. , the Floyd Warshall algorithm is very efficient. It has a time complexity of O(n3), which means it can handle large graphs with ease. Additionally, the algorithm is parallelisable. meaning it can be run on multiple processors to further improve its efficiency. advantages: care a few disadvantages to using the Floyd Warshall algorithm. 1h y The algorithm is quite complex and can be difficult to understand. y The algorithm can be slow when used on large graphs. ry The algorithm does not always find the shortest path between two nodes; it can sometimes find a path that is longer than the shortest path. pplications: are many applications of the Floyd Warshall algorithm. Some of 10st popular applications are: y Finding the shortest path between two vertices in a graph. 7 Detecting negative cycles in a graph, and Computing the transitive closure of a graph. ry ‘The Floyd Warshall algorithm can also be used for other purposes such as- © Solving the all-pairs shortest path problem in weighted graphs, © Finding the closest pairs of vertices in a graph. and © Computing the diameter of a graph. “oyd Warshall Algorithm: |. Initialize the solution matris first step. — 2. ‘Then update the solution matrix by considering all vertices intermediate vertex. same as the input graph matrix as a san that? 180 | Data Structures ant AS yaks wed sy 3. The idea is bee by ane aa vont \ paths which ie facto ded iy the shortest pall A. When we pick veh nmiber helt ws ms am 7 have cansisterest ere ' Fy atta emia tiaie 8, Porevery pall (l yank the sete jad destination contig there are WO possible abs rrediite yertes We oborbent pathy a. kis not an ile We keep the value bo Risan intermediate V6 Hho What pall thay | fue ab dist Ufib ee Het : \ tl IHL shang update the Va clist{i]li alist} yl ALR ALT # define NA ob lint{ Pipe Ht void warshall) i int cost [N|[NI int i,j, ky tor(i-O; is Not) torG-0; ps NophY) cost {iH cost Mae TT fork Os KON. KEN) ‘ for(i-O; is Ns itt) for Op Ny jth) itjeost [iLL cost [aL TAL | cont TRILL cost fiffif cost fi} PhP oust TALL display the matrey cost | N] EN] } Time Complexity y” bloyd Wars 2 shall Meonthin consists of theres loopy aver saan y the innermo St Loo, . , PCONSISINOF ONLY Constant jesiny en vy Hence, the as “_ hn (li O(n’). 7 Here, nis the number at ne { vides SYTPLOLe c plotic Complesity at Playa Waniball lee! vinthe piven yraph ees, _ Graph Structure wie mpl , » Consider the following directed weighted ; | feral Algorithm. find the shortest path dista We tices Solution: step-O1: 7 Remove all the self-loops and parallel edges (keeping the loves! weight edge) from the graph. y Inthe given graph. there are neither self-edges nor parallel « Step-02: ry Write the initial distance matrix. y Itrepresents the distance between every pair of vertices in the of given weights. ~ For diagonal elements (representing self-loops). distance va y For vertices having a direct edge between them, distanc! weight of that edge. > For vertices having no direct edge between them. distance value Initial distance matrix for the given graph is: valuc Step.03: ‘sing Floyd Warshall Algorithm, write the following 4 matrives 182 | Data Structures and Algorithms J ; | D,= 3 4 41 2 D,= ; 4 1 2 D,= 3 4 ' pee 4 3 4 The last matrix D4 represents the shortest path distance betwee? aw pair of vertices. ‘ ; 3), th’ In the above problem, there are 4 vertices in the given graph. So. i will be total 4 matrices of order 4 x 4 in the solution excluding !!" distance matrix. Diagonal elements of each matrix will alway be ensuite Le Graph Structure | 183 ~ is the implementation of the above approach in C: elo ; 7 rc pve to Solves the all-pairs shortest path problem using Floyd : rshall algorithm wa ‘ include “ stdio.h~ adefine V4 agefine INF 99999 // define infinite value ) Afunction to print the solution matrix wid printSolution(int dist{ |] V]); soid floyd Warshall(int dist{ |[V]) inti. j. ks (|Add all vertices one by one to the set of intermediate vertices. for (k — 0; k < Vi k++) § // Pick all vertices as source one by one for (i 0:1 < Veit +) f // Pick all vertices as destination for the above picked source for (j = 0; 5 < V3 j++) { // \f vertex k is on the shortest path from i to j, then update the value of // dist{il{j] if (dist[i]fk] + dist{k][j] ~ distliJij)) dist[ilfj| = dist{iJ[k] + dist[k][j}; j // Print the shortest distance matrix printSolution(dist); 5 * A utility function to print solution */ void printSolution(int dist{|[V}) printf “The following matrix shows the shortest distances between SYery pair of vertices n”): for (inti 0:1 Veit 4) 4 for (int ~ 0.) Vide) : if (distfi}fi} © INFO

You might also like