AOA Pract 3
AOA Pract 3
#include <stdio.h>
#include <limits.h>
min = dist[v];
minIndex = v;
return minIndex;
// Function to implement Dijkstra's algorithm for a given graph and source vertex
int dist[MAX_VERTICES]; // The output array dist[i] holds the shortest distance from src to i
int sptSet[MAX_VERTICES]; // sptSet[i] will be true if vertex i is included in the shortest path tree or the shortest
distance from src to i is finalized
dist[i] = INT_MAX;
sptSet[i] = 0;
dist[src] = 0;
// Pick the minimum distance vertex from the set of vertices not yet processed.
sptSet[u] = 1;
// and the total weight of path from src to v through u is smaller than the current value of dist[v]
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v]) {
printSolution(dist, vertices);
int main() {
int vertices;
scanf("%d", &vertices);
int graph[MAX_VERTICES][MAX_VERTICES];
printf("Input the adjacency matrix for the graph (use INT_MAX for infinity):\n");
scanf("%d", &graph[i][j]);
int source;
scanf("%d", &source);
return 1;
return 0;
Output:
#include <stdio.h>
int n = 5;
int W = 10;
int main(){
int cur_w;
float tot_v;
int i, maxi;
int used[10];
used[i] = 0;
cur_w = W;
maxi = -1;
if ((used[i] == 0) &&
maxi = i;
used[maxi] = 1;
cur_w -= p[maxi];
tot_v += w[maxi];
if (cur_w >= 0)
printf("Added object %d (%d, %d) completely in the bag. Space left: %d.\n", maxi + 1, w[maxi], p[maxi],
cur_w);
else {
printf("Added %d%% (%d, %d) of object %d in the bag.\n", (int)((1 + (float)cur_w/p[maxi]) * 100), w[maxi],
p[maxi], maxi + 1);
tot_v -= w[maxi];
return 0;
Output
Added object 5 (8, 1) completely in the bag. Space left: 9.
Added object 2 (15, 3) completely in the bag. Space left: 6.
Added object 3 (10, 2) completely in the bag. Space left: 4.
Added object 1 (10, 3) completely in the bag. Space left: 1.
Added 19% (12, 5) of object 4 in the bag.
Filled the bag with objects worth 45.40.
#include <stdio.h>
#include <stdlib.h>
} Jobs;
// This function is used for sorting all Jobss according to
// profit
int main(){
Jobs arr[] = {
{ 'a', 2, 100 },
{ 'b', 2, 20 },
{ 'c', 1, 40 },
{ 'd', 3, 35 },
{ 'e', 1, 25 }
};
slot[i] = false;
if (slot[j] == false) {
result[j] = i;
slot[j] = true;
break;
if (slot[i])
return 0;
Output
Following is maximum profit sequence of Jobs:
cad