Skip to content

Commit 8390810

Browse files
committed
[Function add]
1. Add leetcode solutions with tag graph.
1 parent b60b914 commit 8390810

File tree

2 files changed

+169
-0
lines changed

2 files changed

+169
-0
lines changed

leetcode/684. Redundant Connection.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
## 684. Redundant Connection
2+
3+
### Question:
4+
In this problem, a tree is an undirected graph that is connected and has no cycles.
5+
6+
The given input is a graph that started as a tree with N nodes (with distinct values 1, 2, ..., N), with one additional edge added. The added edge has two different vertices chosen from 1 to N, and was not an edge that already existed.
7+
8+
The resulting graph is given as a 2D-array of edges. Each element of edges is a pair [u, v] with u < v, that represents an undirected edge connecting nodes u and v.
9+
10+
Return an edge that can be removed so that the resulting graph is a tree of N nodes. If there are multiple answers, return the answer that occurs last in the given 2D-array. The answer edge [u, v] should be in the same format, with u < v.
11+
12+
```
13+
Example 1:
14+
15+
Input: [[1,2], [1,3], [2,3]]
16+
Output: [2,3]
17+
Explanation: The given undirected graph will be like this:
18+
1
19+
/ \
20+
2 - 3
21+
22+
Example 2:
23+
24+
Input: [[1,2], [2,3], [3,4], [1,4], [1,5]]
25+
Output: [1,4]
26+
Explanation: The given undirected graph will be like this:
27+
5 - 1 - 2
28+
| |
29+
4 - 3
30+
```
31+
32+
Note:
33+
* The size of the input 2D-array will be between 3 and 1000.
34+
* Every integer represented in the 2D-array will be between 1 and N, where N is the size of the input array.
35+
36+
### Solution:
37+
* Method 1: dfs
38+
* how to find a cycle?
39+
* before we add current path (u, v) to the graph, we can already find the path from u to v, so after adding current edge, there exists a cycle.
40+
```Java
41+
class Solution {
42+
private Map<Integer, List<Integer>> graph;
43+
public int[] findRedundantConnection(int[][] edges) {
44+
graph = new HashMap<>();
45+
for(int[] edge : edges){
46+
Set<Integer> visited = new HashSet<>();
47+
if(hasPath(edge[0], edge[1], visited))
48+
return edge;
49+
List<Integer> temp = graph.containsKey(edge[0]) ? graph.get(edge[0]): new ArrayList<>();
50+
temp.add(edge[1]);
51+
graph.put(edge[0], temp);
52+
temp = graph.containsKey(edge[1]) ? graph.get(edge[1]): new ArrayList<>();
53+
temp.add(edge[0]);
54+
graph.put(edge[1], temp);
55+
}
56+
return new int[]{-1, -1};
57+
}
58+
private boolean hasPath(int u, int v, Set<Integer> visited){
59+
if(u == v) return true;
60+
visited.add(u);
61+
if(!graph.containsKey(u) || !graph.containsKey(v)) return false;
62+
List<Integer> nexts = graph.get(u);
63+
for(Integer next : nexts){
64+
if(visited.contains(next)) continue;
65+
if(hasPath(next, v, visited)) return true;
66+
}
67+
return false;
68+
}
69+
}
70+
```
71+
72+
* Method 2: Union find set to be added
73+
74+
### Reference
75+
1. [花花酱 LeetCode 684. Redundant Connection](http://zxi.mytechroad.com/blog/tree/leetcode-684-redundant-connection/)

leetcode/785. Is Graph Bipartite.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
## 785. Is Graph Bipartite?
2+
3+
### Question:
4+
Given an undirected graph, return true if and only if it is bipartite.
5+
6+
Recall that a graph is bipartite if we can split it's set of nodes into two independent subsets A and B such that every edge in the graph has one node in A and another node in B.
7+
8+
The graph is given in the following form: graph[i] is a list of indexes j for which the edge between nodes i and j exists. Each node is an integer between 0 and graph.length - 1. There are no self edges or parallel edges: graph[i] does not contain i, and it doesn't contain any element twice.
9+
10+
```
11+
Example 1:
12+
Input: [[1,3], [0,2], [1,3], [0,2]]
13+
Output: true
14+
Explanation:
15+
The graph looks like this:
16+
0----1
17+
| |
18+
| |
19+
3----2
20+
We can divide the vertices into two groups: {0, 2} and {1, 3}.
21+
22+
Example 2:
23+
Input: [[1,2,3], [0,2], [0,1,3], [0,2]]
24+
Output: false
25+
Explanation:
26+
The graph looks like this:
27+
0----1
28+
| \ |
29+
| \ |
30+
3----2
31+
We cannot find a way to divide the set of nodes into two independent subsets.
32+
```
33+
34+
Note:
35+
* graph will have length in range [1, 100].
36+
* graph[i] will contain integers in range [0, graph.length - 1].
37+
* graph[i] will not contain i or duplicate values.
38+
* The graph is undirected: if any element j is in graph[i], then i will be in graph[j].
39+
40+
### Solution:
41+
* Method 1: dfs + bopartite
42+
```Java
43+
class Solution {
44+
private int[][] g;
45+
private int[] visited;
46+
public boolean isBipartite(int[][] graph) {
47+
g = graph;
48+
visited = new int[graph.length];
49+
for(int i = 0; i < graph.length; i++){
50+
if(visited[i] == 0){
51+
if(!dfs(i, 1)) return false;
52+
}
53+
}
54+
return true;
55+
}
56+
private boolean dfs(int node, int color){
57+
if(visited[node] == -color) return false;
58+
else if(visited[node] == color) return true;
59+
visited[node] = color;
60+
int[] neighbours = g[node];
61+
for(Integer neighbour : neighbours){
62+
if(!dfs(neighbour, -color)) return false;
63+
}
64+
return true;
65+
}
66+
}
67+
```
68+
69+
* Method 2: bfs
70+
```Java
71+
class Solution {
72+
public boolean isBipartite(int[][] graph) {
73+
int[] visited = new int[graph.length];
74+
Queue<Integer> queue = new LinkedList<>();
75+
for(int i = 0; i < graph.length; i++){
76+
if(visited[i] == 0){
77+
visited[i] = 1;
78+
queue.offer(i);
79+
while(!queue.isEmpty()){
80+
int cur = queue.poll();
81+
for(Integer next : graph[cur]){
82+
if(visited[next] == visited[cur]) return false;
83+
else if(visited[next] == 0){
84+
visited[next] = -visited[cur];
85+
queue.offer(next);
86+
}
87+
}
88+
}
89+
}
90+
}
91+
return true;
92+
}
93+
}
94+
```

0 commit comments

Comments
 (0)