From d94905e8888c536eaf8d0b97ad34082cec75a0c3 Mon Sep 17 00:00:00 2001 From: /Charchit_Gangwar/ <114564628+ATOMworkplace@users.noreply.github.com> Date: Tue, 15 Oct 2024 18:12:14 +0530 Subject: [PATCH 1/6] Create bellman_ford.cpp --- graph/bellman_ford.cpp | 116 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 graph/bellman_ford.cpp diff --git a/graph/bellman_ford.cpp b/graph/bellman_ford.cpp new file mode 100644 index 00000000000..c96f3fd8eea --- /dev/null +++ b/graph/bellman_ford.cpp @@ -0,0 +1,116 @@ +#include +#include + +using namespace std; + +// Wrapper class for storing an edge +class Edge { + public: + int src, dst, weight; +}; + +// Wrapper class for storing a graph +class Graph { + public: + int vertexNum, edgeNum; + Edge *edges; + + // Constructs a graph with V vertices and E edges + 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) { + static int edgeInd = 0; + if (edgeInd < this->edgeNum) { + Edge newEdge; + newEdge.src = src; + newEdge.dst = dst; + newEdge.weight = weight; + this->edges[edgeInd++] = newEdge; + } + } +}; + +// 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 Bellman-Ford.It also detects negative +// weight cycle +void BellmanFord(Graph graph, int src) { + int V = graph.vertexNum; + int E = graph.edgeNum; + int dist[V]; + + // Initialize distances array as INF for all except source + // Intialize source as zero + for (int i = 0; i < V; i++) dist[i] = INT_MAX; + dist[src] = 0; + + // 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++) { + 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]) + dist[v] = dist[u] + w; + } + + // Iterate inner loop once more to check for negative cycle + 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]) { + cout << "Graph contains negative weight cycle. Hence, shortest " + "distance not guaranteed." + << endl; + return; + } + } + + print(dist, V); + + return; +} + +// 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, E); + 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; + G.addEdge(src, dst, weight); + } + cout << "\nEnter source: "; + cin >> gsrc; + BellmanFord(G, gsrc); + + return 0; +} From b005e56a84c8876990ec0414bbe735d9c8d445a2 Mon Sep 17 00:00:00 2001 From: /Charchit_Gangwar/ <114564628+ATOMworkplace@users.noreply.github.com> Date: Tue, 15 Oct 2024 18:12:34 +0530 Subject: [PATCH 2/6] Delete dynamic_programming/bellman_ford.cpp --- dynamic_programming/bellman_ford.cpp | 116 --------------------------- 1 file changed, 116 deletions(-) delete mode 100644 dynamic_programming/bellman_ford.cpp diff --git a/dynamic_programming/bellman_ford.cpp b/dynamic_programming/bellman_ford.cpp deleted file mode 100644 index c96f3fd8eea..00000000000 --- a/dynamic_programming/bellman_ford.cpp +++ /dev/null @@ -1,116 +0,0 @@ -#include -#include - -using namespace std; - -// Wrapper class for storing an edge -class Edge { - public: - int src, dst, weight; -}; - -// Wrapper class for storing a graph -class Graph { - public: - int vertexNum, edgeNum; - Edge *edges; - - // Constructs a graph with V vertices and E edges - 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) { - static int edgeInd = 0; - if (edgeInd < this->edgeNum) { - Edge newEdge; - newEdge.src = src; - newEdge.dst = dst; - newEdge.weight = weight; - this->edges[edgeInd++] = newEdge; - } - } -}; - -// 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 Bellman-Ford.It also detects negative -// weight cycle -void BellmanFord(Graph graph, int src) { - int V = graph.vertexNum; - int E = graph.edgeNum; - int dist[V]; - - // Initialize distances array as INF for all except source - // Intialize source as zero - for (int i = 0; i < V; i++) dist[i] = INT_MAX; - dist[src] = 0; - - // 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++) { - 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]) - dist[v] = dist[u] + w; - } - - // Iterate inner loop once more to check for negative cycle - 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]) { - cout << "Graph contains negative weight cycle. Hence, shortest " - "distance not guaranteed." - << endl; - return; - } - } - - print(dist, V); - - return; -} - -// 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, E); - 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; - G.addEdge(src, dst, weight); - } - cout << "\nEnter source: "; - cin >> gsrc; - BellmanFord(G, gsrc); - - return 0; -} From 46be5901dced44dc3df04547e985039fec1ff8ea Mon Sep 17 00:00:00 2001 From: /Charchit_Gangwar/ <114564628+ATOMworkplace@users.noreply.github.com> Date: Tue, 15 Oct 2024 18:20:38 +0530 Subject: [PATCH 3/6] Create Sieve of Eratosthenes.cpp Created Siever_of_Eratosthenes.cpp --- others/Sieve of Eratosthenes.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 others/Sieve of Eratosthenes.cpp diff --git a/others/Sieve of Eratosthenes.cpp b/others/Sieve of Eratosthenes.cpp new file mode 100644 index 00000000000..329014ea636 --- /dev/null +++ b/others/Sieve of Eratosthenes.cpp @@ -0,0 +1,14 @@ +void sieveoferatosthenes(vectora,ll x){ + bool prime(x+1,true); + + for(ll i=2;i*i<=x;i++){ + if(prime[i]==true){ + for(ll j=i*i;j<=x;j+=i){ + prime[i]=false; + } + } + } + for(ll i=2;i<=x;i++){ + if(prime[i])a.push_back(i); + } +} From 2d7a8ec26c1fa278e3639202bbb1aaff1e9b79f5 Mon Sep 17 00:00:00 2001 From: /Charchit_Gangwar/ <114564628+ATOMworkplace@users.noreply.github.com> Date: Tue, 15 Oct 2024 18:27:14 +0530 Subject: [PATCH 4/6] Delete others/Sieve of Eratosthenes.cpp --- others/Sieve of Eratosthenes.cpp | 14 -------------- 1 file changed, 14 deletions(-) delete mode 100644 others/Sieve of Eratosthenes.cpp diff --git a/others/Sieve of Eratosthenes.cpp b/others/Sieve of Eratosthenes.cpp deleted file mode 100644 index 329014ea636..00000000000 --- a/others/Sieve of Eratosthenes.cpp +++ /dev/null @@ -1,14 +0,0 @@ -void sieveoferatosthenes(vectora,ll x){ - bool prime(x+1,true); - - for(ll i=2;i*i<=x;i++){ - if(prime[i]==true){ - for(ll j=i*i;j<=x;j+=i){ - prime[i]=false; - } - } - } - for(ll i=2;i<=x;i++){ - if(prime[i])a.push_back(i); - } -} From 03a181a25cb7c6b30d49805641102725c5ffb4e3 Mon Sep 17 00:00:00 2001 From: /Charchit_Gangwar/ <114564628+ATOMworkplace@users.noreply.github.com> Date: Tue, 15 Oct 2024 18:27:51 +0530 Subject: [PATCH 5/6] Create sieveoferatosthenes.cpp create code/file for algorithm sieve of Eratosthenes --- others/sieveoferatosthenes.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 others/sieveoferatosthenes.cpp diff --git a/others/sieveoferatosthenes.cpp b/others/sieveoferatosthenes.cpp new file mode 100644 index 00000000000..329014ea636 --- /dev/null +++ b/others/sieveoferatosthenes.cpp @@ -0,0 +1,14 @@ +void sieveoferatosthenes(vectora,ll x){ + bool prime(x+1,true); + + for(ll i=2;i*i<=x;i++){ + if(prime[i]==true){ + for(ll j=i*i;j<=x;j+=i){ + prime[i]=false; + } + } + } + for(ll i=2;i<=x;i++){ + if(prime[i])a.push_back(i); + } +} From 3706345b143d1d513969e5fe8b8e4baea1ba85d5 Mon Sep 17 00:00:00 2001 From: /Charchit_Gangwar/ <114564628+ATOMworkplace@users.noreply.github.com> Date: Tue, 15 Oct 2024 18:53:09 +0530 Subject: [PATCH 6/6] Delete others/sieveoferatosthenes.cpp --- others/sieveoferatosthenes.cpp | 14 -------------- 1 file changed, 14 deletions(-) delete mode 100644 others/sieveoferatosthenes.cpp diff --git a/others/sieveoferatosthenes.cpp b/others/sieveoferatosthenes.cpp deleted file mode 100644 index 329014ea636..00000000000 --- a/others/sieveoferatosthenes.cpp +++ /dev/null @@ -1,14 +0,0 @@ -void sieveoferatosthenes(vectora,ll x){ - bool prime(x+1,true); - - for(ll i=2;i*i<=x;i++){ - if(prime[i]==true){ - for(ll j=i*i;j<=x;j+=i){ - prime[i]=false; - } - } - } - for(ll i=2;i<=x;i++){ - if(prime[i])a.push_back(i); - } -}