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
d7a68ee3
Commit
d7a68ee3
authored
Apr 21, 2014
by
trh010
Browse files
lots of progress made
parent
59292373
Changes
6
Hide whitespace changes
Inline
Side-by-side
CS311_final_project/src/heap/Heap.java
View file @
d7a68ee3
...
...
@@ -30,10 +30,10 @@ public class Heap {
HeapElt
temp
=
array
[
pos2
];
array
[
pos2
]
=
array
[
pos1
];
array
[
pos2
].
set
Handle
(
pos2
);
array
[
pos2
].
set
AdjListIndex
(
pos2
);
array
[
pos1
]
=
temp
;
array
[
pos1
].
set
Handle
(
pos1
);
array
[
pos1
].
set
AdjListIndex
(
pos1
);
}
...
...
@@ -121,7 +121,7 @@ public class Heap {
heapsize
++;
array
[
heapsize
]
=
inElt
;
array
[
heapsize
].
set
Handle
(
heapsize
);
array
[
heapsize
].
set
AdjListIndex
(
heapsize
);
heapifyUp
(
heapsize
);
}
...
...
@@ -164,7 +164,7 @@ public class Heap {
public
void
printHeap
()
{
for
(
int
i
=
1
;
i
<=
heapsize
;
i
++)
{
String
out
=
i
+
": key- "
+
array
[
i
].
get
Handle
()
+
" | value- "
String
out
=
i
+
": key- "
+
array
[
i
].
get
Index
()
+
" | value- "
+
array
[
i
].
getRecord
();
System
.
out
.
println
(
out
);
}
...
...
CS311_final_project/src/heap/HeapElt.java
View file @
d7a68ee3
...
...
@@ -20,11 +20,11 @@ public class HeapElt {
return
record
;
}
public
void
set
Handle
(
int
inHandle
)
{
public
void
set
AdjListIndex
(
int
inHandle
)
{
handle
=
inHandle
;
}
public
int
get
Handle
()
{
public
int
get
Index
()
{
return
handle
;
}
...
...
CS311_final_project/src/heap/TestHeap.java
View file @
d7a68ee3
...
...
@@ -49,7 +49,7 @@ public class TestHeap {
for
(
int
i
=
1
;
i
<=
hsize
;
i
++)
{
System
.
out
.
println
(
" "
+
array
[
i
].
getRecord
().
toString
()
+
" "
+
array
[
i
].
get
Handle
());
+
" "
+
array
[
i
].
get
Index
());
}
System
.
out
.
println
(
"\n\nPrinting heap with printHeap(): \n"
);
...
...
@@ -61,7 +61,7 @@ public class TestHeap {
array
[
8
].
setRecord
(
new
Integer
(
11
));
myHeap
.
heapifyDown
(
array
[
8
].
get
Handle
());
myHeap
.
heapifyDown
(
array
[
8
].
get
Index
());
System
.
out
.
println
(
"\nRevised handle info:\n"
);
System
.
out
.
println
(
"value handle"
);
...
...
@@ -69,7 +69,7 @@ public class TestHeap {
for
(
int
i
=
1
;
i
<=
hsize
;
i
++)
{
System
.
out
.
println
(
" "
+
array
[
i
].
getRecord
().
toString
()
+
" "
+
array
[
i
].
get
Handle
());
+
" "
+
array
[
i
].
get
Index
());
}
System
.
out
.
println
(
"\n\nSorting by removing minimum at each step:\n"
);
...
...
CS311_final_project/src/main/AdjacencyList.java
View file @
d7a68ee3
package
main
;
import
heap.Heap
;
import
java.util.ArrayList
;
public
class
AdjacencyList
{
...
...
@@ -21,7 +23,7 @@ public class AdjacencyList {
ArrayList
<
Edge
>
neighbors
=
new
ArrayList
<
Edge
>();
for
(
int
i
=
0
;
i
<
adjList
.
size
();
i
++)
{
Vertex
testVertex
=
adjList
.
get
(
i
);
int
numDifferences
=
numDifferences
(
(
String
)
testVertex
.
get
Record
(),
newWord
);
int
numDifferences
=
numDifferences
(
testVertex
.
get
Key
(),
newWord
);
if
(
2
>=
numDifferences
)
{
int
weight
=
0
;
if
(
numDifferences
==
1
)
...
...
@@ -55,7 +57,7 @@ public class AdjacencyList {
StringBuffer
out
=
new
StringBuffer
();
for
(
int
i
=
0
;
i
<
v
.
getEdges
().
size
();
i
++)
{
int
neighborHandle
=
v
.
getEdges
().
get
(
i
).
toVertex
;
String
neighborKey
=
(
String
)
adjList
.
get
(
neighborHandle
).
get
Record
();
String
neighborKey
=
adjList
.
get
(
neighborHandle
).
get
Key
();
out
.
append
(
neighborKey
+
" ("
+
v
.
getEdges
().
get
(
i
).
weight
+
"), "
);
}
return
out
.
toString
();
...
...
@@ -65,7 +67,7 @@ public class AdjacencyList {
StringBuffer
out
=
new
StringBuffer
();
for
(
int
i
=
0
;
i
<
v
.
getEdges
().
size
();
i
++)
{
int
neighborHandle
=
v
.
getEdges
().
get
(
i
).
toVertex
;
String
neighborKey
=
(
String
)
adjList
.
get
(
neighborHandle
).
get
Record
();
String
neighborKey
=
adjList
.
get
(
neighborHandle
).
get
Key
();
out
.
append
(
" "
+
neighborKey
+
" ("
+
v
.
getEdges
().
get
(
i
).
weight
+
") "
);
if
(
i
%
6
==
5
)
...
...
@@ -80,46 +82,91 @@ public class AdjacencyList {
public
int
findStringHandle
(
String
s
)
{
for
(
int
i
=
0
;
i
<
adjList
.
size
();
i
++)
{
if
(
s
.
equals
(
adjList
.
get
(
i
).
get
Record
()))
if
(
s
.
equals
(
adjList
.
get
(
i
).
get
Key
()))
return
i
;
}
return
-
1
;
}
public
String
toString
()
{
StringBuffer
out
=
new
StringBuffer
();
out
.
append
(
"Handle Key | Neighbors \n"
);
out
.
append
(
"------------------|------------\n"
);
for
(
int
i
=
0
;
i
<
adjList
.
size
();
i
++)
{
out
.
append
(
String
.
format
(
"%5d %5s"
,
i
,
adjList
.
get
(
i
).
get
Record
())
+
" | "
+
neighborsToString
(
adjList
.
get
(
i
))
+
"\n"
);
}
return
out
.
toString
();
StringBuffer
out
=
new
StringBuffer
();
out
.
append
(
"Handle Key | Neighbors \n"
);
out
.
append
(
"------------------|------------\n"
);
for
(
int
i
=
0
;
i
<
adjList
.
size
();
i
++)
{
out
.
append
(
String
.
format
(
"%5d %5s"
,
i
,
adjList
.
get
(
i
).
get
Key
())
+
" | "
+
neighborsToString
(
adjList
.
get
(
i
))
+
"\n"
);
}
return
out
.
toString
();
}
private
void
buildDijkstraGraph
(
int
startingVertex
){
Vertex
start
=
getVertexFromHandle
(
startingVertex
);
//Initialize graph
Vertex
temp
;
for
(
int
i
=
0
;
i
<
adjList
.
size
();
i
++){
temp
=
adjList
.
get
(
i
);
temp
.
setDist
(
-
1
);
temp
.
setDist
(
Integer
.
MAX_VALUE
);
temp
.
setPred
(-
1
);
temp
.
setRecord
(
Integer
.
MAX_VALUE
);
}
getVertexFromHandle
(
startingVertex
)
.
setDist
(
0
);
start
.
setDist
(
0
);
//Initialize heap
Heap
heap
=
new
Heap
();
heap
.
insert
(
start
);
for
(
int
i
=
0
;
i
<
adjList
.
size
();
i
++)
heap
.
insert
(
adjList
.
get
(
i
));
//Loop through
Vertex
adj
;
while
(
heap
.
getHeapsize
()
!=
0
){
try
{
temp
=
(
Vertex
)
heap
.
removeMin
();
for
(
Edge
e:
temp
.
getEdges
()){
adj
=
getVertexFromHandle
(
e
.
toVertex
);
if
(
temp
.
getDist
()
+
e
.
weight
<
adj
.
getDist
()){
adj
.
setPred
(
temp
.
getAdjListIndex
());
adj
.
setDist
(
temp
.
getDist
()
+
e
.
weight
);
heap
.
heapifyUp
(
adj
.
getIndex
());
}
}
}
catch
(
Exception
e
)
{
System
.
err
.
println
(
"trying to remove vertex from empty heap. while loop should have caught this already"
);
}
}
}
private
ArrayList
<
Integer
>
findPath
(
int
start
,
int
end
,
ArrayList
<
Integer
>
path
){
return
path
;
private
ArrayList
<
Integer
>
findPathRev
(
Vertex
start
,
Vertex
end
,
ArrayList
<
Integer
>
path
){
if
(
end
.
getPred
()
==
-
1
)
return
path
;
Vertex
next
=
adjList
.
get
(
end
.
getPred
());
path
.
add
(
next
.
getAdjListIndex
());
return
findPathRev
(
start
,
next
,
path
);
}
public
String
printPath
(
ArrayList
<
Integer
>
reversedPath
){
StringBuffer
out
=
new
StringBuffer
();
for
(
int
i
=
reversedPath
.
size
()
-
1
;
i
>=
0
;
i
--){
out
.
append
(
" "
+
adjList
.
get
(
reversedPath
.
get
(
i
)));
}
return
out
.
toString
();
}
public
ArrayList
<
Integer
>
findMinimumPath
(
String
s1
,
String
s2
){
int
s1Handle
=
findStringHandle
(
s1
);
int
s2Handle
=
findStringHandle
(
s2
);
buildDijkstraGraph
(
s1Handle
);
buildDijkstraGraph
(
s1Handle
);
return
findPath
(
s1Handle
,
s2Handle
,
new
ArrayList
<
Integer
>());
return
findPathRev
(
adjList
.
get
(
s1Handle
),
adjList
.
get
(
s2Handle
),
new
ArrayList
<
Integer
>());
}
public
int
getDistance
(
String
end
){
int
index
=
findStringHandle
(
end
);
return
adjList
.
get
(
index
).
getDist
();
}
}
CS311_final_project/src/main/Main.java
View file @
d7a68ee3
...
...
@@ -96,6 +96,7 @@ public class Main {
+
" is not in the dictionary"
);
continue
;
}
System
.
out
.
println
(
"Please provide the second 5 letter word: "
);
String
s2
=
in
.
next
();
int
s2Handle
=
adjList
.
findStringHandle
(
s2
.
toUpperCase
());
if
(
s2Handle
==
-
1
){
...
...
@@ -137,7 +138,7 @@ public class Main {
try
{
Main
main
=
new
Main
();
main
.
createGraph
(
args
[
0
]);
main
.
queryUserPart1
();
//
main.queryUserPart1();
main
.
queryUserPart2
();
}
catch
(
Exception
e
)
{
System
.
err
.
println
(
"Error! - "
+
e
.
toString
());
...
...
CS311_final_project/src/main/Vertex.java
View file @
d7a68ee3
...
...
@@ -5,16 +5,23 @@ import java.util.ArrayList;
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
)
{
this
.
record
=
word
;
this
.
handle
=
handle
;
this
.
key
=
word
;
this
.
adjListIndex
=
handle
;
this
.
edges
=
neighbors
;
pred
=
-
1
;
dist
=
-
1
;
record
=
-
1
;
}
public
String
getKey
(){
return
key
;
}
public
int
getPred
()
{
...
...
@@ -33,12 +40,12 @@ public class Vertex extends HeapElt{
this
.
dist
=
dist
;
}
public
int
get
Handle
()
{
return
handle
;
public
int
get
AdjListIndex
()
{
return
adjListIndex
;
}
public
void
set
Handle
(
int
handle
)
{
this
.
handle
=
handle
;
public
void
set
AdjListIndex
(
int
handle
)
{
this
.
adjListIndex
=
handle
;
}
public
void
addEdge
(
int
n
,
int
weight
)
{
...
...
@@ -52,5 +59,7 @@ public class Vertex extends HeapElt{
public
ArrayList
<
Edge
>
getEdges
()
{
return
edges
;
}
}
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