Assignment 13
Assignment 13
CODE:
#include <iostream>
#include <vector>
#include <climits>
struct Edge {
int src, dest, weight;
};
public:
Graph(int V, int E) {this-
>V = V;
this->E = E;
}
}
};
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>
}
}
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;
}
}
}
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:
CODE:
#include <iostream>
#include <climits> Using
namespace std;
#define V 4
}
}
}
}
}
}
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;
}
}
}
return variance / nums.size();
}
int main() {
vector<int> data = {10, 23, 15, 7, 45, 32, 9};double
median = findMedian(data);
cout << "Median: " << median << endl;
return 0;
}
OUTPUT: