Any two nodes which are connected by an edge in a graph are called adjacent node.



Similar documents
Data Structure [Question Bank]

Social Media Mining. Graph Essentials

IE 680 Special Topics in Production Systems: Networks, Routing and Logistics*

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Ordered Lists and Binary Trees

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Binary Search Trees CMPSC 122

TREE BASIC TERMINOLOGIES

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

DATA STRUCTURES USING C

GRAPH THEORY LECTURE 4: TREES

Data Structures and Algorithms

Binary Search Trees (BST)

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

Exam study sheet for CS2711. List of topics

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Algorithms and Data Structures

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Analysis of Algorithms I: Binary Search Trees

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Data Structure with C

Binary Trees and Huffman Encoding Binary Search Trees

PES Institute of Technology-BSC QUESTION BANK

International Journal of Software and Web Sciences (IJSWS)

Symbol Tables. Introduction

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Data Structures. Level 6 C Module Descriptor

Data Structures and Algorithms Written Examination

DATA ANALYSIS II. Matrix Algorithms

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Converting a Number from Decimal to Binary

Data Structures UNIT III. Model Question Answer

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Graph Theory and Topology Design

Parallelization: Binary Tree Traversal

10CS35: Data Structures Using C

Chapter 14 The Binary Search Tree

CMPSCI611: Approximating MAX-CUT Lecture 20

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Warshall s Algorithm: Transitive Closure

Cpt S 223. School of EECS, WSU

V. Adamchik 1. Graph Theory. Victor Adamchik. Fall of 2005

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. Course Curriculum. DATA STRUCTURES (Code: )

International Journal of Advanced Research in Computer Science and Software Engineering

Handout #Ch7 San Skulrattanakulchai Gustavus Adolphus College Dec 6, Chapter 7: Digraphs

From Last Time: Remove (Delete) Operation

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

CS711008Z Algorithm Design and Analysis

Load balancing Static Load Balancing

Big Data and Scripting. Part 4: Memory Hierarchies

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

Data Structures Fibonacci Heaps, Amortized Analysis

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Algorithms Chapter 12 Binary Search Trees

Java Software Structures

Rotation Operation for Binary Search Trees Idea:

Network (Tree) Topology Inference Based on Prüfer Sequence

Exercises Software Development I. 11 Recursion, Binary (Search) Trees. Towers of Hanoi // Tree Traversal. January 16, 2013

Data Structures. Chapter 8

Heaps & Priority Queues in the C++ STL 2-3 Trees

Approximation Algorithms

Binary Trees. Wellesley College CS230 Lecture 17 Thursday, April 5 Handout #28. PS4 due 1:30pm Tuesday, April

Scheduling Shop Scheduling. Tim Nieberg

Operations: search;; min;; max;; predecessor;; successor. Time O(h) with h height of the tree (more on later).

Chinese postman problem

Algorithms and data structures

Binary Heap Algorithms

Graph Theory Problems and Solutions

Load Balancing and Termination Detection

Data Structures and Data Manipulation

Euclidean Minimum Spanning Trees Based on Well Separated Pair Decompositions Chaojun Li. Advised by: Dave Mount. May 22, 2014

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

CSE 326: Data Structures B-Trees and B+ Trees

Practical Graph Mining with R. 5. Link Analysis

Data Structures Using C++

Introduction to Data Structures and Algorithms

Discrete Mathematics & Mathematical Reasoning Chapter 10: Graphs

Sample Questions Csci 1112 A. Bellaachia

Mathematics for Algorithm and System Analysis

A Fast Algorithm For Finding Hamilton Cycles

Solutions to Homework 6

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Data Structures and Algorithm Analysis (CSC317) Intro/Review of Data Structures Focus on dynamic sets

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

Outline. NP-completeness. When is a problem easy? When is a problem hard? Today. Euler Circuits

Optimal Binary Search Trees Meet Object Oriented Programming

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

Row Echelon Form and Reduced Row Echelon Form

Full and Complete Binary Trees

6 March Array Implementation of Binary Trees

