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
0199fbdc
Commit
0199fbdc
authored
11 months ago
by
Gianluca
Browse files
Options
Downloads
Patches
Plain Diff
need fix
parent
0f0702bc
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Algoritmi_2/Laboratorio/Lab4/src/BFSAndDFSApp.java
+18
-8
18 additions, 8 deletions
Algoritmi_2/Laboratorio/Lab4/src/BFSAndDFSApp.java
with
18 additions
and
8 deletions
Algoritmi_2/Laboratorio/Lab4/src/BFSAndDFSApp.java
+
18
−
8
View file @
0199fbdc
...
...
@@ -4,7 +4,8 @@ import it.uniupo.graphLib.UndirectedGraph;
import
java.util.Arrays
;
import
java.util.ArrayList
;
public
class
BFSAndDFSApp
{
public
class
BFSAndDFSApp
{
private
GraphInterface
myGraph
;
private
BFS
bfs
;
private
DFS
dfs
;
...
...
@@ -13,7 +14,8 @@ 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
);
...
...
@@ -23,7 +25,8 @@ public class BFSAndDFSApp {
Arrays
.
fill
(
this
.
fathers
,
-
1
);
}
private
boolean
visitaDFS
(
int
sorg
)
{
private
boolean
visitaDFS
(
int
sorg
)
{
if
(
sorg
>=
this
.
myGraph
.
getOrder
()
||
sorg
<
0
)
{
throw
new
IllegalArgumentException
(
"Sorgente non presenten nel grafo"
);
}
...
...
@@ -40,7 +43,8 @@ public class BFSAndDFSApp {
return
false
;
}
public
boolean
hasUndirectedCycle
()
{
public
boolean
hasUndirectedCycle
()
{
for
(
int
nodo
=
0
;
nodo
<
this
.
myGraph
.
getOrder
();
nodo
++)
{
if
(
visitaDFS
(
nodo
))
{
return
true
;
...
...
@@ -49,7 +53,8 @@ public class BFSAndDFSApp {
return
false
;
}
private
void
visitaDFSPostVisit
(
int
sorg
,
ArrayList
<
Integer
>
orderPostVisit
)
{
private
void
visitaDFSPostVisit
(
int
sorg
,
ArrayList
<
Integer
>
orderPostVisit
)
{
if
(
sorg
>=
this
.
myGraph
.
getOrder
()
||
sorg
<
0
)
{
throw
new
IllegalArgumentException
(
"Sorgente non presenten nel grafo"
);
}
...
...
@@ -62,13 +67,15 @@ 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
)
{
private
boolean
hasDirCycleImpl
(
int
sorg
)
{
scoperti
[
sorg
]
=
true
;
for
(
Integer
node
:
myGraph
.
getNeighbors
(
sorg
))
{
if
(!
scoperti
[
node
])
{
...
...
@@ -81,13 +88,16 @@ public class BFSAndDFSApp {
return
false
;
}
public
boolean
hasDirCycle
()
{
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
()];
Arrays
.
fill
(
fathers
,
-
1
);
scoperti
=
new
boolean
[
myGraph
.
getOrder
()];
Arrays
.
fill
(
scoperti
,
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