Find the correct order of alphabets in a given dictionary of ancient origin

Given a dictionary of ancient origin where the words are arranged alphabetically, find the correct order of alphabets in the ancient language.

For example,

Input: Ancient dictionary { ¥€±, €±€, €±‰ð, ðß, ±±ð, ±ßß }

Output: The correct order of alphabets in the ancient language are: {¥ € ‰ ð ± ß}
Since the input is small, more than one ordering is possible. Another such ordering is {¥ € ð ± ß ‰}

Input: Ancient dictionary { ÿ€±š, €€€ß, €€‰ð, ðß, ±ß¥š }

Output: The correct order of alphabets in the ancient language are: {ÿ € ‰ ð ±}
The alphabets {š, ß, ¥} are not included in the order as they are not properly defined.

If we carefully analyze the problem, we can see that it is variation of topological sorting of a DAG. A Topological Sorting of a directed acyclic graph is a linear ordering of its vertices such that for every directed edge (u -> v) from vertex u to vertex v, u comes before v in the ordering.

For example, consider the dictionary { ¥€±, €±€, €±‰ð, ðß, ±±ð, ±ßß } of ancient words. For each of the edge (x -> y) shown below, x should appear before y in the final ordering.

¥ --> €
€ --> ð, ‰
± --> ß
ð --> ±

If we go a topological sorting on above graph, we get the correct order of alphabets in the ancient language: {¥ € ‰ ð ± ß} or {¥ € ð ± ß ‰}.

The idea is to iterate through the complete dictionary and compare adjacent words for a character mismatch. If a mismatch between adjacent words is seen, we insert such pair into a graph. The resultant graph is a DAG since the all words the dictionary are arranged alphabetically. Now since the graph has no directed cycles, we can perform topological sorting on it which gives correct order of alphabets in the ancient language.

The algorithm can be implemented as follows in C++:

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

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

#include <iostream>

#include <vector>

#include <unordered_map>

#include <unordered_set>

#include <algorithm>

usingnamespacestd;

// define maximum number of alphabets in the ancient dictionary

#define N 100

// Data structure to store graph edges

structEdge{

intsrc,dest;

};

// Class to represent a graph object

classGraph

{

public:

// construct a vector of vectors to represent an adjacency list

vector<unordered_set<int>>adjList;

// Graph Constructor

Graph()

{

// resize the vector to N elements of type unordered_set<int>

adjList.resize(N);

}

};

// Perform DFS on graph and set departure time of all vertices of the graph