Skip to content

Commit d5de21f

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents bc08f85 + bc8a574 commit d5de21f

14 files changed

Lines changed: 423 additions & 3 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class ArticulationPoints{
2+
private static int time;
3+
private static int[] disc, low;
4+
private static boolean[] visited;
5+
6+
public static boolean[] articulationPoints(int[][] graph){
7+
int NumberOfNodes = graph.length;
8+
9+
time = -1;
10+
disc = new int[NumberOfNodes];
11+
low = new int[NumberOfNodes];
12+
visited = new boolean[NumberOfNodes];
13+
java.util.Arrays.fill(low, 2*NumberOfNodes);
14+
15+
boolean[] isAP = new boolean[NumberOfNodes];
16+
for(int i = 0; i< NumberOfNodes; i++)if(!visited[i] && graph[i] != null)apUtil(graph, isAP, i, -1);
17+
return isAP;
18+
}
19+
20+
private static void apUtil(int[][] g, boolean[] isAP, int u, int p){
21+
visited[u] = true;
22+
disc[u] = low[u] = ++time;
23+
24+
int childCount = 0;
25+
for(int v:g[u]){
26+
if(!visited[v]){
27+
apUtil(g, isAP, v, u);
28+
29+
low[u] = Math.min(low[u], low[v]);
30+
childCount++;
31+
32+
if(p != -1 && low[v] >= disc[u])isAP[u] = true;
33+
}else if(v != p)low[u] = Math.min(low[u], disc[v]);
34+
}
35+
if(p == -1 && childCount > 1)isAP[u] = true;
36+
}
37+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class ArticulationPointsUsage{
2+
//Usage example ArticulationPoint.articulationPoints()
3+
public static void articulationPointsUsage(){
4+
int N = 6, E = 6;
5+
int[] from = new int[]{0,1,1,2,2,4};
6+
int[] to = new int[]{5,4,5,3,4,5};
7+
8+
int[][] graph = GraphBuilder.makeGraph(N, E, from, to, true);
9+
boolean[] articulationPoint = ArticulationPoints.articulationPoints(graph);
10+
11+
for(int i = 0; i< N; i++){
12+
if(articulationPoint[i])
13+
System.out.println(i+" is an articulation point");
14+
}
15+
}
16+
17+
public static void main(String[] args){
18+
articulationPointsUsage();
19+
}
20+
}

‎ArticulationPoint/README.md‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Class ArticulationPoint
2+
3+
Given a graph generated using GraphBuilder.makeGraph(), this class finds all articulation points in graph. Since GraphBuilder assumes 0-based indexing, this class also works assuming 0-based indexing.
4+
5+
All members of this class are static, so no constructor defined.
6+
7+
## Methods
8+
9+
### articulationPoints
10+
```
11+
public static boolean[] articulationPoints(int[][] graph)
12+
```
13+
Returns boolean array of length $N$ where $N$ denotes the number of nodes in given graph, i-th element in this boolean array denotes whether i-th node in given graph is an articulation point or not.
14+
15+
** Constraints **
16+
* graph is generated using GraphBuilder.makeGraph(), so all constraints applicable to makeGraph method applies.
17+
18+
** Computational complexity **
19+
* $ O (NumberOfNodes + NumberOfEdges) $

‎Bridge/Bridge.java‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Bridge{
2+
private static int time;
3+
private static int[] disc, low;
4+
private static boolean[] visited;
5+
6+
public static boolean[] bridges(int[][][] graphWithEdgeInfo){
7+
int edgeCount = 0;
8+
for(int[][] row:graphWithEdgeInfo)edgeCount += row.length;
9+
return bridges(graphWithEdgeInfo, edgeCount/2);
10+
}
11+
12+
public static boolean[] bridges(int[][][] graphWithEdgeInfo, int edgeCount){
13+
int NumberOfNodes = graphWithEdgeInfo.length;
14+
15+
time = -1;
16+
disc = new int[NumberOfNodes];
17+
low = new int[NumberOfNodes];
18+
visited = new boolean[NumberOfNodes];
19+
java.util.Arrays.fill(low, 2*NumberOfNodes);
20+
21+
boolean[] isBridge = new boolean[edgeCount];
22+
for(int i = 0; i< NumberOfNodes; i++)if(!visited[i] && graphWithEdgeInfo[i] != null)bridgeUtil(graphWithEdgeInfo, isBridge, i, -1);
23+
return isBridge;
24+
}
25+
26+
private static void bridgeUtil(int[][][] g, boolean[] isBridge, int u, int parentEdge){
27+
visited[u] = true;
28+
disc[u] = low[u] = ++time;
29+
30+
for(int[] edge: g[u]){
31+
int v = edge[0], edgeIndex = edge[1];
32+
if(parentEdge == edgeIndex)continue;
33+
34+
if(visited[v])
35+
low[u] = Math.min(low[u], disc[v]);
36+
else{
37+
bridgeUtil(g, isBridge, v, edgeIndex);
38+
low[u] = Math.min(low[u], low[v]);
39+
40+
if(low[v] > disc[u])isBridge[edgeIndex] = true;
41+
}
42+
}
43+
}
44+
}

‎Bridge/BridgeUsage.java‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class BridgeUsage{
2+
//Usage Example Bridge.bridges()
3+
public static void bridgesUsage(){
4+
int N = 6, E = 9;
5+
int[] from = new int[]{0,1,1,2,2,4,4,3,1};
6+
int[] to = new int[]{5,4,5,3,4,5,4,2,4};
7+
8+
int[][][] graph = GraphBuilder.makeGraphWithEdgeInfo(N, E, from, to, true);
9+
boolean[] isBridge = Bridge.bridges(graph);
10+
11+
for(int i = 0; i< E; i++){
12+
if(isBridge[i])
13+
System.out.println(from[i] + " - " + to[i] + " is a bridge");
14+
}
15+
}
16+
17+
public static void main(String[] args){
18+
bridgesUsage();
19+
}
20+
}

‎Bridge/README.md‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Class Bridge
2+
3+
Given an undirected graph generated using GraphBuilder.makeGraphWithEdgeInfo(), this class finds all bridges in graph. Since GraphBuilder assumes 0-based indexing, this class also works assuming 0-based indexing. This class can handle multi-edges as well as self loops.
4+
5+
All members of this class are static, so no constructor defined.
6+
7+
## Methods
8+
9+
### bridges
10+
```
11+
public static boolean[] bridges(int[][][] graphWithEdgeInfo, int edgeCount)
12+
```
13+
Returns boolean array of length $edgeCount$ where $EdgeCount$ denotes the number of edges in given graph, i-th element in this boolean array denotes whether i-th edge in given graph is a bridge or not. The edges are numbered as per their order in input, defined in GraphBuilder.makeGraphWithEdgeInfo()
14+
15+
** Constraints **
16+
* graph is generated using GraphBuilder.makeGraphWithEdgeInfo(), so all constraints applicable to makeGraphWithEdgeInfo method applies.
17+
* edgeCount = Number of edges specified in GraphBuilder.makeGraphWithEdgeInfo()
18+
19+
** Computational complexity **
20+
* $ O (NumberOfNodes + NumberOfEdges) $
21+
22+
23+
```
24+
public static boolean[] bridges(int[][][] graphWithEdgeInfo)
25+
```
26+
This method computed the number of edges in specified graph, and invokes ```boolean[] bridges(int[][][] graphWithEdgeInfo, int edgeCount)``` to return boolean array, i-th element in this boolean array denotes whether i-th edge in given graph is a bridge or not.
27+
28+
** Constraints **
29+
* graph is generated using GraphBuilder.makeGraphWithEdgeInfo(), so all constraints applicable to makeGraphWithEdgeInfo method applies.
30+
31+
** Computational complexity **
32+
* $ O (NumberOfNodes + NumberOfEdges) $

‎FenwickTree/FenwickTree.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ public FenwickTree(long[] data) {
1616
build(data);
1717
}
1818

19+
public void set(int p, long x){
20+
add(p, x - get(p));
21+
}
22+
1923
public void add(int p, long x){
2024
assert(0<=p && p<_n);
2125
p++;
@@ -29,6 +33,10 @@ public long sum(int l, int r){
2933
return sum(r)-sum(l);
3034
}
3135

36+
public long get(int p){
37+
return sum(p, p+1);
38+
}
39+
3240
private long sum(int r){
3341
long s = 0;
3442
while(r>0){

‎FenwickTree/README.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ public void add(int p, long x)
3232
配列の第$p$要素に$x$を加える. すなわち, `a[p] += x` のこと.
3333
計算量: $O(\log N)$
3434

35+
### set
36+
```java
37+
public void set(int p, long x)
38+
```
39+
配列の第$p$要素を$x$に変更する. すなわち, `a[p] = x` のこと.
40+
計算量: $O(\log N)$
41+
42+
### get
43+
```java
44+
public long get(int p)
45+
```
46+
配列の第$p$要素を取得する. 計算量: $O(\log n)$
47+
3548
### sum
3649
```java
3750
public long sum(int l, int r)

‎GraphBuilder/GraphBuilder.java‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class GraphBuilder{
2+
public static int[][] makeGraph(int NumberOfNodes, int NumberOfEdges, int[] from, int[] to, boolean undirected){
3+
int[][] graph = new int[NumberOfNodes][];
4+
int[] outdegree = new int[NumberOfNodes];
5+
for(int i = 0; i< NumberOfEdges; i++){
6+
outdegree[from[i]]++;
7+
if(undirected)outdegree[to[i]]++;
8+
}
9+
for(int i = 0; i< NumberOfNodes; i++)graph[i] = new int[outdegree[i]];
10+
for(int i = 0; i< NumberOfEdges; i++){
11+
graph[from[i]][--outdegree[from[i]]] = to[i];
12+
if(undirected)graph[to[i]][--outdegree[to[i]]] = from[i];
13+
}
14+
return graph;
15+
}
16+
17+
public static int[][][] makeGraphWithEdgeInfo(int NumberOfNodes, int NumberOfEdges, int[] from, int[] to, boolean undirected){
18+
int[][][] graph = new int[NumberOfNodes][][];
19+
int[] outdegree = new int[NumberOfNodes];
20+
for(int i = 0; i< NumberOfEdges; i++){
21+
outdegree[from[i]]++;
22+
if(undirected)outdegree[to[i]]++;
23+
}
24+
for(int i = 0; i< NumberOfNodes; i++)graph[i] = new int[outdegree[i]][];
25+
for(int i = 0; i< NumberOfEdges; i++){
26+
graph[from[i]][--outdegree[from[i]]] = new int[]{to[i], i, 0};
27+
if(undirected)graph[to[i]][--outdegree[to[i]]] = new int[]{from[i], i, 1};
28+
}
29+
return graph;
30+
}
31+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class GraphBuilderUsage{
2+
//Usage Exmaple
3+
public static void makeGraphUsage(){
4+
int N = 6, E = 8;
5+
int[] from = new int[]{0,0,0,1,1,2,2,4};
6+
int[] to = new int[]{2,4,5,4,5,3,4,5};
7+
8+
int[][] graph = GraphBuilder.makeGraph(N, E, from, to, true);
9+
for(int i = 0; i< N; i++){
10+
System.out.print(i+": ");
11+
for(int j:graph[i])System.out.print(j+" ");
12+
System.out.println();
13+
}
14+
}
15+
16+
//Usage Example
17+
public static void makeGraphWithEdgeInfoUsage(){
18+
int N = 6, E = 8;
19+
int[] from = new int[]{0,0,0,1,1,2,2,4};
20+
int[] to = new int[]{2,4,5,4,5,3,4,5};
21+
22+
int[][][] graph = GraphBuilder.makeGraphWithEdgeInfo(N, E, from, to, true);
23+
for(int i = 0; i< N; i++){
24+
System.out.print(i+": ");
25+
for(int[] j:graph[i])System.out.print("["+j[0]+", "+j[1]+", "+j[2]+"],");
26+
System.out.println();
27+
}
28+
}
29+
30+
public static void main(String[] args){
31+
makeGraphUsage();
32+
makeGraphWithEdgeInfoUsage();
33+
}
34+
}

0 commit comments

Comments
 (0)