Task 6 7 Daa New
Task 6 7 Daa New
Laboratory Experiments
Total: 30 Hours (P)
Part – 1
ALGORITHM
Program:
#include <stdio.h>
{ int i, w;
if (i == 0 || w == 0)
K[i][w] = 0;
K[i][w] = max(val[i - 1]
K[i - 1][w]);
else
return K[n][W];
int main()
return 0;
Output
220
Time Complexity: O(N * W). where ‘N’ is the number of elements and ‘W’ is capacity.
Auxiliary Space: O(N * W). The use of a 2-D array of size ‘N*W’.
Below is the implementation of the same approach but with optimized space complexity:
Result
Thus Implementing 0/1 Knapsack problem using Dynamic Programming and find its
complexity was learn successfully
Task 7: Greedy Technique – Prim’s algorithm
AIM
To Find Minimum Cost Spanning Tree of a undirected graph using Prim’s algorithm
ALGORITHM
1. Step 1: Select a starting vertex
2. Step 2: Repeat Steps 3 and 4 until there are fringe vertices
3. Step 3: Select an edge 'e' connecting the tree vertex and fringe vertex that has minimum weight
4. Step 4: Add the selected edge and the vertex to the minimum spanning tree T
5. [END OF LOOP]
6. Step 5: EXIT
Program:
#include<stdio.h>
#include<conio.h>
int a,b,u,v,n,i,j,ne=1;
int visited[10]={0},min,mincost=0,cost[10][10];
void main()
clrscr();
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
visited[1]=1;
printf("\n");
while(ne<n)
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]<min)
if(visited[i]!=0)
min=cost[i][j];
a=u=i;
b=v=j;
if(visited[u]==0 || visited[v]==0)
mincost+=min;
visited[b]=1;
cost[a][b]=cost[b][a]=999;
}
printf("\n Minimum cost=%d",mincost);
getch();
/* OUTPUT
063000
602500
320340
053023
004205
000350
Minimum cost=13
Test case 1:
Aim:
Assume an undirected graph and find whether the graph is connected or not and then apply
Prim’s algorithm to find MST
Algorithm:
1. Create a Min Heap of size V where V is the number of vertices in the given graph. Every
node of min heap contains vertex number and key value of the vertex.
2. Initialize Min Heap with first vertex as root (the key value assigned to first vertex is 0).
The key value assigned to all other vertices is INF (infinite).
3. While Min Heap is not empty, do following
1. Extract the min value node from Min Heap. Let the extracted vertex be u.
2. For every adjacent vertex v of u, check if v is in Min Heap (not yet included in MST).
If v is in Min Heap and its key value is more than weight of u-v, then update the key
value of v as weight of u-v.
Program:
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
struct AdjListNode {
int dest;
int weight;
};
struct AdjList {
struct AdjListNode*
};
// vertices in graph)
struct Graph {
int V;
};
= (struct AdjListNode*)malloc(
sizeof(struct AdjListNode));
newNode->dest = dest;
newNode->weight = weight;
newNode->next = NULL;
return newNode;
graph->V = V;
// will be V
V * sizeof(struct AdjList));
// Initialize each adjacency list as empty by making
// head as NULL
graph->array[i].head = NULL;
return graph;
int weight)
// beginning
= newAdjListNode(dest, weight);
newNode->next = graph->array[src].head;
graph->array[src].head = newNode;
// src also
newNode->next = graph->array[dest].head;
graph->array[dest].head = newNode;
struct MinHeapNode {
int v;
int key;
};
struct MinHeap {
};
= (struct MinHeapNode*)malloc(
sizeof(struct MinHeapNode));
minHeapNode->v = v;
minHeapNode->key = key;
return minHeapNode;
minHeap->size = 0;
minHeap->capacity = capacity;
return minHeap;
struct MinHeapNode** b)
*a = *b;
*b = t;
smallest = idx;
left = 2 * idx + 1;
right = 2 * idx + 2;
&& minHeap->array[left]->key
< minHeap->array[smallest]->key)
smallest = left;
a
if (right < minHeap->size
&& minHeap->array[right]->key
< minHeap->array[smallest]->key)
smallest = right;
if (smallest != idx) {
MinHeapNode* smallestNode
= minHeap->array[smallest];
// Swap positions
minHeap->pos[smallestNode->v] = idx;
minHeap->pos[idxNode->v] = smallest;
// Swap nodes
swapMinHeapNode(&minHeap->array[smallest],
&minHeap->array[idx]);
minHeapify(minHeap, smallest);
// or not
return minHeap->size == 0;
if (isEmpty(minHeap))
return NULL;
// Store the root node
= minHeap->array[minHeap->size - 1];
minHeap->array[0] = lastNode;
minHeap->pos[root->v] = minHeap->size - 1;
minHeap->pos[lastNode->v] = 0;
--minHeap->size;
minHeapify(minHeap, 0);
return root;
}
// Function to decrease key value of a given vertex v. This
int i = minHeap->pos[v];
minHeap->array[i]->key = key;
while (i
&& minHeap->array[i]->key
minHeap->pos[minHeap->array[(i - 1) / 2]->v] = i;
swapMinHeapNode(&minHeap->array[i],
&minHeap->array[(i - 1) / 2]);
i = (i - 1) / 2;
return true;
return false;
}
// A utility function used to print the constructed MST
// edge in cut
// infinite
parent[v] = -1;
key[v] = INT_MAX;
minHeap->pos[v] = v;
// is extracted first
key[0] = 0;
minHeap->pos[0] = 0;
// Initially size of min heap is equal to V
minHeap->size = V;
while (!isEmpty(minHeap)) {
= extractMin(minHeap);
int u
= minHeapNode
int v = pCrawl->dest;
// If v is not yet included in MST and weight of
if (isInMinHeap(minHeap, v)
key[v] = pCrawl->weight;
parent[v] = u;
decreaseKey(minHeap, v, key[v]);
pCrawl = pCrawl->next;
printArr(parent, V);
}
// Driver program to test above functions
int main()
int V = 9;
addEdge(graph, 0, 1, 4);
addEdge(graph, 0, 7, 8);
addEdge(graph, 1, 2, 8);
addEdge(graph, 1, 7, 11);
addEdge(graph, 2, 3, 7);
addEdge(graph, 2, 8, 2);
addEdge(graph, 2, 5, 4);
addEdge(graph, 3, 4, 9);
addEdge(graph, 3, 5, 14);
addEdge(graph, 4, 5, 10);
addEdge(graph, 5, 6, 2);
addEdge(graph, 6, 7, 1);
addEdge(graph, 6, 8, 6);
addEdge(graph, 7, 8, 7);
PrimMST(graph);
return 0;
Output
0-1
5-2
2-3
3-4
6-5
7-6
0-7
2-8
Result
Thus Finding Minimum Cost Spanning Tree of a undirected graph using Prim’s
algorithm was learn successfully