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
957f5211
Commit
957f5211
authored
2 years ago
by
Pedro Pablo Cardona Arroyave
Browse files
Options
Downloads
Patches
Plain Diff
Inverse graph method was added
parent
0e4e6391
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Task9.java
+32
-46
32 additions, 46 deletions
src/Task9.java
with
32 additions
and
46 deletions
src/Task9.java
+
32
−
46
View file @
957f5211
import
java.io.*
;
import
java.io.BufferedReader
;
import
java.io.FileOutputStream
;
import
java.io.FileReader
;
import
java.io.IOException
;
import
java.nio.ByteBuffer
;
import
java.nio.channels.FileChannel
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.util.*
;
import
java.util.stream.Stream
;
public
class
Task9
{
public
static
void
main
(
String
[]
args
)
{
Graph
g
=
null
;
try
{
g
=
new
Graph
(
"noder.txt"
);
g
=
new
Graph
(
"noder
Iceland
.txt"
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
Objects
.
requireNonNull
(
g
).
loadEdges
(
"kanter.txt"
);
//g.loadNodes("noder.txt");
/*int startNumber = 0;
Objects
.
requireNonNull
(
g
).
loadEdges
(
"kanterIceland.txt"
);
int
startNumber
=
0
;
List
<
Edge
>
edges
=
g
.
getShortestPath
(
0
,
3
);
System
.
out
.
println
(
startNumber
);
for
(
Edge
edge:
edges
)
{
System
.
out
.
println
(
edge
.
getDestination
());
}
System
.
out
.
println
(
"----------------------- Dijkstra Done --------------------------------"
);
g.readNodeInformation("interessepkt.txt");
g
.
loadNodes
(
"noder Iceland.txt"
);
g
.
readNodeInformation
(
"interessepktIceland.txt"
);
Node
[]
listOfPlaces
=
g
.
getListOfPlaces
(
0
,
Byte
.
parseByte
(
"2"
),
Byte
.
parseByte
(
"8"
));
for
(
Node
node:
listOfPlaces
){
System
.
out
.
println
(
node
.
getLatitude
()+
","
+
node
.
getLongitude
());
}*/
int
[]
array
=
g
.
fromLandmark
(
3430400
);
/*try {
Utils.arrayToFile(array, "testOslo.txt");
} catch (Exception e) {
e.printStackTrace();
}*/
try
{
int
[]
loadedArray
=
Utils
.
fileToArray
(
"testOslo.txt"
,
g
.
getSize
());
System
.
out
.
println
(
loadedArray
.
length
==
g
.
getSize
());
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
...
...
@@ -163,7 +154,6 @@ class Edge implements Comparable<Edge>{
}
class
Graph
{
private
final
int
size
;
private
final
HashMap
<
Integer
,
List
<
Edge
>>
adjacencyList
=
new
HashMap
<>();
private
final
List
<
Node
>
nodes
=
new
ArrayList
<>();
...
...
@@ -172,14 +162,12 @@ class Graph{
}
public
Graph
(
String
inFile
)
throws
IOException
{
size
=
Utils
.
readFirstLine
(
inFile
);
for
(
int
i
=
0
;
i
<
size
;
++
i
)
{
int
numberOfNodes
=
Utils
.
readFirstLine
(
inFile
);
for
(
int
i
=
0
;
i
<
numberOfNodes
;
++
i
)
{
adjacencyList
.
put
(
i
,
new
ArrayList
<>());
}
}
public
int
getSize
()
{
return
size
;}
public
void
loadNodes
(
String
inFile
)
{
List
<
String
>
data
=
Utils
.
readFile
(
inFile
);
for
(
int
i
=
1
;
i
<
data
.
size
();
++
i
)
{
...
...
@@ -198,6 +186,15 @@ class Graph{
System
.
out
.
println
(
"Edges loaded."
);
}
public
void
loadInverseEdges
(
String
inFile
){
List
<
String
>
data
=
Utils
.
readFile
(
inFile
);
for
(
int
i
=
1
;
i
<
data
.
size
();
++
i
)
{
String
[]
splitText
=
Utils
.
dividedText
(
data
.
get
(
i
),
3
);
addEdge
(
Integer
.
parseInt
(
splitText
[
1
]),
Integer
.
parseInt
(
splitText
[
0
]),
Integer
.
parseInt
(
splitText
[
2
]));
}
System
.
out
.
println
(
"Edges loaded."
);
}
public
void
readNodeInformation
(
String
inFile
){
List
<
String
>
data
=
Utils
.
readFile
(
inFile
);
for
(
int
i
=
1
;
i
<
data
.
size
();
++
i
)
{
...
...
@@ -210,6 +207,7 @@ class Graph{
}
public
void
addEdge
(
int
start
,
int
destination
,
int
drivingTime
)
{
adjacencyList
.
get
(
start
).
add
(
new
Edge
(
start
,
destination
,
drivingTime
));
}
...
...
@@ -280,7 +278,6 @@ class Graph{
public
int
[]
fromLandmark
(
int
landMark
){
PriorityQueue
<
Edge
>
priorityQueue
=
new
PriorityQueue
<>();
int
nodeSize
=
adjacencyList
.
size
();
System
.
out
.
println
(
nodeSize
);
int
[]
pathWeight
=
new
int
[
nodeSize
];
Arrays
.
fill
(
pathWeight
,
Integer
.
MAX_VALUE
);
pathWeight
[
landMark
]
=
0
;
...
...
@@ -296,11 +293,11 @@ class Graph{
for
(
Edge
edge:
adjacencyList
.
get
(
nextNode
)){
edge
.
setCurrentWeight
(
pathWeight
[
start
]);
priorityQueue
.
add
(
edge
);
}
index
++;
}
priorityQueue
.
addAll
(
adjacencyList
.
get
(
nextNode
));
}
if
(
index
==
s
ize
)
return
pathWeight
;
if
(
index
==
nodeS
ize
)
return
pathWeight
;
}
return
null
;
}
...
...
@@ -342,25 +339,14 @@ class Utils{
return
currentDirectoryPath
+
System
.
getProperty
(
"file.separator"
)
+
fileName
;
}
public
static
void
arrayToFile
(
int
[]
array
,
String
fileName
)
throws
IOException
{
FileWriter
writer
=
new
FileWriter
(
fileName
);
for
(
int
j
:
array
)
{
writer
.
write
(
j
+
"\n"
);
}
writer
.
close
();
}
public
static
int
[]
fileToArray
(
String
fileName
,
int
size
)
throws
Exception
{
int
[]
array
=
new
int
[
size
];
try
(
BufferedReader
reader
=
Files
.
newBufferedReader
(
Paths
.
get
(
fileName
)))
{
String
currentLine
=
null
;
int
index
=
0
;
while
((
currentLine
=
reader
.
readLine
())
!=
null
)
{
array
[
index
]
=
Integer
.
parseInt
(
currentLine
);
++
index
;
}
public
void
arrayToFile
(
int
[]
array
,
String
fileName
)
throws
IOException
{
FileOutputStream
out
=
new
FileOutputStream
(
fileName
);
FileChannel
file
=
out
.
getChannel
();
ByteBuffer
but
=
file
.
map
(
FileChannel
.
MapMode
.
READ_WRITE
,
0
,
4L
*
array
.
length
);
for
(
int
i
:
array
){
but
.
putInt
(
i
);
}
return
array
;
file
.
close
()
;
}
public
static
String
[]
dividedText
(
String
line
,
int
number
){
...
...
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