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
2c028de7
Commit
2c028de7
authored
11 months ago
by
Gianluca Mastrolonardo
Browse files
Options
Downloads
Patches
Plain Diff
fix es3
parent
7d462819
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/Lab4/Test/TestGraph.java
+13
-0
13 additions, 0 deletions
Algoritmi_2/Laboratorio/Lab4/Test/TestGraph.java
Algoritmi_2/Laboratorio/Lab4/src/BFSAndDFSApp.java
+46
-18
46 additions, 18 deletions
Algoritmi_2/Laboratorio/Lab4/src/BFSAndDFSApp.java
with
59 additions
and
18 deletions
Algoritmi_2/Laboratorio/Lab4/Test/TestGraph.java
+
13
−
0
View file @
2c028de7
import
it.uniupo.graphLib.DirectedGraph
;
import
it.uniupo.graphLib.Edge
;
import
it.uniupo.graphLib.GraphInterface
;
import
it.uniupo.graphLib.UndirectedGraph
;
import
org.junit.jupiter.api.Assertions
;
import
org.junit.jupiter.api.Test
;
import
java.security.interfaces.EdECKey
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
...
...
@@ -65,4 +67,15 @@ public class TestGraph {
testApp
=
new
BFSAndDFSApp
(
g
);
Assertions
.
assertTrue
(
testApp
.
hasDirCycle
());
}
@Test
void
testgetShortestPath
()
{
DirectedGraph
g
=
new
DirectedGraph
(
"5; 3 4; 4 2; 2 3; 4 1; 1 2; 1 0"
);
BFSAndDFSApp
testApp
=
new
BFSAndDFSApp
(
g
);
ArrayList
<
Edge
>
path
=
testApp
.
getShortestPath
(
4
,
0
);
Assertions
.
assertTrue
(
path
.
contains
(
new
Edge
(
4
,
1
)));
Assertions
.
assertTrue
(
path
.
contains
(
new
Edge
(
1
,
0
)));
ArrayList
<
Edge
>
path2
=
testApp
.
getShortestPath
(
0
,
4
);
Assertions
.
assertTrue
(
path2
.
isEmpty
());
}
}
This diff is collapsed.
Click to expand it.
Algoritmi_2/Laboratorio/Lab4/src/BFSAndDFSApp.java
+
46
−
18
View file @
2c028de7
import
it.uniupo.graphLib.Edge
;
import
it.uniupo.graphLib.GraphInterface
;
import
it.uniupo.graphLib.UndirectedGraph
;
import
java.util.Arrays
;
import
java.util.ArrayList
;
import
java.util.*
;
public
class
BFSAndDFSApp
{
...
...
@@ -14,8 +14,7 @@ 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
);
...
...
@@ -25,8 +24,7 @@ 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"
);
}
...
...
@@ -43,8 +41,24 @@ class BFSAndDFSApp {
return
false
;
}
public
boolean
hasUndirectedCycle
()
{
private
void
visitaBFS
(
int
sorg
)
{
scoperti
[
sorg
]
=
true
;
Queue
<
Integer
>
q
=
new
LinkedList
<>();
q
.
add
(
sorg
);
while
(!
q
.
isEmpty
())
{
int
node
=
q
.
remove
();
for
(
Integer
neighbour
:
this
.
myGraph
.
getNeighbors
(
node
))
{
if
(!
scoperti
[
neighbour
])
{
scoperti
[
neighbour
]
=
true
;
q
.
add
(
neighbour
);
fathers
[
neighbour
]
=
node
;
}
}
}
}
public
boolean
hasUndirectedCycle
()
{
for
(
int
nodo
=
0
;
nodo
<
this
.
myGraph
.
getOrder
();
nodo
++)
{
if
(
visitaDFS
(
nodo
))
{
return
true
;
...
...
@@ -53,8 +67,7 @@ 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"
);
}
...
...
@@ -67,33 +80,33 @@ 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
])
{
fathers
[
node
]
=
sorg
;
return
hasDirCycleImpl
(
node
);
}
else
if
(
node
!=
fathers
[
sorg
])
{
if
(
hasDirCycleImpl
(
node
))
{
return
true
;
}
}
else
if
(
fathers
[
sorg
]
!=
-
1
&&
node
!=
fathers
[
sorg
])
{
return
true
;
}
}
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
++)
{
System
.
out
.
println
(
"pup!"
);
fathers
=
new
int
[
myGraph
.
getOrder
()];
Arrays
.
fill
(
fathers
,
-
1
);
scoperti
=
new
boolean
[
myGraph
.
getOrder
()];
...
...
@@ -104,4 +117,19 @@ class BFSAndDFSApp {
}
return
false
;
}
public
ArrayList
<
Edge
>
getShortestPath
(
int
sorg
,
int
dest
)
{
ArrayList
<
Edge
>
path
=
new
ArrayList
<>();
visitaBFS
(
sorg
);
int
tmp
=
dest
;
while
(
fathers
[
tmp
]
!=
-
1
)
{
int
x
=
tmp
;
tmp
=
fathers
[
tmp
];
path
.
add
(
new
Edge
(
tmp
,
x
));
}
//path.add(new Edge(sorg, tmp));
Collections
.
reverse
(
path
);
return
path
;
}
}
\ 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