ROOT: A node which doesn't have a parent. In the above tree. The Root is A.

Size: px
Start display at page:

Download "ROOT: A node which doesn't have a parent. In the above tree. The Root is A."

Transcription

1 TREE: A tree is a finite set of one or more nodes such that there is a specially designated node called the Root, and zero or more non empty sub trees T 1, T 2...T k, each of whose roots are connected by a directed edge from Root R. Example ROOT: A node which doesn't have a parent. In the above tree. The Root is A. NODE: Item of Information LEAF: A node which doesn't have children is called leaf or Terminal node. Here B, K, L, G, H, M, J are leafs. SIBLINGS: Children of the same parents are said to be siblings, Here B, C, D, E, F, G are siblings. Similarly I, J & K, L are siblings. PATH: A path from node n, to n k is defined as a sequence of nodes n 1, n 2,n 3...n k such that

2 n i is the parent of n i+1. There is exactly only one path from each node to root. E.g.: Path from A to Q is A, E, J, Q. where A is the parent for E, E is the parent of J and J is the parent of Q. LENGTH: The length is defined as the number of edges on the path. The length for the path A to Q is 3. DEGREE: The number of subtrees of a node is called its degree. Degree of A is 3 Degree of C is 0 Degree of D is 1 LEVEL: The level of a node is defined by initially letting the root be at level Zero, if a node is at level L then its children are at level L + 1. Level of A is 0. Level of B, C, D, is 1. Level of F, G, H, I, J is 2 Level of K, L, M is 3. DEPTH: For any node n, the depth of n is the length of the unique path from root to n. The depth of the root is zero. HEIGHT: For any node n, the height of the node n is the length of the longest path from n to the leaf. The height of the leaf is zero The height of the root A is 3. The height of the node E is 2. Note: The height of the tree is equal to the height of the root. Depth of the tree is equal to the height of the tree. ANCESTOR AND DESCENDANT: If there is a path from n1 to n2. n1 is an ancestor of n2, n2 is a descendant of n1.

3 IMPLEMENTATION OF TREES Each node having a data, and pointer to each child of node. The number of children per node can vary and is not known in advance, so it might be infeasible to make a children direct links in the data datastructure, because there would be too much wasted space. Here the arrow that point downward are FirstChild pointers. Arrow that go left to right are NextSibling pointers. Null pointers are not drawn, there are too many. BINARY TREE Definition: - Binary Tree is a tree in which no node can have more than two children. Maximum number of nodes at level i of a binary tree is 2 i-1. BINARY TREE NODE DECLARATIONS Struct TreeNode { int Element; Struct TreeNode *Left ;

4 Struct TreeNode *Right; }; Types of Binary Tress 1) Rooted Binary tree: - In which every node has atmost two children. 2) Full Binary Tree:-In which every node has zero or two children. 3) Perfect Binary Tree:- In which all the leaves are at same depth. 4) Complete Binary Tree:- In which all the leaves are at same depth n or n-1 for some n. REPRESENTATION OF A BINARY TREE There are two ways for representing binary tree, they are * Linear Representation * Linked Representation Linear Representation The elements are represented using arrays. For any element in position i, the left child is in position 2i, the right child is in position (2i + 1), and the parent is in position (i/2) A B C D E F G E.g i=1, For node A, Left child at 2i=>2x1=2=>B, Right child at 2i+1=>(2x1)+1=3=>C Linked Representation The elements are represented using pointers. Each node in linked representation has three fields, namely, * Pointer to the left subtree * Data field

5 * Pointer to the right subtree In leaf nodes, both the pointer fields are assigned as NULL. Tree Traversals Traversing means visiting each node only once. Tree traversal is a method for visiting all the nodes in the tree exactly once. There are three types of tree traversal techniques, namely 1. Inorder Traversal 2. Preorder Traversal 3. Postorder Traversal Inorder Traversal The inorder traversal of a binary tree is performed as * Traverse the left subtree in inorder * Visit the root * Traverse the right subtree in inorder. Example :

6 Inorder: A B C D E G H I J K The inorder traversal of the binary tree for an arithmetic expression gives the expression in an infix form. RECURSIVE ROUTINE FOR INORDER TRAVERSAL void Inorder (Tree T) { if (T! = NULL) { Inorder (T->left); printelement (T->Element); Inorder (T->right); } } Preorder Traversal The preorder traversal of a binary tree is performed as follows, * Visit the root * Traverse the left subtree in preorder * Traverse the right subtree in preorder. Preorder: D C A B I G E H K J The preorder traversal of the binary tree for the given expression gives in prefix form.

7 RECURSIVE ROUTINE FOR PREORDER TRAVERSAL void Preorder (Tree T) { if (T! = NULL) { printelement (T->Element); Preorder (T->left); Preorder (T->right); } Postorder Traversal The postorder traversal of a binary tree is performed by the following steps. * Traverse the left subtree in postorder. * Traverse the right subtree in postorder. * Visit the root. Post order : B A C E H G J K I D The postorder traversal of the binary tree for the given expression gives in postfix form. RECURSIVE ROUTINE FOR POSTORDER TRAVERSAL void Postorder (Tree T) { if (T! = NULL) { Postorder (T->Left); Postorder (T->Right);

8 PrintElement (T->Element); } EXAMPLE FOR TREE TRAVERSAL Inorder : Preorder : Postorder : EXPRESSION TREE Expression Tree is a binary tree in which the leaf nodes are operands and the interior nodes are operators. Like binary tree, expression tree can also be travesed by inorder, preorder and postorder traversal. Constructing an Expression Tree

9 Let us consider postfix expression given as an input for constructing an expression tree by performing the following steps : 1. Read one symbol at a time from the postfix expression. 2. Check whether the symbol is an operand or operator. (a) If the symbol is an operand, create a one - node tree and push a pointer on to the stack. (b) If the symbol is an operator pop two pointers from the stack namely T 1 and T 2 and form a new tree with root as the operator and T 2 as a left child and T 1 as a right child. A pointer to this new tree is then pushed onto the stack. Example: ab+cde+** 1) The first two symbols are operands, so create one-node tree & push pointers to them on to a stack. 2) Next + is read, so two pointers to trees are popped a new tree is formed and a pointer is pushed on to a stack. 3) Next c,d,e are read and for each a one-node tree is created and a pointer to the corresponding tree is pushed on to the stack

10 4) Now + is read, so two tree are merged. 5) Next * is read, so pop two tree pointers and form a new tree with * as root.

11 6) Next * is read, two trees are merged, and a pointer to the final tree is left on the stack. THE SEARCH TREE ADT: - BINARY SEARCH TREE Definition: - Binary search tree is a binary tree in which for every node X in the tree, the values of all the keys in its left subtree are smaller than the key value in X, and the values of all the keys in its right subtree are larger than the key value in X. Note : * Every binary search tree is a binary tree. * All binary trees need not be a binary search tree. DECLARATION ROUTINE FOR BINARY SEARCH TREE Struct TreeNode; typedef struct TreeNode * SearchTree; SearchTree Insert (int X, SearchTree T); SearchTree Delete (int X, SearchTree T); int Find (int X, SearchTree T); int FindMin (Search Tree T); int FindMax (SearchTree T); SearchTree MakeEmpty (SearchTree T); Struct TreeNode

12 { int Element ; SearchTree Left; SearchTree Right; }; Make Empty :- This operation is mainly for initialization when the programmer prefer to initialize the first element as a one - node tree. ROUTINE TO MAKE AN EMPTY TREE :- SearchTree MakeEmpty (SearchTree T) { if (T! = NULL) { MakeEmpty (T-> left); MakeEmpty (T-> Right); free (T); } return NULL ; } INSERT: - To insert the element X into the tree, * Check with the root node T * If it is less than the root, Traverse the left subtree recursively until it reaches the T-> left equals to NULL. Then X is placed in T-> left. * If X is greater than the root. Traverse the right subtree recursively until it reaches the T-> right equals to NULL. Then x is placed in T->Right.

