Skip to main content

Posts

Showing posts from January, 2018

Dynamic Programming - 0/1 Knapsack Problem

➤ Greedy algo does not give optimal solution everytime.       Exa. Consider  n=3 and Capacity W=30               w={20,10,5}               p={180,80,50}               p/w={9,8,10}               greedy solution={1,0,1} ➝ profit =180+50=230               optimal solution={1,1,0} ➝ profit=180+80=260 Algo :-  w[i] gives weight of ith item. p[i] gives value(profit) of ith item. sol[i][j] gives the maximum profit for i items and j capacity(sub problem) Use Desktop Version Of This Site For Better View. for(j=0 to m) ➝ No Capicity No Packets      sol[0][j]=0 for(i=0 to m) ➝ No Items      sol[i][0]=0 for(i=1 to n)       for(j=1 to m)           if(w[i] > j)      ...

MAKE CHANGE PROBLEM - DYNAMIC PROGRAMMING

Greedy may not give optimal solution everytime. Make change Problem - Greedy Algo Example : Coins  : { 1 ,  3 , 4  } Amount : 6 Solution Given By Greedy : { 4 , 1 , 1 } Optimal Solution : { 3 , 3 } Dynamic Programming : Algo : fun makechange(N) { array d[1...n]=[1,4,6]; array c[1...n,0...N] for i=1 to n do c[i,0]=0 for j=1 to N do c[i,j] = if i=1 & j<d[i] then +  ∞ else if i=1 then 1+c[1,j-d[1]]; else if j<d[i] then c[i-1,j] else min(c[i-1][j] ,1+c[i,j-d[i]]); return c[n,N]; } Programme : Download Programme #include<stdio.h> #include<stdlib.h> #include<limits.h> int min(int x,int y) { int min; if(x>y) { min=y; } else { min=x; } return min; } void make_change(int N) { int i,j; int d[4]={0,1,4,6}; int c[4][N+1]; printf("...

Dijkstra's algorithm

Djikstra's algorithm   solves the problem of finding the shortest path from a point in a graph (the  source ) to a destination. It turns out that one can find the shortest paths from a given source to  all  points in a graph in the same time, hence this problem is sometimes called the  single-source shortest paths  problem. This problem is related to the spanning tree one. The graph representing all the paths from one vertex to all the others must be a spanning tree - it must include all vertices. There will also be no cycles as a cycle would define more than one path from the selected vertex to at least one other vertex. For a graph, G = (V,E) where V is a set of vertices and E is a set of edges. Dijkstra's algorithm keeps two sets of vertices: S    the set of vertices whose shortest paths from the source have already been determined  and V-    S :   the remaining v...

Dynamic Systems Development Method (DSDM)

Dynamic Systems Development Method(DSDM) :- Dynamic system development method is approach to system development which as the name suggests develops the system dynamically it is a Rapid application development that uses incremental Prototyping. The DSDM philosophy is borrowed from a modified version of " Pareto Principle " Pareto Principle :-  80% of an application can be delivered in 20% of the time it would take to deliver the complete(100%) application. DSDM is an iterative software process in which each iteration follows 80% rule. That is only enough work is required for each increment to start next increment The remaining work can be completed later when more requirements are known or changes have been requested. DSDM life cycle defines 3 different cycles preceded by 2 additional life cycle activities ( FBFDI ) Feasibility Study :-  establishes the basic business requirements and constraint associated with the application to be built. and Identifie...

Adaptive Software Development (ASD)

Adaptive Software Development (ASD) :- Adaptive Software Development(ASD) has been proposed by Jim High smith as technique for building complex software and system. ASD focus on human collaboration and team self-organization. Jim High smith defines an ASD "Life Cycle" that consist of three phases  Speculation Collaboration learning  During speculation, the project is initiated and adaptive cycle planning is conducted. Adaptive cycle planning uses project Initiation information,the customer's mission statement ,basic requirements and project constraint to define the set of release cycles that will be required for the project. Collaboration encompasses communication and team-work but it also enphasizes individualism because individual creativity plays an important role in collaborative thinking.It is all above all,a matter of trust. people working together must trust one another to comment without war help without irritation work as hard as ...

Kruskal’s Minimum Spanning Tree Algorithm & Programme

PROBLEM :                  Kruskal's algorithm is a minimum-spanning-tree algorithm which finds an edge of the least possible weight that connects any two trees in the forest. It is a greedy algorithm in graph theory as it finds a minimum spanning tree for a connected weighted graph adding increasing cost arcs at each step. 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 ove 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. Th...

KNAPSACK PROBLEM

PROBLEM :  The  knapsack problem  or rucksack  problem  is a problem  in combinatorial optimization: Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible. PROGRAM : // PROGRAMME FOR KNAPSACK PROBLEM #include<stdio.h> int notin(int ind,int c[],int flag) {     int i;     for(i=0;i<flag;i++)     {         if(c[i]==ind)         {             return -1;         }     }     return 1; } void knapsack(int w[],int v[],int W,int n) { int i,weight; float x[n]; for(i=0;i<n;i++) { x[i]=0; } weight=0; /*for(i=0;i<n;i++) { printf("%d ",w[i]); printf("%d \n",v[i]); }*/      float...

MAKE CHANGE PROBLEM - GREEDY ALGO

The change making problem is an optimization problem that asks  " What is the minimum number of coins I need to make up a specific total?" The input to the Change Making Problem is a sequence of positive integers  [d1, d2, d3 ... dn]  and  T , where  di  represents a coin denomination and  T  is the target amount. Assuming an unlimited supply of coins of each denomination, we need to find the number of coins  N  required to form the given amount // PROGRAMME FOR MAKE CHANGE PROBLEM #include<stdio.h> int findlargest(int c[],int sum,int n) { int i; for(i=0;i<5;i++) { if(sum+c[i]<=n) { return c[i]; } } return -1; } void make_change(int n) { int c[]={100,25,10,5,1}; int s[n],sum=0,x; int ind=0,i; while(sum!=n) { x=findlargest(c,sum,n); if(x==-1) { printf("OOPS ! NO SOLUTION FOUND "); return; } s[ind++]=x; sum+=x; } printf("SOLUTION : "); for(i=0;i<i...