Skip to content

Commit 2b80de4

Browse files
authored
Merge pull request sherxon#16 from prafful1/master
Minimize size of array
2 parents ed1a0ea + 17a2740 commit 2b80de4

4 files changed

Lines changed: 257 additions & 2 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ Here are questions divided by difficulty level:
126126
9) [Is Graph Bipartite](https://github.com/sherxon/AlgoDS/blob/master/src/algo/graph/IsBipartite.java)
127127
10) [Is Graph Connected](https://github.com/sherxon/AlgoDS/blob/master/src/algo/graph/IsConnected.java)
128128
11) [Cycle Detection](https://github.com/sherxon/AlgoDS/blob/master/src/algo/graph/CycleDetection.java)
129+
12) [Undirected Graph Bridge Detection](https://github.com/prafful1/AlgoDS/blob/master/src/algo/graph/BridgeUndirectedGraph.java)
129130

130131
### String
131132
1) [Rabin Karp Subsequence search](https://github.com/sherxon/AlgoDS/blob/master/src/algo/string/RabinKarpSubsequenceSearch.java)

‎src/algo/dp/MaxWeightTransformation.java‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,3 @@ public int maxWeightTransformationNoDPSol(char str1[], int i, int size) {
9797
}
9898

9999

100-
}
101-

