0% found this document useful (0 votes)
76 views59 pages

Trees and Graphs

There are two main non-linear data structures: trees and graphs. Trees have a hierarchical structure with one root node and child nodes arranged in a parent-child relationship. Binary trees are a type of tree where each node has at most two child nodes. Common operations on binary trees include insertion, deletion, and traversal using preorder, inorder, and postorder techniques. Heaps are a type of complete binary tree where the value of each node is greater than or equal to its children in a max heap and less than or equal in a min heap. Heaps can be represented efficiently using a one-dimensional array and support insertion and deletion operations through upheaping and downheaping procedures.

Uploaded by

ongakimary8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views59 pages

Trees and Graphs

There are two main non-linear data structures: trees and graphs. Trees have a hierarchical structure with one root node and child nodes arranged in a parent-child relationship. Binary trees are a type of tree where each node has at most two child nodes. Common operations on binary trees include insertion, deletion, and traversal using preorder, inorder, and postorder techniques. Heaps are a type of complete binary tree where the value of each node is greater than or equal to its children in a max heap and less than or equal in a min heap. Heaps can be represented efficiently using a one-dimensional array and support insertion and deletion operations through upheaping and downheaping procedures.

Uploaded by

ongakimary8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

None linear data structures

There are two types of data structures


 Tree
 Graph
Trees

A tree is a collection of elements (called nodes),one of which is


distinguished as root, along with a relation (parenthood)that
places a hierarchical structure on the nodes.
–This structure can be partitioned into disjoint subsets
–These subsets are themselves trees and are also
subtreesof the tree
•A non-linear structure in which components may have more than one
predecessor and more than one successor is called a graph.
•A tree thus us a special case of a graph

All elements (except the root) here has 1 predecessor


whereas in true family tree there would be 2
predecessors
The unique predecessor of a node is called its parent, its successors
are its children
•A leafhas no children and the
root has no parent.
•Children of the same parent are called
siblings,
•The lines connecting the nodes are called branches and a sequence of branches
from one node to another node lower down the tree is called a path
Degree:number of subtreesof a
node
Nodes of degree 0 are leaf nodes (terminal
nodes)
Size:the number of nodes
Depth of node:number of links leading to a node from the root
node
Depth of tree:largest depth of all the nodes

Trees: Binary tree

•A binary tree is a hierarchical data structure (a tree)


which
–May be empty (empty tree or null tree)
–All nodes can have degree of 0, 1 or 2
–A node is explicitly defined as a left child or a right child
–Consists of subtreescalled left subtreeand right subtree
Trees: Balanced binary tress

•An empty tree is balanced.


•if none empty tree is balanced if and only if
–its sub trees are balanced and
–A balanced binary tree is a binary tree that is also a balanced tree.
–A balanced tree is a tree where no leaf is much farther away from the root than
any other leaf.

Complete tree

•A complete tree is one in which there are no gaps between


leaves. For instance, a tree with a root node that has only
one child must have its child as the left node.
•A complete tree is one that has every level filled in before adding a node to
the next level, and one that has the nodes in a given level filled in from left
to right, with no breaks.
Complete binary tree

•What is a complete binary tree ???????


•A complete binary tree is a binary tree with all the characteristics of a complete
tree.
Nb Balanced tree is complete but complete tree is not necessarily balanced
Operation of a tree
Add node
Delete node
Search –traverse /
Binary tree traversal and search techniques

 Data is organized in a tree because it is easy to


find items in a tree(searching )
 Search means finding a path or traversal between
a start node and one of a set of goal nodes.
 Search is a study of states and their transitions.
 Search involves visiting nodes in a graph/tree in a
systematic manner, and may or may not result
into a visit to all nodes.

 When the search necessarily involved the


examination of every vertex in the tree, it is called
the traversal.

Techniques for Traversal of a Binary Tree:


 A binary tree is a finite (possibly empty) collection
of elements.
o When the binary tree is not empty, it has a
root element and remaining elements (if any)
are partitioned into two binary trees,
o which are called the left and right subtrees.

 There are three common ways to traverse a binary


tree:
 Preorder
 Inorder
 postorder
 In all the three traversal methods,
o the left subtree of a node is traversed before
the right subtree.
o The difference among the three orders comes
from the difference in the time at which a
root node is visited.

Inorder Traversal:
 In the case of inorder traversal, the root of each
subtree is visited after its left subtree has been
traversed but before the traversal of its right
subtree begins.
 The steps for traversing a binary tree in inorder
traversal are:
 Visit the left subtree, using inorder.
 Visit the root.
 Visit the right subtree, using inorder.

Preorder Traversal:
 In a preorder traversal, each node is visited before
