import
java.util.*;
class
Graph{
static
void
LexiDFS(HashMap<Character, Set<Character>> G,
char
S, HashMap<Character, Boolean> vis)
{
vis.put(S,
true
);
System.out.print(S +
" "
);
if
(G.containsKey(S))
{
for
(
char
i : G.get(S))
{
if
(!vis.containsKey(i) || !vis.get(i))
{
LexiDFS(G, i, vis);
}
}
}
}
static
void
CreateGraph(
int
N,
int
M,
char
S,
char
[][] Edges)
{
HashMap<Character, Set<Character>> G =
new
HashMap<>();
for
(
int
i =
0
; i < M; i++)
{
if
(G.containsKey(Edges[i][
0
]))
{
Set<Character> temp = G.get(Edges[i][
0
]);
temp.add(Edges[i][
1
]);
G.put(Edges[i][
0
], temp);
}
else
{
Set<Character> temp =
new
HashSet<>();
temp.add(Edges[i][
1
]);
G.put(Edges[i][
0
], temp);
}
}
HashMap<Character, Boolean> vis =
new
HashMap<>();
LexiDFS(G, S, vis);
}
public
static
void
main(String[] args)
{
int
N =
10
, M =
10
;
char
S =
'a'
;
char
[][] Edges = { {
'a'
,
'y'
}, {
'a'
,
'z'
},
{
'a'
,
'p'
}, {
'p'
,
'c'
},
{
'p'
,
'b'
}, {
'y'
,
'm'
},
{
'y'
,
'l'
}, {
'z'
,
'h'
},
{
'z'
,
'g'
}, {
'z'
,
'i'
} };
CreateGraph(N, M, S, Edges);
}
}