Skip to content

Commit

Permalink
Added content for CP class 7
Browse files Browse the repository at this point in the history
  • Loading branch information
embiway committed Oct 31, 2021
1 parent 964d090 commit d799e84
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 0 deletions.
82 changes: 82 additions & 0 deletions CompetitiveProgramming/2021_10_31_CPClass-7/Codes/bfs.cpp
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);
}
54 changes: 54 additions & 0 deletions CompetitiveProgramming/2021_10_31_CPClass-7/Codes/dfs.cpp
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.
19 changes: 19 additions & 0 deletions CompetitiveProgramming/2021_10_31_CPClass-7/README.md
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)
1 change: 1 addition & 0 deletions CompetitiveProgramming/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- [Class 4 (Problems on Dynamic Programming): held on August 15, 2021](2021_08_15_CPClass-4)
- [Class 5 (DP tricks and Bitmask DP): held on August 21, 2021](2021_08_21_CPClass-5)
- [Class 6 (SOS DP and Intro to Graphs): held on October 27, 2021](2021_10_27_CPClass-6)
- [Class 7 (Graph Traversals Implementation Details): held on October 31, 2021](2021_10_31_CPClass-7)

## Reading Resources

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ If you are a part of MNNIT join us on Microsoft Team [MNNIT CC Queries Official]
- [Competitive Programming Class-4](CompetitiveProgramming/2021_08_15_CPClass-4)
- [Competitive Programming Class-5](CompetitiveProgramming/2021_08_21_CPClass-5)
- [Competitive Programming Class-6](CompetitiveProgramming/2021_10_27_CPClass-6)
- [Competitive Programming Class-7](CompetitiveProgramming/2021_10_31_CPClass-7)

- [Git/GitHub classes](Git-GitHub)
- [Git Class-1](Git-GitHub/2021_04_20_GitClass-1)
Expand Down

0 comments on commit d799e84

Please sign in to comment.