Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IMPLEMENTATION OFF BELLMAND FORD #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions Bellman_Ford.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

using namespace std;

struct Edge
{
// This structure is equal to an edge. Edge contains two end points. These edges are directed edges so they
//contain source and destination and some weight. These 3 are elements in this structure
int source, destination, weight;
};

// a structure to represent a connected, directed and weighted graph
struct Graph
{
int V, E;
// V is number of vertices and E is number of edges

struct Edge* edge;
// This structure contain another structure which we already created edge.
};

struct Graph* createGraph(int V, int E)
{
struct Graph* graph = (struct Graph*) malloc( sizeof(struct Graph));
//Allocating space to structure graph

graph->V = V; //assigning values to structure elements that taken form user.

graph->E = E;

graph->edge = (struct Edge*) malloc( graph->E * sizeof( struct Edge ) );
//Creating "Edge" type structures inside "Graph" structure, the number of edge type structures are equal to number of edges

return graph;
}

void FinalSolution(int dist[], int n)
{
// This function prints the final solution
cout<<"\nVertex\tDistance from Source Vertex\n";
int i;

for (i = 0; i < n; ++i){
cout<<i<<"\t\t"<<dist[i]<<"\n";
}
}

void BellmanFord(struct Graph* graph, int source)
{
int V = graph->V;

int E = graph->E;

int StoreDistance[V];

int i,j;

// This is initial step that we know , we initialize all distance to infinity except source.
// We assign source distance as 0(zero)

for (i = 0; i < V; i++)
StoreDistance[i] = INT_MAX;

StoreDistance[source] = 0;

//The shortest path of graph that contain V vertices, never contain "V-1" edges. So we do here "V-1" relaxations
for (i = 1; i <= V-1; i++)
{
for (j = 0; j < E; j++)
{
int u = graph->edge[j].source;

int v = graph->edge[j].destination;

int weight = graph->edge[j].weight;

if (StoreDistance[u] + weight < StoreDistance[v])
StoreDistance[v] = StoreDistance[u] + weight;
}
}

// Actually upto now shortest path found. But BellmanFord checks for negative edge cycle. In this step we check for that
// shortest distances if graph doesn't contain negative weight cycle.

// If we get a shorter path, then there is a negative edge cycle.
for (i = 0; i < E; i++)
{
int u = graph->edge[i].source;

int v = graph->edge[i].destination;

int weight = graph->edge[i].weight;

if (StoreDistance[u] + weight < StoreDistance[v])
cout<<"\nThis graph contains negative edge cycle\n";
}

FinalSolution(StoreDistance, V);

return;
}

int main()
{
int V,E,S; //V = no.of Vertices, E = no.of Edges, S is source vertex

cout<<"Enter number of vertices in graph\n";
cin>>V;

cout<<"Enter number of edges in graph\n";
cin>>E;

cout<<"Enter your source vertex number\n";
cin>>S;

struct Graph* graph = createGraph(V, E); //calling the function to allocate space to these many vertices and edges

int i;
for(i=0;i<E;i++){
cout<<"\nEnter edge "<<i+1<<" properties Source, destination, weight respectively\n";
cin>>graph->edge[i].source;
cin>>graph->edge[i].destination;
cin>>graph->edge[i].weight;
}

BellmanFord(graph, S);
//passing created graph and source vertex to BellmanFord Algorithm function

return 0;
}
145 changes: 145 additions & 0 deletions dijktra.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#include <iostream>
#include <limits.h>

using namespace std;

//Wrapper class for storing a graph
class Graph
{
public:
int vertexNum;
int **edges;

//Constructs a graph with V vertices and E edges
Graph(const int V)
{

// initializes the array edges.
this->edges = new int *[V];
for (int i = 0; i < V; i++)
{
edges[i] = new int[V];
}

// fills the array with zeros.
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
edges[i][j] = 0;
}
}

this->vertexNum = V;
}

//Adds the given edge to the graph
void addEdge(int src, int dst, int weight)
{
this->edges[src][dst] = weight;
}
};
//Utility function to find minimum distance vertex in mdist
int minDistance(int mdist[], bool vset[], int V)
{
int minVal = INT_MAX, minInd = 0;
for (int i = 0; i < V; i++)
{
if (!vset[i] && (mdist[i] < minVal))
{
minVal = mdist[i];
minInd = i;
}
}

return minInd;
}

//Utility function to print distances
void print(int dist[], int V)
{
cout << "\nVertex Distance" << endl;
for (int i = 0; i < V; i++)
{
if (dist[i] < INT_MAX)
cout << i << "\t" << dist[i] << endl;
else
cout << i << "\tINF" << endl;
}
}

//The main function that finds the shortest path from given source
//to all other vertices using Dijkstra's Algorithm.It doesn't work on negative
//weights
void Dijkstra(Graph graph, int src)
{
int V = graph.vertexNum;
int mdist[V]; //Stores updated distances to vertex
bool vset[V]; // vset[i] is true if the vertex i included
// in the shortest path tree

//Initialise mdist and vset. Set distance of source as zero
for (int i = 0; i < V; i++)
{
mdist[i] = INT_MAX;
vset[i] = false;
}

mdist[src] = 0;

//iterate to find shortest path
for (int count = 0; count < V - 1; count++)
{
int u = minDistance(mdist, vset, V);

vset[u] = true;

for (int v = 0; v < V; v++)
{
if (!vset[v] && graph.edges[u][v] && mdist[u] + graph.edges[u][v] < mdist[v])
{
mdist[v] = mdist[u] + graph.edges[u][v];
}
}
}

print(mdist, V);
}

//Driver Function
int main()
{
int V, E, gsrc;
int src, dst, weight;
cout << "Enter number of vertices: ";
cin >> V;
cout << "Enter number of edges: ";
cin >> E;
Graph G(V);
for (int i = 0; i < E; i++)
{
cout << "\nEdge " << i + 1 << "\nEnter source: ";
cin >> src;
cout << "Enter destination: ";
cin >> dst;
cout << "Enter weight: ";
cin >> weight;

// makes sure source and destionation are in the proper bounds.
if (src >= 0 && src < V && dst >= 0 && dst < V)
{
G.addEdge(src, dst, weight);
}
else
{
cout << "source and/or destination out of bounds" << endl;
i--;
continue;
}
}
cout << "\nEnter source:";
cin >> gsrc;
Dijkstra(G, gsrc);

return 0;
}