Skip to content

Commit e0e2724

Browse files
Migrated Graph-2 bundle
1 parent ac2bbcc commit e0e2724

6 files changed

Lines changed: 86 additions & 0 deletions

File tree

docs/graph/bipartite-checking.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
title: Bipartite Checking
3+
tags:
4+
- Bipartite Checking
5+
- Graph
6+
---
7+
8+
The question is in the title. Is the given graph bipartite? We can use BFS or DFS on graph. Lets first focus on BFS related algorithm. This procedure is very similar to BFS, we have an extra color array and we assign a color to each vertex when we are traversing the graph. Algorithm proof depends on fact that BFS explores the graph level by level. If the graph contains an odd cycle it means that there must be a edge between two vertices that are in same depth (layer, proof can be found on [[1 - Algorithm Design, Kleinberg, Tardos]]()). Let's say the colors are red and black and we traverse the graph with BFS and assign red to odd layers and black to even layers. Then we check the edges to see if there exists an edge that its vertices are same color. If there is a such edge, the graph is not bipartite, else the graph is bipartite.
9+
10+
<figure markdown="span">
11+
![If two nodes x and y in the same layer are joined by an edge, then the cycle through x, y, and their lowest common ancestor z has odd length, demonstrating that the graph cannot be bipartite.](img/bipartite_check.png)
12+
<figcaption>If two nodes x and y in the same layer are joined by an edge, then the cycle through x, y, and their lowest common ancestor z has odd length, demonstrating that the graph cannot be bipartite.</figcaption>
13+
</figure>
14+
15+
```cpp
16+
typedef vector<int> adjList;
17+
typedef vector<adjList> graph;
18+
typedef pair<int,int> ii;
19+
enum COLOR {RED, GREEN};
20+
bool bipartite_check(graph &g){
21+
int root = 0; // Pick 0 indexed node as root.
22+
vector<bool> visited(g.size(),false);
23+
vector<int> Color(g.size(),0);
24+
queue<ii> Q( { {root,0}} ); // insert root to queue, it is first layer_0
25+
visited[root] = true;
26+
Color[root] = RED;
27+
while ( !Q.empty() )
28+
{
29+
/*top.first is node, top.second its depth i.e layer */
30+
auto top = Q.front();
31+
Q.pop();
32+
for (int u : g[top.first]){
33+
if ( !visited[u] ){
34+
visited[u] = true;
35+
//Mark even layers to red, odd layers to green
36+
Color[u] = (top.second+1) % 2 == 0 ? RED : GREEN;
37+
Q.push({u, top.second+1 });
38+
}
39+
}
40+
}
41+
for(int i=0; i < g.size(); ++i){
42+
for( auto v: g[i]){
43+
if ( Color[i] == Color[v] ) return false;
44+
}
45+
}
46+
return true;
47+
}
48+
int main(){
49+
graph g(3);
50+
g[0].push_back(1);
51+
g[1].push_back(2);
52+
g[2].push_back(3);
53+
cout << (bipartite_check(g) == true ? "YES" : "NO") << endl;
54+
return 0;
55+
}
56+
```
57+
58+
The complexity of algorithm is is $O(V + E) + O(E) $, BFS and loop over edges. But we can say it $O(V+E)$ since it is Big-O notation.

docs/graph/definitions.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,31 @@ tags:
3232
- an undirected graph is connected and has no cycles. an undirected graph is acyclic, and a simple cycle is formed if any edge is added to the graph.
3333
- an undirected graph is connected, it will become disconnected if any edge is removed.
3434
- an undirected graph is connected, and has (number of nodes - 1) edges.
35+
36+
### Bipartite Graphs
37+
38+
A bipartite graph is a graph whose vertices can be divided into two disjoint and independent sets U and V such that every edge connects a vertex in U to one in V. Vertex sets U and V are usually called the parts of the graph. [[1]](https://en.wikipedia.org/wiki/Bipartite\_graph). The figure is shown in below. It is similar to graph coloring with two colors. Coloring graph with two colors is that every vertex have a corresponding color, and for any edge, it's vertices should be different color. In other words, if we can color neighbours two different colors, we can say that graph is bipartite.
39+
40+
<figure markdown="span">
41+
![Example bipartite graph, all edges satisfy the coloring constraint](img/bipartite.png)
42+
<figcaption>Example bipartite graph, all edges satisfy the coloring constraint</figcaption>
43+
</figure>
44+
45+
We have some observations here.
46+
- A graph 2- colorable if and only if it is bipartite.
47+
- A graph does not contain odd-length cycle if and only if it is bipartite.
48+
- Every tree is a bipartite graph since trees do not contain any cycles.
49+
50+
### Directed Acyclic Graphs
51+
52+
A directed acyclic graph(DAG) is a finite directed graph with no directed cycles. Equivalently, a DAG is a directed graph that has a topological ordering (we cover it in this bundle), a sequence of the vertices such that every edge is directed from earlier to later in the sequence [[2]](https://en.wikipedia.org/wiki/Directed_acyclic_graph). DAGs can be used to encode precedence relations or dependencies in a natural way [[3 - Algorithm Design, Kleinberg, Tardos]](). There are several applications using topological ordering directly such as finding critical path or automatic differentiation on computational graphs (this is extremely useful for deep learning frameworks [[4]](https://pytorch.org/docs/stable/autograd.html)).
53+
54+
<figure markdown="span">
55+
![Example Directed Acyclic Graphs](img/dag.png)
56+
<figcaption>Example Directed Acyclic Graphs</figcaption>
57+
</figure>
58+
59+
<figure markdown="span" style="width: 64%">
60+
![Example computational graph also a DAG, partial derivatives are written to edges respect to topological order](img/tree-def.png)
61+
<figcaption>Example computational graph also a DAG, partial derivatives are written to edges respect to topological order</figcaption>
62+
</figure>

docs/graph/img/bipartite.png

18.1 KB
Loading

docs/graph/img/bipartite_check.png

25 KB
Loading

docs/graph/img/dag.png

19.4 KB
Loading

docs/graph/img/tree-def.png

96.3 KB
Loading

0 commit comments

Comments
 (0)