Unit 8


Published on

Published in: Business, Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Unit 8

  1. 1. DATA STRUCTURE: It is a method of representation of logical relationships between individual dataelements related to the solution of a given problem.LINEAR: In the linear data structure, values are arranged in a linear fashion. Linked list,stacks and queues are the examples of linear data structure in which values are stored insequence.NON LINEAR: The data values in this structure are not arranged in order. Trees, graphs areexamples of non-linear data structure.
  2. 2.  Trees are used extensively in computer science to represent algebraic formulas as anefficient method for searching large, dynamic lists and for such diverse applications asartificial intelligence systems and encoding algorithms. A tree consists of a finite set of elements called nodes, and a finite set of directedlines, called branches that called branches that connect the nodes. DATA STRUCTURE LINEAR NON-LINEARLINEARLINEAR LINKED LISTS LINKED LISTS STACKS STACKS QUEUES QUEUES TREES TREES GRAPHS GRAPHS
  3. 3.  The number of branches associated with a node is the degree of the node. When the branch is directed towards the node, it is an in degree branch. When the branch is directed away from the node, it is an out degree branch. The sum of the in degree and out degree branches is degree of node. If the tree is not empty, the first node is called the root.TERMINOLOGY:• A leaf is any node with an out degree of zero i.e., a node with no successors.• A node is a parent if it has successor nodes i.e., if it has out degree > zero.• A node with a predecessor is a child. A child node has an in degree of one.• Two or more nodes with the same parent are siblings.• An ancestor is any node in the path from the root to the node.• A descendent is any node in the path below.
  4. 4.  A tree may be defined as a set of certain elements called as nodes. One node is named as root, while the remaining nodes may be grouped intosub-sets, each of which is a tree in itself. Each sub tree is referred to as child of the root, while the root is referred to asthe parent of each sub tree. If a tree consists of a simple node, then that node is called a leaf. A B C D E F G H I In the above figure . A is root and E,F,G,H,I,D are leafs.
  5. 5. BINARY TREE: A binary tree is a tree, which is either empty , or in which every nodea) has no children orb) has just a left child orc) has just a right child ord) has just a left and right child.LEVEL OF A NODE: The level of a node refers to its distance from the root node. 1 LEVEL 0 2 LEVEL 1 3 4 5 LEVEL 2 6 LEVEL 3
  6. 6. COMPLETE BINARY TREE: A complete binary tree may be defined as a tree in which , exceptthe last level, at each level n there should be 2^n nodes. 1 LEVEL 0 2 3 LEVEL 1 4 5 6 9 LEVEL 2 7 8DEPTH( OR HEIGHT) OF A TREE: 1 1 Length of path from x to y is the total number of 2 3branches lying along that path. 2 4 5Max level number of a binary tree is known as its 3height( or depth). 7 6Depth of a complete binary tree with n nodes=log n 4 8
  7. 7. BINARY SEARCH TREE: A binary search tree is a binary tree that is either empty or in which every node contains a key and satisfies the conditions 1. The key in the left child of a node ( if exists) is less than the key in its parent node. 2. The key in the right child of a node (if exists ) is greater than the key in its parent node. 3. The left and right sub trees are again binary search trees. d The nodes of right sub A binary b tree are greater than d and f nodes of left sub tree are search tree. less than d. a c cOperations on binary search tree: The main operations performed on binary search trees are a. Insertion b. Deletion.
  8. 8. a. Inserting a new node in binary search tree To insert a new key into binary search tree, The following actions are performed: 1. If the tree is empty, the root pointer is assigned address of new node. 2. If the tree is not empty, then compare the key with one in the root. If it is less than the key of root, then move to left sub tree otherwise, more to right sub tree. Of the key is equal then it is a duplicate. 3. Repeat the step2 until, the node is inserted at any place. The node is placed at the position when there are no nodes to compare after repeating the step2. 18 18 18 18 10 28 10 28 10 2810 28 8 8 11 8 11 25
  9. 9. b. Deletion from a binary search tree: g We have to consider different cases for deletion: Case1: If the node to be deleted is leaf b i Then replace the link to the deleted node by NULL a c h g b i nullnull a null null c null null h null
  10. 10. Delete c: The binary search tree after deletion will be g g b null i null b inull a null null h null a h
  11. 11. Case 2: If the deleted node has only one sub tree Then adjust the link from parent of the deletednode to point to its right sub tree.Example : delete ‘i’ from tree as shown in figure below g g b h b h a c a c
  12. 12. Case 3 : If the deleted node has two sub trees: In this case attach right sub tree is place of deleted node. Thenhang left sub tree onto an appropriate node of right sub tree.Example: delete node ‘b’ from binary tree as shown in fig below g g c i c i a a h h
  13. 13. TRAVERSING INTO A BINARY SEARCH TREE:In order traversing: 181) Traverse the left sub tree. 10 282) Visit the root.3) Traverse the right sub tree. 8 11 25 8 10 11 18 25 28Pre order traversing:1) Visit the root. 18 10 8 11 28 252) Traverse the left sub tree.3) Traverse the right sub tree.Post order traversing:1) Traverse the left sub tree. 8 11 10 25 28 182) Traverse the right sub tree.3) Traverse the root.
  14. 14. GRAPHSDEFINITION: A graph is a collection of nodes, called vertices, and a collection ofsegments, called lines, connecting pairs of vertices.A graph consists of 2 sets, a set of vertices and a set of lines or A graph is a set ofnodes and arcs. The nodes are also termed as vertices and arcs are termed asedges.The set of nodes denoted as O P{O,P,R,S,Q}The set of arcs denoted as R Q{ (O,P),(O,R),(O,Q),(Q,S),(R,S)} SGraphs may be either directed or undirected. A directed graph or digraph is a graph inwhich each line has a direction ( arrow head) to its successor.The lines in a directed graph are known as arcs.
  15. 15. An undirected graph is graph in which there is no direction (arrow head) onany of the lines, which are known as edges.In an undirected graph, the flow between 2 vertices can go in either direction. A A B E B E F F C D C D a) Directed graph. b) Undirected graph. •A path is a sequence of vertices in which each vertex is adjacent to the next one. {A,B,C,E } is one path. { A,B,E,F} is another path. •A cycle is a path consisting of at least three vertices that starts and ends with the same vertex. from above fig: B,C,D,E,B is a cycle
  16. 16. Loop is a special case id a cycle in which a single A Bare begins and ends with the same vertex. C DA graph is linked if there is a pathway between any 2 nodes of the graph, such agraph is called a connected graph or else it is a non connected graph. P Q P Q R S R S Connected graph Non connected graphSUB GRAPH: A sub graph of a graph G=(V,E) is a graph G where V(G) is asubset of V(G). E(G) consists of edges (V1,V2) in E(G), such that both V1 and V2are in V(G).
  17. 17. a) A directed graph is strongly connected, if there is a path from each vertex toevery other vertex in the digraph. A G B E F C D H I Ab) A directed graph is weaklyconnected if at least 2 vertices are not B E Gconnected F C D H I Weakly connected
  18. 18. A c) A graph is a disjoint graph B E if it is not connected. G F C D H I Strongly connected.The degree of a vertex is the number of lines incident to it.The out degree of a vertex is the number of arcs leaving the vertex.In degree is the number of arcs entering the vertex.From the above figure:The degree of vertex B is 3. The maximum number of edges in anThe degree of vertex E is 4. undirected graph with n vertices is n(n-1)/2.In degree of vertex E is 2 In a directed graph , it is n(n-1).Out degree of vertex E is 2.
  19. 19. GRAPH REPRESENTATION:Array representation: S.NO 1 2 3 4 One way of representing a graph with 1 0 1 1 1 n vertices is to use an n^2 matrix. 1 (i.e., n rows and n columns) 2 1 0 1 1 2 4 If G is a directed graph, only e 3 1 1 0 1 entries would have been set to 1 in the adjacency matrix. 1 1 1 0 3 4Linked list representation:Another way of representing agraph G is to maintain a list for 2 3 4 NULLevery vertex containing all vertices 1adjacent to that vertex . NULL 2 1 3 4Struct edge{ int num; 3 1 2 4 NULLSturuct edge *p; 4 1 2 3 NULL};
  20. 20. Adjacency matrix: It can be used to represent the graph. The 1information of adjacency nodes can be storedin the matrix. 2 5Nodes [i] [k]-0 indicates absence 6 1 indicates the presence of edge between two nodes i and k 3 4 1 2 3 4 5 6 In degree Out degree 1 0 0 0 0 0 0 1 3 0 2 1 0 0 1 0 0 2 2 2 3 0 1 0 0 0 1 3 1 2 4 0 0 1 0 1 1 4 1 3 5 1 0 0 0 0 0 5 2 1 6 1 1 0 0 1 0 2 6 3
  21. 21. Spanning tree: It is an un directed tree, containing only those nodes that arenecessary to join all the nodes in the graph. The nodes of spanning treeshave only one path between them. In spanning trees, the number of edges are less by one than the numberof nodes. In other words, a tree that contains edges including all vertices is calledspanning tree. Also, in a graph, when the edges are shown connected between thenodes in the order in which they are to be visited, resulting sub graph iscalled spanning tree.
  22. 22. DIGRAPH (DIRECTED GRAPH):A digraph is a graph in which edges have a direction v1 E5 e=(u, v) E1 E8Edge ‘e’ can be traversed only from u to v, v2 v3 E6u- initial vertex. E2 E7 E4v-terminal vertex. v4 v5 E3 SIMPLE DIRECTED GRAPH: V1 E4 Simple directed graph is a simple graph in E1 E5 which edges have a direction. E6 V2 V4 E2 V3 E3
  23. 23. WEIGHTED GRAPH: In a weighted graph, each edge is assigned some weight i.e., a real positive numberis associated with each edge.PATH:In a non- weighted graph, a path P of length n from u to v can be defined as follows:P={ V0, V1, V2, …….., Vn}Where u=V0 and v=Vn and there is an edge from Vi-1 to Vi (0<i<n).In a directed graph Vi-1 is taken as initial vertex and Vi is taken as terminal vertex.A simple path from u to v is a path where no vertex appears more than once.CYCLE:A path P={ V1, V2,………….. Vn} is said to be a cycle, when all its vertices aredistinct except V0 and Vn , meaning thereby V0=Vn then a simple path P is called acycle.
  24. 24. v1CONNECTED GRAPH:A graph G is called a connected graph, if there v2 v3is at least one path between every pair ofvertices. v4 v5COMPLETE GRAPH: v1A graph with an edge between each pair ofvertices is said to be a complete graph.|E| =n*(n-1)/2 v2 v5|E|- total number of edgesn- number of vertices.0<= |E|<= n(n-1)/2. v3 v4
  25. 25. DEPTH FIRST TRAVERSAL: In this method, we start from a given vertex v in the graph. We mark this vertex is visited. Now any of the unvisited vertex adjacent to v is visited. Then neighbor of v is visited. The process is continued until no new node can be visited. Now we bock track from the path to visit unvisited vertices if any left. Stack is used to keep track of all nodes adjacent to a vertex.NOTE: There may be several depth firsttraversals and depth first spanning trees for A B Ca given graph. It mainly depends on howthe graph is represented (adjacency matrixor adjacency list), on how the nodes are D Enumbered, on the starting node and on howthe traversal technique is implemented Here i) D is first successor of A(i.e., using first successor, next successor and (B is next successor).etc.) ii) C is first successor of B.
  26. 26. ALGORITHM DEPTH FIRST[V]:1. [Initialize all the nodes to ready state and stack to empty] state [v]=1 ( 1 indicates ready state)2. [Begin with any arbitrary node s in graph and push it on to stack and change its state to waiting] state [s]=2 (2 indicates waiting state)3. Repeat through step5 while stack not empty4. [ pop node N of stack and mark the status of node to be visited] state [N]= 3 (3 indicates visited)5. [push all nodes w adjacent to N to stack and mark their status as waiting] state [W]=26. If the graph still contains nodes which are in ready state go to step27. Return.
  27. 27. BREADTH FIRST TRAVERSAL: In this we start with given vertexV and visit all the nodes adjacent to V from leftto right. Data structure queue is use to keep track of all the adjacent nodes.The first node visited is the first node whose successors are visited.ALGORITHM FOR BREADTH FIRST [V]:1. [Initialize all nodes to ready state] state[V]=1 [ here V represents all nodes of graph]2. [Place starting node ‘S’ in queue and change its state to waiting] state [S]=23. Repeat through step5 until queue not empty.4. [Remove a node N from queue and change status of N to visited state] state [N]=3.5. [ add to queue all neighbors W of ‘N’ which are in ready state and change their states to waiting state] state [W]= 26. End.