Load Balancing and Termination Detection

Transcription:

. iscuss following. Graph graph G consist of a non empty set V called the set of nodes (points, vertices) of the graph, a set which is the set of edges and a mapping from the set of edges to a set of pairs of elements of V. It is also convenient to write a graph as G=(V,). Notice that definition of graph implies that to every edge of a graph G, we can associate a pair of nodes of the graph. If an edge X Є is thus associated with a pair of nodes (U,V) where U, V Є V then we says that edge x connect U and V.. djacent Nodes ny two nodes which are connected by an edge in a graph are called adjacent node.. irected & Undirected dge In a graph G=(V,) an edge which is directed from one end to another end is called a directed edge, while the edge which has no specific direction is called undirected edge.. irected graph (igraph) graph in which every edge is directed is called directed graph or digraph.. Undirected graph graph in which every edge is undirected is called directed graph or digraph.. Mixed Graph If some of the edges are directed and some are undirected in graph then the graph is called mixed graph. 7. Loop (Sling) n edge of a graph which joins a node to itself is called a loop (sling). 8. Multigraph ny graph which contains some parallel edges is called multigraph. 9. Weighted Graph graph in which weights are assigned to every edge is called weighted graph. 0. Isolated Node In a graph a node which is not adjacent to any other node is called isolated node. Prof. Pradyumansinh Jadeja (987988) 070 ata & ile Structure

. Null Graph graph containing only isolated nodes are called null graph. In other words set of edges in null graph is empty.. Path of Graph Let G=(V, ) be a simple digraph such that the terminal node of any edge in the sequence is the initial node of the edge, if any appearing next in the sequence defined as path of the graph.. Length of Path The number of edges appearing in the sequence of the path is called length of path.. egree of vertex The no of edges which have V as their terminal node is call as indegree of node V The no of edges which have V as their initial node is call as outdegree of node V Sum of indegree and outdegree of node V is called its Total egree or egree of vertex.. Simple Path (dge Simple) path in a diagraph in which the edges are distinct is called simple path or edge simple.. lementary Path (Node Simple) path in which all the nodes through which it traverses are distinct is called elementary path. 7. ycle (ircuit) path which originates and ends in the same node is called cycle (circuit). 8. irected directed tree is an acyclic digraph which has one node called its root with in degree 0, while all other nodes have in degree. very directed tree must have at least one node. n isolated node is also a directed tree. 9. Terminal Node (Leaf Node) In a directed tree, any node which has out degree 0 is called terminal node or left node. 0. Level of Node The level of any node is the length of its path from the root.. Ordered In a directed tree an ordering of the nodes at each level is prescribed then such a tree is called ordered tree. Prof. Pradyumansinh Jadeja (987988) 070 ata & ile Structure

. orest If we delete the root and its edges connecting the nodes at level, we obtain a set of disjoint tree. set of disjoint tree is a forest.. M ary If in a directed tree the out degree of every node is less than or equal to m then tree is called an m ary tree.. ull or omplete M ary If the out degree of each and every node is exactly equal to m or 0 and their number of nodes at level i is m (i ) then the tree is called a full or complex m ary tree.. Positional M ary If we consider m ary trees in which the m children of any node are assumed to have m distinct positions, if such positions are taken into account, then tree is called positional m ary tree.. Height of the tree The height of a tree is the length of the path from the root to the deepest node in the tree. 7. inary tree If in a directed tree the out degree of every node is less than or equal to then tree is called binary tree. 8. Strictly binary tree strictly binary tree (sometimes proper binary tree or tree or full binary tree) is a tree in which every node other than the leaves has two children. 9. omplete binary tree If the out degree of each and every node is exactly equal to or 0 and their number of nodes at level i is (i ) then the tree is called a full or complete binary tree. 0. Sibling Siblings are nodes that share the same parent node.. inary search tree binary search tree is a binary tree in which each node possessed a key that satisfy the following conditions. ll key (if any) in the left sub tree of the root precedes the key in the root.. The key in the root precedes all key (if any) in the right sub tree.. The left and right sub tree sub trees of the root are again search trees. Prof. Pradyumansinh Jadeja (987988) 070 ata & ile Structure

. Height alanced inary tree (VL ) tree is called VL (height balance binary tree), if each node possesses one of the following properties. node is called left heavy if the longest path in its left sub tree is one longer then the longest path of its right sub tree.. node is called right heavy if the longest path in the right sub tree is one longer than path in its left sub tree.. node is called balanced, if the longest path in both the right and left sub tree are equal.. xplain the Preorder, Inorder and Postorder traversal techniques of the binary tree with suitable example. The most common operations performed on tree structure is that of traversal. This is a procedure by which each node in the tree is processed exactly once in a systematic manner. There are three ways of traversing a binary tree. Preorder Preorder traversal of a binary tree is defined as follow o Process the root node o Traverse the left subtree in preorder o Traverse the right subtree in preorder If particular subtree is empty (i.e., node has no left or right descendant) the traversal is performed by doing nothing, In other words, a null subtree is considered to be fully traversed when it is encountered. Preorder traversal : G Inorder traversal : G Postorder traversal : G G onverse Preorder traversal : onverse Inorder traversal : G G ig.. onverse Postorder traversal : G The preorder traversal of a tree (ig..) is given by G Prof. Pradyumansinh Jadeja (987988) 070 ata & ile Structure

Inorder The Inorder traversal of a binary tree is given by following steps, o Traverse the left subtree in Inorder o Process the root node o Traverse the right subtree in Inorder The Inorder traversal of a tree (ig..) is given by G Postorder The postorder traversal is given by o Traverse the left subtree in postorder o Traverse the right subtree in postorder o Process the root node The Postorder traversal of a tree (ig..) is given by G onverse If we interchange left and right words in the preceding definitions, we obtain three new traversal orders which are called o onverse Preorder ( G ) o onverse Inorder (G ) o onverse Postorder (G ). Write the algorithm of Preorder, Inorder and Postorder traversal techniques of the binary tree. Procedure : RPRORR(T) Given a binary tree whose root node address is given by pointer variable T and whose node structure is same as described below. This procedure traverse the tree in preorder, in a recursive manner. Prof. Pradyumansinh Jadeja (987988) 070 ata & ile Structure

LPTR T RPTR. [heck for empty ] If T = NULL then write ( mpty ) return else write (T(T)). [Process the Left Subtree] If LPTR (T) NULL then RPRORR (LPTR (T)). [Process the Right Subtree] If RPTR (T) NULL then RPRORR (RPTR (T)). [inished] return Procedure : RINORR(T) Given a binary tree whose root node address is given by pointer variable T and whose node structure is same as described below. This procedure traverse the tree in inorder, in a recursive manner.. [heck for empty ] If T = NULL then write ( mpty ) return. [Process the Left Subtree] If LPTR (T) NULL then RINORR (LPTR (T)). [Process the root node] write (T(T)). [Process the Right Subtree] If RPTR (T) NULL then RINORR (RPTR (T)). [inished] return Prof. Pradyumansinh Jadeja (987988) 070 ata & ile Structure

Procedure : RPOSTORR(T) Given a binary tree whose root node address is given by pointer variable T and whose node structure is same as described below. This procedure traverse the tree in postorder, in a recursive manner.. [heck for empty ] If T = NULL then write ( mpty ) return. [Process the Left Subtree] If LPTR (T) NULL then RPOSTORR (LPTR (T)). [Process the Right Subtree] If RPTR (T) NULL then RPOSTORR (RPTR (T)). [Process the root node] write (T(T)). [inished] return. Give traversal order of following tree into Inorder, Preorder and Postorder. Inorder: Preorder: Post order: 7 Prof. Pradyumansinh Jadeja (987988) 070 ata & ile Structure

. onstruct a tree for the given Inorder and Postorder traversals Inorder : G H I Postorder : G H I G H I G H I G H I 8 Prof. Pradyumansinh Jadeja (987988) 070 ata & ile Structure

