vertexNum, edgeNum;
17std::vector<Edge> edges;
20Graph(
intV,
intE) {
23this->edges.reserve(E);
27 voidaddEdge(
intsrc,
intdst,
intweight) {
28 static intedgeInd = 0;
29 if(edgeInd < this->edgeNum) {
33newEdge.weight = weight;
34this->edges[edgeInd++] = newEdge;
40voidprint(
conststd::vector<int>& dist,
intV) {
41cout <<
"\nVertex Distance"<<
endl;
42 for(
inti = 0; i < V; i++) {
43 if(dist[i] != INT_MAX)
44cout << i <<
"\t"<< dist[i] <<
endl;
46cout << i <<
"\tINF"<<
endl;
54 intV =
graph.vertexNum;
55 intE =
graph.edgeNum;
56std::vector<int> dist;
61 for(
inti = 0; i < V; i++) dist[i] = INT_MAX;
66 for(
inti = 0; i <= V - 1; i++)
67 for(
intj = 0; j < E; j++) {
68 intu =
graph.edges[j].src;
69 intv =
graph.edges[j].dst;
70 intw =
graph.edges[j].weight;
72 if(dist[u] != INT_MAX && dist[u] + w < dist[v])
73dist[v] = dist[u] + w;
77 for(
intj = 0; j < E; j++) {
78 intu =
graph.edges[j].src;
79 intv =
graph.edges[j].dst;
80 intw =
graph.edges[j].weight;
82 if(dist[u] != INT_MAX && dist[u] + w < dist[v]) {
83cout <<
"Graph contains negative weight cycle. Hence, shortest " 84 "distance not guaranteed." 99cout <<
"Enter number of vertices: ";
101cout <<
"Enter number of edges: ";
104 for(
inti = 0; i < E; i++) {
105cout <<
"\nEdge "<< i + 1 <<
"\nEnter source: ";
107cout <<
"Enter destination: ";
109cout <<
"Enter weight: ";
111G.addEdge(src, dst, weight);
113cout <<
"\nEnter source: ";
115BellmanFord(G, gsrc);
int main()
Main function.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4