13 ROUTINE TO INSERT INTO A BINARY SEARCH TREE SearchTree Insert (int X, searchtree T) { if (T = = NULL) { T = malloc (size of (Struct TreeNode)); if (T! = NULL) // First element is placed in the root. { T ->Element = X; T-> left = NULL; T -> Right = NULL; } } else if (X < T ->Element) T ->left = Insert (X, T ->left); else if (X > T ->Element) T ->Right = Insert (X, T ->Right); // Else X is in the tree already. return T; } Example : - To insert 8, 4,1,6,5,7,10 * First element 8 is considered as Root. *As 4< 8, Traverse towards left

14 *1<8, towards left & 1<4, towards left *6<8, towards left & 6>4 then towards right. *5<8, towards left and 5>4 towards right and 5<6 towards left

15 *7<8, towards left & 7>4, towards right and 7>6, towards right. *finally 10>8 so move towards right. FIND: - * Check whether the root is NULL if so then return NULL. * Otherwise, Check the value X with the root node value (i.e. T ->data) (1) If X is equal to T ->data, return T. (2) If X is less than T ->data, Traverse the left of T recursively. (3) If X is greater than T ->data, traverse the right of T recursively. ROUTINE FOR FIND OPERATION Int Find (int X, SearchTree T) {

16 If T = = NULL) Return NULL ; If (X < T->Element) return Find (X, T->left); else If (X > T->Element) return Find (X, T->Right); else return T; // returns the position of the search element. } Example : - To Find an element 7 (consider Previous example, X = 7) 7 is checked with the Root 7< 8, Go to the left child of 8 7 is checked with Root 4 7>4, Go to the right child of 4. 7 is checked with Root 6 7>6, Go to the right child of 6. 7 is checked with root 7 (Found) FIND MIN This operation returns the position of the smallest element in the tree. To perform FindMin, start at the root and go left as long as there is a left child. The stopping point is the smallest element.

17 RECURISVE ROUTINE FOR FINDMIN int FindMin (SearchTree T) { if (T = = NULL); return NULL ; else if (T->left = = NULL) return T; else return FindMin (T->left); } Example : - Root T (a) T! = NULL and T->left!=NULL, (b) T! = NULL and T->left!=NULL, Traverse left Traverse left (b) T! = NULL and T->left!=NULL, Traverse left Traverse left (c) Since T left is Null, return T as a minimum element. FINDMAX

18 FindMax routine return the position of largest elements in the tree. To perform a FindMax, start at the root and go right as long as there is a right child. The stopping point is the largest element. RECURSIVE ROUTINE FOR FINDMAX int FindMax (SearchTree T) { if (T = = NULL) return NULL ; else if (T->Right = = NULL) return T; else FindMax (T->Right); } Example :- Root T (a) T! = NULL and T->Right!=NULL, Traverse Right Traverse Right (b) T! = NULL and T->Right!=NULL, Traverse Right Traverse Right Max (c) Since T Right is NULL, return T as a Maximum element

19 Delete : Deletion operation is the complex operation in the Binary search tree. To delete an element, consider the following three possibilities. CASE 1: Node to be deleted is a leaf node (ie) No children. CASE 2: Node with one child. CASE 3: Node with two children. CASE 1 Node with no children (Leaf node) If the node is a leaf node, it can be deleted immediately. CASE 2 : - Node with one child If the node has one child, it can be deleted by adjusting its parent pointer that points to its child node. To Delete 5 To delete 5, the pointer currently pointing the node 5 is now made to to its child node 6

20 Before deletion After Deletion Case 3 : Node with two children It is difficult to delete a node which has two children. The general strategy is to replace the data of the node to be deleted with its smallest data of the right subtree and recursively delete that node. Example 1 : To Delete 5 : * The minimum element at the right subtree is 7. * Now the value 7 is replaced in the position of 5. * Since the position of 7 is the leaf node delete immediately. DELETION ROUTINE FOR BINARY SEARCH TREES

21 SearchTree Delete (int X, searchtree T) { int Tmpcell ; if (T = = NULL) Error ("Element not found"); else if (X < T->Element) // Traverse towards left T->Left = Delete (X, T->Left); else if (X > T->Element) // Traverse towards right T->Right = Delete (X, T->Right); // Found Element to be deleted else // Two children if (T->Left && T->Right) { // Replace with smallest data in right subtree Tmpcell = FindMin (T->Right); T->Element = Tmpcell->Element ; T->Right = Delete (T->Element; T->Right); } else // one or zero children { Tmpcell = T; if (T->Left = = NULL) T = T->Right; else if (T->Right = = NULL) T = T->Left ; free (TmpCell); } return T; } THREADED BINARY TREE

22 During binary tree creation, for the leaf nodes there is no sub-tree further. So we just setting the left and right field of leaf nodes as NULL, it is just wastage of the memory. So to avoid NULL values in the node set the threads which are actually the links to predecessor and successor nodes. The Structure of the node is struct thread { int data; int lth,rth; struct thread *left; struct thread *right; } Inorder Threading Technique: The basic idea in inorder threading is that the left thread should point to the predecessor and the right thread points to the successor. Here assume head node as the starting node and the root node of the tree is attached to left of head node. Logic Explanation for Threaded binary tree: In threaded binary tree, the NULL pointers are avoided. The left thread NULL pointer should point to the predecessor and the right thread NULL pointer points to the successor. There are two additional fields in each node named as lth and rth these fields indicate whether left or right child is present. To set lth and rth field value as either 0 or 1. The value 0 indicates there is no left child. The value 1 indicates there is no right child. Example: 10,8,6,12,9,11,14. 1) Initially create a dummy node which acts as a header of the tree. lth Left data Right rth 0 NULL 1000 NULL 0 2) Take first value i.e. 10. The node with value 10 will be the root node and attach this root node as left child of dummy node.

23 0 NULL 10 NULL 0 The NULL links of root s left and right child will be pointed to dummy NULL ) Read 8. The 8 will be compared with 10, 8<10, so attach 8 as left child of 10 and also set root s lth field to 1, indicating that the node 10 is having left child NULL ) Read 6, first compared to 10 (6<10), so go to left subtree then compared to 8 (6<8) move to left sub tree. But lth field of node 8 is 0, indicating there is no left child to 8, so attach 6 as left child to NULL

24 5) Next 12. Compare to 10 (12>10).so attach 12 as right child of NULL ) Similarly attach 9, 11 and 14 as appropriate children. AVL Tree : - (Adelson - Velskill and LANDIS) An AVL tree is a binary search tree except that for every node in the tree, the height of the left and right subtrees can differ by atmost 1. The height of the empty tree is defined to be - 1. A balance factor is the height of the left subtree minus height of the right subtree. For an AVL tree all balance factor should be +1, 0, or -1. If the balance factor of any node in an AVL tree becomes less than -1 or greater than 1, the tree has to be balanced by making either single or double rotations. An AVL tree causes imbalance, when any one of the following conditions occur.

