Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
trh010
cs311_final_project
Commits
c6508433
Commit
c6508433
authored
Apr 21, 2014
by
trh010
Browse files
working
parent
09039f55
Changes
6
Hide whitespace changes
Inline
Side-by-side
CS311_final_project/src/5lw-s.dat
View file @
c6508433
BEAST BLAST BOAST BOATS
COAST COATS CODES COSTS
ROADS ROAST TOADS TOAST
CS311_final_project/src/heap/Heap.java
View file @
c6508433
...
...
@@ -30,10 +30,10 @@ public class Heap {
HeapElt
temp
=
array
[
pos2
];
array
[
pos2
]
=
array
[
pos1
];
array
[
pos2
].
set
AdjListIndex
(
pos2
);
array
[
pos2
].
set
Handle
(
pos2
);
array
[
pos1
]
=
temp
;
array
[
pos1
].
set
AdjListIndex
(
pos1
);
array
[
pos1
].
set
Handle
(
pos1
);
}
...
...
@@ -45,7 +45,7 @@ public class Heap {
* Running time = O(n)
*/
private
void
doubleHeap
()
{
System
.
out
.
println
(
"Doubling size"
);
//
System.out.println("Doubling size");
HeapElt
[]
newArray
=
new
HeapElt
[
2
*
array
.
length
];
for
(
int
i
=
0
;
i
<
array
.
length
;
i
++)
...
...
@@ -121,7 +121,7 @@ public class Heap {
heapsize
++;
array
[
heapsize
]
=
inElt
;
array
[
heapsize
].
set
AdjListIndex
(
heapsize
);
array
[
heapsize
].
set
Handle
(
heapsize
);
heapifyUp
(
heapsize
);
}
...
...
CS311_final_project/src/heap/HeapElt.java
View file @
c6508433
...
...
@@ -20,7 +20,7 @@ public class HeapElt {
return
record
;
}
public
void
set
AdjListIndex
(
int
inHandle
)
{
public
void
set
Handle
(
int
inHandle
)
{
handle
=
inHandle
;
}
...
...
CS311_final_project/src/main/AdjacencyList.java
View file @
c6508433
...
...
@@ -13,9 +13,10 @@ public class AdjacencyList {
}
public
void
addNode
(
String
newWord
)
{
int
newHandle
=
adjList
.
size
();
ArrayList
<
Edge
>
neighbors
=
findAndSetNeighbors
(
newWord
,
newHandle
);
Vertex
newVertex
=
new
Vertex
(
newWord
,
newHandle
,
neighbors
);
int
newIndex
=
adjList
.
size
();
ArrayList
<
Edge
>
neighbors
=
findAndSetNeighbors
(
newWord
,
newIndex
);
Vertex
newVertex
=
new
Vertex
(
newWord
,
newIndex
,
neighbors
);
adjList
.
add
(
newVertex
);
}
...
...
@@ -94,8 +95,8 @@ public class AdjacencyList {
out
.
append
(
"------------------|------------\n"
);
for
(
int
i
=
0
;
i
<
adjList
.
size
();
i
++)
{
out
.
append
(
String
.
format
(
"%5d %5s"
,
i
,
adjList
.
get
(
i
).
getKey
())
+
" | "
+
neighborsToString
(
adjList
.
get
(
i
))
+
"\n"
);
.
format
(
"%5d %5s"
,
adjList
.
get
(
i
).
getAdjListIndex
()
,
adjList
.
get
(
i
).
getKey
())
+
" | "
+
neighborsToString
(
adjList
.
get
(
i
))
+
"
"
+
adjList
.
get
(
i
).
getPred
()+
"
\n"
);
}
return
out
.
toString
();
}
...
...
@@ -106,17 +107,18 @@ public class AdjacencyList {
Vertex
temp
;
for
(
int
i
=
0
;
i
<
adjList
.
size
();
i
++){
temp
=
adjList
.
get
(
i
);
temp
.
setDist
(
Integer
.
MAX_VALUE
);
temp
.
setPred
(-
1
);
temp
.
setRecord
(
Integer
.
MAX_VALUE
);
}
start
.
set
Dist
(
0
);
start
.
set
Record
(
0
);
//Initialize heap
Heap
heap
=
new
Heap
();
heap
.
insert
(
start
);
for
(
int
i
=
0
;
i
<
adjList
.
size
();
i
++)
heap
.
insert
(
adjList
.
get
(
i
));
for
(
int
i
=
0
;
i
<
adjList
.
size
();
i
++){
if
(
start
.
getAdjListIndex
()!=
i
)
heap
.
insert
(
adjList
.
get
(
i
));
}
//Loop through
Vertex
adj
;
...
...
@@ -125,9 +127,9 @@ public class AdjacencyList {
temp
=
(
Vertex
)
heap
.
removeMin
();
for
(
Edge
e:
temp
.
getEdges
()){
adj
=
getVertexFromHandle
(
e
.
toVertex
);
if
(
temp
.
get
Dist
()
+
e
.
weight
<
adj
.
get
Dist
()){
if
(
(
Integer
)
temp
.
get
Record
()
+
e
.
weight
<
(
Integer
)
adj
.
get
Record
()){
adj
.
setPred
(
temp
.
getAdjListIndex
());
adj
.
set
Dist
(
temp
.
get
Dist
()
+
e
.
weight
);
adj
.
set
Record
((
Integer
)
temp
.
get
Record
()
+
e
.
weight
);
heap
.
heapifyUp
(
adj
.
getIndex
());
}
}
...
...
@@ -138,10 +140,12 @@ public class AdjacencyList {
}
private
ArrayList
<
Integer
>
findPathRev
(
Vertex
start
,
Vertex
end
,
ArrayList
<
Integer
>
path
){
if
(
end
.
equals
(
start
))
if
(
end
.
equals
(
start
)){
path
.
add
(
end
.
getAdjListIndex
());
return
path
;
}
Vertex
next
=
adjList
.
get
(
end
.
getPred
());
path
.
add
(
next
.
getAdjListIndex
());
path
.
add
(
end
.
getAdjListIndex
());
return
findPathRev
(
start
,
next
,
path
);
}
...
...
@@ -150,7 +154,7 @@ public class AdjacencyList {
StringBuffer
out
=
new
StringBuffer
();
for
(
int
i
=
reversedPath
.
size
()
-
1
;
i
>=
0
;
i
--){
out
.
append
(
" "
+
adjList
.
get
(
reversedPath
.
get
(
i
)));
out
.
append
(
" "
+
adjList
.
get
(
reversedPath
.
get
(
i
))
.
getKey
()
);
if
((
reversedPath
.
size
()
-
i
)
%
10
==
0
)
out
.
append
(
"\n"
);
}
...
...
@@ -160,19 +164,20 @@ public class AdjacencyList {
public
String
outputToString
(
int
s1
,
int
s2
){
ArrayList
<
Integer
>
revPath
=
findMinimumPath
(
s1
,
s2
);
return
pathToString
(
revPath
);
StringBuffer
out
=
new
StringBuffer
();
out
.
append
(
"The best score for "
+
adjList
.
get
(
s1
).
getKey
()
+
" to "
+
adjList
.
get
(
s2
).
getKey
()
+
" is "
);
out
.
append
(
getDistance
(
s2
)
+
"\n\n"
);
out
.
append
(
pathToString
(
revPath
)
+
"\n"
);
return
out
.
toString
();
}
public
ArrayList
<
Integer
>
findMinimumPath
(
int
s1
,
int
s2
){
System
.
out
.
println
(
s1
);
public
ArrayList
<
Integer
>
findMinimumPath
(
int
s1
,
int
s2
){
buildDijkstraGraph
(
s1
);
return
findPathRev
(
adjList
.
get
(
s1
),
adjList
.
get
(
s2
),
new
ArrayList
<
Integer
>());
}
public
int
getDistance
(
String
end
){
int
index
=
findStringHandle
(
end
);
return
adjList
.
get
(
index
).
getDist
();
public
int
getDistance
(
int
end
){
return
(
Integer
)
adjList
.
get
(
end
).
getRecord
();
}
}
CS311_final_project/src/main/Main.java
View file @
c6508433
...
...
@@ -105,7 +105,6 @@ public class Main {
continue
;
}
System
.
out
.
println
(
adjList
.
toString
());
System
.
out
.
println
(
adjList
.
outputToString
(
s1Handle
,
s2Handle
));
boolean
validResponse
=
false
;
...
...
CS311_final_project/src/main/Vertex.java
View file @
c6508433
...
...
@@ -8,15 +8,13 @@ public class Vertex extends HeapElt{
private
String
key
;
private
ArrayList
<
Edge
>
edges
;
private
int
pred
;
private
int
dist
;
private
int
adjListIndex
;
public
Vertex
(
String
word
,
int
handle
,
ArrayList
<
Edge
>
neighbors
)
{
public
Vertex
(
String
word
,
int
index
,
ArrayList
<
Edge
>
neighbors
)
{
this
.
key
=
word
;
this
.
adjListIndex
=
handle
;
this
.
adjListIndex
=
index
;
this
.
edges
=
neighbors
;
pred
=
-
1
;
dist
=
-
1
;
record
=
-
1
;
}
...
...
@@ -32,20 +30,12 @@ public class Vertex extends HeapElt{
this
.
pred
=
pred
;
}
public
int
getDist
()
{
return
dist
;
}
public
void
setDist
(
int
dist
)
{
this
.
dist
=
dist
;
}
public
int
getAdjListIndex
()
{
return
adjListIndex
;
}
public
void
setAdjListIndex
(
int
handle
)
{
this
.
adjListIndex
=
handle
;
public
void
setAdjListIndex
(
int
index
)
{
this
.
adjListIndex
=
index
;
}
public
void
addEdge
(
int
n
,
int
weight
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment