Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
gs-algo
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
graphstream
gs-algo
Commits
581a9c5e
Commit
581a9c5e
authored
Jul 25, 2012
by
sbalev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Unit tests for Kruskal and Prim
parent
643c5f4c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
92 additions
and
0 deletions
+92
-0
src-test/org/graphstream/algorithm/test/TestKruskalPrim.java
src-test/org/graphstream/algorithm/test/TestKruskalPrim.java
+92
-0
No files found.
src-test/org/graphstream/algorithm/test/TestKruskalPrim.java
0 → 100644
View file @
581a9c5e
package
org.graphstream.algorithm.test
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
org.graphstream.algorithm.Kruskal
;
import
org.graphstream.algorithm.Prim
;
import
org.graphstream.graph.Edge
;
import
org.graphstream.graph.Graph
;
import
org.graphstream.graph.implementations.SingleGraph
;
import
org.junit.Test
;
public
class
TestKruskalPrim
{
// B-----8-----C-----7-----D
// /| / \ |\
// / | / \ | \
// 4 | 2 \ | 9
// / | / \ | \
// / | / \ | \
// A 11 I 4 14 E
// \ | / \ \ | /
// \ | / \ \ | /
// 8 | 7 6 \ | 10
// \ | / \ \ | /
// \|/ \ \|/
// H-----1-----G-----2-----F
public
static
Graph
toyGraph
()
{
String
[]
eIds
=
{
"AB"
,
"AH"
,
"BH"
,
"BC"
,
"HI"
,
"HG"
,
"IC"
,
"IG"
,
"CD"
,
"CF"
,
"GF"
,
"DF"
,
"DE"
,
"FE"
};
int
[]
weights
=
{
4
,
8
,
11
,
8
,
7
,
1
,
2
,
6
,
7
,
4
,
2
,
14
,
9
,
10
};
Graph
g
=
new
SingleGraph
(
"test"
,
false
,
true
);
for
(
int
i
=
0
;
i
<
eIds
.
length
;
i
++)
{
String
eId
=
eIds
[
i
];
Edge
e
=
g
.
addEdge
(
eId
,
eId
.
substring
(
0
,
1
),
eId
.
substring
(
1
,
2
));
e
.
addAttribute
(
"weight"
,
weights
[
i
]);
}
return
g
;
}
@Test
public
void
toyTest
()
{
Graph
g
=
toyGraph
();
Kruskal
k
=
new
Kruskal
(
"weight"
,
"kruskal"
);
k
.
init
(
g
);
k
.
compute
();
helper
(
k
,
g
,
37.0
,
8
);
Prim
p
=
new
Prim
(
"weight"
,
"prim"
);
p
.
init
(
g
);
p
.
compute
();
helper
(
p
,
g
,
37.0
,
8
);
// remove the lightest edge
g
.
removeEdge
(
"HG"
);
k
.
compute
();
helper
(
k
,
g
,
43.0
,
8
);
p
.
compute
();
helper
(
p
,
g
,
43.0
,
8
);
// now cut the graph in 2 CC
g
.
removeEdge
(
"BC"
);
g
.
removeEdge
(
"HI"
);
k
.
compute
();
helper
(
k
,
g
,
36.0
,
7
);
p
.
compute
();
helper
(
p
,
g
,
36.0
,
7
);
}
public
void
helper
(
Kruskal
k
,
Graph
g
,
double
expectedWeight
,
int
expectedCount
)
{
assertEquals
(
expectedWeight
,
k
.
getTreeWeight
(),
0
);
int
edgeCount
=
0
;
for
(
Edge
e
:
k
.
getTreeEdges
())
{
boolean
b
=
e
.
getAttribute
(
k
.
getFlagAttribute
());
assertTrue
(
b
);
edgeCount
++;
}
assertEquals
(
expectedCount
,
edgeCount
);
edgeCount
=
0
;
double
treeWeight
=
0
;
for
(
Edge
e
:
g
.
getEachEdge
())
{
boolean
b
=
e
.
getAttribute
(
k
.
getFlagAttribute
());
if
(
b
)
{
edgeCount
++;
treeWeight
+=
e
.
getNumber
(
"weight"
);
}
}
assertEquals
(
expectedWeight
,
treeWeight
,
0
);
assertEquals
(
expectedCount
,
edgeCount
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment