Skip to content

Commit 7d820d9

Browse files
dhananjay-ngDhananjay Nagargoje
authored and
Dhananjay Nagargoje
committed
diagraph, toplogical sorting
1 parent d19f777 commit 7d820d9

File tree

6 files changed

+221
-9
lines changed

6 files changed

+221
-9
lines changed

Diff for: src/main/java/datastructures/graph/DepthFirstPaths.java

+7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ public DepthFirstPaths(Graph G, int s) {
1818
dfs(G, s);
1919
}
2020

21+
public DepthFirstPaths(Graph G) {
22+
this.s = s;
23+
marked = new boolean[G.V()];
24+
edgeTo = new int[G.V()];
25+
Arrays.fill(edgeTo, -1);
26+
}
27+
2128
public void dfs(Graph g, int v) {
2229
marked[v] = true;
2330
for (int i : g.adj(v)) {

Diff for: src/main/java/datastructures/graph/Digraph.java

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package datastructures.graph;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Digraph implements Graph {
7+
int vertices;
8+
int edges;
9+
List<Integer>[] adj;
10+
11+
public Digraph(int vertices) {
12+
this.vertices = vertices;
13+
this.edges = 0;
14+
adj = new ArrayList[vertices];
15+
for (int i = 0; i < vertices; i++)
16+
adj[i] = new ArrayList<>();
17+
18+
}
19+
20+
@Override
21+
public void addEdge(int from, int to) {
22+
this.edges++;
23+
this.adj[from].add(to);
24+
}
25+
26+
@Override
27+
public Iterable<Integer> adj(int vertex) {
28+
return this.adj[vertex];
29+
}
30+
31+
@Override
32+
public int V() {
33+
return this.vertices;
34+
}
35+
36+
@Override
37+
public int E() {
38+
return this.edges;
39+
}
40+
41+
@Override
42+
public String toString() {
43+
StringBuilder stringBuilder = new StringBuilder();
44+
45+
stringBuilder.append("-------Graph-------\n");
46+
47+
for (int i = 0; i < this.vertices; i++) {
48+
stringBuilder.append(i).append(adj[i].toString()).append("\n");
49+
}
50+
51+
return stringBuilder.toString();
52+
}
53+
}

Diff for: src/main/java/datastructures/graph/TestUndirectedGraph.java

+28-7
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,43 @@ public static void main(String[] args) {
4848
graph2.addEdge(11, 12);
4949

5050

51+
graph = new Digraph(5);
5152

53+
graph.addEdge(0, 1);
54+
graph.addEdge(1, 2);
55+
graph.addEdge(2, 3);
56+
graph.addEdge(2, 4);
5257

58+
// graph.adj(1).forEach(integer -> System.out.println(integer));
59+
System.out.println(graph.toString());
5360

61+
depthFirstPaths = new DepthFirstPaths(graph, 0);
62+
System.out.println(depthFirstPaths.hasPathTo(4));
5463

55-
ConnectedComponent connectedComponent = new ConnectedComponent(graph2);
64+
System.out.println();
65+
System.out.println("DFS");
66+
for (int path : depthFirstPaths.pathTo(4)) {
67+
System.out.print(path + "->");
68+
}
5669

70+
breadthFirstPaths = new BreadthFirstPaths(graph, 0);
5771
System.out.println();
72+
System.out.println("BFS");
73+
for (int path : breadthFirstPaths.pathTo(4)) {
74+
System.out.print(path + "->");
75+
}
5876

59-
System.out.println("Number of Connected Components : "+ connectedComponent.count());
60-
System.out.println("Connected 0 & 6 : "+ connectedComponent.connected(0, 6));
61-
System.out.println("Connected 0 & 8 : "+ connectedComponent.connected(0, 8));
62-
System.out.println("Component of 4 : "+ connectedComponent.id(4));
63-
System.out.println("Component of 7 : "+ connectedComponent.id(7));
64-
System.out.println("Component of 12 : "+ connectedComponent.id(12));
6577

78+
ConnectedComponent connectedComponent = new ConnectedComponent(graph2);
79+
80+
System.out.println();
6681

82+
System.out.println("Number of Connected Components : " + connectedComponent.count());
83+
System.out.println("Connected 0 & 6 : " + connectedComponent.connected(0, 6));
84+
System.out.println("Connected 0 & 8 : " + connectedComponent.connected(0, 8));
85+
System.out.println("Component of 4 : " + connectedComponent.id(4));
86+
System.out.println("Component of 7 : " + connectedComponent.id(7));
87+
System.out.println("Component of 12 : " + connectedComponent.id(12));
6788

6889

6990
}

Diff for: src/main/java/problems/onRecursionAndDp/PhoneKeypad.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ public class PhoneKeypad {
77
private String[] MAPPINGS = {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
88

99

10-
public List<String> letterCombinations(String digits) {
11-
List<String> res = new ArrayList<>();
10+
public ArrayList<String> letterCombinations(String digits) {
11+
ArrayList<String> res = new ArrayList<>();
1212
if (digits == null || digits.length() == 0) return res;
1313
StringBuilder builder = new StringBuilder();
1414
find(res, 0, builder, digits);

Diff for: src/main/java/problems/ongraph/FIndJudge.java

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package problems.ongraph;
2+
3+
public class FIndJudge {
4+
/**
5+
* 997. Find the Town Judge
6+
* Easy
7+
*
8+
* 290
9+
*
10+
* 41
11+
*
12+
* Favorite
13+
*
14+
* Share
15+
* In a town, there are N people labelled from 1 to N. There is a rumor that one of these people is secretly the town judge.
16+
*
17+
* If the town judge exists, then:
18+
*
19+
* The town judge trusts nobody.
20+
* Everybody (except for the town judge) trusts the town judge.
21+
* There is exactly one person that satisfies properties 1 and 2.
22+
* You are given trust, an array of pairs trust[i] = [a, b] representing that the person labelled a trusts the person labelled b.
23+
*
24+
* If the town judge exists and can be identified, return the label of the town judge. Otherwise, return -1.
25+
*
26+
*
27+
*
28+
* Example 1:
29+
*
30+
* Input: N = 2, trust = [[1,2]]
31+
* Output: 2
32+
* Example 2:
33+
*
34+
* Input: N = 3, trust = [[1,3],[2,3]]
35+
* Output: 3
36+
* Example 3:
37+
*
38+
* Input: N = 3, trust = [[1,3],[2,3],[3,1]]
39+
* Output: -1
40+
* Example 4:
41+
*
42+
* Input: N = 3, trust = [[1,2],[2,3]]
43+
* Output: -1
44+
* Example 5:
45+
*
46+
* Input: N = 4, trust = [[1,3],[1,4],[2,3],[2,4],[4,3]]
47+
* Output: 3
48+
*
49+
*
50+
* Note:
51+
*
52+
* 1 <= N <= 1000
53+
* trust.length <= 10000
54+
* trust[i] are all different
55+
* trust[i][0] != trust[i][1]
56+
* 1 <= trust[i][0], trust[i][1] <= N
57+
* @param N
58+
* @param trust
59+
* @return
60+
*/
61+
public int findJudge(int N, int[][] trust) {
62+
int[] in = new int[N + 1];
63+
int[] out = new int[N + 1];
64+
for (int i = 0; i < trust.length; i++) {
65+
out[trust[i][0]]++;
66+
in[trust[i][1]]++;
67+
}
68+
69+
for (int i = 1; i <= N; i++) {
70+
if (out[i] == 0 && in[i] == N - 1) return i;
71+
}
72+
73+
return -1;
74+
}
75+
76+
//concept of indegree and outdegree is used
77+
//judge will satisfy, out[i] == 0 && in[i] == N - 1
78+
}
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package problems.ongraph;
2+
3+
import datastructures.graph.DepthFirstPaths;
4+
import datastructures.graph.Digraph;
5+
import datastructures.graph.Graph;
6+
7+
import java.util.Arrays;
8+
import java.util.Stack;
9+
10+
public class TopologicalSorting {
11+
boolean[] marked;
12+
int s;
13+
Stack<Integer> topoOrder;
14+
15+
public TopologicalSorting(Graph G, int s) {
16+
this.s = s;
17+
marked = new boolean[G.V()];
18+
topoOrder = new Stack<>();
19+
dfs(G, s);
20+
}
21+
22+
23+
public void dfs(Graph g, int v) {
24+
marked[v] = true;
25+
for (int i : g.adj(v)) {
26+
if (!marked[i]) {
27+
dfs(g, i);
28+
}
29+
}
30+
topoOrder.push(v);
31+
}
32+
33+
public static void main(String[] args) {
34+
Graph graph = new Digraph(5);
35+
36+
/**
37+
* 4<--0-->1-->2
38+
* |
39+
* 3-->2
40+
*
41+
*/
42+
43+
graph.addEdge(0, 1);
44+
graph.addEdge(1, 2);
45+
graph.addEdge(1, 3);
46+
graph.addEdge(3, 2);
47+
graph.addEdge(4, 3);
48+
graph.addEdge(0, 4);
49+
50+
TopologicalSorting topologicalSorting = new TopologicalSorting(graph, 0);
51+
topologicalSorting.topoOrder.forEach(integer -> System.out.println(integer));
52+
}
53+
}

0 commit comments

Comments
 (0)