Postorder : H G I Inorder : G H I G H I G H I G H I G I H 9 Prof. Pradyumansinh Jadeja (987988) 070 ata & ile Structure

. onstruct a tree for the given Inorder and Preorder traversals Preorder : G Q K P R H Inorder : Q K G P H R G Q K P H R G P Q K R H G P Q K R H G P Q R K H 0 Prof. Pradyumansinh Jadeja (987988) 070 ata & ile Structure

7. reate a binary search tree for the following data : 0,,7,,0,0,80,90,,0 0 7 0 80 0 0 90 8. onstruct binary search tree for the following data and find its Inorder, Preorder and Postorder traversal 0,,,,,,,,78,, 0 78 Preorder : 0,,,,,,,,,, 78 Inorder :,,, 0,,,,,,, 78 Postorder :,,,,, 78,,,,, 0 Prof. Pradyumansinh Jadeja (987988) 070 ata & ile Structure

9. Write a short note on threaded binary tree The wasted NULL links in the binary tree storage representation can be replaced by threads. binary tree is threaded according to particular traversal order. e.g.: Threads for the inorder traversals of tree are pointers to its higher nodes, for this traversal order. o If left link of node P is null, then this link is replaced by the address of its predecessor. o If right link of node P is null, then it is replaced by the address of its successor ecause the left or right link of a node can denote either structural link or a thread, we must somehow be able to distinguish them. Method : Represent thread a ve address. Method : To have a separate oolean flag for each of leaf and right pointers, node structure for this is given below, LPTR LTHR ata RTHR RPTR lternate node for threaded binary tree. LTHR = true = enotes leaf thread link LTHR = false = enotes leaf structural link RTHR = true = enotes right threaded link RTHR = false = enotes right structural link Head node is simply another node which serves as the predecessor and successor of first and last tree nodes. is attached to the left branch of the head node Head dvantages Inorder traversal I faster than unthreaded version as tack is not required. ffectively determines the predecessor and successor for inorder traversal, for unthreaded tree this task is more difficult. stack is required to provide upward pointing information in tree which threading provides. It is possible to generate successor or predecessor of any node without having over head of stock with the help of threading. isadvantages Threaded trees are unable to share common subtrees If ve addressing is not permitted in programming language, two additional fields are required. Prof. Pradyumansinh Jadeja (987988) 070 ata & ile Structure

Insertion into and deletion from threaded binary tree are more time consuming because both thread and structural link must be maintained. inary Inorder Traversal G G ully In threaded binary tree of given binary tree H G Prof. Pradyumansinh Jadeja (987988) 070 ata & ile Structure

0. raw a right in threaded binary tree for the given tree Right In threaded binary tree of given binary tree H H G Prof. Pradyumansinh Jadeja (987988) 070 ata & ile Structure

. What is the meaning of height balanced tree? How rebalancing is done in height balanced tree. tree is called VL (height balance binary tree), if each node possesses one of the following properties. node is called left heavy if the longest path in its left sub tree is one longer then the longest path of its right sub tree.. node is called right heavy if the longest path in the right sub tree is one longer than path in its left sub tree.. node is called balanced, if the longest path in both the right and left sub tree are equal. If tree becomes unbalanced by inserting any node, then based on position of insertion, we need to rotate the unbalanced node. Rotation is the process to make tree balanced ) Insertion into Left sub tree of nodes Left child Single Right Rotation ) Insertion into Right sub tree of node s Left child Left Right Rotation ) Insertion into Left sub tree of node s Right child Right Left Rotation ) Insertion into Right sub tree of node s Right child Single Left Rotation ) Insertion into Left sub tree of nodes Left child Single Right Rotation If node becomes unbalanced after insertion of new node at Left sub tree of nodes Left child, then we need to perform Single Right Rotation for unbalanced node. Right Rotation a. etach leaf child s right sub tree b. onsider leaf child to be the new parent c. ttach old parent onto right of new parent d. ttach old leaf child s old right sub tree as leaf sub tree of new right child ritical Node J K K Z Right Rotation X J X Y N Y Z N Prof. Pradyumansinh Jadeja (987988) 070 ata & ile Structure

