Skip to content

Commit

Permalink
added DFS finding bridges
Browse files Browse the repository at this point in the history
  • Loading branch information
kothariji committed Sep 30, 2020
1 parent a868325 commit bae7f06
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Graph/DFS (Depth First Search).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <bits/stdc++.h>
#define lli long long int
#define endl "\n"
using namespace std;

bool visited[105]; //by default it is initialised with false;
vector <int> v1[105];


void DFS(int vertex)
{
if(visited[vertex] == false)
{
cout<<vertex<<" ";
visited[vertex] = true;
for(int j = 0; j < v1[vertex].size(); j++)
{
DFS(v1[vertex][j]);
}
}
return;
}

int main()
{
int no_of_vertex, no_of_edges;
cout<<"Enter the number of vertices: ";
cin>>no_of_vertex;


memset(visited, false, sizeof(visited));

int i, j;
cout<<"Enter the number of edges: ";
cin>>no_of_edges;

for(int edge = 1; edge <= no_of_edges; edge++)
{
cout<<"Enter the vertices for edge "<<edge<<": ";
cin>>i>>j;
v1[i].push_back(j);
v1[j].push_back(i);
}

cout<<"DFS: ";
for(i = 1; i <= no_of_vertex; i++)
DFS(i);
return 0;
}
57 changes: 57 additions & 0 deletions Graph/Finding Bridges.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <bits/stdc++.h>
#define lli long long int
#define endl "\n"
#define MAX 105
using namespace std;


vector <int> v1[MAX];
vector <bool> visited(MAX, false);
vector <int> in(MAX, 0);
vector <int> low(MAX, 0);
int timer;

void DFS(int i, int par)
{
visited[i] = true;
in[i] = timer;
low[i] = timer;
timer++;
for(int j =0; j<v1[i].size(); j++)
{
if(v1[i][j] == par)
{
//that means its his parent, so its not the back edge
continue;
}
if(visited[v1[i][j]] == true)
{
//its a backedge
low[i] = min(low[i], in[v1[i][j]]);
}
else
{
//forward edge...we will call dfs
DFS(v1[i][j], i);
if(low[v1[i][j]] > in[i])
cout<<"Found a bridge: "<<i<<" -> "<<v1[i][j]<<endl;
low[i] = min(low[i], low[v1[i][j]]);
}
}
}

int main()
{
int v, e, x, y;
cin>>v>>e;
timer = 0;
for(int i = 0; i<e; i++)
{
cin>>x>>y;
v1[x].push_back(y);
v1[y].push_back(x);
}

DFS(1,-1);

}
48 changes: 48 additions & 0 deletions Graph/In Out Time of Nodes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <bits/stdc++.h>
#define lli long long int
#define endl "\n"
using namespace std;

bool visited[105];
vector <int> v1[105];
vector <int> timein(105, 0);
vector <int> timeout(105, 0);
int counter;

void dfs(int i)
{
visited[i] = true;
timein[i] = ++counter;
for(int j = 0; j<v1[i].size(); j++)
{
if(visited[v1[i][j]] == false)
dfs(v1[i][j]);
}
timeout[i] = ++counter;
}

int main()
{
counter = 0;
int v, e, x, y;
cout<<"Enter the number of vertices and edges: ";
cin>>v>>e;
for(int i = 0 ; i<e; i++)
{
cin>>x>>y;
v1[x].push_back(y);
v1[y].push_back(x);
}


dfs(1);

for(int i = 1; i<=v; i++)
cout<<timein[i]<<" ";
cout<<endl;

for(int i = 1; i<=v; i++)
cout<<timeout[i]<<" ";
cout<<endl;

}
43 changes: 43 additions & 0 deletions Graph/Subtree size using DFS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <bits/stdc++.h>
#define lli long long int
#define endl "\n"
#define MAX 105
using namespace std;

vector <int> v1[MAX];
vector <int> subSize(MAX, 0);
vector <bool> visited(MAX, false);

int DFS(int i)
{
visited[i] = true;
int count = 1;
for(int j = 0; j<v1[i].size(); j++)
{
if(visited[v1[i][j]] == false)
{
count += DFS(v1[i][j]);
}
}
subSize[i] = count;
return subSize[i];
}


int main()
{
int v, e, x, y;
cout<<"Enter the number of vertices and edges: ";
cin>>v>>e;
for(int i =0 ; i<e; i++)
{
cin>>x>>y;
v1[x].push_back(y);
v1[y].push_back(x);
}

int ans = DFS(1);

for(int i = 1; i<=v; i++)
cout<<i<<": "<<subSize[i]<<endl;
}

0 comments on commit bae7f06

Please sign in to comment.