25 Case 1: An insertion into the left subtree of the left child of node α Case 2: An insertion into the right subtree of the left child of node α Case 3: An insertion into the left subtree of the right child of node α Case 4: An insertion into the right subtree of the right child of node α These imbalances can be overcome by 1. Single Rotation 2. Double Rotation. Single Rotation Single Rotation is performed to fix case 1 and case 4. Case 1. An insertion into the left subtree of the left child of K 2. [LL ROTATION] Single Rotation to fix Case 1. General Representation ROUTINE TO PERFORM SINGLE ROTATION WITH LEFT SingleRotatewithLeft (Position K 2) { Position K 1; K 1 = K 2 ->Left ; K 2 ->left = K 1->Right ;

26 K 1->Right = K 2 ; K 2 ->Height = Max (Height (K 2->Left), Height (K 2-> Right)) + 1 ; K 1->Height = Max (Height (K 1 ->left), K 2->Height ) + 1; return K 1 ; } Example : Inserting the value `1' in the following AVL Tree makes AVL Tree imbalance Before After Single Rotation to fix Case 4 :- Case 4 : - An insertion into the right subtree of the right child of K 1. [RR ROTATION] General Representation ROUTINE TO PERFORM SINGLE ROTATION WITH RIGHT :- Single Rotation With Right (Position K 2) { Position K 2 ; K 2 = K 1->Right; K 1 ->Right = K 2 ->Left ;

27 K 2->Left = K 1 ; K 1->Height = Max (Height (K 1->Left), Height (K 1-> Right)) +1 ; K 2 ->Height = Max (K 1->Height, Height (K 2 ->Right)) +1 ; Return K 2 ; } Example: Double Rotation Double Rotation is performed to fix case 2 and case 3. Case 2 : An insertion into the right subtree of the left child. [RL ROTATION] General Representation ROUTINE TO PERFORM DOUBLE ROTATION WITH RIGHT: Double Rotate with right (Position K 1) { /* Rotation Between K 2 & K 3 */ K 1 ->Right = Single Rotate with Left (K 1 ->Right); /* Rotation between K 1 & K 2 */

28 Return Single Rotate With Right (K 1); } Example: Case 3 : An insertion into the left subtree of the right child. [LR ROTATION] General Representation ROUTINE TO PERFORM DOUBLE ROTATION WITH LEFT: Double Rotate with left (Position K 3) { /* Rotation Between K 1 & K 2 */ K 3 ->Left = Single Rotate with Right (K 3 ->Left); /* Rotation Between K 3 & K 2 */

29 Return Single Rotate With Left (K 3); } Example: ROUTINE TO INSERT IN AN AVL TREE : - AVLTree Insert (AVL tree T, int X) { if (T = = NULL) { T = malloc (size of (Struct AVLnode)); if (T = = NULL) Error ("out of space"); else {

30 T->data =X; T ->Height = 0; T ->Left = NULL; T ->Right = NULL; } } else if (X < T->data) { T ->left = Insert (T->left, X); if (Height (T -> left) - Height (T->Right) = = 2) if (X < T->left->data) T = Single Rotate With left (T); else T = Double Rotate is the left (T); } else if (X > T->data) { T ->Right = Insert (T->Right, X); if (Height (T->Right) - Height (T->left) = = 2) if(x > T->Right->Element) T = Single Rotate with Right (T); else

31 T = Double Rotate with Right (T); } T->Height = Max (Height (T->left), Height (T->Right))+1; return T; } Example: Example: 3, 6, 5, 1, 2, 4

32 SPLAY TREES 1) Splay tress are variation of binary search tree. 2) The basic idea is not to spend too much time on balancing. Key Idea: The frequently accessed nodes are rotated as close as possible to the root. Whenever an object is accessed, it becomes the new root. It should be done without adversely affecting other nodes.

33 SPLAYING 1) If the accessed node is already is already in the root, nothing to do. 2) If the accessed node X has root as its parent then rotate X with its parent. 3) Otherwise ( X has both parent & Grant parent) there are four possible cases:- a) X is left - right of GP b) X is right left of GP c) X is left left of P d) X is right right of P Zig Zag (Double Rotation) Zig Zig (Single Rotation) Zig Zag Zig Zig 1) K2 is the root that has no parent 2) X and P has either both left or both right children. Transforms the tree on the left to right.

34 EXAMPLE: Make K1 as TOP 1) The first splay step is at k1, and is clearly a zig-zag, so we perform a standard AVL double rotation using k1, k2, and k3. The resulting tree follows. 2) The next splay step at k1 is a zig-zig, so we do the zig-zig rotation with k1, k4, and k5, obtaining the final tree.

35 GRAPH A graph G = (V, E) consists of a set of vertices, V and set of edges E. Vertices are referred to as nodes and the arc between the nodes are referred to as Edges. Each edge is a pair (v, w) where u, w V. (i.e.) v = V 1, w = V 2... Here V 1, V 2, V 3, V 4 are the vertices and (V 1, V 2), (V 2, V 3), (V 3, V 4), (V 4, V 1), (V 2, V 4), (V 1, V 3) are edges. BASIC TERMINOLOGIES Directed Graph (or) Digraph Directed graph is a graph which consists of directed edges, where each edge in E is unidirectional. It is also referred as Digraph. If (v, w) is a directed edge then (v, w) # (w, v) (V 1, V 2) # (V 2, V 1) Undirected Graph An undirected graph is a graph, which consists of undirected edges. If (v, w) is an undirected edge then (v,w) = (w, v) (V 1, V 2) = (V 2, V 1)

36 Weighted Graph A graph is said to be weighted graph if every edge in the graph is assigned a weight or value. It can be directed or undirected grap Complete Graph A complete graph is a graph in which there is an edge between every pair of vertices. A complete graph with n vertices will have n (n - 1)/2 edges. Degree The number of edges incident on a vertex determines its degree. The degree of the vertex V is written as degree (V). The indegree of the vertex V, is the number of edges entering into the vertex V. Similarly the out degree of the vertex V is the number of edges exiting from that vertex V. Number of vertics is 4 Number of edges is 6 (i.e) there is a path from every vertex to every other vertex. A complete digraph is a strongly connected graph. Strongly Connected Graph If there is a path from every vertex to every other vertex in a directed graph then it is said to be strongly connected graph. Otherwise, it is said to be weakly connected graph.

37 Graph Strongly Connected Graph Weakly Connected Path A path in a graph is a sequence of vertices ω 1, ω 2,. ω n such that ω i, ω i+1 Є E for 1 i N. The path from above diagram V 1 to V 3 is V 1, V 2 and V3. Length The length of the path is the number of edges on the path, which is equal to N-1, where N represents the number of vertices. The length of the above path V 1 to V 3 is 2. (i.e) (V 1, V 2), (V 2, V 3). If there is a path from a vertex to itself, with no edges, then the path length is 0. Loop If the graph contains an edge (v, v) from a vertex to itself, then the path is referred to as a loop. Simple Path A simple path is a path such that all vertices on the path, except possibly the first and the last are distinct. A simple cycle is the simple path of length atleast one that begins and ends at the same vertex. Cycle A cycle in a graph is a path in which first and last vertex are the same. A graph which has cycle is referred to as cyclic graph Degree The number of edges incident on a vertex determines its degree. The degree of the vertex V is written as degree (V). The indegree of the vertex V, is the number of edges entering into the vertex V. Similarly the out degree of the vertex V is the number of edges exiting from that vertex V.

38 Indegree (V 1) = 2 Outdegree (V 2) = 1 Acyclic Graph A directed graph which has no cycles is referred to as acyclic graph. It is abbreviated as DAG. DAG - Directed Acyclic Graph. REPRESENTATION OF GRAPH Graph can be represented by Adjacency Matrix and Adjacency list. Adjacency Matrix One simple way to represents a graph is Adjacency Matrix. The adjacency Matrix A for a graph G = (V, E) with n vertices is an n x n matrix, such that A ij = 1, if there is an edge V i to V j A ij = 0, if there is no edge. Adjacency Matrix For Directed Graph

39 Example V 1,2 = 1 Since there is an edge V 1 to V 2 Similarly V 1,3 = 1, there is an edge V 1 to V 3 V 1,1 & V 1,4 = 0, there is no edge. Adjacency Matrix for Undirected Graph Adjacency Matrix for Weighted Graph To solve some graph problems, Adjacency matrix can be constructed as A ij = C ij, if there exists an edge from V i to V j A ij = 0, if there is no edge & i = j If there is no arc from i to j, Assume C[i, j] = where i j. Adjacency Matrix for Undirected Graph Advantage * Simple to implement. Disadvantage * Takes O(n 2 ) space to represents the graph * It takes O(n 2 ) time to solve the most of the problems. Adjacency List Representation In this representation, we store a graph as a linked structure. We store all vertices in a list and then for each vertex, we have a linked list of its adjacency vertices

40 Disadvantage * It takes 0(n) time to determine whether there is an arc from vertex i to vertex j. Since there can be 0(n) vertices on the adjacency list for vertex i. Topological Sort A topological sort is a linear ordering of vertices in a directed acyclic graph such that if there is a path from V i to V j, then V j appears after V i in the linear ordering. Topological ordering is not possible. If the graph has a cycle, since for two vertices v and w on the cycle, v precedes w and w precedes v. To implement the topological sort, perform the following steps.

41 Step 1: - Find the indegree for every vertex. Step 2: - Place the vertices whose indegree is `0' on the empty queue. Step 3: - Dequeue the vertex V and decrement the indegree's of all its adjacent vertices. Step 4: - Enqueue the vertex on the queue, if its indegree falls to zero. Step 5: - Repeat from step 3 until the queue becomes empty. Step 6: - The topological ordering is the order in which the vertices dequeued. Routine to perform Topological Sort /* Assume that the graph is read into an adjacency matrix and that the indegrees are computed for every vertices and placed in an array (i.e. Indegree [ ] ) */ void Topsort (Graph G) { Queue Q ; int counter = 0; Vertex V, W ; Q = CreateQueue (NumVertex); Makeempty (Q); for each vertex V if (indegree [V] = = 0) Enqueue (V, Q); while (! IsEmpty (Q)) { V = Dequeue (Q); TopNum [V] = + + counter; for each W adjacent to V if (--Indegree [W] = = 0) Enqueue (W, Q); } if (counter! = NumVertex) Error (" Graph has a cycle"); DisposeQueue (Q); /* Free the Memory */ } Note : Enqueue (V, Q) implies to insert a vertex V into the queue Q. Dequeue (Q) implies to delete a vertex from the queue Q. TopNum [V] indicates an array to place the topological numbering. Example 1:

42 Step 1 Number of 1's present in each column of adjacency matrix represents the indegree of the corresponding vertex. Indegree [a] = 0 Indegree [b] = 1 Indegree [c] = 2 Indegree [d] = 2 Step 2 Enqueue the vertex, whose indegree is `0' Vertex `a' is 0, so place it on the queue. Step 3 Dequeue the vertex `a' from the queue and decrement the indegree's of its adjacent vertex `b' & `c' Hence, Indegree [b] = 0 & Indegree [c] = 1 Now, Enqueue the vertex `b' as its indegree becomes zero. Step 4 Dequeue the vertex `b' from Q and decrement the indegree's of its adjacent vertex `c' and `d'. Hence, Indegree [c] = 0 & Indegree [d] = 1 Now, Enqueue the vertex `c' as its indegree falls to zero. Step 5 Dequeue the vertex `c' from Q and decrement the indegree's of its adjacent vertex `d'. Hence, Indegree [d] = 0 Now, Enqueue the vertex `d' as its indegree falls to zero. Step 6 Dequeue the vertex `d'. Step 7 As the queue becomes empty, topological ordering is performed, which is nothing but, the order in which the vertices are dequeued. Example 2 VERTEX a b c d ENQUEUE a b c d DEQUEUE a b c d

43 Adjacency matrix:- V1 V2 V3 V4 V5 V6 V7 V V V V V V V INDEGREE Indegree [v1] = 0 Indegree [v2] = 1 Indegree [v3] = 2 Indegree [v4] = 3 Indegree [v5] = 1 Indegree [v6] = 3 Indegree [v7] = 2 VERTEX V V V V V V V ENQUEUE V1 V2 V5 V4 V3,V7 V6 DEQUEUE V1 V2 V5 V4 V3 V7 V6 Therefore, the topological order is V1, V2, V3, V4, V5, V7 and V6. Shortest Path Algorithm The Shortest path algorithm determines the minimum cost of the path from source to every other vertex. The cost of the path V 1, V 2, --V N is. This is referred as weighted path length. The unweighted path length is merely the number of the edges on the path, namely N - 1. Two types of shortest path problems, exist namely, 1. The single source shortest path problem

