forked from WaderManasi/Knowing-DataStructures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |