Skip to content

Commit

Permalink
dp - longest common substring pattern.
Browse files Browse the repository at this point in the history
tree - strucuture setup, path sum III leetcode.
graph -structure setup.
  • Loading branch information
kaustubhdeokar committed Aug 27, 2024
1 parent 96816ac commit 1a4d475
Show file tree
Hide file tree
Showing 33 changed files with 195 additions and 11 deletions.
11 changes: 0 additions & 11 deletions topics/dp/topic.iml

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
46 changes: 46 additions & 0 deletions topics/topics/graph/Cycle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package graph;

import java.util.ArrayDeque;
import java.util.ArrayList;

public class Cycle {

public void traverseBfs(ArrayList<ArrayList<Integer>> list, int nodes){
boolean[] visited = new boolean[nodes];
for(int i=0;i<nodes;i++){
if(!visited[i]){
traverseBfs(list, i, visited);
}
}
}

private void traverseBfs(ArrayList<ArrayList<Integer>> list, int curr, boolean[] visited) {

ArrayDeque<Integer> queue = new ArrayDeque<>();
queue.add(curr);
visited[curr] = true;

while(!queue.isEmpty())
{
curr = queue.remove();
System.out.println(curr);
for(int neigh: list.get(curr))
{
if(!visited[neigh]){
visited[neigh] = true;
queue.add(neigh);
}
else{
System.out.println("cycle.");
}
}
}
}

public static void main(String[] args) {

Graph g = new Graph();
ArrayList<ArrayList<Integer>> graph = g.sampleGraph();

}
}
27 changes: 27 additions & 0 deletions topics/topics/graph/Graph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package graph;

import java.util.ArrayList;

public class Graph {

public ArrayList<ArrayList<Integer>> sampleGraph() {

int[][] graph = new int[][]{{0, 1}, {0, 2}, {1, 2}, {1, 3}, {3, 4}, {2, 4}};
return createGraph(graph, 5, true);
}

private ArrayList<ArrayList<Integer>> createGraph(int[][] graph, int nodes, boolean bidirectional) {
ArrayList<ArrayList<Integer>> list = new ArrayList<>();
for (int i = 0; i < nodes; i++) {
list.add(new ArrayList<>());
}
for(int[] vertex: graph){
list.get(vertex[0]).add(vertex[1]);
if(bidirectional) {
list.get(vertex[1]).add(vertex[0]);
}
}
return list;
}

}
76 changes: 76 additions & 0 deletions topics/topics/graph/Traversal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package graph;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Stack;

public class Traversal {

public void traverseBfs(ArrayList<ArrayList<Integer>> list, int nodes){
boolean[] visited = new boolean[nodes];
for(int i=0;i<nodes;i++){
if(!visited[i]){
traverseBfs(list, i, visited);
}
}
}

private void traverseBfs(ArrayList<ArrayList<Integer>> list, int curr, boolean[] visited) {

ArrayDeque<Integer> queue = new ArrayDeque<>();
queue.add(curr);
visited[curr] = true;

while(!queue.isEmpty())
{
curr = queue.remove();
System.out.println(curr);
for(int neigh: list.get(curr))
{
if(!visited[neigh]){
visited[neigh] = true;
queue.add(neigh);
}
}
}
}

private void traverseDfs(ArrayList<ArrayList<Integer>> list, int nodes){
boolean[] visited = new boolean[nodes];
for(int i=0;i<nodes;i++){
if(!visited[i]){
traverseDfs(list, i, visited);
}
}
}

private void traverseDfs(ArrayList<ArrayList<Integer>> list, int curr, boolean[] visited) {
Stack<Integer> stack = new Stack<>();
stack.add(curr);
visited[curr] = true;

while(!stack.isEmpty()){
curr = stack.pop();
System.out.println(curr);
for(int neigh: list.get(curr)){
if(!visited[neigh]){
visited[neigh] = true;
stack.add(neigh);
}
}
}
}


public static void main(String[] args) {

Graph g = new Graph();
ArrayList<ArrayList<Integer>> graph = g.sampleGraph();
Traversal traversal = new Traversal();
System.out.println("BFS");
traversal.traverseBfs(graph, 5);
System.out.println("\nDFS");
traversal.traverseDfs(graph, 5);
}

}
46 changes: 46 additions & 0 deletions topics/topics/graph/problems/PathWithMaxProbability.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package graph.problems;

import java.util.ArrayDeque;

public class PathWithMaxProbability {

public static void main(String[] args) {

int[][] graph = {{0, 1}, {1, 2}, {0, 2}};
int n = 3;
double[] succProb = new double[]{0.5, 0.5, 0.2};
int startNode = 0;
int endNode = 3;

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

}

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

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

}

//written a plain dfs.. to compute
private static void traversal(int[][] graph, int startNode, int endNode, boolean[] visited) {

ArrayDeque<Integer> queue = new ArrayDeque<>();
queue.add(startNode);
visited[startNode] = 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);
}
}
}

}

}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 1a4d475

Please sign in to comment.