ritical Node 7 7 Steps of Right Rotation 0 0 7 7 0 0 ) Insertion into Right sub tree of node s Left child Left Right Rotation If node becomes unbalanced after insertion of new node at Right sub tree of node s Left child, then we need to perform Left Right Rotation for unbalanced node. Leaf rotation of leaf child followed by right rotation of parent J J Y X K Y Z Left Rotation of K K Y n Z Right Rotation of J K X n Z J n X ) Insertion into Left sub tree of node s Right child Right Left Rotation If node becomes unbalanced after insertion of new node at Left sub tree of node s Right child, then we need to perform Right Left Rotation for unbalanced node. Single right rotation of right child followed by left rotation of parent Prof. Pradyumansinh Jadeja (987988) 070 ata & ile Structure

X Unbalanced node X Y T Y Z T Right Rotation of Z T T Y Z Left Rotation of X X Z T T T T T T T T ) Insertion into Right sub tree of node s Right child Single Left Rotation If node becomes unbalanced after insertion of new node at Right sub tree of nodes Right child, then we need to perform Single Left Rotation for unbalanced node. Left Rotation a. etach right child s leaf sub tree b. onsider right child to be new parent c. ttach old parent onto left of new parent d. ttach old right child s old left sub tree as right sub tree of new left child X Unbalanced node Y T Y Leaf Rotation of X X T T T T T n n xample 0 Unbalanced node 70 70 0 70 0 80 0 80 90 0 80 0 90 0 0 90 0 7 Prof. Pradyumansinh Jadeja (987988) 070 ata & ile Structure

. onstruct VL Search tree by inserting following elements in order of their occurrence,,,,, Insert Insert : Insert : Right Insert : Rotate Insert : Righ Insert : Rotate Right Rotate ssignment: efine height of the binary tree. efine height balanced tree with its advantages. onstruct a height balanced binary tree (VL tree) for the following data,0,,,88,0,,,, onstruct the VL search tree by inserting the following elements in the order of their occurrence.,,,,, 0, 98, 8 8 Prof. Pradyumansinh Jadeja (987988) 070 ata & ile Structure

. What are the advantages of Multiway search tree in disc access? onstruct tree of order for the following data,7,,,,,0,,,0,,,,,8,9,, Insert: Insert: Insert: 7 Insert: Insert:,,, 7,,, 7,,, 7, Overflow, 7, Insert: Insert: 0 7 8 Insert:,, 7,,, 7, 0,,, 7, 0,, Insert: 9 0 Insert: 0,,,, 7, 0,,, Overflow,, 7, 0,,, 7, 0,, 0 Insert: Insert:,,, 7, 0,,, 0,,,,, 7, 0,,, 0, Overflow,, 7, 0, 0,, 9 Prof. Pradyumansinh Jadeja (987988) 070 ata & ile Structure

Insert:,,,,,,, 7, 0, 0,,, 7, 0, 0, Overflow Insert: 8, 9,,, 7, 0,, 8, 9, 0, Insert:,, 7, 0,, 8, 9, 0,, Overflow,, 0,, 7, 0,, 8, 9, ssignment: onstruct mutiway search tree for the following data of order for 00, 0, 0,, 0, 00, 70,, 7, 0, 0,, 0,, 80, 90, 00, 0 Prof. Pradyumansinh Jadeja (987988) 070 ata & ile Structure

