-
Notifications
You must be signed in to change notification settings - Fork 60
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
6 changed files
with
157 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,82 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
// adjacency list of unweighted graph | ||
vector<vector<int>> a; | ||
|
||
// adjacency list of weighted graph | ||
vector<vector<pair<int, int>>> adj; | ||
|
||
vector<bool> used; | ||
vector<int> d , p; | ||
|
||
// BFS for any graph | ||
void bfs(int s) { // s -> root | ||
queue<int> q; | ||
q.push(s); | ||
used[s] = true; | ||
p[s] = -1; | ||
while (!q.empty()) { | ||
int v = q.front(); | ||
q.pop(); | ||
for (int u : a[v]) { | ||
if (!used[u]) { | ||
used[u] = true; | ||
q.push(u); | ||
d[u] = d[v] + 1; | ||
p[u] = v; | ||
} | ||
else { | ||
if(u = p[v]) { | ||
continue; | ||
} | ||
// graph has cycle | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
|
||
void bfs01(int s) { // s -> root | ||
deque<int> q; | ||
q.push_back(s); | ||
used[s] = true; | ||
while (!q.empty()) { | ||
int v = q.front(); | ||
q.pop_front(); | ||
for (pair<int , int> p : adj[v]) { | ||
int u = p.first; | ||
int w = p.second; | ||
if (!used[u]) { | ||
if(w == 0) { | ||
d[u] = d[v]; | ||
q.push_front(u); | ||
used[u] = true; | ||
} | ||
else { | ||
used[u] = true; | ||
q.push_back(u); | ||
d[u] = d[v] + 1; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
int main() { | ||
int n , m; | ||
cin >> n >> m; | ||
used.resize(n); | ||
a.resize(n); | ||
p.resize(n); | ||
d.resize(n); | ||
adj.resize(n); | ||
for(int i = 0 ; i < m ; i++) { | ||
int x , y , w; | ||
cin >> x >> y >> w; | ||
a[x].push_back(y); | ||
adj[x].push_back({y , w}); | ||
} | ||
|
||
bfs(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,54 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
// adjacency list of unweighted graph | ||
vector<vector<int>> a; | ||
|
||
// adjacency list of weighted graph | ||
vector<vector<pair<int, int>>> adj; | ||
|
||
vector<int> visited; | ||
|
||
// DFS for any graph | ||
void dfs(int s) { | ||
visited[s] = 1; | ||
for(int i = 0 ; i < a[s].size() ; i++) { | ||
if(!visited[a[s][i]]) { | ||
dfs(a[s][i]); | ||
} | ||
} | ||
} | ||
|
||
/* | ||
dfs(1) -> dfs(2) -> dfs(3) -> dfs(2) -> dfs(4) -> dfs(2) -> dfs(1) -> dfs(5) -> dfs(6) -> dfs(5) -> dfs(1) -> main | ||
*/ | ||
|
||
|
||
// Tree DFS. We only to need to make sure that we don't go back to the parent. | ||
void tree_dfs(int s , int p = -1) { | ||
for(int i = 0 ; i < a[s].size() ; i++) { | ||
if(a[s][i] != p) { | ||
tree_dfs(a[s][i] , s); | ||
} | ||
} | ||
} | ||
|
||
int main() { | ||
int n , m; | ||
cin >> n >> m; | ||
visited.resize(n); | ||
a.resize(n); | ||
adj.resize(n); | ||
for(int i = 0 ; i < m ; i++) { | ||
int x , y , w; | ||
cin >> x >> y >> w; | ||
a[x].push_back(y); | ||
adj[x].push_back({y , w}); | ||
} | ||
|
||
for(int i = 0 ; i < n ; i++) { | ||
if(!visited[i]) | ||
dfs(i); | ||
} | ||
tree_dfs(0); | ||
} |
Binary file not shown.
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,19 @@ | ||
# Competitive Programming Class 7 | ||
|
||
## Get the presentation used in class: [Here](./Graph_Traversals.pdf) | ||
|
||
## Get the class recording: [Here](https://drive.google.com/file/d/1p1VladoCu1PHSA6hGY0KMjU0xQiGKP73/view?usp=sharing) | ||
|
||
#### October 31, 2021 | ||
|
||
<hr> | ||
|
||
## Class Coverage | ||
|
||
- Graph Traversals (Implementation Details and problems) | ||
|
||
## Additional Resources | ||
|
||
- [Graph Algorithms](https://codeforces.com/blog/entry/16221) | ||
- [DFS Tutorial](https://www.hackerearth.com/practice/algorithms/graphs/depth-first-search/tutorial/) | ||
- [Bipartite Check](https://cp-algorithms.com/graph/bipartite-check.html) |
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
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