Skip to content

Commit 43660ff

Browse files
committed
Fork project from homework
1 parent 69d1907 commit 43660ff

File tree

18 files changed

+848
-0
lines changed

18 files changed

+848
-0
lines changed

Dijkstra.sh

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
3+
PWD=`pwd`
4+
INPUT=${PWD}/input/adjacency-list.txt
5+
MAINCLASS=org.ucf.java.Dijkstra.DijkstraMain
6+
mvn compile
7+
mvn exec:java -Dexec.mainClass="${MAINCLASS}" -Dexec.args="${INPUT}"
8+
mvn clean

HeapSort.sh

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
3+
PWD=`pwd`
4+
5+
INPUT=${PWD}/input/adjacency-list.txt
6+
MAINCLASS=org.ucf.java.HeapSort.HeapSortMain
7+
8+
mvn compile
9+
mvn exec:java -Dexec.mainClass="${MAINCLASS}" -Dexec.args="${INPUT}"
10+
mvn clean

Kruskal.sh

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
3+
PWD=`pwd`
4+
INPUT=${PWD}/input/adjacency-list.txt
5+
MAINCLASS=org.ucf.java.Kruskal.KruskalMain
6+
mvn compile
7+
mvn exec:java -Dexec.mainClass="${MAINCLASS}" -Dexec.args="${INPUT}"
8+
mvn clean

Readme.txt

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
1. Preliminary
2+
3+
In this programming assignment, I use Maven tool to construct my project,
4+
So in order to run my program, you need to install Maven tool and java JDK.
5+
For now the information of Software version is followed:
6+
7+
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=512m; support was removed in 8.0
8+
Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T07:57:37-04:00)
9+
Maven home: /usr/local/src/apache-maven-3.3.3
10+
Java version: 1.8.0_65, vendor: Oracle Corporation
11+
Java home: /usr/local/src/jdk1.8.0_65/jre
12+
Default locale: en_US, platform encoding: UTF-8
13+
OS name: "linux", version: "2.6.32-573.18.1.el6.x86_64", arch: "amd64", family: "unix"
14+
15+
16+
*** In you Operation System, you should set the PATH environment to make mvn and java command can work without any prefix.
17+
18+
19+
20+
21+
22+
23+
2. How to Run the Algorithms
24+
25+
2.1 Use ShellScript to run program
26+
* HeapSort Algorithm
27+
# sh HeapSort.sh
28+
29+
* Dijkstra Algorithm
30+
# sh Dijkstra.sh
31+
32+
* Kruskal Algorithm
33+
# sh Kruskal.sh
34+
35+
2.2 Use Maven tool to run Junit Test
36+
#mvn compile
37+
#mvn test
38+
39+

input/adjacency-list.txt

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
1 2,22 3,9 4,12
2+
2 1,22 3,35 6,36 8,34
3+
3 1,9 2,35 4,4 5,65 6,42
4+
4 1,12 3,4 5,33 9,30
5+
5 3,65 4,33 6,18 7,23
6+
6 2,36 3,42 5,18 7,39 8,24
7+
7 5,23 6,39 8,25 9,21
8+
8 2,34 6,24 7,25 9,19
9+
9 4,30 7,21 8,19