44 2. The all pairs shortest path problem The single source shortest path algorithm finds the minimum cost from single source vertex to all other vertices. Dijkstra's algorithm is used to solve this problem which follows the greedy technique. All pairs shortest path problem finds the shortest distance from each vertex to all other vertices. To solve this problem dynamic programming technique known as floyd's algorithm is used. These algorithms are applicable to both directed and undirected weighted graphs provided that they do not contain a cycle of negative length. Single Source Shortest Path Given an input graph G = (V, E) and a distinguished vertex S, find the shortest path from S to every other vertex in G. This problem could be applied to both weighted and unweighted graph. Unweighted Shortest Path In unweighted shortest path all the edges are assigned a weight of "1" for each vertex, the following three pieces of information is maintained. Algorithm for unweighted graph Known Specifies whether the vertex is processed or not. It is set to `1' after it is processed, otherwise `0'. Initially all vertices are marked unknown. (i.e) `0'. dv Specifies the distance from the source `s', initially all vertices are unreachable except for s, whose path length is `0'. P v Specifies the bookkeeping variable which will allow us to print. The actual path. (i.e) The vertex which makes the changes in dv. To implement the unweighted shortest path, perform the following steps : Step 1: - Assign the source node as `s' and Enqueue `s'. Step 2: - Dequeue the vertex `s' from queue and assign the value of that vertex to be known and then find its adjacency vertices. Step 3:- If the distance of the adjacent vertices is equal to infinity then change the distance of that vertex as the distance of its source vertex increment by `1' and Enqueue the vertex. Step 4:- Repeat from step 2, until the queue becomes empty. ROUTINE FOR UNWEIGHTED SHORTEST PATH

45 void Unweighted (Table T) { Queue Q; Vertex V, W ; Q = CreateQueue (NumVertex); MakeEmpty (Q); /* Enqueue the start vertex s */ Enqueue (s, Q); while (! IsEmpty (Q)) { V = Dequeue (Q); V = Dequeue (Q); T[V]. Known = True; /* Not needed anymore*/ for each W adjacent to V if (T[W]. Dist = = INFINITY) { T[W]. Dist = T[V]. Dist + 1 ; T[W]. path = V; Enqueue (W, Q); } } DisposeQueue (Q) ; /* Free the memory */ } Example Source vertex a is initially assigned a path length 0. After finding all vertices whose path a is 1. V Known d v p v a b 0 0 c 0 0 d 0 0 Queue a length from

46 After finding all vertices whose path length from a is 2. V Known d v p v a b 0 1 a c 0 1 a d 0 0 Queue b, c Dequeue a V Known d v p v a b 1 1 a c 0 1 a d 0 2 b Queue c, d Dequeue b V Known d v p v a b 1 1 a c 1 1 a d 0 2 b Queue d Dequeue c V Known d v p v a b 1 1 a c 1 1 a d 1 2 b Queue empty Dequeue d Shortest path from source a vertex a to other vertices are a b is 1 a c is 1 a d is 2 Example 2 V3 is taken as a source node and its path length is initialized to 0.

47 Graph after marking the start node as reachable in zero edges Graph after finding all vertices whose path length from source vertex is 1 Graph after finding all vertices whose path length from source vertex is 1 Initial configuration

48 V Known d v p v V1 0 0 V2 0 0 V V4 0 0 V5 0 0 V6 0 0 V7 0 0 In general when the vertex `V' is dequeued and the distance of its adjacency vertex `w' is infinitive then distance and path of `w' is calculated as follows T [W]. Dist = T[V]. Dist + 1 T[W]. path = V When V 3 is dequeued, known is set to 1 for that vertex the distance of its adjacent vertices V 1 and V 6 are updated if INFINITY. Path length of V 1 is calculated as T [V 1]. Dist = T [V 3]. Dist + 1 = = 1 and its actual path is also calculated as T [V 1].path = V 3 lll ly T [V 6]. Dist = T[V 3]. Dist + 1 = 1 T [V 6]. Path = V 3 Similarly, when the other vertices are dequeued, the table is updated. Initial state V3 Dequeued V1 Dequeued V Known d v p v Known d v p v Known d v p v V V3 1 1 V3 V V1 V V V1 V V V3 0 1 V3 V Q: V3 V1, V6 V6, V2, V4 V6 Dequeued V2 Dequeued V4 Dequeued

49 V Known d v p v Known d v p v Known d v p v V1 1 1 V3 1 1 V3 1 1 V3 V2 0 2 V1 1 2 V1 1 2 V1 V V4 0 2 V1 0 2 V1 1 2 V1 V V2 0 3 V2 V6 1 1 V3 1 1 V3 1 1 V3 V V4 Q: V2, V4 V4, V5 V5, V7 V5 Dequeued V7 Dequeued V Known d v p v Known d v p v V1 1 1 V3 1 1 V3 V2 1 2 V1 1 2 V1 V V4 1 2 V1 1 2 V1 V5 1 3 V2 1 3 V2 V6 1 1 V3 1 1 V3 V7 0 3 V4 1 3 V4 Q: V7 EMPTY Data changes during the unweighted shortest path algorithm. The shortest distance from the source vertex V 3 to all other vertex is listed below: V 3 V 1 is 1 V 3 V 2 is 2 V 3 V 4 is 2 V 3 V 5 is 3 V 3 V 6 is 1 V 3 V 7 is 3 Dijkstra's Algorithm The general method to solve the single source shortest path problem is known as Dijkstra's algorithm. This is applied to the weighted graph G. Dijkstra's algorithm is the prime example of Greedy technique, which generally solves a problem in stages by doing what appears to be the best thing at each stage. This algorithm proceeds in stages, just like the unweighted shortest path algorithm. At each stage, it selects a vertex v, which has the smallest dv among all the unknown vertices, and declares that as the shortest path from S to V and mark it to be known. We should set dw = dv + Cvw, if the new value for dw would be an improvement.

50 ROUTINE FOR ALGORITHM Void Dijkstra (Graph G, Table T) { int i ; vertex V, W; Read Graph (G, T) /* Read graph from adjacency list */ /* Table Initialization */ for (i = 0; i < Numvertex; i++) { T [i]. known = False; T [i]. Dist = Infinity; T [i]. path = NotA vertex; } T [start]. dist = 0; for ( ; ;) { V = Smallest unknown distance vertex; if (V = = Not A vertex) break ; T[V]. known = True; for each W adjacent to V if (! T[W]. known) { T [W]. Dist = Min [T[W]. Dist, T[V]. Dist + C VW] T[W]. path = V; } } } Example 1: V Known d v p v a b 0 0 c 0 0 d 0 0 Vertex `a' is chooses as source and is declared as known vertex. Then the adjacent vertices of `a' is found and their distances are updated as follows: T [b]. Dist = Min [T[b]. Dist, T[a]. Dist + C a,b] = Min [, 0 + 2] = 2 T [d]. Dist = Min [T[d]. Dist, T[a]. Dist + C a,d] = Min [, 0 + 1] = 1 V Known d v p v

51 a b 0 2 a c 0 0 d 0 1 a Now, select the vertex with minimum distance, which is not known and mark that vertex as visited. Here `d' is the next minimum distance vertex. The adjacent vertext to `d' is `c', therefore, the distance of c is updated a follows, T[c]. Dist = Min [T[c]. Dist, T[d]. Dist + C d, c] = Min [, 1 + 1] = 2 V Known d v p v a b 0 2 a c 0 2 d d 1 1 a The next minimum vertex is b and marks it as visited. Since the adjacent vertex d is already visited, select the next minimum vertex `c' and mark it as visited. V Known d v p v a b 1 2 a c 0 2 d d 1 1 a V Known d v p v a b 1 2 a c 1 2 d d 1 1 a Example 2 V Known d v p v V

52 V2 0 0 V3 0 0 V4 0 0 V5 0 0 V6 0 0 V7 0 0 V 1 is taken as source vertex, and is marked known. Then the d v and P v of its adjacent vertices are updated. V Known d v p v V V2 0 2 V1 V3 0 0 V4 0 1 V1 V5 0 0 V6 0 0 V7 0 0 V Known d v p v V V2 0 2 V1 V3 0 3 V4 V4 1 1 V1 V5 0 3 V4 V6 0 9 V4 V7 0 5 V4

53 V Known d v p v V V2 1 2 V1 V3 0 3 V4 V4 1 1 V1 V5 0 3 V4 V6 0 9 V4 V7 0 5 V4 V Known d v p v V V2 1 2 V1 V3 0 3 V4 V4 1 1 V1 V5 1 3 V4 V6 0 9 V4 V7 0 5 V4 V Known d v p v V V2 1 2 V1 V3 1 3 V4 V4 1 1 V1 V5 1 3 V4 V6 0 8 V3 V7 0 5 V4

54 V Known d v p v V V2 1 2 V1 V3 1 3 V4 V4 1 1 V1 V5 1 3 V4 V6 0 6 V7 V7 1 5 V4 V Known d v p v V V2 1 2 V1 V3 1 3 V4 V4 1 1 V1 V5 1 3 V4 V6 1 6 V7 V7 1 5 V4 The shortest distance from the source vertex V 1 to all other vertex is listed below : V 1 V 2 is 2 V 1 V 3 is 3 V 1 V 4 is 1 V 1 V 5 is 3 V 1 V 6 is 6 V 1 V 7 is 5 MINIMUM SPANNING TREE A spanning tree of a connected graph is its connected a cyclic subgraph that contains all the vertices of the graph. A minimum spanning tree of a weighted connected graph G is its spanning tree of the smallest weight, where the weight of a tree is defined as the sum of the weights on all its edges. The total number of edges in Minimum Spanning Tree (MST) is V -1 where V is the number of vertices. A minimum spanning tree exists if and only if G is connected. For any spanning Tree T, if an edge e that is not in T is added, a cycle is created. The removal of any edge on the cycle reinstates the spanning tree property.

55 Example Spanning tree for the above graph cost = 7 cost = 8 Cost=9 cost =8 cost = 5 Minimum spanning tree Prim's Algorithm

56 Prim's algorithm is one of the way to compute a minimum spanning tree which uses a greedy technique. This algorithm begins with a set U initialised to {1}. It this grows a spanning tree, one edge at a time. At each step, it finds a shortest edge (u,v) such that the cost of (u, v) is the smallest among all edges, where u is in Minimum Spanning Tree and V is not in Minimum Spanning Tree. Example:- V Known d v p v a b 0 0 c 0 0 d 0 0 Here, `a' is taken as source vertex and marked as visited. Then the distance of its adjacent vertex is updated as follows: T[b].dist = Min [T[b].Dist, C a,b] = Min (, 2) = 2 T[b].dist = Min [T[d].Dist, C a,b] = Min (, 1) = 1 T[c].dist = Min [T[c].Dist, C a,b] = Min (, 3) = 3 V Known d v p v a b 0 2 a c 0 3 a d 0 1 a Next, vertex `d' with minimum distance is marked as visited and the distance of its unknown adjacent vertex is updated. T[b].Dist = Min [T[b].Dist, C d,b] = Min (2, 2) = 2