its left and right subtrees are traversed.
 Preorder search is also called backtracking. The
steps for traversing a binary tree in preorder
traversal are:
 Visit the root.
 Visit the left subtree, using preorder.
 Visit the right subtree, using preorder.
Postorder Traversal:
 In a postorder traversal, each root is visited
after its left and right subtrees have been
traversed.
 The steps for traversing a binary tree in
postorder traversal are:
 Visit the left subtree, using postorder.
 Visit the right subtree, using postorder
 Visit the root.

Examples for binary tree traversal/search technique:

Example 1:
Traverse the following binary tree in pre, post and in-
order.

Example 2:
Traverse the following binary tree in pre, post, inorder and
level order.
Example 3
Heap tree

heap is a binary tree :

•a complete binary tree-refer the properties of


complete tree
•It is empty or
•The value in any node <= value of all that node’s
descendants I.e. the key in the root is less than that in
either child and both sub trees have the heap
property(max heap)
Or value in any node >= value of all that node’s
descendants(min heap)
•Can be thought of as a priority queue; the most
important node will always be at the top, and when
removed, its replacement will be the most important.
•This can be useful when coding algorithms that
require certain things to be processed in a complete
order, but when you don't want to perform a full sort
or need to know anything about the rest of the nodes..

Type of heaps

Max and Min Heap data structures:


 A max heap is an almost complete binary tree
such that the value of each node is greater than
or equal to those in its children.
95 15

85 45 45 25

75 25 35 15 55 65 35 75

55 65 85 95 Min heap
Max heap

Figure 2. 1. Max. and Min heap

A min heap is an almost complete binary tree such that


the value of each node is less than or equal to those in
its children.

Representation of Heap Tree:

 Since heap is a complete binary tree, a heap tree


can be efficiently represented using one
dimensional array.
 This provides a very convenient way of figuring
out where children belong to.
o The root of the tree is in location 1.
 The left child of an element stored at
location i can be found in location 2*i.
 The right child of an element stored at
location i can be found in location 2*i + 1.
 The parent of an element stored at location i can
be found at location floor(i/2).
For example let us consider the following elements
arranged in the form of array as follows:

X[1] X[2] X[3] X[4] X[5] X[6] X[7] X[8]


65 45 60 40 25 50 55 30

 The elements of the array can be thought of as


lying in a tree structure.
 A heap tree represented using a single array
looks as follows:

Operations on heap tree:


The major operations required to be performed on a heap
tree:
 Insertion-upheaping
 Deletion –down heaping
 Merging.

Up heaping (Insertion into a heap tree)


 used to insert a node into an existing heap tree
satisfying the properties of heap tree(max or min
heap ).
 Using repeated insertions of data, starting from
an empty heap tree, one can build up a heap tree.
Example
 Let us consider the heap (max) tree.
o The principle of insertion is that, first we
have to adjoin the data in the complete
binary tree.
 Next, we have to compare new node value with
the data values of its parent
o if the value is greater than that at parent
then interchange the values.
 This will continue between two nodes on
path from the newly inserted node to the
root node till we get a parent whose
value is greater than its child or we
reached the root.

For illustration, 35 is added as the right child of 80.


 Its value is compared with its parent’s value, and
to be a max heap, parent’s value greater than
child’s value is satisfied
o , hence interchange as well as further
comparisons are no more required.
Example:
Form a heap by using the above algorithm for the
given data 40, 80, 35, 90, 45, 50, 70.
Down heaping (Deletion of a node from heap tree)
 Any node can be deleted from a heap tree.
o But from the application point of view,
deleting the root node has some special
importance.
The principle of deletion is as follows:
 Read the root node into a temporary storage say,
 ITEM.
 Replace the root node by the last node in the
heap tree. Then re-heap the tree as stated
 below:
o Let newly modified root node be the current
node. 
 Compare its value with the value of its
two child.
 Let X be the child whose value is the
largest. Interchange the value of X with
 the value of the current node.
o Make X as the current node.
o Continue re-heap, if the current node is not an
empty node.

The algorithm for the above is as follows:

 Here the root node is 99. The last node is 26, it is


in the level 3.
 So, 99 is replaced by 26 and this node with data
26 is removed from the tree.
 Next 26 at root node is compared with its two
child 45 and 63. As 63 is greater, they are
interchanged.
 Now, 26 is compared with its children, namely, 57
and 42, as 57 is greater, so they are
interchanged.
 Now, 26 appear as the leave node, hence re-heap
is completed. This is shown in figure 2.3.
Merging two heap trees:
 Consider, two heap trees H1 and H2.
 Merging the tree H2 with H1 means to include all
the node from H2 to H1.
o H2 may be min heap or max heap and the
resultant tree will be min heap if H1 is min
heap else it will be max heap.
Merging operation consists of two steps: Continue
steps 1 and 2 while H2 is not empty:
 Delete the root node, say x, from H2. Re-heap H2.
 Insert the node x into H1 satisfying the property of
H1.
92 13
.

Applications of heap tree:

They are two main applications of heap trees known:


 Sorting (Heap sort) and
 Priority queue implementation.
HEAP SORT:
 A heap sort algorithm works by first organizing
the data to be sorted into a special type of binary
tree called a heap.
o Any kind of data can be sorted either in
ascending order or in descending order using
heap tree.
o It does this with the following steps:
 Build a heap tree with the given set of data.
 a.Remove the top most item (the largest) and
replace it with the last element in the heap.
 Re-heapify the complete binary tree.
 Place the deleted node in the output.
 Continue step 2 until the heap tree is empty.

Priority queue
 Priority queue can be implemented using circular
array, linked list etc.
 Another simplified implementation is possible
o using heap tree;
o the heap, however, can be represented using
an array. This implementation is therefore
free from the complexities of circular array
and linked list
o but getting the advantages of simplicities of
array.
 As heap trees allow the duplicity of data in it.
 Elements associated with their priority values are
to be stored in from of heap tree, which can be
formed based on their priority values.
 The top priority element that has to be processed
first is at the root; so it can be deleted and heap
can be rebuilt to get the next element to be
processed, and so on.
As an illustration, consider the following processes with
their priorities:

Process P1 P2 P3 P4 P5 P6 P7 P8 P9 P10
Priority 5 4 3 4 5 5 3 2 1 5

 These processes enter the system in the order as


listed above at time 0, say. Assume that a process
having higher priority value will be serviced first.
 The heap tree can be formed considering the
process priority values.
 The order of servicing the process is successive
deletion of roots from the heap.

Binary Search Trees:


 A binary search tree has binary nodes and the
following additional property.
 Given a node t, each node to the left is “smaller”
than t, and each node to the right is “larger”.
 This definition applies recursively down the left
and right sub-trees.
 Figure shows a binary search tree where characters
are stored in the nodes.
Inorder: a b c d e f g h

Figure 2.5. Binary Search tree

 Figure 2.5 also shows what happens if you do an


inorder traversal of a binary search tree: you will
get a list of the node contents in sorted order.
 In fact, that’s probably how the name inorder
originated.

Binary Tree Searching:


 The search operation starts from root node R,
 if item is less than the value in the root node R,
we proceed to the left child;
 if item is greater than the value in the node R,
we proceed to its right child.
 The process will be continued till the item is found
or we reach to a dead end.
 The Figure 2.6 shows the path taken when
searching for a node “c”.

Figure 2.6.
Searching a
binary tree

Why use
binary search
trees?
 Binary search trees provide an efficient way to
search through an ordered collection of items.
 Consider the alternative of searching an ordered
list.
 The search must proceed sequentially from one
end of the list to the other.
o On average, n/2 nodes must be compared for
an ordered list that contains n nodes.
 In the worst case, all n nodes might need
to be compared.
 For a large collection of items, this
can get very expensive.
 The inefficiency is due to the one-dimensionality
of a linked list.
 We would like to have a way to jump into the
middle of the list, in order to speed up the search
process.
 In essence, that’s what a binary search tree does.
 The longest path we will ever have to search is
equal to the height of the tree.
 The efficiency of a binary search tree thus
depends on the height of the tree.
o For a tree holding n nodes, the smallest
possible height is log(n).
o For 2 nodes –height is 1, 4-2, 8-3 etc
 To obtain the smallest height, a tree must be balanced,

o where both the left and right sub trees have


approximately the same number of nodes.
o Also, each node should have as many
children as possible, with all levels being full
except possibly the last.
o Figure 2.7 shows an example of a well-
constructed tree.
Figure 2.7. Well constructed binary search tree.

 Unfortunately, trees can become so unbalanced


that they’re no better for searching than linked
lists.
 Such trees are called degenerate trees.
 Figure 2.8 shows an example. For a degenerate
tree, an average of n/2 comparisons are needed,
with a worst case of n comparisons – the same as
for a linked list.

Figure 2.8. A degenerate binary search tree.


 When nodes are being added and deleted in a
binary search tree, it’s difficult to maintain the
balance of the tree.
 We will investigate methods of balancing trees in
the next section.

Inserting Nodes into a Binary Search Tree:


 When adding nodes to a binary search tree, we
must be careful to maintain the binary search tree
property.
o This can be done by first searching the
tree to see whether the key we are about to
add is already in the tree.
 If the key cannot be found, a new node
is allocated and added at the same
location where it would go
the search had been successful.

Deleting nodes from a Binary search tree:


 Deletions from a binary search tree are more
involved than insertions.
 Given a node to delete, we need to consider
these tree cases:
 The node is a leaf
 The node has only one child.
 The node has two children.

Case 1: It is easy to handle because the node can


simply be deleted and the corresponding
child pointer of its parent set to null. Figure
2.10 shows an example.
Case 2: It is almost as easy to manage. Here, the
single child can be promoted up the tree to
take the place of the deleted node, as shown
in figure 2.11.
Figure 2. 11. Deleting a node that has one child.

Case 3:The node to be deleted has two children, is more


difficult. We must
find some node to take the place of the one
deleted and still maintain the binary search
tree property. There are two obvious cases:
 The inorder predecessor of the deleted node.
 The inorder successor of the deleted node.
We can detach one of these nodes from the tree
and insert it where the node to be deleted.
 The predecessor of a node can be found by going
down to the left once, and then all the way to the
right as far as possible.
 To find the successor, an opposite traversal is
used:
 first to the right, and then down to the left as far
as possible.

 Figure 2.12 shows the path taken to find both the
predecessor and successor of a node.

Balanced Trees:
 For maximum efficiency, a binary search tree
should be balanced.
 Every un-balanced trees are referred to as
degenerate trees, so called because their
searching performance degenerates to that of
linked lists.
 Figure 2.14 shows an example.

Figure 2.14. A degenerate binary search tree.

 The first tree was built by inserting the keys


‘a’ through ‘i’ in sorted order into binary search
tree.
 The second tree was built using the insertion
sequence. a-g-b-f-c-e-d.
 This pathological sequence is often used to test
the balancing capacity of a tree.
 Figure 2.15 shows what the tree in 2.14-(b) above
would look like if it were balanced.
Figure 2.15 Balanced Tree

Balancing Acts:
There are two basic ways used to keep trees balanced.
 Use tree rotations.
 Allow nodes to have more than two children.

Tree Rotations:
Certain types of tree-restructurings, known as rotations,
can aid in balancing trees.
Figure 2.16 shows two types of single rotations.
Figure 2.16. Single Rotations
Another type of rotation is known as a double
rotation. Figure 2.17 shows the two symmetrical
cases.

 Both single and double rotations have one


important feature:
o in order traversals of the trees before and
after the rotations are preserved.
 Thus, rotations help balance trees, but still
maintain the binary search tree property.
 Rotations don’t guarantee a balanced tree,
however for example, figure 2.18 shows a right
rotations that makes a tree more un-balanced.
The trick lies in determining when to do rotation
and what kind of notations to do.

b a
b
a c
c

Figure 2.18. Un-balanced rotation.


The Red-black trees have certain rules to be used to
determine when a how to rotate.

AVL Tree
 An AVL tree is a height balanced Binary Search Tree.
 The number of null branches is more in a normal BST if the
elements are almost in order, this leads to more levels and in
turn need more space.
 This problem is solved by balancing the height whenever a
node is inserted into an AVL tree.
 The re-balancing is recommended based on the balancing
factor.
Balancing factor

 Balancing factor of each node is calculated by finding the


difference in levels between the left and right sub tree.
 Balancing factor of X = height of left sub tree of X - height of
right sub tree of X
o If the balancing factor of all the nodes in the tree is
within the range of -1 and 1, then the tree is already in
balanced form, otherwise balancing is needed.
AVL Tree Rotations
 As mentioned previously, an AVL Tree and the nodes it
contains must meet strict balance requirements to maintain
its O(log n) search capabilities.
 These balance restrictions are maintained using various
rotation functions.
 Below is a diagrammatic overview of the four possible
rotations that can be performed on an unbalanced AVL
Tree, illustrating the before and after states of an AVL
Tree requiring the rotation.
Inserting in an AVL Tree
 Nodes are initially inserted into AVL Trees in the same
manner as an ordinary binary search tree (that is, they are
always inserted as leaf nodes).
 After insertion, however, the insertion algorithm for an AVL
Tree travels back along the path it took to find the point of
insertion, and checks the balance at each node on the path.
 If a node is found that is unbalanced (that is, it has a
balance factor of either -2 or +2),
 then a rotation is performed based on the inserted
nodes position relative to the node being examined
(the unbalanced node).
 NB. There will ever be at most one rotation required
after an insert operation.

Deletion in AVL tree


