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
9a778586
Commit
9a778586
authored
10 months ago
by
Gianluca
Browse files
Options
Downloads
Patches
Plain Diff
fixed Lab5
parent
4f596e42
No related branches found
Branches containing commit
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/Lab5/Test/TestLab.java
+4
-1
4 additions, 1 deletion
Algoritmi_2/Laboratorio/Lab5/Test/TestLab.java
Algoritmi_2/Laboratorio/Lab5/src/DFS.java
+8
-10
8 additions, 10 deletions
Algoritmi_2/Laboratorio/Lab5/src/DFS.java
with
12 additions
and
11 deletions
Algoritmi_2/Laboratorio/Lab5/Test/TestLab.java
+
4
−
1
View file @
9a778586
...
...
@@ -72,11 +72,14 @@ class TestLab {
DFS
dfsTest
=
new
DFS
(
dag
);
Assertions
.
assertTrue
(
dfsTest
.
topologicalOrderDag
().
indexOf
(
0
)
<
dfsTest
.
topologicalOrder
().
indexOf
(
3
));
System
.
out
.
println
(
dfsTest
.
hasDirCycle
());
DirectedGraph
cyclicGraph
=
new
DirectedGraph
(
"3; 0 1; 1 2; 2 0"
);
DFS
dfsCyclicTest
=
new
DFS
(
cyclicGraph
);
System
.
out
.
println
(
dfsCyclicTest
.
hasDirCycle
());
Assertions
.
assertThrows
(
IllegalArgumentException
.
class
,
()
->
dfsCyclicTest
.
topologicalOrderDag
());
//
Da sistemare, bisogna usare ordine fine visita invece che array di padri (per grafi orientati)
//
Ora funziona anche con questo grafo
DirectedGraph
cyclicGraph2
=
new
DirectedGraph
(
"2; 0 1; 1 0;"
);
DFS
dfsCyclicTest2
=
new
DFS
(
cyclicGraph2
);
Assertions
.
assertThrows
(
IllegalArgumentException
.
class
,
()
->
dfsCyclicTest2
.
topologicalOrderDag
());
...
...
This diff is collapsed.
Click to expand it.
Algoritmi_2/Laboratorio/Lab5/src/DFS.java
+
8
−
10
View file @
9a778586
...
...
@@ -12,7 +12,7 @@ class DFS {
private
GraphInterface
myGraph
;
private
boolean
[]
founded
;
private
GraphInterface
treeDFS
;
private
int
[]
fathers
;
private
ArrayList
<
Integer
>
terminated
=
new
ArrayList
<>()
;
private
ArrayList
<
Integer
>
postVisitOrder
=
new
ArrayList
<>();
...
...
@@ -66,16 +66,16 @@ class DFS {
private
boolean
hasDirCycleImpl
(
int
sorg
)
{
founded
[
sorg
]
=
true
;
for
(
Integer
node
:
myGraph
.
getNeighbors
(
sorg
))
{
if
(!
founded
[
node
])
{
fathers
[
node
]
=
sorg
;
if
(
hasDirCycleImpl
(
node
))
{
return
true
;
for
(
int
neighbour
:
myGraph
.
getNeighbors
(
sorg
))
{
if
(!
founded
[
neighbour
])
{
if
(
hasDirCycleImpl
(
neighbour
))
{
// Check the result of the recursive call
return
true
;
// If a cycle is detected in the recursive call, propagate it up
}
}
else
if
(
fathers
[
sorg
]
!=
-
1
&&
node
!=
fathers
[
sorg
]
)
{
}
else
if
(
!
terminated
.
contains
(
neighbour
)
)
{
return
true
;
}
}
terminated
.
add
(
sorg
);
return
false
;
}
...
...
@@ -85,9 +85,7 @@ class DFS {
throw
new
IllegalArgumentException
(
"Impossibile usare hasDirCycle su un grafo non orientato"
);
}
for
(
int
i
=
0
;
i
<
myGraph
.
getOrder
();
i
++)
{
fathers
=
new
int
[
myGraph
.
getOrder
()];
Arrays
.
fill
(
fathers
,
-
1
);
founded
=
new
boolean
[
myGraph
.
getOrder
()];
terminated
.
clear
();
Arrays
.
fill
(
founded
,
false
);
if
(
hasDirCycleImpl
(
i
))
{
return
true
;
...
...
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