-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dp - longest common substring pattern.
tree - strucuture setup, path sum III leetcode. graph -structure setup.
- Loading branch information
1 parent
96816ac
commit 1a4d475
Showing
33 changed files
with
195 additions
and
11 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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.
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.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.