57 T[c].dist = Min [T[c].Dist, C d,c] = Min (3,1) V Known d v p v a b 0 2 a c 0 1 d d 1 1 a Next, the vertex with minimum cost `c' is marked as visited and the distance of its unknown adjacent vertex is updated. V Known d v p v a b 0 2 a c 1 1 d d 1 1 a Since, there is no unknown vertex adjacent to `c', there is no updation in the distance. Finally, the vertex `b' which is not visited is marked. V Known d v p v a b 1 2 a c 1 1 d d 1 1 a

58 The minimum cost of this spanning Tree is 4 [i.e C a, b + C a,d + C c, d] Example 2 V Known d v p v V V2 0 0 V3 0 0 V4 0 0 V5 0 0 V6 0 0 V7 0 0 Consider V 1 as source vertex and proceed from there. Vertex V 1 is marked as visited and then the distance of its adjacent vertices are updated as follows. T[V 2].dist = Min [T[V 2].dist, C v1, v2] = Min [, 2]

59 = 2. T[V 4].dist = Min [T[V 4].dist, C v1, v4] = Min [, 1] = 1. T[V 3].dist = Min [T[V 3].dist, C v1, v3] = 4 V Known d v p v V V2 0 2 V1 V3 0 4 V1 V4 0 1 V1 V5 0 0 V6 0 0 V7 0 0 Vertex V 4 is marked as visited and then the distance of its adjacent vertices are updated. V Known d v p v V V2 0 2 V1 V3 0 2 V4 V4 1 1 V1 V5 0 7 V4 V6 0 8 V4 V7 0 4 V4 Vertex V 2 is visited andits adjacent vertices distances were updated. T[V 4].dist = Min [T[V 4].dist, C v2, v4]

60 = Min [1, 3] = 1. T[V 5].dist = Min [T[V 5].dist, C v2, v5] = Min [7, 10] = 7. V Known d v p v V V2 1 2 V1 V3 0 2 V4 V4 1 1 V1 V5 0 7 V4 V6 0 8 V4 V7 0 4 V4 T[V 6].dist = Min [T[V 6].dist, C v3, v6] T[V 6].dist = 5. = Min [8, 5] = 5. V Known d v p v V V2 1 2 V1 V3 1 2 V4 V4 1 1 V1 V5 0 7 V4 V6 0 5 V3 V7 0 4 V4 T[V 6].dist = Min [T[V 6].dist, C v7,v6] = Min [5, 1] = 1. T[V 6].dist = Min [T[V 6].dist, C v7,v5] = Min (7, 6) = 6.

61 V Known d v p v V V2 1 2 V1 V3 1 2 V4 V4 1 1 V1 V5 0 6 V7 V6 0 1 V7 V7 1 4 V4 V Known d v p v V V2 1 2 V1 V3 1 2 V4 V4 1 1 V1 V5 0 6 V7 V6 1 1 V7 V7 1 4 V4 V Known d v p v V V2 1 2 V1 V3 1 2 V4 V4 1 1 V1 V5 1 6 V7 V6 1 1 V7 V7 1 4 V4 The minimum cost of this spanning tree is 16.

62 Kruskal s algorithm Kruskal s algorithm uses a greedy technique to compute a minimum spanning tree. This algorithm selects the edges in the order of smallest weight and accept an edge if it does not cause a cycle. The algorithm terminates if enough edges are accepted. In general Kruskal s algorithm maintains a forest a collection of trees. Initially, there are V single node trees. Adding an edge merges two trees into one. When the algorithm terminates, there is only one tree, which is called as minimum spanning tree. Example 1 Step 1: Initially each vertex is in its own set.

63 Step 2: Now, select the edge with minimum weight Edge (a, d) is added to the minimum spanning tree. Step 3: Select the next edge, with minimum weight and check whether it forms a cycle. If it forms a cycle, then that edge is rejected, else add the edge to the minimum spanning tree. Step 4: Repeat step 3 until all vertices are included in the minimum spanning tree. The minimum cost of this spanning tree is 4. [i.e., cost(a, d) + cost (c, d) + cost (a, b)] Edge Weight Action (a, d) 1 Accepted (c, d) 1 Accepted (a, d) 2 Accepted Since all vertices are added the algorithm terminates. Example 2

64 Step 1: Step 2: Step 3 Step 4:

65 Step 5: Step 6 Edge Weight Action (V1,V4) 1 Accepted (V6,V7) 1 Accepted (V1,V2) 2 Accepted (V3,V4) 2 Accepted (V2,V4) 3 Rejected (V1,V3) 4 Rejected

66 (V4,V7) 4 Accepted (V3,V6) 5 Rejected (V5,V7) 6 Accepted GRAPH TRAVERSAL A graph traversal is a systematic way of visiting the nodes in a specific order. There are two types namely, 1) Breadth first traversal 2) Depth first traversal Breadth first traversal Breadth first search (BFS) of a graph G starts from an unvisited vertex u. then all the unvisited vertices v i adjacent to u are visited and then all unvisited vertices w j adjacent to v i are visited and so on. The traversal terminates when there are no more nodes tp visit. BFS uses a queue data structure to keep track of the order of nodes whose adjacent nodes are to be visited. Steps to be implemented in BFS Step 1: choose any node in the graph, designate it as search node and mark it as visited. Step 2: using the adjacency matrix of the graph, find all the unvisited adjacent node to the search node and enqueue them into the queue Q. Step 3: Then the node is dequeued from the queue. Mark that node as visited and designate it as new search node. Step 4: Repeat step 2 and 3 using the new search node. Step 5: This process continues until the queue Q which keeps track of the adjacent nodes is empty. Example: Adjacency matrix A B C D

67 A B C D Implementation 1. Let A be the source vertex. Mark it to as visited. 2. Find the adjacent unvisited vertices of A and enqueue then into the queue. Here B and C are adjacent nodes of A 3. Then vertex B is dequeued and its adjacent vertices C and D are taken from the adjacency matrix for enqueuing. Since vertex C is already in the queue, vertex D alone is enqueued. Here B is dequeued, D is enqueued. 4. Then vertex C is dequeued and its adjacent vertices A,B and D are found. Since vertices A and B are already visited and vertex D is also in the queue, no enqueue operation takes place. Here C is dequeued. 5. Then vertex D is dequeued. This process terminates as all the vertices are visited and the queue is empty. Depth first search Depth first works by selecting one vertex V of G as a start vertex ; V is marked visited. Then each unvisited vertex adjacent to V is searched in turn using depth first search recursively. This process continues until a dead end (i.e.) a vertex with no adjacent unvisited vertices is encountered. At a dead end, the algorithm backs up one edge to the vertex it came from and tries to continue visiting unvisited vertices from there. The algorithm eventually halts after backing up to the starting vertex, with the latter being a dead end. By then, all the vertices in the same connected component as the starting vertex have been visited. If unvisited vertices still remain, the depth first search must be restarted at any one of them. To implement the Depth first Search performs the following Steps: Step 1: Choose any node in the graph. Designate it as the search node and mark it as visited.

68 Step 2: Using the adjacency matrix of the graph, find a node adjacent to the search node that has not been visited yet. Designate this as the new search node and mark it as visited. Step 3: Repeat step 2 using the new search node. If no nodes satisfying (2) can be found, return to the previous search node and continue from there. Step 4: When a return to the previous search node in (3) is impossible, the search from the originally choosen search node is complete. Step 5: If the graph still contains unvisited nodes, choose any node that has not been visited and repeat step (1) through (4). Example: Adjacency matrix A B C D A B C D Implementation 1. Let `A' be the source vertex. Mark it to be visited. 2. Find the immediate adjacent unvisited vertex `B' of `A' Mark it to be visited. 3. From `B' the next adjacent vertex is `d' Mark it has visited. 4. From `D' the next unvisited vertex is `C' Mark it to be visited.

69 Applications of Depth First Search 1. To check whether the undirected graph is connected or not. 2. To check whether the connected undirected graph is Bioconnected or not. 3. To check the Acyclicity of the directed graph. UNDIRECTED GRAPHS A undirected graph is `connected' if and only if a depth first search starting from any node visits every node. Example: Adjacency matrix A B C D E A B C D E Implementation We start at vertex `A'. Then Mark A as visited and call DFS (B) recursively, Dfs (B) Marks B as visited and calls Dfs(c) recursively.

