Skip to content

Commit

Permalink
week of 26th august, graph, dp, ll, pq
Browse files Browse the repository at this point in the history
  • Loading branch information
kaustubhdeokar committed Aug 31, 2024
1 parent 1a4d475 commit a41b25e
Show file tree
Hide file tree
Showing 5 changed files with 353 additions and 18 deletions.
28 changes: 28 additions & 0 deletions topics/topics/array/problems/IntervalCount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package array.problems;

public class IntervalCount {

public static void main(String[] args) {

int[] startTime = {1, 2, 3};
int[] endTime = {3, 2, 7};

int queryTime = 4;

System.out.println(studentsStudyingAtQueryTime(startTime, endTime, queryTime));

}

private static int studentsStudyingAtQueryTime(int[] startTime, int[] endTime, int queryTime) {

int count = 0;
for (int i = 0; i < startTime.length; i++) {
if (startTime[i] <= queryTime && endTime[i] >= queryTime) {
count += 1;
}
}
return count;

}

}
68 changes: 68 additions & 0 deletions topics/topics/dp/lcs/MinimumInsertionDeletionToCreateString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package dp.lcs;

import java.util.Arrays;

public class MinimumInsertionDeletionToCreateString {

public static void main(String[] args) {

String from = "abcde";
String to = "acdef";
MinimumInsertionDeletionToCreateString min = new MinimumInsertionDeletionToCreateString();
String lcs = min.convertString(from, to);

System.out.println(lcs);

int insertions = 0;
int fromItr = 0;
int lcsIter = 0;
while (fromItr < from.length() && lcsIter < lcs.length()) {
while (from.charAt(fromItr) != lcs.charAt(lcsIter)) {
System.out.println("to remove:" + from.charAt(fromItr));
fromItr += 1;
}
System.out.println("matched:" + lcs.charAt(lcsIter));
fromItr += 1;
lcsIter += 1;
}
while (fromItr < from.length()) {
System.out.println(from.charAt(fromItr));
fromItr += 1;
}




}

private String convertString(String from, String to) {

int[][] dp = new int[from.length()][to.length()];
for (int[] arr : dp) {
Arrays.fill(arr, -1);
}
StringBuilder answer = new StringBuilder();
findCommonSubsequence(from, from.length() - 1, to, to.length() - 1, dp, answer);
return answer.toString();
}

private int findCommonSubsequence(String from, int fromLen, String to, int toLen, int[][] dp, StringBuilder answer) {
if (fromLen < 0 || toLen < 0) {
return 0;
}
if (dp[fromLen][toLen] != -1) {
return dp[fromLen][toLen];
}

if (from.charAt(fromLen) == to.charAt(toLen)) {
answer.append(from.charAt(fromLen));
dp[fromLen][toLen] = 1 + findCommonSubsequence(from, fromLen - 1, to, toLen - 1, dp, answer);
} else {
dp[fromLen][toLen] = Integer.max(findCommonSubsequence(from, fromLen - 1, to, toLen, dp, answer),
findCommonSubsequence(from, fromLen, to, toLen - 1, dp, answer));

}
return dp[fromLen][toLen];
}

}
81 changes: 63 additions & 18 deletions topics/topics/graph/problems/PathWithMaxProbability.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package graph.problems;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.PriorityQueue;

