Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
Appunti
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Gianluca Mastrolonardo
Appunti
Commits
0f0702bc
Commit
0f0702bc
authored
11 months ago
by
Gianluca
Browse files
Options
Downloads
Patches
Plain Diff
to finish
parent
a2d15ce1
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Algoritmi_2/Laboratorio/Lab4/Test/TestGraph.java
+29
-9
29 additions, 9 deletions
Algoritmi_2/Laboratorio/Lab4/Test/TestGraph.java
Algoritmi_2/Laboratorio/Lab4/src/BFSAndDFSApp.java
+32
-5
32 additions, 5 deletions
Algoritmi_2/Laboratorio/Lab4/src/BFSAndDFSApp.java
with
61 additions
and
14 deletions
Algoritmi_2/Laboratorio/Lab4/Test/TestGraph.java
+
29
−
9
View file @
0f0702bc
...
...
@@ -9,7 +9,7 @@ import java.util.Arrays;
public
class
TestGraph
{
@Test
void
hasUndirectedCycleTest
(){
void
hasUndirectedCycleTest
()
{
UndirectedGraph
myGraph
=
new
UndirectedGraph
(
"4; 1 0; 0 2; 2 3; 3 0"
);
BFSAndDFSApp
testApp
=
new
BFSAndDFSApp
(
myGraph
);
Assertions
.
assertTrue
(
testApp
.
hasUndirectedCycle
());
...
...
@@ -22,12 +22,10 @@ public class TestGraph {
}
@Test
void
orderPostVisitTest
(){
void
orderPostVisitTest
()
{
UndirectedGraph
myGraph
=
new
UndirectedGraph
(
"4; 2 0; 1 0; 3 2; 3 1"
);
BFSAndDFSApp
testApp
=
new
BFSAndDFSApp
(
myGraph
);
ArrayList
<
Integer
>
output
=
testApp
.
getNodesInOrderPostVisit
(
0
);
System
.
out
.
println
(
output
);
ArrayList
<
Integer
>
expected1
=
new
ArrayList
<>();
...
...
@@ -37,12 +35,34 @@ public class TestGraph {
expected1
.
add
(
0
);
ArrayList
<
Integer
>
expected2
=
new
ArrayList
<>();
expected1
.
add
(
2
);
expected1
.
add
(
3
);
expected1
.
add
(
1
);
expected1
.
add
(
0
);
expected2
.
add
(
2
);
expected2
.
add
(
3
);
expected2
.
add
(
1
);
expected2
.
add
(
0
);
Assertions
.
assertTrue
(
output
.
equals
(
expected1
)
||
output
.
equals
(
expected2
));
}
@Test
void
hasDirCycleTest
()
{
DirectedGraph
g
=
new
DirectedGraph
(
"1"
);
BFSAndDFSApp
testApp
=
new
BFSAndDFSApp
(
g
);
Assertions
.
assertFalse
(
testApp
.
hasDirCycle
());
g
=
new
DirectedGraph
(
"2; 0 1"
);
testApp
=
new
BFSAndDFSApp
(
g
);
Assertions
.
assertFalse
(
testApp
.
hasDirCycle
());
g
=
new
DirectedGraph
(
"3; 1 0; 1 2; 0 2"
);
testApp
=
new
BFSAndDFSApp
(
g
);
Assertions
.
assertFalse
(
testApp
.
hasDirCycle
());
g
=
new
DirectedGraph
(
"3; 0 2; 2 1; 1 0"
);
testApp
=
new
BFSAndDFSApp
(
g
);
Assertions
.
assertTrue
(
testApp
.
hasDirCycle
());
Assertions
.
assertTrue
(
output
.
equals
(
expected1
)
||
output
.
equals
(
expected2
));
g
=
new
DirectedGraph
(
"5; 0 4; 4 1; 4 2; 3 4; 2 3"
);
testApp
=
new
BFSAndDFSApp
(
g
);
Assertions
.
assertTrue
(
testApp
.
hasDirCycle
());
}
}
This diff is collapsed.
Click to expand it.
Algoritmi_2/Laboratorio/Lab4/src/BFSAndDFSApp.java
+
32
−
5
View file @
0f0702bc
import
it.uniupo.graphLib.GraphInterface
;
import
it.uniupo.graphLib.UndirectedGraph
;
import
java.util.Arrays
;
import
java.util.ArrayList
;
...
...
@@ -12,7 +13,7 @@ public class BFSAndDFSApp {
private
int
[]
fathers
;
GraphInterface
treeDFS
;
public
BFSAndDFSApp
(
GraphInterface
g
){
public
BFSAndDFSApp
(
GraphInterface
g
)
{
myGraph
=
g
;
BFS
bfs
=
new
BFS
(
g
);
DFS
dfs
=
new
DFS
(
g
);
...
...
@@ -32,8 +33,7 @@ public class BFSAndDFSApp {
fathers
[
node
]
=
node
;
treeDFS
.
addEdge
(
sorg
,
node
);
return
visitaDFS
(
node
);
}
else
if
(
node
!=
fathers
[
node
]){
}
else
if
(
node
!=
fathers
[
node
])
{
return
true
;
}
}
...
...
@@ -42,7 +42,7 @@ public class BFSAndDFSApp {
public
boolean
hasUndirectedCycle
()
{
for
(
int
nodo
=
0
;
nodo
<
this
.
myGraph
.
getOrder
();
nodo
++)
{
if
(
visitaDFS
(
nodo
)){
if
(
visitaDFS
(
nodo
))
{
return
true
;
}
}
...
...
@@ -62,9 +62,36 @@ public class BFSAndDFSApp {
orderPostVisit
.
add
(
sorg
);
}
public
ArrayList
<
Integer
>
getNodesInOrderPostVisit
(
int
sorg
){
public
ArrayList
<
Integer
>
getNodesInOrderPostVisit
(
int
sorg
)
{
ArrayList
<
Integer
>
orderPostVisit
=
new
ArrayList
<>();
visitaDFSPostVisit
(
sorg
,
orderPostVisit
);
return
orderPostVisit
;
}
private
boolean
hasDirCycleImpl
(
int
sorg
)
{
scoperti
[
sorg
]
=
true
;
for
(
Integer
node
:
myGraph
.
getNeighbors
(
sorg
))
{
if
(!
scoperti
[
node
])
{
fathers
[
node
]
=
sorg
;
return
hasDirCycleImpl
(
node
);
}
else
if
(
node
!=
fathers
[
sorg
])
{
return
true
;
}
}
return
false
;
}
public
boolean
hasDirCycle
()
{
if
(
myGraph
instanceof
UndirectedGraph
)
{
throw
new
IllegalArgumentException
(
"Impossibile usare hasDirCycle su un grafo non orientato"
);
}
for
(
int
i
=
0
;
i
<
myGraph
.
getOrder
();
i
++)
{
fathers
=
new
int
[
myGraph
.
getOrder
()];
scoperti
=
new
boolean
[
myGraph
.
getOrder
()];
if
(
hasDirCycleImpl
(
i
))
{
return
true
;
}
}
return
false
;
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment