DAA Pastpaper Solve by M.Noman Tariq
DAA Pastpaper Solve by M.Noman Tariq
DAA Pastpaper Solve by M.Noman Tariq
Stack
A stack is a linear data structure that follows a particular order in which operations are performed. The
order can be summarized by the acronym LIFO (Last In First Out), meaning the last element added to the
stack will be the first element to be removed from it. You can think of a stack like a stack of plates: you
add (or remove) plates from the top only.
Operations
Push:
This operation is used to insert an item into the stack. If the stack is full, then it is said to be at an
overflow condition. When an element is pushed onto the stack, it becomes the topmost element and all
other elements are shifted down.
Example:
Let's consider a stack with the following elements: [5, 3, 9]
• If we then pop from the stack, the number 7 will be removed, and the stack goes back to: [5, 3,
9]
Binary Search:
It is a technique to find or search a value in array. But array should be "sorted. This algorithm uses DAC
approach to find an array /or value in sorted array.
Key Points:
1 Sorted Array:-
Binary Search technique is used.if array is not sorted so first Sorted then binary search technique is used
or linear search is used.
2. Target Value:-
Target Value is any value X which we want to search in sorted array.
3. Mid Value:
(a) This algorithm works by dividing sorted array into two parts on the bases of Mid value i+j/2
(b) check weather mid value is target value. If it is target value searching is complete. else find target
value is less than or greater than mid value. If target value less than mid value we will search that value
in left half otherwise search in right half.
Algorithm:
An algorithm is a finite, well-defined sequence of steps or set of rules that provides a solution to a
specific problem. If a procedure has all the characteristics of an algorithm, then it is an algorithm.
Characteristics of an Algorithm:
Clear Steps:
Has an End:
It shouldn't go on forever. After following all the steps, you should finish.
Doable Steps:
Inputs:
Like ingredients in a recipe. Before starting, you might need some information or things.
Outputs:
The result you get after following all the steps. Like a finished dish in a recipe.
If you have a type of problem, the algorithm should work for all similar problems, not just one.
Language-Free:
Think of this like a recipe that can be understood no matter what language you speak. An algorithm
should make sense even if you change the "language" (or way) you use to follow it.
1. Root
In a tree data structure, the first node is called as Root Node. Every tree must have a root node. We can
say that the root node is the origin of the tree data structure. In any tree, there must be only one root
node. We never have multiple root nodes in a tree.
3. Parent
In a tree data structure, the node which is a predecessor of any node is called as PARENT NODE. In
simple words, the node which has a branch from it to any other node is called a parent node. Parent
node can also be defined as "The node which has child / children".
4. Child
In a tree data structure, the node which is descendant of any node is called as CHILD Node. In simple
words, the node which has a link from its parent node is called as child node. In a tree, any parent node
can have any number of child nodes. In a tree, all the nodes except root are child nodes.
6. Leaf
In a tree data structure, the node which does not have a child is called as LEAF Node. In simple words, a
leaf is a node with no child.
In a tree data structure, the leaf nodes are also called as External Nodes. External node is also a node
with no child. In a tree, leaf node is also called as 'Terminal' node.
In a tree data structure, nodes other than leaf nodes are called as Internal Nodes. The root node is also
said to be Internal Node if the tree has more than one node. Internal nodes are also called as 'Non-
Terminal' nodes.
8. Degree
In a tree data structure, the total number of children of a node is called as DEGREE of that Node. In
simple words, the Degree of a node is total number of children it has. The highest degree of a node
among all the nodes in a tree is called as 'Degree of Tree'
10. Height
In a tree data structure, the total number of edges from leaf node to a particular node in the longest
path is called as HEIGHT of that Node. In a tree, height of the root node is said to be height of the tree. In
a tree, height of all leaf nodes is '0'.
12. Path
In a tree data structure, the sequence of Nodes and Edges from one node to another node is called
as PATH between that two Nodes. Length of a Path is total number of nodes in that path. In below
example the path A - B - E - J has length 4.
Explain Depth first search and Breath first search methods with example.
S.
No. Parameters BFS DFS
BFS(Breadth First
DFS(Depth First Search)
Search) uses Queue data
uses Stack data
Data structure for finding the
structure.
2. Structure shortest path.
Conceptual BFS builds the tree level DFS builds the tree sub-
5. Difference by level. tree by sub-tree.
Visiting of
Here, siblings are visited Here, children are visited
Siblings/
before the children. before the siblings.
10. Children
DFS algorithm is a
In BFS there is no recursive algorithm that
concept of backtracking. uses the idea of
12. Backtracking backtracking
In BFS, there is no
In DFS, we may be
Tapping in problem of trapping into
trapped in infinite loops.
18, loops infinite loops.
14 33 27 10 35 19 42 43
Starting with the second element (33), compare it to the first element (14) and insert it in the correct
position within the sorted subarray.
14 33 27 10 35 19 42 43
Move to the third element (27) and insert it into the sorted subarray (14, 33).
14 33 27 10 35 19 42 43
Move to the fourth element (10) and insert it into the sorted subarray (14, 27, 33).
10 14 27 33 35 19 42 43
Move to the fifth element (35) and insert it into the sorted subarray (10, 14, 27, 33).
10 14 27 33 35 19 42 43
Move to the sixth element (19) and insert it into the sorted subarray (10, 14, 27, 33, 35).
10 14 19 27 33 35 42 43
Move to the seventh element (42) and insert it into the sorted subarray (10, 14, 19, 27, 33, 35).
10 14 19 27 33 35 42 43
Finally, move to the last element (43) and insert it into the sorted subarray (10, 14, 19, 27, 33, 35, 42).
10 14 19 27 33 35 42 43
So , Shortest path is Starting from S to C then to D via C and then to A via C and then to B via A.
salesman Problem