FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/DataStructures/Graphs/ConnectedComponent.java at master · devil00/Java · GitHub
devil00
/
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
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
Java
/
DataStructures
/
Graphs
/
ConnectedComponent.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
150 lines (128 loc) · 3.43 KB
Breadcrumbs
Java
/
DataStructures
/
Graphs
/
ConnectedComponent.java
Copy path
File metadata and controls
150 lines (128 loc) · 3.43 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
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
import
java
.
util
.
ArrayList
;
import
java
.
util
.
HashSet
;
import
java
.
util
.
Set
;
/**
* A class that counts the number of different connected components in a graph
*
* @author Lukas Keul, Florian Mercks
*
*/
class
Graph
<
E
extends
Comparable
<
E
>> {
class
Node
{
E
name
;
public
Node
(
E
name
) {
this
.
name
=
name
;
}
}
class
Edge
{
Node
startNode
,
endNode
;
public
Edge
(
Node
startNode
,
Node
endNode
) {
this
.
startNode
=
startNode
;
this
.
endNode
=
endNode
;
}
}
ArrayList
<
Edge
>
edgeList
;
ArrayList
<
Node
>
nodeList
;
public
Graph
() {
edgeList
=
new
ArrayList
<
Edge
>();
nodeList
=
new
ArrayList
<
Node
>();
}
/**
* Adds a new Edge to the graph. If the nodes aren't yet in nodeList, they
* will be added to it.
*
* @param startNode
* the starting Node from the edge
*
* @param endNode
* the ending Node from the edge
*/
public
void
addEdge
(
E
startNode
,
E
endNode
) {
Node
start
=
null
,
end
=
null
;
for
(
Node
node
:
nodeList
) {
if
(
startNode
.
compareTo
(
node
.
name
) ==
0
) {
start
=
node
;
}
else
if
(
endNode
.
compareTo
(
node
.
name
) ==
0
) {
end
=
node
;
}
}
if
(
start
==
null
) {
start
=
new
Node
(
startNode
);
nodeList
.
add
(
start
);
}
if
(
end
==
null
) {
end
=
new
Node
(
endNode
);
nodeList
.
add
(
end
);
}
edgeList
.
add
(
new
Edge
(
start
,
end
));
}
/**
* Main method used for counting the connected components. Iterates through
* the array of nodes to do a depth first search to get all nodes of the
* graph from the actual node. These nodes are added to the array
* markedNodes and will be ignored if they are chosen in the nodeList.
*
* @return returns the amount of unconnected graphs
*
*/
public
int
countGraphs
() {
int
count
=
0
;
Set
<
Node
>
markedNodes
=
new
HashSet
<
Node
>();
for
(
Node
n
:
nodeList
) {
if
(!
markedNodes
.
contains
(
n
)) {
markedNodes
.
add
(
n
);
markedNodes
.
addAll
(
depthFirstSearch
(
n
,
new
ArrayList
<
Node
>()));
count
++;
}
}
return
count
;
}
/**
* Implementation of depth first search.
*
* @param n
* the actual visiting node
*
* @param visited
* A list of already visited nodes in the depth first search
*
* @return returns a set of visited nodes
*
*/
public
ArrayList
<
Node
>
depthFirstSearch
(
Node
n
,
ArrayList
<
Node
>
visited
) {
visited
.
add
(
n
);
for
(
Edge
e
:
edgeList
) {
if
(
e
.
startNode
.
equals
(
n
) && !
visited
.
contains
(
e
.
endNode
)) {
depthFirstSearch
(
e
.
endNode
,
visited
);
}
}
return
visited
;
}
}
public
class
ConnectedComponent
{
public
static
void
main
(
String
[]
args
) {
Graph
graphChars
=
new
Graph
();
// Graph 1
graphChars
.
addEdge
(
'a'
,
'b'
);
graphChars
.
addEdge
(
'a'
,
'e'
);
graphChars
.
addEdge
(
'b'
,
'e'
);
graphChars
.
addEdge
(
'b'
,
'c'
);
graphChars
.
addEdge
(
'c'
,
'd'
);
graphChars
.
addEdge
(
'd'
,
'a'
);
graphChars
.
addEdge
(
'x'
,
'y'
);
graphChars
.
addEdge
(
'x'
,
'z'
);
graphChars
.
addEdge
(
'w'
,
'w'
);
Graph
graphInts
=
new
Graph
();
// Graph 2
graphInts
.
addEdge
(
1
,
2
);
graphInts
.
addEdge
(
2
,
3
);
graphInts
.
addEdge
(
2
,
4
);
graphInts
.
addEdge
(
3
,
5
);
graphInts
.
addEdge
(
7
,
8
);
graphInts
.
addEdge
(
8
,
10
);
graphInts
.
addEdge
(
10
,
8
);
System
.
out
.
println
(
"Amount of different char-graphs: "
+
graphChars
.
countGraphs
());
System
.
out
.
println
(
"Amount of different int-graphs: "
+
graphInts
.
countGraphs
());
}
}
Back
|
FazBrowse Home
|
New Git URL