The deletion algorithm for AVL Trees is a little more complex, as
there are several extra steps involved in the deletion of a node.
o If the node is not a leaf node (that is, it has at least one
child), then the node must be swapped with either it's in-
order successor or predecessor (based on availability).
o Once the node has been swapped we can delete it (and
have its parent pick up any children it may have - bear in
mind that it will only ever have at most one child).
 If a deletion node was originally a leaf node, then it can
simply be removed.
 Now, as with the insertion algorithm, we traverse back up the
path to the root node, checking the balance of all nodes along
the path.
 If we encounter an unbalanced node we perform an
appropriate rotation to balance the node.
 NB. Unlike the insertion algorithm, more than one rotation
may be required after a delete
 operation, so in some cases we will have to continue back up
the tree after a rotation

Weight Balanced Trees

 The Tree structures support various basic dynamic set


operations including Search, Predecessor, Successor,
Minimum, Maximum, Insert, and Delete in time proportional
to the height of the tree.

 Ideally, a tree will be balanced and the height will be log


n where n is the number of nodes in the tree.

 To ensure that the height of the tree is as small as possible


and therefore provide the best running time, a balanced tree
structure like a red-black tree, AVL tree, or b-tree must be
used.

 When working with large sets of data, it is often not possible


or desirable to maintain the entire structure in primary
storage (RAM).
o Instead, a relatively small portion of the data structure is
maintained in primary storage, and additional data is
read from secondary storage as needed.
 Unfortunately, a magnetic disk, the most common form of
secondary storage, is significantly slower than random access
memory (RAM).
o In fact, the system often spends more time in retrieving
data than actually processing data.

B-trees

 B-trees are weight balanced trees that are optimized for


situations when part or the entire tree must be maintained
in secondary storage such as a magnetic disk.
o Since disk accesses are expensive (time consuming)
operations, a b-tree tries to minimize the number of disk
accesses.
 For example, a b-tree with a height of 2 and a
branching factor of 1001 can store over one billion
keys but requires at most two disk accesses to
search for any node

Graphs
 Graph G is a pair (V, E), where V is a finite set
(set of vertices) and E is a finite set of pairs from
V (set of edges). We will often denote n := |V|,
m := |E|.

 Graph G can be directed, if E consists of ordered
pairs, or undirected, if E consists of unordered
pairs. If (u, v)  E, then vertices u, and v are
adjacent.

 We can assign weight function to the edges:
wG(e) is a weight of edge e  E. The graph which
has such function assigned is called weighted.

 Degree of a vertex v is the number of vertices u
for which (u, v)  E (denote deg(v)). The number
of incoming edges to a vertex v is called in–
degree of the vertex (denote indeg(v)). The
number of outgoing edges from a vertex is called
out-degree (denote outdeg(v)).
 Definition: The degree of a vertex in an
undirected graph is the number of edges
incident with it, except that a loop at a vertex
contributes twice to the degree of that vertex.
 In other words, you can determine the degree of
a vertex in a displayed graph by counting the
lines that touch it.
 The degree of the vertex v is denoted by deg(v).

 A vertex of degree 0 is called isolated , since it


is not adjacent to any vertex. isolated,
 Note: A vertex with a loop at it has at least
degree 2 and, by definition, is not isolated , even
if it is not adjacent to any other vertex. isolated,
 A vertex of degree 1 is called pendant . It is
adjacent to exactly one other vertex. pendant.

 Example: Which vertices in the following graph are


isolated, which are pendant, and what is the maximum
degree? What type of graph is it?

 Solution: Vertex f is isolated, and vertices a, d and j


are pendant. The maximum degree is deg(g) = 5.
 This graph is a pseudograph (undirected, loops).
aabbccddiihhggjjffee
Representation of Graphs:

Definition:In a graph with directed edges, the inin--degreedegreeof a


vertex v, denoted by of degdeg--(v)(v), is the , number of edges with v
as their terminal (head) vertexvertex..The outout--degreedegreeof v,
denoted by of degdeg++(v)(v), is the , number of edges with v as their
initial (tail) vertex.Question:How does adding a loop to a vertex change
the inchange in--degree and outdegree out--degree of that
vertex?Answer:It increases both the inIt in--degree and the outout--
degree by one.

Example: What are the in degrees and out degrees of the vertices a, b,
c, d in this graph: in-out-

Representation Digraphs
 Just like graphs, digraphs can be represented
using.
o Linear list
o Adjacently matrix
Adjacency matrix VS adjacent list
 adjacency matrix store the rows and column in
a table format
 comparing the matrix and the list,
o An adjacency matrix requires Θ (n2)
storage while an adjacency list requires Θ
(n + e) storage.
However
 Adjacency matrices allow faster access to
edge queries (for example, is (u, v)  E)
While

