-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathGraph_Problem_16.java
More file actions
137 lines (122 loc) · 5.12 KB
/
Graph_Problem_16.java
File metadata and controls
137 lines (122 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Given a sorted Dictionary of an Alien language, find order of character
import java.util.*;
public class Graph_Problem_17 {
// Worst Approach: Brute Force
// This approach involves comparing each pair of words to determine the order of characters.
// Time Complexity: O(N^2 * M) where N is the number of words and M is the average length of a word.
public static String findOrderBruteForce(String[] words) {
// ...implementation...
return "";
}
// Better Approach: Using Adjacency List and Topological Sort (Kahn's Algorithm)
// This approach involves building a graph where each node is a character and edges represent the order.
// Then, perform a topological sort to determine the order of characters.
// Time Complexity: O(N * M) where N is the number of words and M is the average length of a word.
public static String findOrderUsingKahnsAlgorithm(String[] words) {
// Step 1: Create a graph
Map<Character, List<Character>> graph = new HashMap<>();
Map<Character, Integer> inDegree = new HashMap<>();
// Initialize the graph
for (String word : words) {
for (char c : word.toCharArray()) {
graph.putIfAbsent(c, new ArrayList<>());
inDegree.putIfAbsent(c, 0);
}
}
// Step 2: Build the graph
for (int i = 0; i < words.length - 1; i++) {
String word1 = words[i];
String word2 = words[i + 1];
int minLength = Math.min(word1.length(), word2.length());
for (int j = 0; j < minLength; j++) {
char parent = word1.charAt(j);
char child = word2.charAt(j);
if (parent != child) {
graph.get(parent).add(child);
inDegree.put(child, inDegree.get(child) + 1);
break;
}
}
}
// Step 3: Perform topological sort
Queue<Character> queue = new LinkedList<>();
for (char c : inDegree.keySet()) {
if (inDegree.get(c) == 0) {
queue.add(c);
}
}
StringBuilder order = new StringBuilder();
while (!queue.isEmpty()) {
char current = queue.poll();
order.append(current);
for (char neighbor : graph.get(current)) {
inDegree.put(neighbor, inDegree.get(neighbor) - 1);
if (inDegree.get(neighbor) == 0) {
queue.add(neighbor);
}
}
}
// If the order length is not equal to the number of unique characters, there is a cycle
if (order.length() != inDegree.size()) {
return "";
}
return order.toString();
}
// Best Approach: Using Depth-First Search (DFS) for Topological Sort
// This approach also involves building a graph and then performing a DFS to determine the order of characters.
// Time Complexity: O(N * M) where N is the number of words and M is the average length of a word.
public static String findOrderUsingDFS(String[] words) {
// Step 1: Create a graph
Map<Character, List<Character>> graph = new HashMap<>();
Set<Character> visited = new HashSet<>();
Set<Character> visiting = new HashSet<>();
StringBuilder order = new StringBuilder();
// Initialize the graph
for (String word : words) {
for (char c : word.toCharArray()) {
graph.putIfAbsent(c, new ArrayList<>());
}
}
// Step 2: Build the graph
for (int i = 0; i < words.length - 1; i++) {
String word1 = words[i];
String word2 = words[i + 1];
int minLength = Math.min(word1.length(), word2.length());
for (int j = 0; j < minLength; j++) {
char parent = word1.charAt(j);
char child = word2.charAt(j);
if (parent != child) {
graph.get(parent).add(child);
break;
}
}
}
// Step 3: Perform DFS
for (char c : graph.keySet()) {
if (!visited.contains(c)) {
if (!dfs(c, graph, visited, visiting, order)) {
return ""; // Cycle detected
}
}
}
return order.reverse().toString();
}
private static boolean dfs(char current, Map<Character, List<Character>> graph, Set<Character> visited, Set<Character> visiting, StringBuilder order) {
visiting.add(current);
for (char neighbor : graph.get(current)) {
if (visited.contains(neighbor)) {
continue;
}
if (visiting.contains(neighbor)) {
return false; // Cycle detected
}
if (!dfs(neighbor, graph, visited, visiting, order)) {
return false;
}
}
visiting.remove(current);
visited.add(current);
order.append(current);
return true;
}
}