FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/DataStructures/Graphs/Cycles.java at master · debugmm/Java · GitHub
debugmm
/
Java
Public
forked from
TheAlgorithms/Java
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java
/
DataStructures
/
Graphs
/
Cycles.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
87 lines (74 loc) · 2.03 KB
Breadcrumbs
Java
/
DataStructures
/
Graphs
/
Cycles.java
Copy path
File metadata and controls
87 lines (74 loc) · 2.03 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
DataStructures
.
Graphs
;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
Scanner
;
class
Cycle
{
private
int
nodes
,
edges
;
private
int
[][]
adjacencyMatrix
;
private
boolean
[]
visited
;
ArrayList
<
ArrayList
<
Integer
>>
cycles
=
new
ArrayList
<
ArrayList
<
Integer
>>();
public
Cycle
() {
Scanner
in
=
new
Scanner
(
System
.
in
);
System
.
out
.
print
(
"Enter the no. of nodes: "
);
nodes
=
in
.
nextInt
();
System
.
out
.
print
(
"Enter the no. of Edges: "
);
edges
=
in
.
nextInt
();
adjacencyMatrix
=
new
int
[
nodes
][
nodes
];
visited
=
new
boolean
[
nodes
];
for
(
int
i
=
0
;
i
<
nodes
;
i
++) {
visited
[
i
] =
false
;
}
System
.
out
.
println
(
"Enter the details of each edges <Start Node> <End Node>"
);
for
(
int
i
=
0
;
i
<
edges
;
i
++) {
int
start
,
end
;
start
=
in
.
nextInt
();
end
=
in
.
nextInt
();
adjacencyMatrix
[
start
][
end
] =
1
;
}
in
.
close
();
}
public
void
start
() {
for
(
int
i
=
0
;
i
<
nodes
;
i
++) {
ArrayList
<
Integer
>
temp
=
new
ArrayList
<>();
dfs
(
i
,
i
,
temp
);
for
(
int
j
=
0
;
j
<
nodes
;
j
++) {
adjacencyMatrix
[
i
][
j
] =
0
;
adjacencyMatrix
[
j
][
i
] =
0
;
}
}
}
private
void
dfs
(
Integer
start
,
Integer
curr
,
ArrayList
<
Integer
>
temp
) {
temp
.
add
(
curr
);
visited
[
curr
] =
true
;
for
(
int
i
=
0
;
i
<
nodes
;
i
++) {
if
(
adjacencyMatrix
[
curr
][
i
] ==
1
) {
if
(
i
==
start
) {
cycles
.
add
(
new
ArrayList
<
Integer
>(
temp
));
}
else
{
if
(!
visited
[
i
]) {
dfs
(
start
,
i
,
temp
);
}
}
}
}
if
(
temp
.
size
() >
0
) {
temp
.
remove
(
temp
.
size
() -
1
);
}
visited
[
curr
] =
false
;
}
public
void
printAll
() {
for
(
int
i
=
0
;
i
<
cycles
.
size
();
i
++) {
for
(
int
j
=
0
;
j
<
cycles
.
get
(
i
).
size
();
j
++) {
System
.
out
.
print
(
cycles
.
get
(
i
).
get
(
j
) +
" -> "
);
}
System
.
out
.
println
(
cycles
.
get
(
i
).
get
(
0
));
System
.
out
.
println
();
}
}
}
public
class
Cycles
{
public
static
void
main
(
String
[]
args
) {
Cycle
c
=
new
Cycle
();
c
.
start
();
c
.
printAll
();
}
}
Back
|
FazBrowse Home
|
New Git URL