Professional Documents
Culture Documents
Characteristics of Algorithms:
There are following characteristics of any algorithms as given below.
▪ Input
▪ Output
▪ Definiteness
▪ Finiteness
▪ Effectiveness
1.Input:- An algorithm should have one or more inputs.
3.Definiteness:-
Every statement in any algorithm should be definiteness. It means. Every statement in
algorithm should have unambiguous. Every statement should have clear and there should not be
more than one way to interpret the single statement in given algorithm.
4.Finiteness:-An algorithm should have a finite number of steps(instructions) to solve the problems
and get a valid output.
5.Effectiveness:-An algorithm should have effectiveness and produce well defined output of any
programs. Effectiveness means, an algorithms should have good method which produce effective
output with less time and less storage capacity.
2] Define time and space complexity ? (2019,2018,2016,2015)
Time complexity
time complexity is a function describing the amount of time an algorithm takes in terms of the
amount of input to the algorithm.
Space complexity
space complexity is a function describing the amount of memory (space) an algorithm takes in
terms of the amount of input to the algorithm.
Here it is, 0 + (9 - 0 ) / 2 = 4 (integer value of 4.5). So, 4 is the mid of the array.
Now we compare the value stored at location 4, with the value being searched, i.e. 31. We find that the
value at location 4 is 27, which is not a match. As the value is greater than 27 and we have a sorted array, so we
also know that the target value must be in the upper portion of the array.
We change our low to mid + 1 and find the new mid value again.
low = mid + 1 mid = low + (high - low) / 2
Our new mid is 7 now. We compare the value stored at location 7 with our target value 31.
The value stored at location 7 is not a match, rather it is more than what we are looking for. So, the value must
be in the lower part from this location.
1. Finite Graph A graph G= (V, E) in case the number of vertices and edges in the graph is finite in number.
2. Infinite Graph A graph G=(V, E) is said to infinite if the number of edges and vertices in the graph is infinite in number.
3. Trivial Graph A graph G= (V, E) is said to be trivial if there only exist single vertex in the graph without any edge.
4. Simple Graph A graph G=(V, E) is said to be a simple graph if there is one and only one edge between each pair of
vertices. Thus there is only edge connecting 2 vertices and can be used to show one to one relationships
between 2 elements.
5. Multi Graph A graph g= (V, E) is said to be a multigraph if there are multiple edges between a pair of vertices in
the graph. A Multigraph does not contain any self-loop. For example, A Road Map.
6. Null Graph It is a modified version of a trivial graph. A graph G= (V, E) is said to a null graph if there are n number of
vertices exist, but no Edge exists that connects then
Multistage graph problem is to determine shortest path from source to destination. This can be solved
by using either forward or backward approach. In forward approach we will find the path from destination to
source, in backward approach we will find the path from source to destination.
The multistage graph problem can be solved using backward approach.
Let bp(i,j) be a minimum-cost path from vertex s to vertex j in Vi Let bcost(i,j) be cost of bp(i,j).
The backward approach to find min. cost is bcost(i,j) = min { bcost(i-1, ) +c( ,j)}
Since bcost(2,j) = c(1,j) if E and bcost(2,j) = if E, bcost(i,j) can be computed using above formula.
Multistage Graphs : backward approach pseudocode algorithm
Algorithm Bgraph(G,k,n,p)
//same function as Fgraph
{
bcost[1]:=0.0;
For j:=2 to n do
{ // compute bcost[j].
Let r be such that is an edge of G and bcost[r] + c[r,j] is minimum;
bcost[j]:=bcost[r]+c[r,j];
d[j]:=r;
}
//Find a minimum-cost path
P[1]:=1;p[k]:=n;
For j:=k-1 to 2 do p[j]:= d[p[j+1]];
}
8]short notes (2019,2018,2017,2016,2015)
1] optimal merger pattern
2] quick sort
3]selection sort
4] binary search
Merge a set of sorted files of different length into a single sorted file. We need to find an optimal solution,
where the resultant file will be generated in minimum time.
If the number of sorted files are given, there are many ways to merge them into a single sorted file. This
merge can be performed pair wise. Hence, this type of merging is called as 2-way merge patterns.
As, different pairings require different amounts of time, in this strategy we want to determine an optimal
way of merging many files together. At each step, two shortest sequences are merged.
To merge a p-record file and a q-record file requires possibly p + q record moves, the obvious choice
being, merge the two smallest files together at each step.
Two-way merge patterns can be represented by binary merge trees. Let us consider a set of n sorted
files {f1, f2, f3, …, fn}. Initially, each element of this is considered as a single node binary tree. To find this optimal
solution, the following algorithm is used.
2] Quick Sort
Quicksort is a divide-and-conquer algorithm. It works by selecting a 'pivot' element from the array and
partitioning the other elements into two sub-arrays, according to whether they are less than or greater than the pivot.
For this reason, it is sometimes called partition-exchange sort.[4] The sub-arrays are then sorted recursively. This
can be done in-place, requiring small additional amounts of memory to perform the sorting.
Quicksort is a comparison sort, meaning that it can sort items of any type for which a "less-than" relation
(formally, a total order) is defined. Efficient implementations of Quicksort are not a stable sort, meaning that the
relative order of equal sort items is not preserved.
3]selection sort
Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison-based
algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right
end. Initially, the sorted part is empty and the unsorted part is the entire list.
The smallest element is selected from the unsorted array and swapped with the leftmost element, and that
element becomes a part of the sorted array. This process continues moving unsorted array boundary by one
element to the right.
This algorithm is not suitable for large data sets as its average and worst case complexities are of Ο(n2),
where n is the number of items.
4] binary search
A binary search algorithm is used to find the position of a specific value contained in a
sorted array. Working with the principle of divide and conquer, this search algorithm can be quite
fast, but the caveat is that the data has to be in a sorted form. It works by starting the search in
the middle of the array and working going down the first lower or upper half of the sequence. If
the median value is lower than the target value, that means that the search needs to go higher, if
not, then it needs to look on the descending portion of the array.
Binary search is a fast search algorithm with run-time complexity of Ο(log n). This search
algorithm works on the principle of divide and conquer. For this algorithm to work properly, the
data collection should be in the sorted form.
Binary search looks for a particular item by comparing the middle most item of the
collection. If a match occurs, then the index of item is returned. If the middle item is greater
than the item, then the item is searched in the sub-array to the left of the middle item.
Otherwise, the item is searched for in the sub-array to the right of the middle item. This process
continues on the sub-array as well until the size of the subarray reduces to zero.