pom.xml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>org.ucf.java</groupId>
6+
<artifactId>algorithm_p1</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>algorithm_p1</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>junit</groupId>
20+
<artifactId>junit</artifactId>
21+
<version>3.8.1</version>
22+
<scope>test</scope>
23+
</dependency>
24+
</dependencies>
25+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package org.ucf.java.Dijkstra;
2+
import org.ucf.java.common.*;
3+
import java.util.*;
4+
5+
/**
6+
* Created by Bing on 3/17/2016.
7+
*/
8+
9+
public class Dijkstra {
10+
public Set<Node> open; // to store nodes which does not deal with
11+
public Set<Node> close; // to store sort results
12+
public Set<Edge> path; // to store the edge information
13+
public Node start;
14+
15+
public Dijkstra(Set<Node> vertex,Node root) {
16+
this.open = new HashSet<Node>(vertex);
17+
this.close = new HashSet<Node>();
18+
this.path = new HashSet<Edge>();
19+
this.start = root;
20+
}
21+
public void computePath() {
22+
start.setDistance(0);
23+
close.add(start);
24+
open.remove(start);
25+
System.out.println("start node is:"+start.getName());
26+
while (!open.isEmpty()) {
27+
Iterator<Node> it = close.iterator();
28+
Edge edge = new Edge(null, null, Integer.MAX_VALUE);
29+
while (it.hasNext()) {
30+
Node node = it.next();
31+
node.getShortestPath(open,edge);
32+
}
33+
path.add(edge);
34+
close.add(edge.getDst());
35+
edge.getDst().setDistance(edge.getWeight());
36+
edge.getDst().getChild().remove(edge.getSrc());
37+
38+
edge.getSrc().getChild().remove(edge.getDst());
39+
open.remove(edge.getDst());
40+
}
41+
}
42+
public void findpath(Node e,List<Edge> link) {
43+
Iterator<Edge> it = path.iterator();
44+
while(it.hasNext()) {
45+
Edge edg = it.next();
46+
if(edg.getDst().equals(e) == true){
47+
link.add(edg);
48+
findpath(edg.getSrc(),link);
49+
}
50+
}
51+
if(e.equals(start) == true) {
52+
if(link.isEmpty() == true)
53+
link.add(new Edge(e,e,e.getDistance()));
54+
return;
55+
}
56+
}
57+
public void printedges(){
58+
Iterator<Edge> it = path.iterator();
59+
while (it.hasNext()){
60+
Edge link = it.next();
61+
link.printinfo();
62+
}
63+
}
64+
public void printpath(Node node){
65+
List<Edge> path = new ArrayList<Edge>();
66+
this.findpath(node,path);
67+
for(int i=path.size()-1;i>=0;i--) {
68+
Edge it = path.get(i);
69+
if(i == path.size() -1){
70+
System.out.print("The Destination Node: "+path.get(0).getDst().getName()+"\t");
71+
System.out.print("\tPath value ="+path.get(0).getWeight()+"\t Path is: ");
72+
System.out.print(it.getSrc().getName()+"->"+it.getDst().getName());
73+
} else{
74+
System.out.print("->"+it.getDst().getName());
75+
}
76+
if(i == 0)
77+
System.out.println();
78+
}
79+
}
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.ucf.java.Dijkstra;
2+
import org.ucf.java.common.*;
3+
4+
public class DijkstraMain {
5+
public static void main(String[] args) {
6+
System.out.println("\n\n\n######################################################");
7+
if(args[0] == null){
8+
System.out.println("There is no input file,please confirm that.");
9+
return;
10+
}
11+
System.out.println("The input file:"+args[0]);
12+
GraphBuilder builder = new GraphBuilder(args[0]);
13+
/**/
14+
Dijkstra mst = new Dijkstra(builder.Vertix_Set,builder.Vertix.get(1));
15+
mst.computePath();
16+
for(int i = 1;i<= 8;i++){
17+
mst.printpath(builder.Vertix.get(i));
18+
}
19+
System.out.println("######################################################\n\n\n");
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package org.ucf.java.HeapSort;
2+
3+
/**
4+
* Created by Bing on 3/16/2016.
5+
*/
6+
7+
public class HeapSort {
8+
private int[] heap;
9+
public HeapSort(int[] array)
10+
{
11+
this.heap = array;
12+
}
13+
public int Parent(int i){
14+
return (i-1)/2;
15+
}
16+
public int Left(int i){
17+
return 2*(i+1)-1;
18+
}
19+
public int Right(int i){
20+
return 2*(i+1);
21+
}
22+
public void siftup(int idx) {
23+
int p_idx = Parent(idx);
24+
if(p_idx<0) {
25+
return;
26+
}
27+
else
28+
{
29+
if(heap[p_idx]>heap[idx])
30+
{
31+
int tmp = heap[p_idx];
32+
heap[p_idx] = heap[idx];
33+
heap[idx] = tmp;
34+
siftup(p_idx);
35+
}
36+
else
37+
return;
38+
}
39+
40+
}
41+
public void siftdown(int idx){
42+
int l = Left(idx);
43+
int r = Right(idx);
44+
int lgt_idx;
45+
if((l<heap.length-1)&&(r<heap.length-1))
46+
{
47+
if(heap[l]>heap[r])
48+
{
49+
/*if the left value is bigger then the right one*/
50+
lgt_idx = r;
51+
}
52+
else
53+
{
54+
/*if the left value is smaller or equal to the right one*/
55+
lgt_idx = l;
56+
}
57+
}
58+
else if(l == heap.length -1){
59+
lgt_idx = l;
60+
}
61+
else if(r == heap.length-1){
62+
lgt_idx = r;
63+
}else{
64+
return;
65+
}
66+
if(heap[lgt_idx]<heap[idx]) {
67+
int tmp = heap[lgt_idx];
68+
heap[lgt_idx] = heap[idx];
69+
heap[idx] = tmp;
70+
siftdown(lgt_idx);
71+
}
72+
else {
73+
return;
74+
}
75+
}
76+
public void BuildMinHeap() {
77+
for(int i = 0;i<heap.length;i++){
78+
siftup(i);
79+
}
80+
}
81+
public void HeapSortTree() {
82+
int[] tmp_a = new int[heap.length];
83+
for(int i=0;i<heap.length;i++)
84+
{
85+
tmp_a[i] = heap[0];
86+
heap[0]=heap[heap.length-1-i];
87+
heap[heap.length-1-i] = Integer.MAX_VALUE;
88+
siftdown(0);
89+
}
90+
for(int i=0;i<heap.length;i++)
91+
{
92+
heap[i] = tmp_a[i];
93+
}
94+
}
95+
96+
public void printHeapTree() {
97+
for(int i=1;i<heap.length;i=i*2){
98+
for(int k = i-1;k<2*i-1&&k<heap.length;k++){
99+
System.out.println(heap[k]+" ");
100+
}
101+
System.out.println();
102+
}
103+
}
104+
public void printHeap(){
105+
for(int i = 0; i<heap.length;i++){
106+
System.out.print(heap[i]+" ");
107+
}
108+
System.out.println();
109+
}
110+
public int[] getHeap(){
111+
return heap;
112+
}
113+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.ucf.java.HeapSort;
2+
import org.ucf.java.common.*;
3+
4+
public class HeapSortMain {
5+
public static void main(String[] args) {
6+
7+
System.out.println("\n\n\n######################################################");
8+
if(args[0] == null){
9+
System.out.println("There is no input file,please confirm that.");
10+
return;
11+
}
12+
System.out.println("The input file:"+args[0]);
13+
GraphBuilder builder = new GraphBuilder(args[0]);
14+
int [] array = new int[builder.edgeList.size()];
15+
16+
for(int i = 0;i<builder.edgeList.size();i++) {
17+
array[i] = builder.edgeList.get(i).getWeight();
18+
}
19+
20+
HeapSort heap = new HeapSort(array);
21+
System.out.println("Before the heapify, the tree is(not a heap tree):");
22+
heap.printHeapTree();
23+
/*sift up nodes to make a min heap tree*/
24+
heap.BuildMinHeap();
25+
26+
System.out.println("After the heapify, the min heap tree:");
27+
heap.printHeapTree();
28+
29+
30+
/*heapsort the array and get the results*/
31+
heap.HeapSortTree();
32+
33+
/*print the heapsort resutls*/
34+
System.out.println("After HeapSort, print the final results(ascending order):");
35+
heap.printHeap();
36+
System.out.println("######################################################\n\n\n");
37+
38+
}
39+
}

0 commit comments

Comments
 (0)