. What is tree? tree is a type of data structure, where every node with children has either two children or three children. very non leaf is a node or a node. ll leaves are at the same level (the bottom level) ll data are kept in sorted order very non leaf node will contain or fields. What is graph? How it can be represented using adjacency matrix, what is path matrix? How path matrix can be found out using adjacency matrix. Graph graph G consist of a non empty set V called the set of nodes (points, vertices) of the graph, a set which is the set of edges and a mapping from the set of edges to a set of pairs of elements of V. It is also convenient to write a graph as G=(V,). Notice that definition of graph implies that to every edge of a graph G, we can associate a pair of nodes of the graph. If an edge X Є is thus associated with a pair of nodes (U,V) where U, V Є V then we says that edge x connect U and V. djacency matrix Let G = (V, ) be a simple diagraph in which V = {v, v,., v n } and the nodes are assumed to be ordered from v to v n. n n x n matrix whose elements are a ij are given by, a ij = 0 is called adjacency matrix of the graph G. ny element of the adjacency matrix is either 0 or. or a given graph G =m (V, ), an adjacency matrix depends upon the ordering of the elements of V. or different ordering of the elements of V we get different adjacency matrices. V V V V V V V V V 0 0 V 0 0 0 V 0 V 0 0 0 digraph and its adjacency matrix Prof. Pradyumansinh Jadeja (987988) 070 ata & ile Structure

We can extend the idea of matrix representation to multigraph sand weighted graphs. In the case of multigraph or weighted graph we write a ji = w, where a ij denotes either the multiplicity it the weight of the edge. Path matrix n entry of in the ith row and jth column of shows the existence of an edge (v i, v j ), that is a path of length from v i to v j. Let denote the elements of by a () ij. Then Therefore a () ij is equal to the number of different paths of exactly length from v i to v j. Similarly element in i th row and j th column of gives number of paths of exactly length from v i to v j. = 0 0 0 0 0 0 0 = 0 0 0 0 0 0 = 0 0 0 0 0 ifferent path matrices. Which are the basic traversing techniques of the Graph? Write the algorithm of them. Most graph problems involve traversal of a graph. Traversal of a graph means visit each node exactly once. Two commonly used graphs traversal techniques are. epth irst Search (S). readth irst Search (S) epth irst Search (S) It is like preorder traversal of tree. Traversal can start from any vertex vi Vi is visited and then all vertices adjacent to vi are traversed recursively using S Prof. Pradyumansinh Jadeja (987988) 070 ata & ile Structure

S (G, ) is given by 7 a) Visit () b) S (G, ) S (G, ) S (G, ) S (G, ) 8 S traversal of given graph is:,,,, 8, 7,, Graph G Since graph can have cycles, we must avoid re visiting a node. To do this when we visit a vertex V, we marks it visited as visited should not be selected for traversal. Procedure : S (vertecx V) This procedure traverse the graph G in S manner. V is a starting vertex to be explored. S is a Stack, visited[] is an array which tells you whether particular vertex is visited or not. W is a adjacent node of vertex V. PUSH and POP are functions to insert and remove from stack respectively.. [Initialize TOP and Visited] visited[] 0 TOP 0. [Push vertex into stack] PUSH (V). [Repeat while stack is not empty] Repeat step while stack is not empty v POP() if visited[v] is 0 then visited [v] for all W adjacent to v if visited [v] is 0 then PUSH (W) end for end if Prof. Pradyumansinh Jadeja (987988) 070 ata & ile Structure

readth irst Search (S) This methods starts from vertex v 0 V 0 is marked as visited. ll vertices adjacent to v 0 are visited next Let vertices adjacent to v 0 are v, v, v, v v, v, v and v are marked visited. ll unvisited vertices adjacent to v, v, v, v are visited next. The method continuous until all vertices are visited The algorithm for S has to maintain a list of vertices which have been visited but not explored for adjacent vertices. The vertices which have been visited but not explored for adjacent vertices can be stored in queue. Initially the queue contains the starting vertex. In every iteration, a vertex is removed from the queue and its adjacent vertices which are not visited as yet are added to the queue. The algorithm terminates when the queue becomes empty. Graph G S traversal of given graph is:,,,, 7 8 7 8 Procedure : S (Vertex V) This procedure traverse the graph G in S manner. V is a starting vertex to be explored. Q is a queue, visited[] is an array which tells you whether particular vertex is visited or not. W is a adjacent node of vertex V.. Initialize Q. [Marks visited of V as ] visited [v]. [dd vertex v to Q] InsertQueue(V). [Repeat while Q is not empty] Repeat while Q is not empty v RemoveromQueue() or all vertices W adjacent to v if visited[w] is 0 then visited[w] InsertQueue(w) Prof. Pradyumansinh Jadeja (987988) 070 ata & ile Structure