70 Dfs (c) marks C as visited and calls Dfs (D) recursively. No recursive calls are made to Dfs (B) since B is already visited. Dfs(D) marks D as visited. Dfs(D) sees A,B,C as marked so no recursive call is made there, and Dfs(D) returns back to Dfs(C). Dfs(C) calls Dfs(E), where E is unseen adjacent vertex to C. Since all the vertices starting from A are visited, so the above graph is said to be visited.

71

UNIT IV -NON-LINEAR DATA STRUCTURES 4.1 Trees TREE: A tree is a finite set of one or more nodes such that there is a specially designated node called the Root, and zero or more non empty sub trees T1,

More information

Trees. (Trees) Data Structures and Programming Spring / 28

Trees. (Trees) Data Structures and Programming Spring / 28 Trees (Trees) Data Structures and Programming Spring 2018 1 / 28 Trees A tree is a collection of nodes, which can be empty (recursive definition) If not empty, a tree consists of a distinguished node r

More information

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list UNIT-4 Graph: Terminology, Representation, Traversals Applications - spanning trees, shortest path and Transitive closure, Topological sort. Sets: Representation - Operations on sets Applications. 1. Name

More information

Binary Trees, Binary Search Trees

Binary Trees, Binary Search Trees Binary Trees, Binary Search Trees Trees Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search, insert, delete)

More information

UNIT 5 GRAPH. Application of Graph Structure in real world:- Graph Terminologies:

UNIT 5 GRAPH. Application of Graph Structure in real world:- Graph Terminologies: UNIT 5 CSE 103 - Unit V- Graph GRAPH Graph is another important non-linear data structure. In tree Structure, there is a hierarchical relationship between, parent and children that is one-to-many relationship.

More information

DATA STRUCTURES AND ALGORITHMS

DATA STRUCTURES AND ALGORITHMS DATA STRUCTURES AND ALGORITHMS UNIT 1 - LINEAR DATASTRUCTURES 1. Write down the definition of data structures? A data structure is a mathematical or logical way of organizing data in the memory that consider

More information

E.G.S. PILLAY ENGINEERING COLLEGE (An Autonomous Institution, Affiliated to Anna University, Chennai) Nagore Post, Nagapattinam , Tamilnadu.

E.G.S. PILLAY ENGINEERING COLLEGE (An Autonomous Institution, Affiliated to Anna University, Chennai) Nagore Post, Nagapattinam , Tamilnadu. 17CA 104DATA STRUCTURES Academic Year : 018-019 Programme : MCA Year / Semester : I / I Question Bank Course Coordinator: Mrs. C.Mallika Course Objectives The student should be able to 1. To understand

More information

CS8391-DATA STRUCTURES QUESTION BANK UNIT I

CS8391-DATA STRUCTURES QUESTION BANK UNIT I CS8391-DATA STRUCTURES QUESTION BANK UNIT I 2MARKS 1.Define data structure. The data structure can be defined as the collection of elements and all the possible operations which are required for those

More information

March 20/2003 Jayakanth Srinivasan,

March 20/2003 Jayakanth Srinivasan, Definition : A simple graph G = (V, E) consists of V, a nonempty set of vertices, and E, a set of unordered pairs of distinct elements of V called edges. Definition : In a multigraph G = (V, E) two or

More information

Basic Graph Definitions

Basic Graph Definitions CMSC 341 Graphs Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices, V, and a finite set of edges, E. Each edge is a pair (v,w) where v, w V. V and E are sets, so each vertex

More information

UNIT III TREES. A tree is a non-linear data structure that is used to represents hierarchical relationships between individual data items.

UNIT III TREES. A tree is a non-linear data structure that is used to represents hierarchical relationships between individual data items. UNIT III TREES A tree is a non-linear data structure that is used to represents hierarchical relationships between individual data items. Tree: A tree is a finite set of one or more nodes such that, there

More information

SNS COLLEGE OF TECHNOLOGY

SNS COLLEGE OF TECHNOLOGY SNS COLLEGE OF TECHNOLOGY COIMBATORE 5 DEPARTMENT OF COMPUTER SIENCE AND ENGINEERING (UG & PG) Second Year Computer Science and Engineering, rd Semester 2 Marks Question and Answer Subject Code & Name:

More information

CS8391-DATA STRUCTURES

CS8391-DATA STRUCTURES ST.JOSEPH COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERI NG CS8391-DATA STRUCTURES QUESTION BANK UNIT I 2MARKS 1.Explain the term data structure. The data structure can be defined

More information

CSCI2100B Data Structures Trees

CSCI2100B Data Structures Trees CSCI2100B Data Structures Trees Irwin King king@cse.cuhk.edu.hk http://www.cse.cuhk.edu.hk/~king Department of Computer Science & Engineering The Chinese University of Hong Kong Introduction General Tree

More information

Why Do We Need Trees?

Why Do We Need Trees? CSE 373 Lecture 6: Trees Today s agenda: Trees: Definition and terminology Traversing trees Binary search trees Inserting into and deleting from trees Covered in Chapter 4 of the text Why Do We Need Trees?

More information

CS 8391 DATA STRUCTURES

CS 8391 DATA STRUCTURES DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING QUESTION BANK CS 8391 DATA STRUCTURES UNIT- I PART A 1. Define: data structure. A data structure is a way of storing and organizing data in the memory for

More information

ROOT A node which doesn t have a parent. In the above tree. The Root is A. LEAF A node which doesn t have children is called leaf or Terminal node.

ROOT A node which doesn t have a parent. In the above tree. The Root is A. LEAF A node which doesn t have children is called leaf or Terminal node. UNIT III : DYNAMIC STORAGE MANAGEMENT Trees: Binary tree, Terminology, Representation, Traversals, Applications. Graph: Terminology, Representation, Traversals Applications - spanning trees, shortest path.

More information

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected. Chapter 4 Trees 4-1 Trees and Spanning Trees Trees, T: A simple, cycle-free, loop-free graph satisfies: If v and w are vertices in T, there is a unique simple path from v to w. Eg. Trees. Spanning trees:

More information

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F)) DHANALAKSHMI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING EC6301 OBJECT ORIENTED PROGRAMMING AND DATA STRUCTURES UNIT IV NONLINEAR DATA STRUCTURES Part A 1. Define Tree [N/D 08]

More information

Data Structures Brett Bernstein

Data Structures Brett Bernstein Data Structures Brett Bernstein Final Review 1. Consider a binary tree of height k. (a) What is the maximum number of nodes? (b) What is the maximum number of leaves? (c) What is the minimum number of

More information

Trees Algorhyme by Radia Perlman

Trees Algorhyme by Radia Perlman Algorhyme by Radia Perlman I think that I shall never see A graph more lovely than a tree. A tree whose crucial property Is loop-free connectivity. A tree which must be sure to span. So packets can reach

More information

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology Introduction Chapter 4 Trees for large input, even linear access time may be prohibitive we need data structures that exhibit average running times closer to O(log N) binary search tree 2 Terminology recursive

More information

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree Chapter 4 Trees 2 Introduction for large input, even access time may be prohibitive we need data structures that exhibit running times closer to O(log N) binary search tree 3 Terminology recursive definition

More information

End-Term Examination Second Semester [MCA] MAY-JUNE 2006

End-Term Examination Second Semester [MCA] MAY-JUNE 2006 (Please write your Roll No. immediately) Roll No. Paper Code: MCA-102 End-Term Examination Second Semester [MCA] MAY-JUNE 2006 Subject: Data Structure Time: 3 Hours Maximum Marks: 60 Note: Question 1.

More information

Fundamentals of Data Structure

Fundamentals of Data Structure Fundamentals of Data Structure Set-1 1. Which if the following is/are the levels of implementation of data structure A) Abstract level B) Application level C) Implementation level D) All of the above 2.

More information

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302 Topics VCU, Department of Computer Science CMSC 302 Trees Vojislav Kecman Terminology Trees as Models Some Tree Theorems Applications of Trees Binary Search Tree Decision Tree Tree Traversal Spanning Trees

More information

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC) SET - 1 II B. Tech I Semester Supplementary Examinations, May/June - 2016 PART A 1. a) Write a procedure for the Tower of Hanoi problem? b) What you mean by enqueue and dequeue operations in a queue? c)

More information

Course Review for Finals. Cpt S 223 Fall 2008

Course Review for Finals. Cpt S 223 Fall 2008 Course Review for Finals Cpt S 223 Fall 2008 1 Course Overview Introduction to advanced data structures Algorithmic asymptotic analysis Programming data structures Program design based on performance i.e.,

More information

TREES. Trees - Introduction

TREES. Trees - Introduction TREES Chapter 6 Trees - Introduction All previous data organizations we've studied are linear each element can have only one predecessor and successor Accessing all elements in a linear sequence is O(n)

More information

CS DATA STRUCTURES AND ALGORITHMS

