FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Data-Structures/Graph/src/graph/DFS.java at master · AlgorithmCrackers/Data-Structures · GitHub
AlgorithmCrackers
/
Data-Structures
Public
Notifications
You must be signed in to change notification settings
Fork
9
Star
12
Code
Issues
2
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
Data-Structures
/
Graph
/
src
/
graph
/
DFS.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
90 lines (86 loc) · 2.37 KB
Breadcrumbs
Data-Structures
/
Graph
/
src
/
graph
/
DFS.java
Copy path
File metadata and controls
90 lines (86 loc) · 2.37 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
88
89
90
package
graph
;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
Collections
;
import
java
.
util
.
HashMap
;
import
java
.
util
.
LinkedHashMap
;
import
java
.
util
.
List
;
import
java
.
util
.
Map
;
class
Tuple
{
public
final
Object
x
;
public
final
Object
y
;
public
Tuple
(
Object
x
,
Object
y
) {
this
.
x
=
x
;
this
.
y
=
y
;
}
public
String
toString
() {
StringBuilder
s
=
new
StringBuilder
();
s
.
append
(
"("
+
x
+
", "
+
y
+
")"
);
return
s
.
toString
();
}
}
public
class
DFS
{
private
Map
<
Object
,
Object
>
parent
;
private
Map
<
Object
,
Integer
>
start_time
;
private
Map
<
Object
,
Integer
>
end_time
;
private
Map
<
Tuple
,
String
>
edges
;
// edge classification for DFS
private
int
time
=
0
;
private
List
<
Object
>
order
;
public
DFS
() {
parent
=
new
LinkedHashMap
<
Object
,
Object
>();
start_time
=
new
HashMap
<
Object
,
Integer
>();
end_time
=
new
HashMap
<
Object
,
Integer
>();
edges
=
new
HashMap
<
Tuple
,
String
>();
order
=
new
ArrayList
<
Object
>();
}
public
String
toString
() {
StringBuilder
s
=
new
StringBuilder
();
String
NEWLINE
=
System
.
getProperty
(
"line.separator"
);
s
.
append
(
"Parent: "
+
parent
+
NEWLINE
);
s
.
append
(
"Order: "
+
order
+
NEWLINE
);
s
.
append
(
"Edges: "
+
edges
+
NEWLINE
);
return
s
.
toString
();
}
public
static
DFS
dfs
(
Graph
g
) {
DFS
results
=
new
DFS
();
for
(
Object
v
:
g
.
itervertices
()) {
if
(!
results
.
parent
.
containsKey
(
v
)) {
dfs_visit
(
g
,
v
,
results
,
null
);
}
}
return
results
;
}
private
static
void
dfs_visit
(
Graph
g
,
Object
v
,
DFS
results
,
Object
parent
) {
results
.
parent
.
put
(
v
,
parent
);
results
.
time
++;
results
.
start_time
.
put
(
v
,
results
.
time
);
if
(
parent
!=
null
) {
Tuple
t
=
new
Tuple
(
parent
,
v
);
results
.
edges
.
put
(
t
,
"tree"
);
}
for
(
Object
a
:
g
.
adj
(
v
)) {
Tuple
t
=
new
Tuple
(
v
,
a
);
if
(!
results
.
parent
.
containsKey
(
a
)) {
// not visited already
dfs_visit
(
g
,
a
,
results
,
v
);
}
else
if
(!
results
.
end_time
.
containsKey
(
a
)) {
results
.
edges
.
put
(
t
,
"back"
);
}
else
if
(
results
.
start_time
.
get
(
v
) <
results
.
start_time
.
get
(
a
)) {
results
.
edges
.
put
(
t
,
"forward"
);
}
else
{
results
.
edges
.
put
(
t
,
"cross"
);
}
}
results
.
time
++;
results
.
end_time
.
put
(
v
,
results
.
time
);
results
.
order
.
add
(
v
);
}
public
static
List
<
Object
>
topological_sort
(
Graph
g
) {
DFS
d
=
DFS
.
dfs
(
g
);
Collections
.
reverse
(
d
.
order
);
return
d
.
order
;
}
public
static
void
main
(
String
[]
args
) {
System
.
out
.
println
(
DFS
.
dfs
(
Graph
.
getDfsExampleGraph
()));
}
}
Back
|
FazBrowse Home
|
New Git URL