Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Ø
øving 9
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
Container 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
Awesome group
øving 9
Commits
6c0933c6
Commit
6c0933c6
authored
2 years ago
by
Pedro Pablo Cardona Arroyave
Browse files
Options
Downloads
Patches
Plain Diff
New implementation of ALT
parent
198531a6
Branches
AltNodeClass
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
.idea/runConfigurations.xml
+10
-0
10 additions, 0 deletions
.idea/runConfigurations.xml
src/Task9.java
+127
-1
127 additions, 1 deletion
src/Task9.java
with
137 additions
and
1 deletion
.idea/runConfigurations.xml
0 → 100644
+
10
−
0
View file @
6c0933c6
<?xml version="1.0" encoding="UTF-8"?>
<project
version=
"4"
>
<component
name=
"RunConfigurationProducerService"
>
<option
name=
"ignoredProducers"
>
<set>
<option
value=
"com.android.tools.idea.compose.preview.runconfiguration.ComposePreviewRunConfigurationProducer"
/>
</set>
</option>
</component>
</project>
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/Task9.java
+
127
−
1
View file @
6c0933c6
...
...
@@ -156,6 +156,59 @@ class Edge implements Comparable<Edge>{
}
}
class
ALTEdge
implements
Comparable
<
ALTEdge
>{
int
start
;
int
destination
;
int
drivingTime
;
int
currentWeight
=
0
;
int
fromLandMark
=
0
;
int
toLandMark
=
0
;
int
destinationDelta
;
ALTEdge
(
int
start
,
int
destination
,
int
drivingTime
){
this
.
start
=
start
;
this
.
destination
=
destination
;
this
.
drivingTime
=
drivingTime
;
}
public
int
getStart
()
{
return
start
;
}
public
int
getDestination
()
{
return
destination
;
}
public
int
getDrivingTime
()
{
return
drivingTime
;
}
public
int
getCurrentWeight
()
{
return
currentWeight
;
}
public
void
setCurrentWeight
(
int
currentWeight
)
{
this
.
currentWeight
=
currentWeight
;
}
public
void
setFromLandMark
(
int
fromLandMark
)
{
this
.
fromLandMark
=
fromLandMark
;
}
public
void
setDestinationDelta
(
int
destinationDelta
){
this
.
destinationDelta
=
destinationDelta
;
}
private
int
getDelta
(){
if
((
destinationDelta
-
fromLandMark
)
<
0
)
return
0
;
else
return
destinationDelta
-
fromLandMark
;
}
@Override
public
int
compareTo
(
ALTEdge
o
)
{
return
Integer
.
compare
(
drivingTime
+
currentWeight
-
getDelta
(),
o
.
drivingTime
+
o
.
currentWeight
-
o
.
getDelta
());
}
}
class
Graph
{
private
final
HashMap
<
Integer
,
List
<
Edge
>>
adjacencyList
=
new
HashMap
<>();
private
final
List
<
Node
>
nodes
=
new
ArrayList
<>();
...
...
@@ -313,6 +366,79 @@ class Graph{
}
}
class
ALTGraph
{
private
final
HashMap
<
Integer
,
List
<
ALTEdge
>>
adjacencyList
=
new
HashMap
<>();
private
final
List
<
Node
>
nodes
=
new
ArrayList
<>();
public
List
<
Node
>
getNodes
()
{
return
nodes
;
}
public
ALTGraph
(
String
inFile
)
throws
IOException
{
int
numberOfNodes
=
Utils
.
readFirstLine
(
inFile
);
for
(
int
i
=
0
;
i
<
numberOfNodes
;
++
i
)
{
adjacencyList
.
put
(
i
,
new
ArrayList
<>());
}
}
public
void
loadNodes
(
String
inFile
)
{
List
<
String
>
data
=
Utils
.
readFile
(
inFile
);
for
(
int
i
=
1
;
i
<
data
.
size
();
++
i
)
{
String
[]
splitText
=
Utils
.
dividedText
(
data
.
get
(
i
),
3
);
nodes
.
add
(
new
Node
(
Integer
.
parseInt
(
splitText
[
0
]),
Double
.
parseDouble
(
splitText
[
1
]),
Double
.
parseDouble
(
splitText
[
2
])));
}
System
.
out
.
println
(
"Nodes loaded."
);
}
public
void
loadALtEdges
(
String
inFileEdges
,
String
distanceFromLandMark
)
throws
Exception
{
List
<
String
>
data
=
Utils
.
readFile
(
inFileEdges
);
int
[]
distances
=
Utils
.
fileToArray
(
distanceFromLandMark
,
adjacencyList
.
size
());
for
(
int
i
=
1
;
i
<
data
.
size
();
++
i
)
{
String
[]
splitText
=
Utils
.
dividedText
(
data
.
get
(
i
),
3
);
addEdge
(
Integer
.
parseInt
(
splitText
[
0
]),
Integer
.
parseInt
(
splitText
[
1
]),
Integer
.
parseInt
(
splitText
[
2
]));
}
for
(
int
i
=
0
;
i
<
distances
.
length
;
i
++){
for
(
ALTEdge
edge:
adjacencyList
.
get
(
i
)){
edge
.
setDestinationDelta
(
distances
[
i
]);
}
}
System
.
out
.
println
(
"Edges loaded."
);
}
public
List
<
ALTEdge
>
getShortestPath
(
int
startNumber
,
int
destinationNumber
){
PriorityQueue
<
ALTEdge
>
priorityQueue
=
new
PriorityQueue
<>();
int
nodeSize
=
adjacencyList
.
size
();
int
[]
pathWeight
=
new
int
[
nodeSize
];
Arrays
.
fill
(
pathWeight
,
Integer
.
MAX_VALUE
);
ArrayList
<
ALTEdge
>[]
pathToNodes
=
new
ArrayList
[
nodeSize
];
pathWeight
[
startNumber
]
=
0
;
Arrays
.
fill
(
pathToNodes
,
new
ArrayList
<>());
priorityQueue
.
addAll
(
adjacencyList
.
get
(
startNumber
));
while
(!
priorityQueue
.
isEmpty
()){
ALTEdge
nextEdge
=
priorityQueue
.
poll
();
int
nextNode
=
nextEdge
.
getDestination
();
int
start
=
nextEdge
.
getStart
();
int
currentSum
=
pathWeight
[
start
]
+
nextEdge
.
getDrivingTime
();
if
(
currentSum
<
pathWeight
[
nextNode
])
{
pathWeight
[
nextNode
]
=
currentSum
;
pathToNodes
[
nextNode
]
=
new
ArrayList
<>(
pathToNodes
[
start
]);
pathToNodes
[
nextNode
].
add
(
nextEdge
);
for
(
ALTEdge
edge:
adjacencyList
.
get
(
nextNode
)){
edge
.
setCurrentWeight
(
pathWeight
[
start
]);
priorityQueue
.
add
(
edge
);
}
priorityQueue
.
addAll
(
adjacencyList
.
get
(
nextNode
));
}
if
(
nextNode
==
destinationNumber
)
return
pathToNodes
[
destinationNumber
];
}
return
null
;
}
public
void
addEdge
(
int
start
,
int
destination
,
int
drivingTime
)
{
adjacencyList
.
get
(
start
).
add
(
new
ALTEdge
(
start
,
destination
,
drivingTime
));
}
}
class
Utils
{
/**
* Static method that return a list that contains all the lines from an extern txt file
...
...
@@ -356,7 +482,7 @@ class Utils{
int
[]
array
=
new
int
[
size
];
try
(
BufferedReader
reader
=
Files
.
newBufferedReader
(
Paths
.
get
(
fileName
)))
{
String
currentLine
=
null
;
int
index
=
0
;
int
index
=
1
;
while
((
currentLine
=
reader
.
readLine
())
!=
null
)
{
array
[
index
]
=
Integer
.
parseInt
(
currentLine
);
++
index
;
...
...
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