CS DATA STRUCTURES AND ALGORITHMS Computer Science and Engineering Third Semester CS1211 - DATA STRUCTURES AND ALGORITHMS UNIT-I - INTRODUCTION TO DATASTRUCTURES 1.Write down the definition of data structures? PART -A A data structure

More information

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop. Tree A tree consists of a set of nodes and a set of edges connecting pairs of nodes. A tree has the property that there is exactly one path (no more, no less) between any pair of nodes. A path is a connected

More information

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n)

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n) CSE 0 Test Your name as it appears on your UTA ID Card Fall 0 Multiple Choice:. Write the letter of your answer on the line ) to the LEFT of each problem.. CIRCLED ANSWERS DO NOT COUNT.. points each. The

More information

[ DATA STRUCTURES ] Fig. (1) : A Tree

[ DATA STRUCTURES ] Fig. (1) : A Tree [ DATA STRUCTURES ] Chapter - 07 : Trees A Tree is a non-linear data structure in which items are arranged in a sorted sequence. It is used to represent hierarchical relationship existing amongst several

More information

CS24 Week 8 Lecture 1

CS24 Week 8 Lecture 1 CS24 Week 8 Lecture 1 Kyle Dewey Overview Tree terminology Tree traversals Implementation (if time) Terminology Node The most basic component of a tree - the squares Edge The connections between nodes

More information

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example. Trees, Binary Search Trees, and Heaps CS 5301 Fall 2013 Jill Seaman Tree: non-recursive definition Tree: set of nodes and directed edges - root: one node is distinguished as the root - Every node (except

More information

S.E. Sem. III [INFT] Data Structures & Analysis. Primitive Linear Non Linear

S.E. Sem. III [INFT] Data Structures & Analysis. Primitive Linear Non Linear S.E. Sem. III [INFT] Data Structures & Analysis Time : 3 Hrs.] Prelim Paper Solution [Marks : 80 Q.1(a) Explain different types of data structures with examples. [5] Ans.: Types of Data Structure : Data

More information

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text) Lec 17 April 8 Topics: binary Trees expression trees Binary Search Trees (Chapter 5 of text) Trees Linear access time of linked lists is prohibitive Heap can t support search in O(log N) time. (takes O(N)

More information

Data Structure. IBPS SO (IT- Officer) Exam 2017

Data Structure. IBPS SO (IT- Officer) Exam 2017 Data Structure IBPS SO (IT- Officer) Exam 2017 Data Structure: In computer science, a data structure is a way of storing and organizing data in a computer s memory so that it can be used efficiently. Data

More information

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w Chapter 7 Trees 7.1 Introduction A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w Tree Terminology Parent Ancestor Child Descendant Siblings

More information

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May www.jwjobs.net R10 SET - 1 II B. Tech I Semester, Supplementary Examinations, May - 2012 (Com. to CSE, IT, ECC ) Time: 3 hours Max Marks: 75 *******-****** 1. a) Which of the given options provides the

More information

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs 2011 Pearson Addison-Wesley. All rights reserved 14 A-1 Terminology G = {V, E} A graph G consists of two sets A set V of vertices, or nodes A set E of edges A subgraph Consists of a subset

More information

Undirected Graphs. Hwansoo Han

Undirected Graphs. Hwansoo Han Undirected Graphs Hwansoo Han Definitions Undirected graph (simply graph) G = (V, E) V : set of vertexes (vertices, nodes, points) E : set of edges (lines) An edge is an unordered pair Edge (v, w) = (w,

More information

Graph. Vertex. edge. Directed Graph. Undirected Graph

Graph. Vertex. edge. Directed Graph. Undirected Graph Module : Graphs Dr. Natarajan Meghanathan Professor of Computer Science Jackson State University Jackson, MS E-mail: natarajan.meghanathan@jsums.edu Graph Graph is a data structure that is a collection

More information

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II Anna University 2 & 16 Mark Questions & Answers Year / Semester: II / III

More information

Chapter 9 Graph Algorithms

Chapter 9 Graph Algorithms Chapter 9 Graph Algorithms 2 Introduction graph theory useful in practice represent many real-life problems can be slow if not careful with data structures 3 Definitions an undirected graph G = (V, E)

More information

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions Introduction Chapter 9 Graph Algorithms graph theory useful in practice represent many real-life problems can be slow if not careful with data structures 2 Definitions an undirected graph G = (V, E) is

More information

Graphs & Digraphs Tuesday, November 06, 2007

Graphs & Digraphs Tuesday, November 06, 2007 Graphs & Digraphs Tuesday, November 06, 2007 10:34 PM 16.1 Directed Graphs (digraphs) like a tree but w/ no root node & no guarantee of paths between nodes consists of: nodes/vertices - a set of elements

More information

CP2 Revision. theme: dynamic datatypes & data structures

CP2 Revision. theme: dynamic datatypes & data structures CP2 Revision theme: dynamic datatypes & data structures structs can hold any combination of datatypes handled as single entity struct { }; ;

More information

CS 310 Advanced Data Structures and Algorithms

CS 310 Advanced Data Structures and Algorithms CS 31 Advanced Data Structures and Algorithms Graphs July 18, 17 Tong Wang UMass Boston CS 31 July 18, 17 1 / 4 Graph Definitions Graph a mathematical construction that describes objects and relations

More information

D. Θ nlogn ( ) D. Ο. ). Which of the following is not necessarily true? . Which of the following cannot be shown as an improvement? D.

D. Θ nlogn ( ) D. Ο. ). Which of the following is not necessarily true? . Which of the following cannot be shown as an improvement? D. CSE 0 Name Test Fall 00 Last Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. The time to convert an array, with priorities stored at subscripts through n,

More information

( ) ( ) C. " 1 n. ( ) $ f n. ( ) B. " log( n! ) ( ) and that you already know ( ) ( ) " % g( n) ( ) " #&

( ) ( ) C.  1 n. ( ) $ f n. ( ) B.  log( n! ) ( ) and that you already know ( ) ( )  % g( n) ( )  #& CSE 0 Name Test Summer 008 Last 4 Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. The time for the following code is in which set? for (i=0; i

More information

Graph Algorithms Using Depth First Search

Graph Algorithms Using Depth First Search Graph Algorithms Using Depth First Search Analysis of Algorithms Week 8, Lecture 1 Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Graph Algorithms Using Depth

More information

Data Structures Question Bank Multiple Choice

Data Structures Question Bank Multiple Choice Section 1. Fundamentals: Complexity, Algorthm Analysis 1. An algorithm solves A single problem or function Multiple problems or functions Has a single programming language implementation 2. A solution

More information

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 The data of the problem is of 2GB and the hard disk is of 1GB capacity, to solve this problem we should Use better data structures

More information

Binary Trees. Height 1

Binary Trees. Height 1 Binary Trees Definitions A tree is a finite set of one or more nodes that shows parent-child relationship such that There is a special node called root Remaining nodes are portioned into subsets T1,T2,T3.

More information

Introduction to Graphs. common/notes/ppt/

Introduction to Graphs.   common/notes/ppt/ Introduction to Graphs http://people.cs.clemson.edu/~pargas/courses/cs212/ common/notes/ppt/ Introduction Graphs are a generalization of trees Nodes or verticies Edges or arcs Two kinds of graphs irected

More information

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees Computer Science 0 Data Structures Siena College Fall 08 Topic Notes: Trees We ve spent a lot of time looking at a variety of structures where there is a natural linear ordering of the elements in arrays,

More information

n 2 C. Θ n ( ) Ο f ( n) B. n 2 Ω( n logn)

n 2 C. Θ n ( ) Ο f ( n) B. n 2 Ω( n logn) CSE 0 Name Test Fall 0 Last Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. The time to find the maximum of the n elements of an integer array is in: A.

More information

SELF-BALANCING SEARCH TREES. Chapter 11

SELF-BALANCING SEARCH TREES. Chapter 11 SELF-BALANCING SEARCH TREES Chapter 11 Tree Balance and Rotation Section 11.1 Algorithm for Rotation BTNode root = left right = data = 10 BTNode = left right = data = 20 BTNode NULL = left right = NULL

More information

Direct Addressing Hash table: Collision resolution how handle collisions Hash Functions:

Direct Addressing Hash table: Collision resolution how handle collisions Hash Functions: Direct Addressing - key is index into array => O(1) lookup Hash table: -hash function maps key to index in table -if universe of keys > # table entries then hash functions collision are guaranteed => need

More information

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees Chapter 11 Copyright McGraw-Hill Education. All rights reserved. No reproduction or distribution without the prior written consent of McGraw-Hill Education. Chapter Summary Introduction to Trees Applications

More information

( ) D. Θ ( ) ( ) Ο f ( n) ( ) Ω. C. T n C. Θ. B. n logn Ο

