Skip to content

Commit 9db4e19

Browse files
committed
Added solutions to the graph problems
1 parent 7b50292 commit 9db4e19

File tree

3 files changed

+310
-0
lines changed

3 files changed

+310
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.eprogrammerz.examples.algorithm.graphs;
2+
3+
import org.junit.Test;
4+
5+
import java.util.*;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
/**
10+
* There are n islands and there are many bridges connecting them. Each bridge has some cost attached to it.
11+
* <p>
12+
* We need to find bridges with minimal cost such that all islands are connected.
13+
* <p>
14+
* It is guaranteed that input data will contain at least one possible scenario in which all islands are connected with each other.
15+
* <p>
16+
* Example :
17+
* Input
18+
* <p>
19+
* Number of islands ( n ) = 4
20+
* 1 2 1
21+
* 2 3 4
22+
* 1 4 3
23+
* 4 3 2
24+
* 1 3 10
25+
*/
26+
public class CommutableIslands {
27+
28+
class Edge implements Comparable<Edge> {
29+
int weight;
30+
int start;
31+
int end;
32+
33+
Edge(int start, int end, int weight) {
34+
this.start = start;
35+
this.end = end;
36+
this.weight = weight;
37+
}
38+
39+
@Override
40+
public int compareTo(Edge o) {
41+
return Integer.compare(this.weight, o.weight);
42+
}
43+
}
44+
45+
class Graph {
46+
List<Edge> edges;
47+
int[] arr;
48+
int n;
49+
50+
Graph(int n) {
51+
this.n = n;
52+
this.arr = new int[n + 1];
53+
this.edges = new ArrayList<>();
54+
}
55+
56+
void initialize() {
57+
for (int i = 0; i <= n; i++) {
58+
arr[i] = i;
59+
}
60+
}
61+
62+
void addEdge(int start, int end, int weight) {
63+
edges.add(new Edge(start, end, weight));
64+
}
65+
66+
public int kruskal() {
67+
initialize();
68+
int minCost = 0;
69+
Collections.sort(edges);
70+
71+
for (int i = 0; i < edges.size(); i++) {
72+
Edge edge = edges.get(i);
73+
int start = edge.start;
74+
int end = edge.end;
75+
76+
if (root(start) != root(end)) {
77+
minCost += edge.weight;
78+
union(start, end);
79+
}
80+
}
81+
return minCost;
82+
}
83+
84+
private void union(int start, int end) {
85+
arr[root(start)] = arr[root(end)];
86+
}
87+
88+
private int root(int vertex) {
89+
while (vertex != arr[vertex])
90+
vertex = arr[vertex];
91+
return vertex;
92+
}
93+
}
94+
95+
public int solve(int n, List<List<Integer>> bridges) {
96+
Graph graph = new Graph(n);
97+
for (List<Integer> bridge : bridges) {
98+
graph.addEdge(bridge.get(0), bridge.get(1), bridge.get(2));
99+
}
100+
101+
return graph.kruskal();
102+
}
103+
104+
@Test
105+
public void testSolve() {
106+
List<Integer> b1 = Arrays.asList(1, 2, 1);
107+
List<Integer> b2 = Arrays.asList(2, 3, 4);
108+
List<Integer> b3 = Arrays.asList(1, 4, 3);
109+
List<Integer> b4 = Arrays.asList(4, 3, 2);
110+
List<Integer> b5 = Arrays.asList(1, 3, 10);
111+
112+
List<List<Integer>> bridges = Arrays.asList(b1, b2, b3, b4, b5);
113+
114+
assertEquals(6, solve(6, bridges));
115+
}
116+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.eprogrammerz.examples.algorithm.graphs;
2+
3+
import com.eprogrammerz.examples.algorithm.trees.TreeNode;
4+
import org.junit.Test;
5+
6+
import java.util.LinkedList;
7+
import java.util.Queue;
8+
9+
import static org.junit.Assert.assertEquals;
10+
11+
/**
12+
* You are given an integer N. You have to find smallest multiple of N which consists of digits 0 and 1 only.
13+
* Since this multiple could be large, return it in form of a string.
14+
*
15+
* Note:
16+
*
17+
* Returned string should not contain leading zeroes.
18+
*
19+
* For N = 55, 110 is smallest multiple consisting of digits 0 and 1.
20+
* For N = 2, 10 is the answer.
21+
*/
22+
public class SmallestMultiple {
23+
24+
public String multiple(int n) {
25+
Queue<Long> queue = new LinkedList<>();
26+
queue.add(1L);
27+
28+
String res = "";
29+
30+
while (!queue.isEmpty()) {
31+
long multiple = queue.poll();
32+
long rem = multiple % n;
33+
if (rem == 0) {
34+
res += multiple;
35+
break;
36+
}
37+
38+
long left = multiple * 10;
39+
if (n % left == 0) {
40+
queue.add(left);
41+
}
42+
long right = multiple * 10 + 1;
43+
if (n % right == 0) {
44+
queue.add(right);
45+
}
46+
}
47+
return res;
48+
}
49+
50+
@Test
51+
public void testMultiple() {
52+
assertEquals("110", multiple(55));
53+
assertEquals("10", multiple(5));
54+
assertEquals("10", multiple(2));
55+
}
56+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package com.eprogrammerz.examples.algorithm.graphs;
2+
3+
import org.junit.Test;
4+
5+
import java.util.Arrays;
6+
import java.util.LinkedList;
7+
import java.util.List;
8+
import java.util.Queue;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
/**
13+
* Given a 2D board and a word, find if the word exists in the grid.
14+
* <p>
15+
* The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring.
16+
* The cell itself does not count as an adjacent cell.
17+
* The same letter cell may be used more than once.
18+
* <p>
19+
* Example :
20+
* <p>
21+
* Given board =
22+
* <p>
23+
* [
24+
* ["ABCE"],
25+
* ["SFCS"],
26+
* ["ADEE"]
27+
* ]
28+
* word = "ABCCED", -> returns 1,
29+
* word = "SEE", -> returns 1,
30+
* word = "ABCB", -> returns 1,
31+
* word = "ABFSAB" -> returns 1
32+
* word = "ABCD" -> returns 0
33+
*/
34+
public class WordSearchBoard {
35+
public int exist(List<String> board, String word) {
36+
if (board == null || board.isEmpty() || word == null) return 0;
37+
38+
int[] dx = {1, 0, -1, 0};
39+
int[] dy = {0, 1, 0, -1};
40+
/**
41+
* Go to each of the cell and see if the letter is same as of starting letter in search word
42+
*/
43+
for (int r = 0; r < board.size(); r++) {
44+
for (int c = 0; c < board.get(0).length(); c++) {
45+
46+
/**
47+
* if letter is same, then do dfs starting that point
48+
*/
49+
if (board.get(r).charAt(c) == word.charAt(0)) {
50+
Queue<int[]> queue = new LinkedList<>();
51+
queue.add(new int[]{r, c});
52+
53+
int chatIdx = 0;
54+
55+
while (!queue.isEmpty()) {
56+
int[] cell = queue.poll();
57+
int row = cell[0];
58+
int col = cell[1];
59+
60+
if (board.get(row).charAt(col) == word.charAt(chatIdx)) {
61+
chatIdx++;
62+
}
63+
64+
if (chatIdx == word.length()) return 1;
65+
66+
/**
67+
* Visit all possible cells and see if it is next char
68+
*/
69+
for (int i = 0; i < 4; i++) {
70+
int newRow = row + dx[i];
71+
int newCol = col + dy[i];
72+
if(isValid(newRow, newCol, board.size(), board.get(0).length()) && word.charAt(chatIdx) == board.get(newRow).charAt(newCol)) {
73+
queue.add(new int[]{newRow, newCol});
74+
}
75+
}
76+
}
77+
}
78+
}
79+
}
80+
81+
82+
return 0;
83+
}
84+
85+
private boolean isValid(int r, int c, int row, int col) {
86+
if (r < 0 || r >= row || c < 0 || c >= col) return false;
87+
return true;
88+
}
89+
90+
@Test
91+
public void testExists() {
92+
List<String> words = Arrays.asList("ABCE", "SFCS", "ADEE");
93+
assertEquals(1, exist(words, "ABCCED"));
94+
assertEquals(1, exist(words, "SEE"));
95+
assertEquals(1, exist(words, "SEED"));
96+
assertEquals(1, exist(words, "SEC"));
97+
assertEquals(1, exist(words, "ABFSAB"));
98+
assertEquals(0, exist(words, "ABCD"));
99+
}
100+
101+
public int existDfs(List<String> board, String word) {
102+
if (board == null || word == null) return 0;
103+
104+
int rows = board.size();
105+
int cols = board.get(0).length();
106+
107+
for (int r = 0; r < rows; r++) {
108+
for (int c = 0; c < cols; c++) {
109+
if (dfs(board, word, r, c, 0)) {
110+
return 1;
111+
}
112+
}
113+
}
114+
115+
return 0;
116+
}
117+
118+
private boolean dfs(List<String> board, String word, int r, int c, int i) {
119+
if (r < 0 || r >= board.size() || c < 0 || c >= board.get(0).length()) return false;
120+
if (board.get(r).charAt(c) != word.charAt(i)) return false;
121+
if (i == word.length() - 1) return true;
122+
else return dfs(board, word, r + 1, c, i + 1) ||
123+
dfs(board,word, r, c + 1, i+1) ||
124+
dfs(board,word, r, c - 1, i+1) ||
125+
dfs(board,word, r - 1, c, i+1);
126+
}
127+
128+
@Test
129+
public void testExistsDfs() {
130+
List<String> words = Arrays.asList("ABCE", "SFCS", "ADEE");
131+
assertEquals(1, existDfs(words, "ABCCED"));
132+
assertEquals(1, existDfs(words, "SEE"));
133+
assertEquals(1, existDfs(words, "SEED"));
134+
assertEquals(1, existDfs(words, "SEC"));
135+
assertEquals(1, existDfs(words, "ABFSAB"));
136+
assertEquals(0, existDfs(words, "ABCD"));
137+
}
138+
}

0 commit comments

Comments
 (0)