7. What is spanning tree? Spanning tree of a graph is an undirected tree consisting of only those edges necessary to connect all the nodes in the original graph spanning tree has the properties that o or any pair of nodes there exists only one path between them o Insertion of any edge to a spanning tree forms a unique cycle The particular Spanning for a graph depends on the criteria used to generate it. If S search is use, those edges traversed by the algorithm forms the edges of tree, referred to as epth irst Spanning. If S Search is used, the spanning tree is formed from those edges traversed during the search, producing readth irst Search Spanning tree. V0 V V V V V V V7 V0 V0 V V V V V V V V V V V V V7 S Spanning V7 S Spanning Prof. Pradyumansinh Jadeja (987988) 070 ata & ile Structure

8. onsider the graph shown in ig ind depth first and breadth first traversals of this graph starting at S : S : Prof. Pradyumansinh Jadeja (987988) 070 ata & ile Structure

9. efine spanning tree and minimum spanning tree. ind the minimum spanning tree of the graph shown in ig. 7 Using Prim s lgorithm: Let X be the set of nodes explored, initially X = { } Step : Taking minimum weight edge of all djacent edges of X = { } X = {, } Step : Taking minimum weight edge of all djacent edges of X = {, } X = {,, } 7 Step : Taking minimum weight edge of all djacent edges of X = {,, } Step : Taking minimum weight edge of all djacent edges of X = {,,, } X = {,,, } X = {,,,, } ll nodes of graph are there with set X, so we obtained minimum spanning tree of cost: + + + = 0 7 Prof. Pradyumansinh Jadeja (987988) 070 ata & ile Structure

7 Using Kruskal s lgorithm Step : Taking min edge (,) Step : Taking next min edge (,) Step : Taking next min edge (,) Step : Taking next min edge (,) Step : Taking next min edge (,) it forms cycle so do not consider Step : Taking next min edge (,) it forms cycle so do not consider Step 7: Taking next min edge (,) it forms cycle so do not consider Step 8: Taking next min edge (,) it forms cycle so do not consider Step 9: Taking next min edge (,) it forms cycle so do not consider ll edges of graph has been visited, so we obtained minimum spanning tree of cost: + + + = 0 0. Give example and applications of directed and undirected graphs. ind the adjacency matrix for the graph shown in ig. djacency matrix for the given graph 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 8 Prof. Pradyumansinh Jadeja (987988) 070 ata & ile Structure

pplications of graph: lectronic ircuits o Printed ircuit oard o Integrated ircuit Transportation networks o Highway networks Modeling a road network with vertexes as towns and edge costs as distances. o Water Supply networks Modeling a water supply network. cost might relate to current or a function of capacity and length. s water flows in only direction, from higher to lower pressure connections or downhill, such a network is inherently an acyclic directed graph. o light network Minimizing the cost and time taken for air travel when direct flights don't exist between starting and ending airports. omputer networks o Local rea Network o Internet ynamically modeling the status of a set of routes by which traffic might be directed over the Internet. o Web Using a directed graph to map the links between pages within a website and to analyze ease of navigation between different parts of the site. atabases o ntity Relationship iagram 9 Prof. Pradyumansinh Jadeja (987988) 070 ata & ile Structure

0 Prof. Pradyumansinh Jadeja (987988) 070 ata & ile Structure. pply ijkstra s algorithm to find shortest path between vertex and vertex for the graph shown in ig. 7 7 Step : Traverse all adjacent node of 0 7 Step : Traverse all adjacent node of 0 7 Step : Traverse all adjacent node of 0 7 Step : Traverse all adjacent node of 0 9 7 Step : Traverse all adjacent node of 0 7 Shortest path from node to is : as shown in step Length of path is 7