ADA Lab Program
ADA Lab Program
#include <stdlib.h>
parent[i] = i;
rank[i] = 0;
if (parent[component] == component)
return component;
return parent[component]
= findParent(parent, parent[component]);
u = findParent(parent, u);
v = findParent(parent, v);
parent[u] = v;
else {
parent[v] = u;
rank[u]++;
int parent[n];
int rank[n];
int minCost = 0;
printf(
int wt = edge[i][2];
if (v1 != v2) {
minCost += wt;
edge[i][1], wt);
}
int main()
int edge[5][3] = { { 0, 1, 10 },
{ 0, 2, 6 },
{ 0, 3, 5 },
{ 1, 3, 15 },
{ 2, 3, 4 } };
kruskalAlgo(5, edge);
return 0;
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#define V 5
return min_index;
printf("Edge \tWeight\n");
graph[i][parent[i]]);
}
void primMST(int graph[V][V])
int parent[V];
int key[V];
bool mstSet[V];
key[0] = 0;
parent[0] = -1;
mstSet[u] = true;
printMST(parent, graph);
int main()
int graph[V][V] = { { 0, 2, 0, 6, 0 },
{ 2, 0, 3, 8, 5 },
{ 0, 3, 0, 0, 7 },
{ 6, 8, 0, 0, 9 },
{ 0, 5, 7, 9, 0 } };
primMST(graph);
return 0;
3a.Floyds Algorithm
#include <stdio.h>
#define V 4
int i, j, k;
printSolution(dist);
printf(
if (dist[i][j] == INF)
printf("%7s", "INF");
else
printf("%7d", dist[i][j]);
printf("\n");
int main()
{ INF, 0, 3, INF },
{ INF, INF, 0, 1 },
floydWarshall(graph);
return 0;
#include<stdio.h>
#define V 4
int reach[V][V], i, j, k;
reach[i][j] = graph[i][j];
reach[i][j] = reach[i][j] ||
printSolution(reach);
if(i == j)
printf("1 ");
else
printf("\n");
int main()
{0, 1, 1, 0},
{0, 0, 1, 1},
{0, 0, 0, 1}
};
transitiveClosure(graph);
return 0;
4.Dijkistras Algorithm
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#define V 9
return min_index;
int dist[V];
bool sptSet[V];
dist[src] = 0;
sptSet[u] = true;
printSolution(dist);
}
int main()
int graph[V][V] = { { 0, 4, 0, 0, 0, 0, 0, 8, 0 },
{ 4, 0, 8, 0, 0, 0, 0, 11, 0 },
{ 0, 8, 0, 7, 0, 4, 0, 0, 2 },
{ 0, 0, 7, 0, 9, 14, 0, 0, 0 },
{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },
{ 0, 0, 4, 14, 10, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },
{ 8, 11, 0, 0, 0, 0, 1, 0, 7 },
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 } };
dijkstra(graph, 0);
return 0;
5. Topological Sort
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
struct Stack {
int data;
};
struct Graph {
int V;
};
struct List {
int data;
struct List* next;
};
newNode->data = data;
newNode->next = NULL;
return newNode;
newNode->data = data;
newNode->next = NULL;
return newNode;
graph->V = V;
graph->adj
graph->adj[i].next = NULL;
return graph;
}
void addEdge(struct Graph* graph, int v, int w)
newNode->next = graph->adj[v].next;
graph->adj[v].next = newNode;
bool visited[],
visited[v] = true;
if (!visited[adjacentVertex]) {
topologicalSortUtil(graph, adjacentVertex,
visited, stack);
current = current->next;
newNode->next = *stack;
*stack = newNode;
visited[i] = false;
if (!visited[i]) {
stack = stack->next;
free(temp);
free(visited);
free(graph->adj);
free(graph);
int main()
addEdge(g, 5, 2);
addEdge(g, 5, 0);
addEdge(g, 4, 0);
addEdge(g, 4, 1);
addEdge(g, 2, 3);
addEdge(g, 3, 1);
printf("Topological Sorting Order: ");
topologicalSort(g);
return 0;
#include <stdio.h>
if (n == 0 || W == 0)
return 0;
if (wt[n - 1] > W)
else
return max(
val[n - 1]
int main()
int W = 50;
return 0;
#include <bits/stdc++.h>
struct Item {
double ratio;
int index;
};
vector<int>& weights)
vector<Item> ratios(N);
ratios[i].ratio
= static_cast<double>(values[i]) / weights[i];
ratios[i].index = i;
int total_value = 0;
int total_weight = 0;
total_value += values[index];
total_weight += weights[index];
return total_value;
int main()
int N = 3;
int W = 4;
vector<int> values = { 1, 2, 3 };
vector<int> weights = { 4, 5, 1 };
return 0;
#include <stdio.h>
#include <stdbool.h>
if (sum == 0)
return true;
if (n == 0)
return false;
int main()
int sum = 9;
else
return 0;