Skip to content

Commit bccfbb9

Browse files
committed
Merge branch 'main' of github.com:admirerr/DSA-Collection
2 parents 9a94b26 + 661c362 commit bccfbb9

2 files changed

Lines changed: 227 additions & 0 deletions

File tree

Java/graphs/BellmanFord.java

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
public class BellmanFord {
2+
static class Edge {
3+
int source;
4+
int destination;
5+
int weight;
6+
7+
Edge(int source, int destination, int weight) {
8+
this.source = source;
9+
this.destination = destination;
10+
this.weight = weight;
11+
}
12+
}
13+
14+
15+
public static Integer[] findShortestPaths(Edge[] edges, int V, int source) {
16+
17+
Integer[] distances = new Integer[V];
18+
for (int i = 0; i < V; i++) {
19+
distances[i] = Integer.MAX_VALUE;
20+
}
21+
distances[source] = 0;
22+
23+
24+
for (int i = 1; i < V; i++) {
25+
for (Edge edge : edges) {
26+
int u = edge.source;
27+
int v = edge.destination;
28+
int weight = edge.weight;
29+
30+
if (distances[u] != Integer.MAX_VALUE &&
31+
distances[u] + weight < distances[v]) {
32+
distances[v] = distances[u] + weight;
33+
}
34+
}
35+
}
36+
37+
38+
for (Edge edge : edges) {
39+
int u = edge.source;
40+
int v = edge.destination;
41+
int weight = edge.weight;
42+
43+
if (distances[u] != Integer.MAX_VALUE &&
44+
distances[u] + weight < distances[v]) {
45+
46+
return null;
47+
}
48+
}
49+
50+
return distances;
51+
}
52+
53+
54+
public static void printShortestPaths(Integer[] distances) {
55+
if (distances == null) {
56+
System.out.println("Graph contains negative weight cycle!");
57+
return;
58+
}
59+
60+
System.out.println("Vertex \t Distance from Source");
61+
for (int i = 0; i < distances.length; i++) {
62+
if (distances[i] == Integer.MAX_VALUE) {
63+
System.out.println(i + "\t\t INFINITY");
64+
} else {
65+
System.out.println(i + "\t\t " + distances[i]);
66+
}
67+
}
68+
}
69+
70+
public static void main(String[] args) {
71+
72+
System.out.println("Test Case 1: Graph without negative cycle");
73+
Edge[] edges1 = new Edge[] {
74+
new Edge(0, 1, 4),
75+
new Edge(0, 2, 2),
76+
new Edge(1, 2, -1),
77+
new Edge(1, 3, 3),
78+
new Edge(2, 3, 1)
79+
};
80+
int V1 = 4;
81+
Integer[] result1 = findShortestPaths(edges1, V1, 0);
82+
printShortestPaths(result1);
83+
System.out.println();
84+
85+
86+
System.out.println("Test Case 2: Graph with negative cycle");
87+
Edge[] edges2 = new Edge[] {
88+
new Edge(0, 1, 1),
89+
new Edge(1, 2, 2),
90+
new Edge(2, 3, 3),
91+
new Edge(3, 1, -7)
92+
};
93+
int V2 = 4;
94+
Integer[] result2 = findShortestPaths(edges2, V2, 0);
95+
printShortestPaths(result2);
96+
System.out.println();
97+
98+
99+
System.out.println("Test Case 3: Graph with unreachable vertices");
100+
Edge[] edges3 = new Edge[] {
101+
new Edge(0, 1, 5),
102+
new Edge(1, 2, 2),
103+
new Edge(3, 4, 1)
104+
};
105+
int V3 = 5;
106+
Integer[] result3 = findShortestPaths(edges3, V3, 0);
107+
printShortestPaths(result3);
108+
}
109+
}