o an adjacency lists allow faster access to


enumeration tasks
 (for example, find all the vertices
adjacet to v).

Definition: A path of length n 1 from u to v, where n is a positive


integer, in an undirected graph is a sequence of edges e , , , e of
the graph such that e = {x , x }, = {x , x }, , e = { , }, where x = u
and = v. n-e1, e2, …, en e1 x0, x1}, e2 x1, x2}, …, en xn-1, xn}, x0 xn
The path is a circuit if it begins and ends at the same vertex, that is,
if u = v.

Definition (continued): The path or circuit is said to pass


through or traverse x , x , , . x1, x2, …, xn-1
A path or circuit is simple if it does not contain the same edge more
than once. .

Co
nn
ect
ivit
y

Let us now look at something new:


Definition: An undirected graph is called connected if there is a path
between every pair of distinct vertices in the graph.
For example, any two computers in a network can communicate if
and only if the graph of this network is connected.
Note: A graph consisting of only one vertex is always connected,
because it does not contain any pair of distinct vertices.
Subgraphs and Spanning Trees:
Subgraphs: A graph G’ = (V’, E’) is a subgraph of
graph G = (V, E) iff V’  V and E’  E.

The undirected graph G is connected,


 if for every pair of vertices u, v there exists a path
from u to v.
 If a graph is not connected, the vertices of the
graph can be divided into connected
components.
 Two vertices are in the same connected component
iff they are connected by a path.

Tree is a connected acyclic graph.


A spanning tree of a graph G = (V, E) is a tree that
contains all vertices of V and is a subgraph of G. A single
graph can have multiple spanning trees.
Lemma 1: Let T be a spanning tree of a graph G. Then
 Any two vertices in T are connected by a unique
simple path.
 If any edge is removed from T, then T becomes
disconnected.
 If we add any edge into T, then the new graph will
contain a cycle.
 Number of edges in T is n-1.

Graphs search algorithms :


[acyclic graphs]
Given a graph G = (V, E) and a vertex V in V (G)
traversing can be done in two ways.
 Depth first search
 Breadth first search
 D-search (Depth Search)

Depth first search:



 With depth first search, the start state is chosen
to begin,
o then some successor of the start state,
 then some successor of that state,
 then some successor of that and so
on,
 trying to reach a goal state.
 If depth first search reaches a state S without
successors,

 or if all the successors of a state S have been


chosen (visited) and a goal state has not get been
found, then it “backs up”
 that means it goes to the immediately previous
state or predecessor formally, the state whose
successor was ‘S’ originally.

For example consider the figure. The circled letters are


state and arrows are branches.
 Suppose S is the start and G is the only goal state.
 Depth first search will first visit S, then A then D.
 But D has no successors, so we must back up to
A and try its second successor, E.
 But this doesn’t have any successors either, so we
back up to A again.
 But now we have tried all the successors of A and
haven’t found the goal state G so we must back to
‘S’.
 Now ‘S’ has a second successor, B.
o But B has no successors, so we back up to S
again and choose its third successor, C.
o C has one successor, F.
o The first successor of F is H, and the first of H is J.
o J doesn’t have any successors, so we back up to
H and try its second successor.
o And that’s G, the only goal state. So the solution
path to the goal is S, C, F, H and G and the states
considered were in order S, A, D, E, B, C, F, H, J,
G.

Disadvantages:
 It works very fine when search graphs are trees
or lattices, but can get struck in an infinite loop
on graphs.
o This is because depth first search can travel
around a cycle in the graph forever.
o To eliminate this keep a list of states
previously visited, and never permit
search to return to any of them.
 One more problem is that, the state space tree
may be of infinite depth,
 to prevent consideration of paths that are too
long, a maximum is often placed on the depth
of nodes to be expanded, and any node at that
depth is treated as if it had no successors.
 We cannot come up with shortest solution to the
problem.

Breadth first search:


 Given an graph G = (V, E),
 breadth-first search starts at some source vertex
S and “discovers" which vertices are reachable
from S.
 Define the distance between a vertex V and S to
be the minimum number of edges on a path from
S to V.
 Breadth-first search discovers vertices in
increasing order of distance, and hence can be
used as an algorithm for computing shortest paths
o (where the length of a path = number of
edges on the path).
 Breadth-first search is named because it visits
vertices across the entire breadth.
To illustrate this let us consider the following tree:
 Breadth first search finds states level by level.
 Here we first check all the immediate successors
of the start state.
o Then all the immediate successors of these,
 then all the immediate successors of
these, and so on until we find a goal
node.
 Suppose S is the start state and G is the goal