( ) D. Θ ( ) ( ) Ο f ( n) ( ) Ω. C. T n C. Θ. B. n logn Ο CSE 0 Name Test Fall 0 Multiple Choice. Write your answer to the LEFT of each problem. points each. The expected time for insertion sort for n keys is in which set? (All n! input permutations are equally

More information

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE A6-R3: DATA STRUCTURE THROUGH C LANGUAGE NOTE: 1. There are TWO PARTS in this Module/Paper. PART ONE contains FOUR questions and PART TWO contains FIVE questions. 2. PART ONE is to be answered in the TEAR-OFF

More information

Chapter 9 Graph Algorithms

Chapter 9 Graph Algorithms Chapter 9 Graph Algorithms 2 Introduction graph theory useful in practice represent many real-life problems can be if not careful with data structures 3 Definitions an undirected graph G = (V, E) is a

More information

Analysis of Algorithms

Analysis of Algorithms Analysis of Algorithms Trees-I Prof. Muhammad Saeed Tree Representation.. Analysis Of Algorithms 2 .. Tree Representation Analysis Of Algorithms 3 Nomenclature Nodes (13) Size (13) Degree of a node Depth

More information

Objective Questions for Online Practical Exams under CBCS Scheme Subject: Data Structure-I (CS-113)

Objective Questions for Online Practical Exams under CBCS Scheme Subject: Data Structure-I (CS-113) Objective Questions for Online Practical Exams under CBCS Scheme Subject: Data Structure-I (CS-113) 1. The number of interchanges required to sort 5, 1, 6, 2 4 in ascending order using Bubble Sort (A)

More information

Trees. Truong Tuan Anh CSE-HCMUT

Trees. Truong Tuan Anh CSE-HCMUT Trees Truong Tuan Anh CSE-HCMUT Outline Basic concepts Trees Trees A tree consists of a finite set of elements, called nodes, and a finite set of directed lines, called branches, that connect the nodes

More information

1 P a g e A r y a n C o l l e g e \ B S c _ I T \ C \

1 P a g e A r y a n C o l l e g e \ B S c _ I T \ C \ BSc IT C Programming (2013-2017) Unit I Q1. What do you understand by type conversion? (2013) Q2. Why we need different data types? (2013) Q3 What is the output of the following (2013) main() Printf( %d,

More information

12 Abstract Data Types

12 Abstract Data Types 12 Abstract Data Types 12.1 Foundations of Computer Science Cengage Learning Objectives After studying this chapter, the student should be able to: Define the concept of an abstract data type (ADT). Define

More information

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR STUDENT IDENTIFICATION NO MULTIMEDIA COLLEGE JALAN GURNEY KIRI 54100 KUALA LUMPUR FIFTH SEMESTER FINAL EXAMINATION, 2014/2015 SESSION PSD2023 ALGORITHM & DATA STRUCTURE DSEW-E-F-2/13 25 MAY 2015 9.00 AM

More information

COMP : Trees. COMP20012 Trees 219

COMP : Trees. COMP20012 Trees 219 COMP20012 3: Trees COMP20012 Trees 219 Trees Seen lots of examples. Parse Trees Decision Trees Search Trees Family Trees Hierarchical Structures Management Directories COMP20012 Trees 220 Trees have natural

More information

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) WINTER 18 EXAMINATION Subject Name: Data Structure Model wer Subject Code: 17330 Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as given in

More information

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305 Q.1 If h is any hashing function and is used to hash n keys in to a table of size m, where n

More information

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE A6-R3: DATA STRUCTURE THROUGH C LANGUAGE NOTE: 1. There are TWO PARTS in this Module/Paper. PART ONE contains FOUR questions and PART TWO contains FIVE questions. 2. PART ONE is to be answered in the TEAR-OFF

More information

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value 1 Possible implementations Sorted linear implementations o Appropriate if

More information

n 2 ( ) ( ) Ο f ( n) ( ) Ω B. n logn Ο

n 2 ( ) ( ) Ο f ( n) ( ) Ω B. n logn Ο CSE 220 Name Test Fall 20 Last 4 Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. 4 points each. The time to compute the sum of the n elements of an integer array is in:

More information

Chapter Summary. Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees

Chapter Summary. Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees Trees Chapter 11 Chapter Summary Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees Introduction to Trees Section 11.1 Section Summary Introduction to Trees

More information

Trees. A tree is a directed graph with the property

Trees. A tree is a directed graph with the property 2: Trees Trees A tree is a directed graph with the property There is one node (the root) from which all other nodes can be reached by exactly one path. Seen lots of examples. Parse Trees Decision Trees

More information

S.SAKTHI, LECTURER/IT

S.SAKTHI, LECTURER/IT NH 67, Karur Trichy Highways, Puliyur C.F, 639 114 Karur District DATA STRUCTURES AND ALGORITHMS QUESTION BANK PREPARED BY: S.SAKTHI-LECTURER/IT BRANCH/YEAR: EEE/II UNIT-I (LINEAR STRUCTURES) 1. What is

More information

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1 Graph Algorithms Chapter 22 CPTR 430 Algorithms Graph Algorithms Why Study Graph Algorithms? Mathematical graphs seem to be relatively specialized and abstract Why spend so much time and effort on algorithms

More information

CE 221 Data Structures and Algorithms

CE 221 Data Structures and Algorithms CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, 4.1 4.2 Izmir University of Economics 1 Preliminaries - I (Recursive) Definition: A tree is a collection of nodes. The

More information

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents Section 5.5 Binary Tree A binary tree is a rooted tree in which each vertex has at most two children and each child is designated as being a left child or a right child. Thus, in a binary tree, each vertex

More information

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct. MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct. In linked list the elements are necessarily to be contiguous In linked list the elements may locate at far positions

More information

Stacks, Queues and Hierarchical Collections

Stacks, Queues and Hierarchical Collections Programming III Stacks, Queues and Hierarchical Collections 2501ICT Nathan Contents Linked Data Structures Revisited Stacks Queues Trees Binary Trees Generic Trees Implementations 2 Copyright 2002- by

More information

CS6301 Programming and Data Structures II Unit -5 REPRESENTATION OF GRAPHS Graph and its representations Graph is a data structure that consists of following two components: 1. A finite set of vertices

More information

Binary Trees

Binary Trees Binary Trees 4-7-2005 Opening Discussion What did we talk about last class? Do you have any code to show? Do you have any questions about the assignment? What is a Tree? You are all familiar with what

More information

& ( D. " mnp ' ( ) n 3. n 2. ( ) C. " n

& ( D.  mnp ' ( ) n 3. n 2. ( ) C.  n CSE Name Test Summer Last Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. The time to multiply two n " n matrices is: A. " n C. "% n B. " max( m,n, p). The

More information

Graphs V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)}

Graphs V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)} Graphs and Trees 1 Graphs (simple) graph G = (V, ) consists of V, a nonempty set of vertices and, a set of unordered pairs of distinct vertices called edges. xamples V={,,,,} ={ (,),(,),(,), (,),(,),(,)}

More information

Minimum Spanning Trees Ch 23 Traversing graphs

Minimum Spanning Trees Ch 23 Traversing graphs Next: Graph Algorithms Graphs Ch 22 Graph representations adjacency list adjacency matrix Minimum Spanning Trees Ch 23 Traversing graphs Breadth-First Search Depth-First Search 11/30/17 CSE 3101 1 Graphs

More information

Konigsberg Bridge Problem

Konigsberg Bridge Problem Graphs Konigsberg Bridge Problem c C d g A Kneiphof e D a B b f c A C d e g D a b f B Euler s Graph Degree of a vertex: the number of edges incident to it Euler showed that there is a walk starting at

More information

Chapter 20: Binary Trees

Chapter 20: Binary Trees Chapter 20: Binary Trees 20.1 Definition and Application of Binary Trees Definition and Application of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two other

More information

) $ f ( n) " %( g( n)

) $ f ( n)  %( g( n) CSE 0 Name Test Spring 008 Last Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. The time to compute the sum of the n elements of an integer array is: # A.

More information

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS Department of Computer Science University of Babylon LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS By Faculty of Science for Women( SCIW), University of Babylon, Iraq Samaher@uobabylon.edu.iq

More information

Chapter 9 Graph Algorithms

Chapter 9 Graph Algorithms Introduction graph theory useful in practice represent many real-life problems can be if not careful with data structures Chapter 9 Graph s 2 Definitions Definitions an undirected graph is a finite set

More information

Analysis of Algorithms

Analysis of Algorithms Algorithm An algorithm is a procedure or formula for solving a problem, based on conducting a sequence of specified actions. A computer program can be viewed as an elaborate algorithm. In mathematics and

More information

Algorithm Design (8) Graph Algorithms 1/2

Algorithm Design (8) Graph Algorithms 1/2 Graph Algorithm Design (8) Graph Algorithms / Graph:, : A finite set of vertices (or nodes) : A finite set of edges (or arcs or branches) each of which connect two vertices Takashi Chikayama School of

More information

CS301 - Data Structures Glossary By

CS301 - Data Structures Glossary By CS301 - Data Structures Glossary By Abstract Data Type : A set of data values and associated operations that are precisely specified independent of any particular implementation. Also known as ADT Algorithm

More information

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge Trees & Heaps Week 12 Gaddis: 20 Weiss: 21.1-3 CS 5301 Fall 2018 Jill Seaman!1 Tree: non-recursive definition! Tree: set of nodes and directed edges - root: one node is distinguished as the root - Every

More information

CSI 604 Elementary Graph Algorithms

CSI 604 Elementary Graph Algorithms CSI 604 Elementary Graph Algorithms Ref: Chapter 22 of the text by Cormen et al. (Second edition) 1 / 25 Graphs: Basic Definitions Undirected Graph G(V, E): V is set of nodes (or vertices) and E is the

More information

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer) COSC 2007 Data Structures II Final Exam Thursday, April 13 th, 2006 This is a closed book and closed notes exam. There are total 3 parts. Please answer the questions in the provided space and use back

More information