Parul University Piet/Pit-It Department: Grade

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

PARUL UNIVERSITY

PIET/PIT-IT DEPARTMENT

Design and Analysis of


Algorithms (203105301)

Grade:
PRATIK DHOLAKIYA ( 190303108012 )
Design and Analysis of Algorithms (203105301)

Assignment 1

1). What is algorithm? What are the algorithm design techniques?List


out the criteria that all algorithms must satisfy.
Ans.
The algorithm is a method that can be utilized by the computer as
when required to provide solutions to a particular problem.
• We can choose any programming language to implement the
Algorithm.
• Pseudocode and flow chart are popular ways to represent an
algorithm.
• Algorithm is nothing but a process of executing actions step by
step.
Algorithm design techniques:
• Greedy Method:
• Divide and Conquer:
• Dynamic Programming:
• Linear Programming:
• Reduction(Transform and Conquer)
An algorithm must satisfy the following criteria:
1. Input: An algorithm should have zero or more but should be a
finite number of inputs. We can also say that it is essential for any
algorithm before starting. Input should be given to it initially before
the Algorithm begins.
2. Output: An algorithm must give at least one required result from
the given set of input values. These output values are known as the
solution to a problem.

2
PRATIK DHOLAKIYA ( 190303108012 )
Design and Analysis of Algorithms (203105301)

3. Definiteness: Each step must be clear, unambiguous, and precisely


defined.
4. Finiteness: Finiteness means Algorithm should be terminated after
a finite number of steps. Also, each step should be finished in a finite
amount of time.
5. Effectiveness: Each step of the Algorithm must be feasible i.e., it
should be practically possible to perform the action. Every Algorithm is
generally expected to be effective.

2). Using Substitution method solve the following recurrence


1. T(n)= T(n-2)+n^2……………..if n>0
1……………………………………..if n=0

3
PRATIK DHOLAKIYA ( 190303108012 )
Design and Analysis of Algorithms (203105301)

2. T(n)=T(n-2)+logn………………if n>0
1………………………………………if n=0

4
PRATIK DHOLAKIYA ( 190303108012 )
Design and Analysis of Algorithms (203105301)

3. T(n)=2T(n/2)+n^2

5
PRATIK DHOLAKIYA ( 190303108012 )
Design and Analysis of Algorithms (203105301)

3). Using master method solve the following recurrence


1. T(n)=9T(n/3)+n

6
PRATIK DHOLAKIYA ( 190303108012 )
Design and Analysis of Algorithms (203105301)

2. T(n)=T(2n/3)+1

7
PRATIK DHOLAKIYA ( 190303108012 )
Design and Analysis of Algorithms (203105301)

3. T(n)=3T(n/4)+nlogn

8
PRATIK DHOLAKIYA ( 190303108012 )
Design and Analysis of Algorithms (203105301)

4). Solve the following recurrence relation using recursion tree


method-
T(n) = T(n/5) + T(4n/5) + n

9
PRATIK DHOLAKIYA ( 190303108012 )
Design and Analysis of Algorithms (203105301)

5). Explain MST with suitable example (Prims and Kruskal algorithm).

10
PRATIK DHOLAKIYA ( 190303108012 )
Design and Analysis of Algorithms (203105301)

Minimum Spanning Tree

• A minimum spanning tree is a spanning tree in which the sum of the


weight of the edges is as minimum as possible.

• Example

11
PRATIK DHOLAKIYA ( 190303108012 )
Design and Analysis of Algorithms (203105301)

The minimum spanning tree from a graph is found using the following
algorithms:

1. Prim's Algorithm
Prim's algorithm is a minimum spanning tree algorithm that takes a graph as
input and finds the subset of the edges of that graph which
• form a tree that includes every vertex

• has the minimum sum of weights among all the trees that can be formed from
the graph

• It falls under a class of algorithms called greedy algorithms that find the local
optimum in the hopes of finding a global optimum.

We start from one vertex and keep adding edges with the lowest weight until
we reach our goal.

The steps for implementing Prim's algorithm:

i. Initialize the minimum spanning tree with a vertex chosen at random.

ii. Find all the edges that connect the tree to new vertices, find the minimum and
add it to the tree

iii. Keep repeating step 2 until we get a minimum spanning tree

2. Kruskal's Algorithm
Kruskal's algorithm is a minimum spanning tree algorithm that takes a graph
as input and finds the subset of the edges of that graph which
• form a tree that includes every vertex

• has the minimum sum of weights among all the trees that can be formed from
the graph.

12
PRATIK DHOLAKIYA ( 190303108012 )
Design and Analysis of Algorithms (203105301)

• It falls under a class of algorithms called greedy algorithms that find the local
optimum in the hopes of finding a global optimum.

We start from the edges with the lowest weight and keep adding edges until
we reach our goal.

The steps for implementing Kruskal's algorithm:

i. Sort all the edges from low weight to high

ii. Take the edge with the lowest weight and add it to the spanning tree. If adding
the edge created a cycle, then reject this edge.

iii. Keep adding edges until we reach all vertices.

6). What are the characteristics of Greedy approach. Explain with


suitable example.
Greedy Algorithm solves problems by making the best choice that
seems best at the particular moment. Many optimization problems can
be determined using a greedy algorithm. Some issues have no efficient
solution, but a greedy algorithm may provide a solution that is close to
optimal. A greedy algorithm works if a problem exhibits the following
two properties:
1. Greedy Choice Property: A globally optimal solution can be
reached at by creating a locally optimal solution. In other words,
an optimal solution can be obtained by creating "greedy" choices.
2. Optimal substructure: Optimal solutions contain optimal
subsolutions. In other words, answers to subproblems of an
optimal solution are optimal.

13
PRATIK DHOLAKIYA ( 190303108012 )
Design and Analysis of Algorithms (203105301)

Steps for achieving a Greedy Algorithm are:


Feasible: Here we check whether it satisfies all possible constraints
or not, to obtain at least one solution to our problems.
Local Optimal Choice: In this, the choice should be the optimum
which is selected from the currently available
Unalterable: Once the decision is made, at any subsequence step
that option is not altered.
Example:

7). Apply the 0/1 Knapsack algorithm to the given data and find out
the best selected items to gain maximum Profit Value. N= 4 Items
(1,2,3,4) respective profit value v (10,40,30,50) and weight w is (5,
4,6,3). Consider Knapsack capacity W is 10.

14
PRATIK DHOLAKIYA ( 190303108012 )
Design and Analysis of Algorithms (203105301)

8). Sort the following functions in the increasing order of their


asymptotic (big-O) complexity:
f1(n) = n^n ,
f2(n) = 2^n,
f3(n) = (1.000001)^n ,
f4(n) = n^(10)*2^(n/2)

15
PRATIK DHOLAKIYA ( 190303108012 )
Design and Analysis of Algorithms (203105301)

16

You might also like