state.
o In the figure, start state S is at level 0; A, B
and C are at level 1;
o D, e and F at level 2;
o H and I at level 3; and
o J, G and K at level 4.
o So breadth first search, will consider in order
S, A, B, C, D, E, F, H, I, J and G and then stop
because it has reached the goal node.
 Breadth first search does not have the danger of
infinite loops as we consider states in order of
increasing number of branches (level) from the
start state.
 One simple way to implement breadth first search
is to use a queue data structure consisting of just
a start state.

 Any time we need a new state, we pick it from the


front of the queue and

o any time we find successors, we put them at


the end of the queue.
o That way we are guaranteed to not try (find
successors of) any states at level ‘N’ until all
states at level ‘N – 1’ have been tried.

Depth Search (D-Search):


 The exploration of a new node cannot begin until
the node currently being explored is fully
explored.
 D-search like state space search is called LIFO
(Last In First Out) search which uses stack data
structure.
 To illustrate the D-search let us consider the
following tree:
The search order for goal node (G) is as follows: S, A, B, C,
F, H, I, J, G.

Spanning Trees (MST):


 A spanning tree for a connected graph is a tree whose
vertex set is the same as the vertex set of the given
graph, and whose edge set is a subset of the edge set
of the given graph. i.e., any connected graph will have
a spanning tree.

Weight of a spanning tree w (T) is the sum of weights


of all edges in T.

Minimum spanning tree

The Minimum spanning tree (MST) is a spanning tree


with the smallest possible weight.
Application of minimum spanning trees

real-world examples:
 One practical application of a MST would be in the
design of a network.
 For instance, a group of individuals, who are
separated by varying distances, wish to be
connected together in a telephone network.
 Although MST cannot do anything about the
distance from one connection to another, it can be used
to determine the least cost paths with no cycles in this
network, thereby connecting everyone at a minimum
cost.
 Another useful application of MST would be finding
airline routes.
 The vertices of the graph would represent
cities, and the edges would represent routes
between the cities.
 Obviously, the further one has to travel, the more it will
cost, so MST can be applied to optimize airline routes by
finding the least costly paths with no cycles

BFS AND DFS spanning Trees


 Using BFS and DFS you can built spanning trees
 BFS and DFS impose a tree (the BFS/DFS tree)
along with some auxiliary edges(cross edges) on
the structure of graph.
 Therefore we can compute a spanning tree in a
graph.
 The computed spanning tree is not a minimum
spanning tree.
Why spanning trees
o Trees are much more structured objects than
graphs.
 For example, trees break up nicely into
subtrees, upon which subproblems can
be solved recursively.
o For directed graphs the other edges of the
graph can be classified as follows:
Example

Depth first search


Recall
 Depth first search of undirected graph proceeds as
follows.
 The start vertex V is visited.
 Next an unvisited vertex 'W' adjacent to 'V' is
selected and a depth first search from 'W' is
initiated.
 When a vertex 'u' is reached such that all its
adjacent vertices have been visited, we back up
to the last vertex visited, which has an unvisited
vertex 'W' adjacent to it
o and initiate a depth first search from
W(reclusive).
 The search terminates when no unvisited vertex
can be reached from any of the visited ones.
Let us consider the following Graph (G):
The adjacency list for G is:

 If the depth first is initiated from vertex 1,


then the vertices of G are visited in the order:
1, 2, 4, 8, 5, 6, 3, 7. The depth first spanning
tree is as follows:
 The spanning trees obtained using depth first searches
are called depth first spanning trees.
 The edges rejected in the context of depth first search
are called a back edges.
 Depth first spanning tree has no cross edges.
Breadth first search and traversal:
 Starting at vertex 'V' and marking it as visited,
 BFS differs from DFS in that all unvisited vertices
adjacent to V are visited next.
o Then unvisited vertices adjacent to there
vertices are visited and so on
Consider the same graph as above

o A breadth first search beginning at vertex 1


of the graph would first visit 1 and then 2 and
3.
Graphs algorithms
There are two more algorithms based on minimum spanning trees
:
 the Kruskal algorithm
 the Prim algorithm.

Kruskal’s Algorithm
 This is a greedy algorithm. A greedy algorithm chooses some
local optimum (i.e.picking an edge with the least weight in a
MST).
 Kruskal's algorithm works as follows:
o Take a graph with 'n' vertices, keep on adding the
shortest (least cost) edge, while avoiding the creation of
cycles, until (n - 1) edges have been added.
o Sometimes two or more edges may have the same cost.
 The order in which the edges are chosen, in this
case, does not matter.
o Different MSTs may result, but they will all have the
same total cost, which will always be the minimum cost.

Example

To understand Kruskal's algorithm let us consider the following


