formatting source-code for 153fb7b8a5

This commit is contained in:
github-actions
2020-05-30 04:02:09 +00:00
parent 92fe9495ec
commit 8a2de9842b
175 changed files with 1671 additions and 3460 deletions

View File

@@ -4,33 +4,28 @@
using namespace std;
// Wrapper class for storing an edge
class Edge
{
class Edge {
public:
int src, dst, weight;
};
// Wrapper class for storing a graph
class Graph
{
class Graph {
public:
int vertexNum, edgeNum;
Edge *edges;
// Constructs a graph with V vertices and E edges
Graph(int V, int E)
{
Graph(int V, int E) {
this->vertexNum = V;
this->edgeNum = E;
this->edges = (Edge *)malloc(E * sizeof(Edge));
}
// Adds the given edge to the graph
void addEdge(int src, int dst, int weight)
{
void addEdge(int src, int dst, int weight) {
static int edgeInd = 0;
if (edgeInd < this->edgeNum)
{
if (edgeInd < this->edgeNum) {
Edge newEdge;
newEdge.src = src;
newEdge.dst = dst;
@@ -41,11 +36,9 @@ class Graph
};
// Utility function to print distances
void print(int dist[], int V)
{
void print(int dist[], int V) {
cout << "\nVertex Distance" << endl;
for (int i = 0; i < V; i++)
{
for (int i = 0; i < V; i++) {
if (dist[i] != INT_MAX)
cout << i << "\t" << dist[i] << endl;
else
@@ -56,8 +49,7 @@ void print(int dist[], int V)
// The main function that finds the shortest path from given source
// to all other vertices using Bellman-Ford.It also detects negative
// weight cycle
void BellmanFord(Graph graph, int src)
{
void BellmanFord(Graph graph, int src) {
int V = graph.vertexNum;
int E = graph.edgeNum;
int dist[V];
@@ -70,8 +62,7 @@ void BellmanFord(Graph graph, int src)
// Calculate shortest path distance from source to all edges
// A path can contain maximum (|V|-1) edges
for (int i = 0; i <= V - 1; i++)
for (int j = 0; j < E; j++)
{
for (int j = 0; j < E; j++) {
int u = graph.edges[j].src;
int v = graph.edges[j].dst;
int w = graph.edges[j].weight;
@@ -81,14 +72,12 @@ void BellmanFord(Graph graph, int src)
}
// Iterate inner loop once more to check for negative cycle
for (int j = 0; j < E; j++)
{
for (int j = 0; j < E; j++) {
int u = graph.edges[j].src;
int v = graph.edges[j].dst;
int w = graph.edges[j].weight;
if (dist[u] != INT_MAX && dist[u] + w < dist[v])
{
if (dist[u] != INT_MAX && dist[u] + w < dist[v]) {
cout << "Graph contains negative weight cycle. Hence, shortest "
"distance not guaranteed."
<< endl;
@@ -102,8 +91,7 @@ void BellmanFord(Graph graph, int src)
}
// Driver Function
int main()
{
int main() {
int V, E, gsrc;
int src, dst, weight;
cout << "Enter number of vertices: ";
@@ -111,8 +99,7 @@ int main()
cout << "Enter number of edges: ";
cin >> E;
Graph G(V, E);
for (int i = 0; i < E; i++)
{
for (int i = 0; i < E; i++) {
cout << "\nEdge " << i + 1 << "\nEnter source: ";
cin >> src;
cout << "Enter destination: ";