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 graph conceptual problems by DevT75 #562

Open
wants to merge 1 commit 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
63 changes: 63 additions & 0 deletions Graph/reviseGraph.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <bits/stdc++.h>
using namespace std;
void dfs(int source,vector<int> adj[],vector<bool>& visited){
cout << source << " ";
visited[source] = true;
for(auto it = adj[source].begin();it != adj[source].end();it++){
if(!visited[*it]){
dfs(*it,adj,visited);
}
}
}
void bfs(int source,vector<int> adj[],vector<bool>& visited){
queue<int> q;
q.push(source);
while(!q.empty()){
int temp = q.front();
cout << temp << " ";
q.pop();
for(auto it = adj[temp].begin();it != adj[temp].end(); it++){
if(!visited[*it]){
visited[*it] = true;
q.push(*it);
}
}
}
}

int main(){
int v,e;
cin >> v >> e;
// 1-based indexing
vector<int> adj[v + 1];
for(int i = 0;i < e;i++){
// Undirected graph
int a,b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
// for(int i = 0;i < v + 1;i++){
// cout << i << " - ";
// for(auto it = adj[i].begin();it != adj[i].end();it++){
// cout << *it << " ";
// }
// cout << "\n";
// }
vector<bool> visited(v + 1,false);
int source = 1;
// dfs(source,adj,visited);
// bfs(source,adj,visited);

// Number of Components
// The number of times dfs is called the number of components
int count = 0;
for(int i = 1;i <= v;i++){
if(!visited[1]) {
dfs(i,adj,visited);
count++;
};
}
cout << endl << count << endl;
return 0;
}
61 changes: 61 additions & 0 deletions Graph/reviseGraph1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
class Solution{
public:
bool detectCycleBfs(vector<int> adj[],int src,vector<bool>&vis){
queue<pair<int,int>> q;
q.push({src,-1});
vis[src] = true;
while(!q.empty()){
int dest = q.front().first;
int par = q.front().second;
q.pop();
for(auto i : adj[dest]){
if(!vis[i]){
vis[i] = true;
q.push({i,dest});
}
else{
if(par != i) return true;
}
}
}
return false;
}
bool detectCycleDfs(vector<int>adj[],int src,int par,vector<bool>&vis){
vis[src] = true;
for(auto i : adj[src]){
if(!vis[i]){
if(detectCycleDfs(adj,i,src,vis)) return true;
}
else if(par != i) return true;
}
return false;
}
bool isCycle(vector<int> adj[],int v){
vector<bool> vis(v + 1,false);
for(int i = 0;i <= v;i++){
if(!vis[i] && detectCycleBfs(adj,i,vis)) return true;
}
return false;
}
};


int main(){
int v,e;
cin >> v >> e;
vector<int> adj[v + 1];
for(int i = 0;i < e;i++){
int a,b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
Solution s;
cout << (s.isCycle(adj,v) ? "true" : "false");
return 0;
}

46 changes: 46 additions & 0 deletions Graph/reviseGraph2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <iostream>
#include <vector>
using namespace std;
int dfs(int n,int src,vector<int> adj[],vector<bool>&vis,vector<int>&ans){
vis[src] = true;
for(auto i : adj[src]){
if(!vis[i]) n = dfs(n,i,adj,vis,ans);
}
ans[n] = src;
return n-1;
}
void topsort(int v,vector<int> adj[],vector<bool> &vis){
vector<int> ans(v);
int n = v - 1;
// Unoptimized version
// for(int i = 1;i <= v;i++){
// if(!vis[i]){
// vector<int> visited;
// dfs(i,adj,vis,visited);
// for(auto visi : visited){
// ans[n] = visi;
// n--;
// }
// }
// }
// Optimized Version
for(int i = 1;i <= v;i++){
if(!vis[i]){
n = dfs(n,i,adj,vis,ans);
}
}
for(auto i : ans) cout << i << " ";
}
int main(){
int v,e;
cin >> v >> e;
vector<int> adj[v + 1];
vector<bool> vis(v + 1,false);
for(int i = 0;i < e;i++){
int a,b;
cin >> a >> b;
adj[a].push_back(b);
}
topsort(v,adj,vis);
return 0;
}
46 changes: 46 additions & 0 deletions Graph/reviseGraph3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
// Kahn's Algorithm for Topological Ordering

void topsort(int v,vector<int> adj[],vector<bool> &vis){
vector<int> inDegree(v + 1,0);
for(int i = 0;i < v; i++){
for(auto j : adj[i]){
inDegree[j]++;
}
}
queue<int> nodes;
for(int i = 0;i < v;i++){
if(inDegree[i] == 0) nodes.push(i);
}
vector<int> ans(v);
int i = 0;
while(!nodes.empty()){
int temp = nodes.front();nodes.pop();
ans[i++] = temp;
for(auto node : adj[temp]){
inDegree[node]--;
if(inDegree[node] == 0) nodes.push(node);
}
}
if(i != v){
cout << "Cycle Found\n";
return;
}
for(auto i : ans) cout << i << " ";
}
int main(){
int v,e;
cin >> v >> e;
vector<int> adj[v];
vector<bool> vis(v,false);
for(int i = 0;i < e;i++){
int a,b;
cin >> a >> b;
adj[a].push_back(b);
}
topsort(v,adj,vis);
return 0;
}
64 changes: 64 additions & 0 deletions Graph/reviseGraph4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <iostream>
#include <vector>
#include <queue>
#include <climits>
using namespace std;
// Kahn's Algorithm for Topological Ordering

vector<int> topsort(int v,vector<pair<int,int>> adj[],vector<bool> &vis){
vector<int> inDegree(v,0);
for(int i = 0;i < v; i++){
for(auto j : adj[i]){
inDegree[j.first]++;
}
}
queue<int> nodes;
for(int i = 0;i < v;i++){
if(inDegree[i] == 0) nodes.push(i);
}
vector<int> ans(v);
int i = 0;
while(!nodes.empty()){
int temp = nodes.front();nodes.pop();
ans[i++] = temp;
for(auto node : adj[temp]){
inDegree[node.first]--;
if(inDegree[node.first] == 0) nodes.push(node.first);
}
}
return ans;
}

// Shortest/Longest Path on a DAG
// for longest path we just need to multiply all the edge values with
// negative one and then find the minimum distanve that way
// and return the ans after multiplying with negative one

void sssp_dag(int src,int v,vector<pair<int,int>> adj[],vector<int>&ts,vector<int> &dist){
dist[src] = 0;
for(int i = 0;i < v;i++){
int currNode = ts[i];
int d = dist[currNode];
for(auto node : adj[currNode]){
dist[node.first] = min(dist[node.first],d + node.second);
}
}
}
int main(){
int v,e;
cin >> v >> e;
vector<pair<int,int>> adj[v];
vector<bool> vis(v,false);
for(int i = 0;i < e;i++){
int a,b,w;
cin >> a >> b >> w;
adj[a].push_back({ b, w });
}
auto ans = topsort(v,adj,vis);
vector<int> dist(v,INT_MAX);
for(auto i : ans) cout << char('A' + i) << " ";
sssp_dag(0,v,adj,ans,dist);
cout << "\n";
for(auto i : ans) cout << dist[i] << " ";
return 0;
}
Loading