Skip to content

Commit ce10a83

Browse files
authored
Merge pull request #11 from Subham1999/back_tracking
Back tracking
2 parents b11d92f + 0dbc7e8 commit ce10a83

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1608
-52
lines changed

.githooks/pre-commit

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/sh
2+
3+
# Run 'mvn clean install' and check if it succeeds
4+
echo "Running 'mvn clean install'..."
5+
if mvn clean install; then
6+
echo "Build successful, proceeding with commit."
7+
else
8+
echo "Build failed. Aborting commit."
9+
exit 1
10+
fi

.idea/workspace.xml

Lines changed: 129 additions & 52 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.thealgorithm.graph;
2+
3+
import java.util.HashSet;
4+
import java.util.Map;
5+
import java.util.Set;
6+
7+
/**
8+
* @author: Subham Santra
9+
*/
10+
public class ConnectedComponentOnUndirectedGraph {
11+
public static void main(String[] args) {
12+
// TEST 1
13+
{
14+
GraphCreator graphCreator =
15+
GraphCreator.getUndirected(7)
16+
.addEdge(1, 2, 1)
17+
.addEdge(2, 3, 1)
18+
.addEdge(3, 4, 1)
19+
.addEdge(5, 6, 1);
20+
21+
System.out.println(
22+
countOfConnectedComponents(graphCreator.getAdjacencyList(), graphCreator.getVertices()));
23+
}
24+
25+
// TEST 2
26+
{
27+
GraphCreator graphCreator =
28+
GraphCreator.getUndirected(7);
29+
System.out.println(
30+
countOfConnectedComponents(graphCreator.getAdjacencyList(), graphCreator.getVertices()));
31+
}
32+
33+
// TEST 3
34+
{
35+
GraphCreator graphCreator =
36+
GraphCreator.getUndirected(3)
37+
.addEdge(1, 2)
38+
.addEdge(2, 0)
39+
.addEdge(1, 0);
40+
System.out.println(
41+
countOfConnectedComponents(graphCreator.getAdjacencyList(), graphCreator.getVertices()));
42+
}
43+
}
44+
45+
public static int countOfConnectedComponents(
46+
Map<Integer, Set<Integer>> adjacencyList, Set<Vertex<Integer, Integer>> vertexSet) {
47+
int count = 0;
48+
Set<Integer> visited = new HashSet<>();
49+
for (Vertex<Integer, Integer> vertex : vertexSet) {
50+
if (!visited.contains(vertex.getKey())) {
51+
count++;
52+
DFS(adjacencyList, vertex.getKey(), visited);
53+
}
54+
}
55+
return count;
56+
}
57+
58+
private static void DFS(
59+
Map<Integer, Set<Integer>> adjacencyList, int vertex, Set<Integer> visited) {
60+
if (visited.contains(vertex)) return;
61+
visited.add(vertex);
62+
for (int child : adjacencyList.get(vertex)) {
63+
DFS(adjacencyList, child, visited);
64+
}
65+
}
66+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.thealgorithm.graph;
2+
3+
import static java.util.AbstractMap.*;
4+
5+
import java.util.*;
6+
7+
public class DijkstraAlgorithm {
8+
public static class InvalidWeightException extends Exception {
9+
private String msg;
10+
11+
public InvalidWeightException(String msg) {
12+
super(msg);
13+
this.msg = msg;
14+
}
15+
}
16+
17+
void dijkstra(int[][] graph, int n, int source) throws InvalidWeightException {
18+
int[] dist = new int[n];
19+
Arrays.fill(dist, Integer.MAX_VALUE);
20+
dist[source] = 0;
21+
22+
Set<Integer> shortestPathFound = new HashSet<>();
23+
PriorityQueue<Entry<Integer, Integer>> priorityQueue =
24+
new PriorityQueue<>(Comparator.comparingInt(Entry::getValue));
25+
26+
priorityQueue.offer(new SimpleEntry<>(source, 0));
27+
shortestPathFound.add(source);
28+
29+
while (!priorityQueue.isEmpty()) {
30+
Entry<Integer, Integer> shortedstEntry = priorityQueue.poll();
31+
int node = shortedstEntry.getKey();
32+
int cost = shortedstEntry.getValue();
33+
34+
for (int i = 0; i < n; ++i) {
35+
if (graph[node][i] < 0) {
36+
throw new InvalidWeightException("Invalid data");
37+
}
38+
if (node != i && graph[node][i] != 0) {
39+
if (dist[i] > cost + graph[node][i]) {
40+
dist[i] = cost + graph[node][i];
41+
int finalI = i;
42+
priorityQueue.removeIf(e -> e.getKey() == finalI);
43+
priorityQueue.offer(new SimpleEntry<>(i, dist[i]));
44+
}
45+
}
46+
}
47+
shortestPathFound.add(node);
48+
if (shortestPathFound.size() == n) break;
49+
}
50+
51+
System.out.println(Arrays.toString(dist));
52+
}
53+
54+
public static void main(String[] args) throws InvalidWeightException {
55+
56+
GraphCreator graphCreator =
57+
GraphCreator.getUndirected(9)
58+
.addEdge(0, 1, 4)
59+
.addEdge(0, 7, 8)
60+
.addEdge(1, 7, 11)
61+
.addEdge(1, 2, 8)
62+
.addEdge(2, 8, 2)
63+
.addEdge(8, 6, 6)
64+
.addEdge(8, 6, 6)
65+
.addEdge(7, 6, 1)
66+
.addEdge(7, 8, 7)
67+
.addEdge(6, 5, 2)
68+
.addEdge(2, 5, 4)
69+
.addEdge(2, 3, 7)
70+
.addEdge(5, 3, 14)
71+
.addEdge(5, 4, 10)
72+
.addEdge(3, 4, 9);
73+
74+
new DijkstraAlgorithm().dijkstra(graphCreator.getAdjacencyMatrix(), graphCreator.getSize(), 0);
75+
new DijkstraAlgorithm().dijkstra(graphCreator.getAdjacencyMatrix(), graphCreator.getSize(), 8);
76+
new DijkstraAlgorithm().dijkstra(graphCreator.getAdjacencyMatrix(), graphCreator.getSize(), 6);
77+
}
78+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.thealgorithm.graph;
2+
3+
import java.util.HashMap;
4+
import java.util.HashSet;
5+
import java.util.Map;
6+
import java.util.Set;
7+
import lombok.Value;
8+
9+
@Value
10+
public class GraphCreator {
11+
int size;
12+
int[][] adjacencyMatrix;
13+
Map<Integer, Set<Integer>> adjacencyList;
14+
Set<Vertex<Integer, Integer>> vertices;
15+
Set<Edge<Integer, Integer>> edgeSet;
16+
boolean undirected;
17+
18+
private GraphCreator(int size, boolean undirected) {
19+
this.size = size;
20+
this.adjacencyMatrix = new int[size][size];
21+
this.undirected = undirected;
22+
this.adjacencyList = new HashMap<>();
23+
this.vertices = new HashSet<>();
24+
this.edgeSet = new HashSet<>();
25+
for (int i = 0; i < size; ++i) {
26+
vertices.add(Vertex.create(i));
27+
}
28+
for (int i = 0; i < size; ++i) {
29+
adjacencyList.put(i, new HashSet<>());
30+
}
31+
}
32+
33+
public static GraphCreator getUndirected(int n) {
34+
return new GraphCreator(n, true);
35+
}
36+
37+
public static GraphCreator getDirected(int n) {
38+
return new GraphCreator(n, false);
39+
}
40+
41+
public GraphCreator addEdge(int u, int v) {
42+
return addEdge(u, v, 1);
43+
}
44+
45+
public GraphCreator addEdge(int u, int v, int w) {
46+
addEdge0(u, v, w);
47+
if (undirected) {
48+
addEdge0(v, u, w);
49+
}
50+
return this;
51+
}
52+
53+
private void addEdge0(int u, int v, int w) {
54+
edgeSet.add(Edge.createEdge(u, v, w));
55+
addToAdjacencyList(u, v);
56+
adjacencyMatrix[u][v] = w;
57+
}
58+
59+
private void addToAdjacencyList(int u, int v) {
60+
adjacencyList.get(u).add(v);
61+
}
62+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.thealgorithm.heap;
2+
3+
import java.io.IOException;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.function.Function;
8+
import java.util.stream.Collectors;
9+
10+
/**
11+
* @author: Subham Santra
12+
*/
13+
public class TopKFrequentWords {
14+
public List<String> topKFrequent(String[] words, int k) {
15+
return Arrays.stream(words)
16+
.collect(Collectors.toMap(Function.identity(), s -> 1, Integer::sum))
17+
.entrySet()
18+
.stream()
19+
.sorted(
20+
(kv1, kv2) -> {
21+
int diff = kv1.getValue() - kv2.getValue();
22+
if (diff == 0) {
23+
return kv1.getKey().compareTo(kv2.getKey());
24+
}
25+
return -diff;
26+
})
27+
.limit(k)
28+
.map(Map.Entry::getKey)
29+
.collect(Collectors.toList());
30+
}
31+
32+
public static void main(String[] args) throws IOException {
33+
// System.out.println(
34+
// new TopKFrequentWords()
35+
// .topKFrequent(new String[] {"i", "love", "leetcode", "i", "love", "coding"}, 2));
36+
}
37+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.thealgorithm.linkedlist;
2+
3+
/**
4+
* @author: Subham Santra
5+
*/
6+
public class DoublyLinkedList {
7+
DoublyLinkedListNode head;
8+
DoublyLinkedListNode tail;
9+
10+
public void addLast(int data) {
11+
if (isEmpty()) {
12+
head = tail = new DoublyLinkedListNode(data, null, null);
13+
} else {
14+
tail.next = new DoublyLinkedListNode(data, null, null);
15+
}
16+
}
17+
18+
private boolean isEmpty() {
19+
return head == null && tail == null;
20+
}
21+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.thealgorithm.linkedlist;
2+
3+
/**
4+
* @author: Subham Santra
5+
*/
6+
public class DoublyLinkedListNode extends LinkedListNode {
7+
DoublyLinkedListNode previous;
8+
9+
public DoublyLinkedListNode(int data, LinkedListNode next, DoublyLinkedListNode previous) {
10+
super(data, next);
11+
this.previous = previous;
12+
}
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.thealgorithm.linkedlist;
2+
3+
import lombok.AllArgsConstructor;
4+
5+
/**
6+
* @author: Subham Santra
7+
*/
8+
@AllArgsConstructor
9+
public class LinkedListNode {
10+
int data;
11+
LinkedListNode next;
12+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.thealgorithm.strings;
2+
3+
/**
4+
* @author: Subham Santra
5+
*/
6+
public class LongestChunkedPalindromeDecomposition {
7+
static class Solution {
8+
private final long p = 31;
9+
private final long mod = (int) 1e9 + 7;
10+
private final long[] p_pow = new long[1001];
11+
12+
public Solution() {
13+
p_pow[0] = 1;
14+
for (int i = 1; i <= 1000; ++i) {
15+
p_pow[i] = p_pow[i - 1] * p;
16+
p_pow[i] = (p_pow[i] + mod) % mod;
17+
}
18+
}
19+
20+
void rollingHash(long[] rh, String txt) {
21+
for (int i = 0; i < txt.length(); ++i) {
22+
int c = txt.charAt(i) - 'a' + 1;
23+
rh[i + 1] = ((rh[i] * 31) % mod + c + mod) % mod;
24+
}
25+
}
26+
27+
long hash(long[] RH, int i, int j) {
28+
return ((RH[j + 1] - RH[i] * p_pow[j - i + 1]) % mod + mod) % mod;
29+
}
30+
31+
public int longestDecomposition(String text) {
32+
int[][] DP = new int[text.length()][text.length()];
33+
long[] RH = new long[text.length() + 1];
34+
for (int i = 0; i < text.length(); ++i) {
35+
for (int j = 0; j < text.length(); ++j) {
36+
DP[i][j] = -1;
37+
}
38+
}
39+
rollingHash(RH, text);
40+
// System.out.println(Arrays.toString(RH));
41+
return decompose(text, 0, text.length() - 1, DP, RH);
42+
}
43+
44+
int decompose(String text, int lo, int hi, int[][] DP, long[] RH) {
45+
if (lo == hi + 1) return 0;
46+
if (lo > hi) return -1;
47+
48+
if (DP[lo][hi] != -1) return DP[lo][hi];
49+
50+
int ans = 1;
51+
52+
for (int len = 1; len <= (hi - lo + 1) / 2; ++len) {
53+
long h1 = hash(RH, lo, lo + len - 1);
54+
long h2 = hash(RH, hi - len + 1, hi);
55+
56+
// System.out.printf(
57+
// "%s %d %s %d\n",
58+
// text.substring(lo, lo + len), h1, text.substring(hi - len + 1, hi + 1), h2);
59+
60+
if (h1 == h2) {
61+
int a = decompose(text, lo + len, hi - len, DP, RH);
62+
if (a > -1) ans = Math.max(a + 2, ans);
63+
}
64+
}
65+
66+
return DP[lo][hi] = ans;
67+
}
68+
}
69+
70+
public static void main(String[] args) {
71+
System.out.println(new Solution().longestDecomposition("ghiabcdefhelloadamhelloabcdefghi"));
72+
System.out.println(
73+
new Solution().longestDecomposition("wuamlwxskgjzegshjhcwchkgjzegshjwuamlwxs"));
74+
System.out.println(new Solution().longestDecomposition("antaprezatepzapreanta"));
75+
System.out.println(new Solution().longestDecomposition("aabaab"));
76+
System.out.println(new Solution().longestDecomposition("aabaabaab"));
77+
System.out.println(new Solution().longestDecomposition("aabaabaabaab"));
78+
System.out.println(new Solution().longestDecomposition("aabaabaabaabc"));
79+
}
80+
}

0 commit comments

Comments
 (0)