0% found this document useful (0 votes)
14 views13 pages

Assignment 13

Uploaded by

Nilesh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views13 pages

Assignment 13

Uploaded by

Nilesh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

ASSIGNMENT - 13

QUESTION: Write a program to implement bellman fordAlgorithm.

CODE:
#include <iostream>
#include <vector>
#include <climits>

using namespace std;

struct Edge {
int src, dest, weight;

};

class Graph {int


V, E;
vector<Edge> edges;

public:
Graph(int V, int E) {this-
>V = V;
this->E = E;

void addEdge(int src, int dest, int weight) {


edges.push_back({src, dest, weight});
}

void BellmanFord(int src) { vector<int>


dist(V, INT_MAX);dist[src] = 0;

for (int i = 1; i < V; i++) {


for (const Edge &edge : edges) {int u =
edge.src;
int v = edge.dest; int w =
edge.weight;
if (dist[u] != INT_MAX && dist[u] + w < dist[v]) {dist[v] =
dist[u] + w;
}
}
}

for (const Edge &edge : edges) {int u =


edge.src;
int v = edge.dest; int w =
edge.weight;
if (dist[u] != INT_MAX && dist[u] + w < dist[v]) {
cout << "Graph contains negative weight cycle" <<
endl;
return;
}
}

cout << "Vertex Distance from Source:" << endl;for (int i = 0;


i < V; i++) {
cout << i << "\t\t" << dist[i] << endl;

}
}
};

int main() {
Graph graph(5, 8);
graph.addEdge(0, 1, -1);
graph.addEdge(0, 2, 4);
graph.addEdge(1, 2, 3);
graph.addEdge(1, 3, 2);
graph.addEdge(1, 4, 2);
graph.addEdge(3, 2, 5);
graph.addEdge(3, 1, 1);
graph.addEdge(4, 3, -3);

graph.BellmanFord(0);

return 0;

OUTPUT:
QUESTION: Write a program to implement dijkstra’sAlgorithm.

CODE:
#include <iostream>
#include <vector> #include
<limits>

using namespace std;

const int INF = numeric_limits<int>::max();

int minDistance(vector<int>& dist, vector<bool>& visited) {int minDist = INF,


minIndex = -1;
for (int i = 0; i < dist.size(); ++i) {
if (!visited[i] && dist[i] <= minDist) {minDist =
dist[i];
minIndex = i;

}
}

return minIndex;
}
void dijkstra(vector<vector<int>>& graph, int start) {int V =
graph.size();
vector<int> dist(V, INF); vector<bool>
visited(V, false);dist[start] = 0;

for (int count = 0; count < V - 1; ++count) {int u =


minDistance(dist, visited); visited[u] = true;

for (int v = 0; v < V; ++v) {


if (!visited[v] && graph[u][v] && dist[u] != INF &&dist[u] +
graph[u][v] < dist[v]) {
dist[v] = dist[u] + graph[u][v];

}
}
}

cout << "Vertex \t Distance from Source\n";for (int i = 0;


i < V; ++i) {

cout << i << " \t\t " << dist[i] << "\n";

}
}

int main() {
vector<vector<int>> graph = {
{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;
OUTPUT:

QUESTION: Write a program to implement Flyod warshall’sAlgorithm.

CODE:
#include <iostream>
#include <climits> Using
namespace std;

#define V 4

void floydWarshall(int graph[V][V]) {int


dist[V][V];

for (int i = 0; i < V; i++) {


for (int j = 0; j < V; j++) {
dist[i][j] = graph[i][j];
}
}

for (int k = 0; k < V; k++) {for (int


i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (dist[i][k] != INT_MAX && dist[k][j] != INT_MAX &&dist[i][k] +
dist[k][j] < dist[i][j]) {
dist[i][j] = dist[i][k] + dist[k][j];

}
}
}
}

std::cout << "Shortest distances between every pair ofvertices:\n";


for (int i = 0; i < V; i++) { for (int j
= 0; j < V; j++) {
if (dist[i][j] == INT_MAX) {cout
<< "INF\t";
} else {
cout << dist[i][j] << "\t";

}
}

cout <<endl;
}
}

int main() {
int graph[V][V] = { {0, 5, INT_MAX, 10},
{INT_MAX, 0, 3, INT_MAX},
{INT_MAX, INT_MAX, 0, 1},
{INT_MAX, INT_MAX, INT_MAX, 0} };

floydWarshall(graph);
return 0;

OUTPUT:
QUESTION: Write a program to implement median andStatistics.
CODE:
#include <iostream>
#include <vector> #include
<algorithm>#include
<cmath> using namespace
std;

double findMedian(vector<int>& nums) {int n =


nums.size();
sort(nums.begin(), nums.end());if (n % 2
== 0) {
return (nums[n/2 - 1] + nums[n/2]) / 2.0;
} else {
return nums[n/2];

}
}

double findMean(vector<int>& nums) {int sum =


0;
for (int num : nums) {
sum += num;
}

return static_cast<double>(sum) / nums.size();


}

double findVariance(vector<int>& nums) {double


mean = findMean(nums); double variance = 0;
for (int num : nums) {
variance += pow(num - mean, 2);

}
return variance / nums.size();
}

double findStandardDeviation(vector<int>& nums) {return


sqrt(findVariance(nums));
}

int main() {
vector<int> data = {10, 23, 15, 7, 45, 32, 9};double

median = findMedian(data);
cout << "Median: " << median << endl;

double mean = findMean(data); cout <<


"Mean: " << mean << endl;

double variance = findVariance(data); cout <<


"Variance: " << variance << endl;

double stdDeviation = findStandardDeviation(data); cout << "Standard


Deviation: " << stdDeviation << endl;

return 0;
}

OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy