Sie sind auf Seite 1von 15

BCA & MCA (IGNOU)

http://www.ignouassignmentguru.com
Course Code : BCS-042

Course Title : Introduction to Algorithm

Assignment Number : BCA (IV)/042/Assignment

October, 2015 - April, 2016


1. Write the bubble sort algorithm and apply the algorithm following numbers, showing the lists
obtained at each statement.
16 12 10 11 25 14 7 15
Ans:
 Begin {Algorithm}
 i, j, temp: integer;
 for i 0 to size //outer loop
 for j0 to size-i-1 //inner loop
 if(arr[j]>=arr[j+1]) then
 begin {if}
 temp arr[j]
 arr[j] arr[j+1]
 arr[j+1] temp
 end of {if}
 end {of algorithm}

1
IGNOU ASSIGNMENT GURU Page-

/ignouassignmentfree
BCA & MCA (IGNOU)
http://www.ignouassignmentguru.com

2
IGNOU ASSIGNMENT GURU Page-

/ignouassignmentfree
BCA & MCA (IGNOU)
http://www.ignouassignmentguru.com

3
IGNOU ASSIGNMENT GURU Page-

/ignouassignmentfree
BCA & MCA (IGNOU)
http://www.ignouassignmentguru.com

2. Also show the time taken to execute each statement of a List all the steps used to search for 25
in the following se a binary search algorithm:

2 5 8 10 15 25 30 32 35 40

and show the time taken to execute each step of the algo write a recurrence relation of binary
search algorithm

4
IGNOU ASSIGNMENT GURU Page-

/ignouassignmentfree
BCA & MCA (IGNOU)
http://www.ignouassignmentguru.com

5
IGNOU ASSIGNMENT GURU Page-

3. Show the following:

/ignouassignmentfree
BCA & MCA (IGNOU)
http://www.ignouassignmentguru.com

6
IGNOU ASSIGNMENT GURU Page-

/ignouassignmentfree
BCA & MCA (IGNOU)
http://www.ignouassignmentguru.com

4. Write the commonly used terminologies for the following of algorithms.

Ans:-
The complexity Ladder:

Notation Name Example


O(1) Constant Constant growth. Does
O(log n) Logarithmic Binary search
O(n) Linear Looping over n elements, of an
array of size n (normally).
O(n log n) Sometimes called Merge sort
“linearithmic”
2
Quadratic Worst time case for insertion sort,
O(n )
matrix multiplication
c
Polynomial, sometimes
O(n )
“geometric”
n
O(a )
O(n!)
Exponential
Factorial
7
IGNOU ASSIGNMENT GURU Page-

5. Write a pseudo code for Kruskal’s algorithm for constructing a minimum cost spanning tree of
the following graph: (12 Marks)

/ignouassignmentfree
BCA & MCA (IGNOU)
http://www.ignouassignmentguru.com

8
IGNOU ASSIGNMENT GURU Page-

/ignouassignmentfree
BCA & MCA (IGNOU)
http://www.ignouassignmentguru.com
Also make a comparison between Kruskal’s and Prim’s algorithm. (12 Marks)

9
IGNOU ASSIGNMENT GURU Page-

/ignouassignmentfree
BCA & MCA (IGNOU)
http://www.ignouassignmentguru.com

10
IGNOU ASSIGNMENT GURU Page-

6. Apply the Quick sort algorithm to sort the given integers:

15 18 20 27 26 13 14

Discuss the performance of Quick sort algorithm in best case, worst case and average case.

(10 Marks)

Ans:

Performance of Quick Sort

The running time of QUICK SORT depends on whether the partitioning is balanced or unbalanced.
Partitioning of the subarrays depends on the input data we receive for sorting.

/ignouassignmentfree
BCA & MCA (IGNOU)
http://www.ignouassignmentguru.com
Best Case: If the input data is not sorted, then the partitioning of subarray is balanced; in this case
the algorithm runs asymptotically as fast as merge -sort (i.e. 0(nlogn)).

Worst Case: If the given input array is already sorted or almost sorted, then the partitioning of the
subarray is unbalancing in this case the algorithm runs asymptotically as slow as Insertion sort (i.e.
(n2)).

Average Case: Except best case or worst case.

11
IGNOU ASSIGNMENT GURU Page-

/ignouassignmentfree
BCA & MCA (IGNOU)
http://www.ignouassignmentguru.com

7. For the following graph, write adjacency list and its DFS & BFS node sequences.

Where are these two algorithm used?

Ans:

Visiting Sequence

DFS:-1-2-3-4-5

BFS:-1-2-5-3-4

DFS(G)
{
for each v in V, //for loop V+1 times 12
IGNOU ASSIGNMENT GURU Page-
{
color[v]=white; // V times
p[v]=NULL; // V times
}
time=0; // constant time O(1)
for each u in V, //for loop V+1 times
if (color[u]==white) // V times
DFSVISIT(u) // call to DFSVISIT(v) , at most V times O(V)
}
DFSVISIT(u)
{
color[u]=gray; // constant time
t[u] = ++time;
for each v in Adj(u) // for loop

/ignouassignmentfree
BCA & MCA (IGNOU)
http://www.ignouassignmentguru.com
if (color[v] == white)
{
p[v] = u;
DFSVISIT(v); // call to DFSVISIT(v)
}
color[u] = black; // constant time
f[u]=++time; // constant time
}
BFS(G,s)
{
for each v in V - {s} // for loop
{
color[v]=white;
d[v]= INFINITY;
p[v]=NULL;
}
color[s] = gray;
d[s]=0;
p[s]=NULL;
Q= ; // Initialize queue is empty
Enqueue(Q,s); /* Insert start vertex s in Queue Q */
while Q is nonempty // while loop
{
u = Dequeue[Q]; /* Remove an element from Queue Q*/
for each v in Adj[u] // for loop
{
if (color[v] == white) /*if v is unvisted*/
{
color[v] = gray; /* v is visted */
d[v] = d[u] + 1; /*Set distance of v to no. of edges from s to u*/
p[v] = u; /*Set parent of v*/
Enqueue(Q,v); /*Insert v in Queue Q*/
}
}
13
IGNOU ASSIGNMENT GURU Page-

color[u] = black; /*finally visted or explored vertex u*/


}
}
8. Briefly describe the following techniques.
 Backtracking
 Branch & bound(6 Marks)

Ans:
Backtrack: Backtrack algorithms try each possibility until they find the right one. It is a depth-first-search of
the set of possible solutions. During the search, if an alternative doesn’t work, the search backtracks to the
choice point, the place which presented different alternatives, and tries the next alternative.
5. Branch-and-Bound (B&B) is a rather general optimization technique that applies where the greedy method
and dynamic programming fail.

/ignouassignmentfree
BCA & MCA (IGNOU)
http://www.ignouassignmentguru.com
9. Apply Dijkstra’s algorithm to find the shortest path from a source vertex A to the remaining
vertices of the following directed graph. Show all the intermediate steps.

(9 Marks)

14
IGNOU ASSIGNMENT GURU Page-

/ignouassignmentfree
BCA & MCA (IGNOU)
http://www.ignouassignmentguru.com

15
IGNOU ASSIGNMENT GURU Page-

/ignouassignmentfree

Das könnte Ihnen auch gefallen