Java/strings/BoyerMoore.java

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/**
2+
* Implementation of the Boyer-Moore string searching algorithm.
3+
* This algorithm preprocesses the pattern to achieve better than O(n) performance
4+
* on many inputs by using two heuristics:
5+
* 1. Bad Character Rule
6+
* 2. Good Suffix Rule
7+
*
8+
* Time Complexity:
9+
* - Preprocessing: O(m + k) where m is pattern length and k is alphabet size
10+
* - Search: O(mn) worst case, but often performs in O(n/m) in practice
11+
* Space Complexity: O(k) where k is the size of the alphabet
12+
*/
13+
public class BoyerMoore {
14+
private static final int NO_OF_CHARS = 256;
15+
16+
/**
17+
* Preprocesses the pattern to create the bad character rule array
18+
*/
19+
private static int[] createBadCharArray(String pattern) {
20+
int[] badChar = new int[NO_OF_CHARS];
21+
22+
// Initialize all occurrences as -1
23+
for (int i = 0; i < NO_OF_CHARS; i++) {
24+
badChar[i] = -1;
25+
}
26+
27+
// Fill the actual value of last occurrence of a character
28+
for (int i = 0; i < pattern.length(); i++) {
29+
badChar[pattern.charAt(i)] = i;
30+
}
31+
32+
return badChar;
33+
}
34+
35+
/**
36+
* Searches for occurrences of pattern in text using Boyer-Moore algorithm
37+
* @param text The text to search in
38+
* @param pattern The pattern to search for
39+
* @return Array of starting indices where pattern is found
40+
*/
41+
public static int[] search(String text, String pattern) {
42+
if (text == null || pattern == null) {
43+
throw new IllegalArgumentException("Text and pattern cannot be null");
44+
}
45+
46+
int[] matches = new int[text.length()];
47+
int matchCount = 0;
48+
49+
int[] badChar = createBadCharArray(pattern);
50+
51+
int shift = 0; // Shift of the pattern with respect to text
52+
while (shift <= text.length() - pattern.length()) {
53+
int j = pattern.length() - 1;
54+
55+
// Keep reducing index j of pattern while characters of
56+
// pattern and text are matching at this shift
57+
while (j >= 0 && pattern.charAt(j) == text.charAt(shift + j)) {
58+
j--;
59+
}
60+
61+
// If the pattern is present at current shift, then j becomes -1
62+
if (j < 0) {
63+
matches[matchCount++] = shift;
64+
shift += (shift + pattern.length() < text.length()) ? 1 : 1;
65+
} else {
66+
// Shift the pattern so that the bad character in text aligns
67+
// with the last occurrence of it in pattern.
68+
// The max function is used to make sure we get a positive shift.
69+
// We may get a negative shift if the last occurrence of bad
70+
// character in pattern is on the right side of the current character.
71+
shift += Math.max(1, j - badChar[text.charAt(shift + j)]);
72+
}
73+
}
74+
75+
// Create result array with exact size
76+
int[] result = new int[matchCount];
77+
System.arraycopy(matches, 0, result, 0, matchCount);
78+
return result;
79+
}
80+
81+
public static void main(String[] args) {
82+
// Test cases
83+
String text1 = "ABAAABCDABC";
84+
String pattern1 = "ABC";
85+
System.out.println("Test Case 1:");
86+
System.out.println("Text: " + text1);
87+
System.out.println("Pattern: " + pattern1);
88+
System.out.print("Pattern found at indices: ");
89+
int[] result1 = search(text1, pattern1);
90+
for (int index : result1) {
91+
System.out.print(index + " ");
92+
}
93+
System.out.println("\n");
94+
95+
String text2 = "AABAACAADAABAAABAA";
96+
String pattern2 = "AABA";
97+
System.out.println("Test Case 2:");
98+
System.out.println("Text: " + text2);
99+
System.out.println("Pattern: " + pattern2);
100+
System.out.print("Pattern found at indices: ");
101+
int[] result2 = search(text2, pattern2);
102+
for (int index : result2) {
103+
System.out.print(index + " ");
104+
}
105+
System.out.println("\n");
106+
107+
String text3 = "ABCDEFGHIJKLMNOP";
108+
String pattern3 = "XYZ";
109+
System.out.println("Test Case 3:");
110+
System.out.println("Text: " + text3);
111+
System.out.println("Pattern: " + pattern3);
112+
System.out.print("Pattern found at indices: ");
113+
int[] result3 = search(text3, pattern3);
114+
if (result3.length == 0) {
115+
System.out.println("Pattern not found");
116+
}
117+
}
118+
}

0 commit comments

Comments
 (0)