public class PathWithMaxProbability {

Expand All @@ -10,37 +12,80 @@ public static void main(String[] args) {
int n = 3;
double[] succProb = new double[]{0.5, 0.5, 0.2};
int startNode = 0;
int endNode = 3;
int endNode = 2;
PathWithMaxProbability path = new PathWithMaxProbability();
System.out.println(path.traversal(graph, n, succProb, startNode, endNode));

traversal(graph, n, succProb, startNode, endNode);
}

class NodeToProbability {
int node;
double prob;

public NodeToProbability(int node, double prob) {
this.node = node;
this.prob = prob;
}

public int getNode() {
return node;
}

public double getProb() {
return prob;
}
}

private static void traversal(int[][] graph, int n, double[] succProb, int startNode, int endNode) {
private double traversal(int[][] graph, int n, double[] succProb, int startNode, int endNode) {

boolean[] visited = new boolean[n];
traversal(graph, startNode, endNode, visited);

ArrayList<ArrayList<NodeToProbability>> probabilityGraph = new ArrayList<>();
for (int i = 0; i < n; i++) {
probabilityGraph.add(new ArrayList<>());
}
for (int i = 0; i < graph.length; i++) {
int to = graph[i][0];
int from = graph[i][1];
double prob = succProb[i];
probabilityGraph.get(to).add(new NodeToProbability(from, prob));
probabilityGraph.get(from).add(new NodeToProbability(to, prob));
}

return findMaximumProbability(probabilityGraph, startNode, endNode, n);

}

//written a plain dfs.. to compute
private static void traversal(int[][] graph, int startNode, int endNode, boolean[] visited) {
private double findMaximumProbability(ArrayList<ArrayList<NodeToProbability>> probabilityGraph, int srcNode, int destNode, int nodes) {

ArrayDeque<Integer> queue = new ArrayDeque<>();
queue.add(startNode);
visited[startNode] = true;
boolean[] visited = new boolean[nodes];
double[] probability = new double[nodes];
probability[srcNode] = 1;
Comparator<NodeToProbability> comp = (c1, c2) -> Double.compare(c2.prob, c1.prob);
PriorityQueue<NodeToProbability> queue = new PriorityQueue<>(comp);
queue.add(new NodeToProbability(srcNode, 1));
visited[srcNode] = true;

while(!queue.isEmpty()){
Integer top = queue.remove();
System.out.println(top+" ");
for(int neigh: graph[top]){
if(!visited[neigh]){
visited[neigh] = true;
queue.add(neigh);
while (!queue.isEmpty()) {
NodeToProbability top = queue.remove();
int topNode = top.node;
double topProb = top.prob;
for (NodeToProbability neighbour : probabilityGraph.get(topNode)) {
int neighNode = neighbour.node;
double probFromTopToNeigh = neighbour.prob;
if (!visited[neighNode] || probability[neighNode] < topProb * probFromTopToNeigh) {
visited[neighNode] = true;
if (probability[neighNode] < topProb * probFromTopToNeigh) {
probability[neighNode] = topProb * probFromTopToNeigh;
queue.add(new NodeToProbability(neighNode, probability[neighNode]));
}
}

}
}

return probability[destNode];

}


}
1 change: 1 addition & 0 deletions topics/topics/graph/problems/input
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{10},{10,13},{3,17},{6,11},{10,5},{9,10},{13},{2,19},{2},{3},{5,25},{8},{9,22},{5,5},{1,30},{11},{9,12},{7},{5},{8},{9},{4,30},{9,3},{9},{10},{10},{6,14},{3,1},{3},{10,11},{8},{2,14},{1},{5},{4},{11,4},{12,24},{5,18},{13},{7,23},{8},{12},{3,27},{2,12},{5},{2,9},{13,4},{8,18},{1,7},{6},{9,29},{8,21},{5},{6,30},{1,12},{10},{4,15},{7,22},{11,26},{8,17},{9,29},{5},{3,4},{11,30},{12},{4,29},{3},{9},{6},{3,4},{1},{10},{3,29},{10,28},{1,20},{11,13},{3},{3,12},{3,8},{10,9},{3,26},{8},{7},{5},{13,17},{2,27},{11,15},{12},{9,19},{2,15},{3,16},{1},{12,17},{9,1},{6,19},{4},{5},{5},{8,1},{11,7},{5,2},{9,28},{1},{2,2},{7,4},{4,22},{7,24},{9,26},{13,28},{11,26}}
Loading

0 comments on commit a41b25e

Please sign in to comment.