‎src/algo/dp/MinimizeArraySize.java‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
Problem Statement: Find minimum possible size of array with given rules for removing elements
3+
4+
Description: Given an array of numbers and a constant k, minimize size of array with following rules for removing elements.
5+
Exactly three elements can be removed at one go.
6+
The removed three elements must be adjacent in array, i.e., arr[i], arr[i+1], arr[i+2]. And the second element must be k greater than first and third element must be k greater than second, i.e., arr[i+1] – arr[i] = k and arr[i+2]-arr[i+1] = k.
7+
8+
Testcses that both solutions were tested against:
9+
Input: arr[] = {100, 101, 105, 2, 10, 12, 14, 4, 22, 24, 26, 6, 500}, k = 2
10+
Output: 4
11+
First we can remove: {10,12,14} and {22,24,26}
12+
Second we can remove: {2,4,6}
13+
Remaining that can't be removed: {100,101,105,500}
14+
15+
Input: arr[] = {2, 3, 4, 5, 6, 4}, k = 1
16+
Output: 0
17+
We can actually remove all elements.
18+
First remove 4, 5, 6 => We get {2, 3, 4}
19+
Now remove 2, 3, 4 => We get empty array {}
20+
21+
Input: arr[] = {2, 3, 4, 7, 6, 4}, k = 1
22+
Output: 3
23+
We can only remove 2 3 4
24+
25+
Input: arr[] = {1,2},k=1
26+
Output: 2
27+
*/
28+
29+
30+
31+
package algo.dp;
32+
33+
public class MinimizeArraySize{
34+
35+
/*
36+
Brute force solution
37+
low: first index of array arr[]
38+
high: last index of array arr[]
39+
arr[]: user input array
40+
k: constant
41+
*/
42+
public int noDPSol(int low, int high, int arr[], int k)
43+
{
44+
//base case
45+
if ((high-low+1)<3)
46+
return high-low+1;
47+
48+
//Considering first element cannot be removed
49+
int res = 1 + noDPSol(low+1, high, arr, k);
50+
51+
for (int i=low +1 ; i<=high-1;i++)
52+
for (int j = i+1; j<=high; j++)
53+
{
54+
// checking if all elements between "low" and "j" can be removed
55+
if(((arr[i]-arr[low])==k) && ((arr[j]-arr[i])==k) && noDPSol(low+1,i-1,arr,k)==0 && noDPSol(i+1,j-1,arr,k)==0)
56+
{
57+
res = Math.min(res,noDPSol(j+1,high,arr,k));
58+
}
59+
}
60+
return res;
61+
}
62+
63+
/*
64+
DP based solution
65+
low: first index of array arr[]
66+
high: last index of array arr[]
67+
arr[]: user input array
68+
k: constant
69+
dp[][]: lookup array prevents from recalculation
70+
of a subproblem. (should be initialized with -1 as all index values)
71+
*/
72+
public int dpSol(int low, int high, int arr[], int k,int dp[][])
73+
{
74+
75+
//base case
76+
if ((high-low+1)<3)
77+
return high-low+1;
78+
79+
//lookup array
80+
//returns already calculated
81+
// subproblem result.
82+
if(dp[low][high] !=-1)
83+
return dp[low][high];
84+
85+
//Considering first element cannot be removed
86+
int res = 1 + dpSol(low+1, high, arr, k,dp);
87+
88+
for (int i=low +1 ; i<=high-1;i++)
89+
for (int j = i+1; j<=high; j++)
90+
{
91+
if(((arr[i]-arr[low])==k) && ((arr[j]-arr[i])==k) && dpSol(low+1,i-1,arr,k,dp)==0 && dpSol(i+1,j-1,arr,k,dp)==0)
92+
{
93+
// checking if all elements between "low" and "j" can be removed
94+
res = Math.min(res,dpSol(j+1,high,arr,k,dp));
95+
}
96+
97+
}
98+
return dp[low][high] = res;
99+
}
100+
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package algo.graph;
2+
3+
import java.io.*;
4+
import java.util.*;
5+
6+
/*
7+
Bridges in an undirected graph:
8+
An edge in an undirected connected graph is a bridge iff removing it disconnects the graph.
9+
10+
Time Complexity of below solution: O(V+E)
11+
12+
Examples: wired computer network, an articulation point indicates the critical computers and a bridge indicates the critical wires or connections.
13+
14+
Java version this problem was tested on: java version "1.8.0_51"
15+
16+
Sample way of running this problem:
17+
1): Enter the maximum number of vertices in the graph
18+
7
19+
Enter an edge or type quit to exit
20+
0
21+
1
22+
Enter an edge or type quit to exit
23+
0
24+
2
25+
Enter an edge or type quit to exit
26+
1
27+
2
28+
Enter an edge or type quit to exit
29+
1
30+
6
31+
Enter an edge or type quit to exit
32+
1
33+
3
34+
Enter an edge or type quit to exit
35+
1
36+
4
37+
Enter an edge or type quit to exit
38+
3
39+
5
40+
Enter an edge or type quit to exit
41+
4
42+
5
43+
Enter an edge or type quit to exit
44+
quit
45+
Finding Bridges
46+
1 6
47+
*/
48+
49+
class Node{
50+
int data;
51+
public Node(int data)
52+
{
53+
this.data = data;
54+
}
55+
}
56+
57+
public class BridgeUndirectedGraph{
58+
LinkedList<Node> ld[];
59+
int temp;
60+
61+
public BridgeUndirectedGraph(int v)
62+
{
63+
ld = new LinkedList[v];
64+
65+
for(int i=0;i<v;i++)
66+
{
67+
ld[i] = new LinkedList<Node>();
68+
}
69+
}
70+
71+
public void addEdge(int a,int b)
72+
{
73+
ld[a].add(new Node(b));
74+
ld[b].add(new Node(a));
75+
}
76+
77+
public static void main(String args[])throws Exception
78+
{
79+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
80+
81+
System.out.println("Enter the maximum number of vertices in the graph");
82+
int V = Integer.parseInt(br.readLine());
83+
BridgeUndirectedGraph obj = new BridgeUndirectedGraph(V);
84+
85+
System.out.println("Enter an edge or type quit to exit");
86+
String str = br.readLine();
87+
88+
while(!str.equalsIgnoreCase("quit")){
89+
90+
int b = Integer.parseInt(br.readLine());
91+
92+
obj.addEdge(Integer.parseInt(str),b);
93+
System.out.println("Enter an edge or type quit to exit");
94+
str = br.readLine();
95+
}
96+
97+
98+
System.out.println("Bridges found: ");
99+
obj.findBridges(V);
100+
}
101+
102+
public void findBridges(int vertices)
103+
{
104+
boolean visited[] = new boolean[vertices];
105+
int parent[] = new int[vertices];
106+
int low[] = new int[vertices];
107+
int disc[] = new int[vertices];
108+
109+
for(int i=0;i<vertices;i++)
110+
{
111+
if(visited[i] != true)
112+
{
113+
bridgeUtil(i,visited,parent,low,disc);
114+
}
115+
}
116+
}
117+
118+
public int min(int a,int b)
119+
{
120+
return (a<b)?a:b;
121+
}
122+
123+
public void bridgeUtil(int u,boolean visited[],int parent[],int low[],int disc[])
124+
{
125+
low[u] = disc[u] = ++temp;
126+
visited[u] = true;
127+
128+
LinkedList<Node> l = ld[u];
129+
Iterator<Node> itr = l.iterator();
130+
while(itr.hasNext())
131+
{
132+
int v = itr.next().data;
133+
134+
if(visited[v] != true)
135+
{
136+
parent[v] = u;
137+
bridgeUtil(v,visited,parent,low,disc);
138+
139+
// Check if the subtree rooted with v has a
140+
// connection to one of the ancestors of u
141+
low[u] = min(low[u], low[v]);
142+
143+
// If the lowest vertex reachable from subtree
144+
// under v is below u in DFS tree, then u-v is
145+
// a bridge
146+
if(low[v] > disc[u])
147+
System.out.println(u + " " + v);
148+
149+
150+
}else if(parent[u] != v) // Update low value of u for parent function calls.
151+
{
152+
low[u] = min(low[u],disc[v]);
153+
}
154+
}
155+
}
156+
}

0 commit comments

Comments
 (0)