example −

Step 1 - Remove all loops and Parallel Edges


Remove all loops and parallel edges from the given graph.

In case of parallel edges, keep the one which has the least cost
associated and remove all others.

Step 2 - Arrange all edges in their increasing order of weight


The next step is to create a set of edges and weight, and arrange
them in an ascending order of weightage (cost).

Step 3 - Add the edge which has the least weightage


 Now we start adding edges to the graph beginning from the
one which has the least weight.
 Throughout, we shall keep checking that the spanning
properties remain intact.
 In case, by adding one edge, the spanning tree property does
not hold then we shall consider not to include the edge in the
graph.

 The least cost is 2 and edges involved are B,D and D,T.
 We add them. Adding them does not violate spanning tree
properties, so we continue to our next edge selection.
Next cost is 3, and associated edges are A,C and C,D. We add them
again −

Next cost in the table is 4, and we observe that adding it will create
a circuit in the graph. −

We ignore it. In the process we shall ignore/avoid all edges that


create a circuit.

We observe that edges with cost 5 and 6 also create circuits. We


ignore them and move on.

Now we are left with only one node to be added. Between the two
least cost edges available 7 and 8, we shall add the edge with cost
7.
By adding edge S,A we have included all the nodes of the graph
and we now have minimum cost spanning tree.

MINIMUM-COST SPANNING TREES:


PRIM'S ALGORITHM
 Like Kruskal’s algorithm, Prim’s algorithm is
also a Greedy algorithm. It starts with an
empty spanning tree.
 The idea is to maintain two sets of vertices.
The first set contains the vertices already
included in the MST, the other set contains the
vertices not yet included.
 At every step, it considers all the edges that
connect the two sets, and picks the minimum
weight edge from these edges.
o After picking the edge, it moves the other
endpoint of the edge to the set containing
MST.
Algorithm
1) Create a set mstSet that keeps track of vertices
already included in MST.
2) Assign a key value to all vertices in the input graph.
Initialize all key values as INFINITE. Assign key value as
0 for the first vertex so that it is picked first.
3) While mstSet doesn’t include all vertices
….a) Pick a vertex u which is not there in mstSet and
has minimum key value.
….b) Include u to mstSet.
….c) Update key value of all adjacent vertices of u. To
update the key values, iterate through all adjacent
vertices. For every adjacent vertex v, if weight of edge u-
v is less than the previous key value of v, update the key
value as weight of u-v

Example
The vertices included in MST are shown in green color.

 Pick the vertex with minimum key value and not already
included in MST (not in mstSET).
 The vertex 1 is picked and added to mstSet.
o So mstSet now becomes {0, 1}. Update the key values
of adjacent vertices of 1. The key value of vertex 2
becomes 8.

 Pick the vertex with minimum key value and not already included in
MST (not in mstSET).
o We can either pick vertex 7 or vertex 2, let vertex 7 is picked. So
mstSet now becomes {0, 1, 7}.
o Update the key values of adjacent vertices of 7. The key value of
vertex 6 and 8 becomes finite (1 and 7 respectively).
 Pick the vertex with minimum key value and not already included in
MST (not in mstSET). Vertex 6 is picked.
o So mstSet now becomes {0, 1, 7, 6}. Update the key values of
adjacent vertices of 6. The key value of vertex 5 and 8 are
updated.

 We repeat the above steps until mstSet includes all vertices of given
graph. Finally, we get the following graph.

The Single Source Shortest-Path Problem:


DIJKSTRA'S ALGORITHMS
 In the previously studied graphs, the edge labels
are called as costs, but here we think them as
lengths.
o In a labeled graph, the length of the path is
defined to be the sum of the lengths of its
edges.

 In the single source, all destinations, shortest path


problem, we must find a shortest path from a given
source vertex to each of the vertices (called
destinations) in the graph to which there is a path.

 Dijkstra’s algorithm is similar to prim's algorithm


for finding minimal spanning trees.

o Dijkstra’s algorithm takes a labeled graph and a pair of


vertices P and Q, and finds the shortest path between
then (or one of the shortest paths) if there is more than
one.
o The principle of optimality is the basis for Dijkstra’s
algorithms.

Dijkstra’s algorithm does not work for negative edges at all.

1. Mark your selected initial node with a current distance of 0 and


the rest with infinity.
2. Set the non-visited node with the smallest current distance as the
current node C.
3. For each neighbour N of your current node C: add the current
distance of C with the weight of the edge connecting C-N. If it's
smaller than the current distance of N, set it as the new current
distance of N.
4. Mark the current node C as visited.
5. If there are non-visited nodes, go to step 2.

Example

You might also like