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

Added OOPS Concepts #110

Open
wants to merge 5 commits into
base: main
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
Empty file.
76 changes: 76 additions & 0 deletions C++ Problem/Graphs/Bellman_ford.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#define ll long long
#define pb push_back
#define vl vector<long long>
#define mll map<long long, long long>
#define pll pair<long long,long long>
#define mod 1000000007
#define fr(c,a,b) for(ll c=a;c<b;c++)
#include<bits/stdc++.h>
using namespace std;

// Bellman Ford Algo is used to find the single source shortest paths in
// both directed and undirected

// But it has some advantages over Dijkstra algo that we can have negative
// edges and can also detect negative cycles as it only takes n-1
// passes to find the shortest path if the values still changes after that
// then negative cycles exist

vector<vector<ll>> edges;
vector<ll> dist;

void BF_Algo(ll source, ll n) {
fr(c, 0, n) {
dist.pb(INT_MAX);
}
dist[source] = 0;
ll cost = 0;

fr(c,0,n-1){
for (auto it : edges) {
ll u = it[1];
ll v = it[2];
ll w = it[0];
dist[v]=min(dist[v],dist[u]+w);
}
}

fr(c,0,n){
if(dist[c]!=INT_MAX){
cout<<dist[c]<<" ";
}else{
cout<<-1<<endl;
}
}

}

void solve() {
ll n, m;
cin >> n >> m;
fr(c, 0, m) {
ll w, u, v;
cin >> w >> u >> v;
edges.pb({w,u,v});
}
BF_Algo(0, n);
}

int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
while (t--) {
solve();
}
return 0;
}

/*
Negative weights are found in various applications of graphs.
For example, instead of paying the cost for a path, we may get some advantage if we follow the path.

Bellman-Ford works better (better than Dijkstra's) for distributed systems. Unlike Dijksra's where we need
to find the minimum value of all vertices, in Bellman-Ford, edges are considered one by one.
*/
99 changes: 99 additions & 0 deletions C++ Problem/Graphs/Bipartite_graph.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#define ll long long
#define pb push_back
#define mod 1000000007
#define fr(c,a,b) for(ll c=a;c<b;c++)
#include<bits/stdc++.h>
using namespace std;

class Graph
{
ll V;
list<ll> *adj;
void Colour_check(ll v,bool visited[], ll color);
public:
Graph(ll V);
void addEdge_U(ll v, ll w);
void addEdge_D(ll v, ll w);
bool isBipartite();
};

Graph::Graph(ll V)
{
this->V = V;
adj = new list<ll>[V];
}

void Graph::addEdge_U(ll v, ll w)
{
adj[v].pb(w);
adj[w].pb(v);
}
void Graph::addEdge_D(ll v, ll w)
{
adj[v].pb(w);
}

vector<ll> color;
bool bipart;
void Graph::Colour_check(ll v,bool visited[], ll col)
{
if(color[v]!=-1 && color[v]!=col){
bipart = false;
return;
}color[v]=col;
if(visited[v]){
return;
}
visited[v] = true;
list<ll>::iterator i;
for(i = adj[v].begin(); i != adj[v].end(); ++i)
{
Colour_check(*i, visited, (col^1));
}

}

bool Graph::isBipartite()
{
bipart = true;
bool *visited = new bool[V];
color = vector<ll> (V,-1);
fr(c,1,V){
visited[c]=false;
}
fr(c,1,V){
if(!visited[c]){
Colour_check(c, visited, 0);
}
}
return bipart;
}

void solve()
{
Graph g(9);
g.addEdge_U(1,2);
g.addEdge_U(2,3);
g.addEdge_U(3,4);
g.addEdge_U(4,5);
g.addEdge_U(5,6);
g.addEdge_U(6,7);
g.addEdge_U(7,8);
g.addEdge_U(8,1);
if(g.isBipartite()){
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}

}

int main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int t=1;
while(t--)
{
solve();
}
}
Loading