- 浏览: 23924 次
- 性别:
最新评论
-
夜CT:
英语不好......这是什么意思啊?
Ruby on Rails Routing - Simple Examples -
may_cauc:
楼主怎么解决这个问题的?我也出现了
ROR新手请教连mysql数据库的问题. -
xiaoqiu369:
清空了缓存还是不行.
请教一个问题,为什么我修改了样式表文件public/stylesheets/scaffold.css,在页面上没有变化. -
ddandyy:
清空浏览器缓存
请教一个问题,为什么我修改了样式表文件public/stylesheets/scaffold.css,在页面上没有变化.
1 树与哈夫曼树
Java代码
package tree;
public class TreeNode {
TreeNode llink;
TreeNode rlink;
int info;
}
package tree;
public class Tree {
TreeNode root;
public Tree() {
}
public boolean isEmpty() {
return root == null;
}
// 插入
public void insertBinaryTree(int info) {
TreeNode node = new TreeNode();
node.info = info;
if (root == null) {
root = node;
} else {
TreeNode currentNode = root;
TreeNode parent;
while (currentNode != null) {
parent = currentNode;
if (info > currentNode.info) {
currentNode = currentNode.rlink;
if (currentNode == null) {
parent.rlink = node;
}
} else if (info < currentNode.info) {
currentNode = currentNode.llink;
if (currentNode == null) {
parent.llink = node;
}
}
}
}
}
// 删除
public void treeDelete(int info) {
TreeNode tNode = serach(info);
TreeNode deleteNode = new TreeNode();
TreeNode dNodeChild = new TreeNode();
if (tNode == null) {
System.out.println("node is not exists");
} else if (tNode.llink == null || tNode.rlink == null) {
deleteNode = tNode;
} else {
deleteNode = treeSuccessor(tNode);
}
if (deleteNode.llink != null) {
dNodeChild = deleteNode.llink;
} else {
dNodeChild = deleteNode.rlink;
}
if (getFatherNode(deleteNode) == null) {
root = dNodeChild;
} else {
if (deleteNode == getFatherNode(deleteNode).llink) {
getFatherNode(deleteNode).llink = dNodeChild;
} else {
getFatherNode(deleteNode).rlink = dNodeChild;
}
}
if (deleteNode != tNode) {
tNode.info = deleteNode.info;
}
}
// 搜索
public TreeNode serach(int info) {
return treeSerach(root, info);
}
// 搜索
public TreeNode treeSerach(TreeNode tNode, int info) {
if (tNode == null || tNode.info == info) {
return tNode;
}
if (info < tNode.info) {
return treeSerach(tNode.llink, info);
} else {
return treeSerach(tNode.rlink, info);
}
}
// 最大节点
public TreeNode treeMaxiMum(TreeNode tNode) {
while (tNode.rlink != null) {
tNode = tNode.rlink;
}
return tNode;
}
// 最小节点
public TreeNode treeMiniMun(TreeNode tNode) {
while (tNode.llink != null) {
tNode = tNode.llink;
}
return tNode;
}
// 找后继
public TreeNode treeSuccessor(TreeNode tNode) {
if (tNode.rlink != null) {
return treeMiniMun(tNode.rlink);
}
TreeNode currentNode = getFatherNode(tNode);
while (currentNode != null && tNode == currentNode.rlink) {
tNode = currentNode;
currentNode = getFatherNode(tNode);
}
return currentNode;
}
// 找前驱
public TreeNode treePrecursor(TreeNode tNode) {
if (tNode.llink != null) {
return treeMaxiMum(tNode.llink);
}
TreeNode currentNode = getFatherNode(tNode);
while (currentNode != null && tNode == currentNode.llink) {
tNode = currentNode;
currentNode = getFatherNode(tNode);
}
return currentNode;
}
// 找节点的父节点
public TreeNode getFatherNode(TreeNode tNode) {
TreeNode current = root;
TreeNode trailCurrent = null;
while (current != null) {
if (current.info == tNode.info) {
break;
}
trailCurrent = current;
if (tNode.info < current.info) {
current = current.llink;
} else {
current = current.rlink;
}
}
return trailCurrent;
}
// 中序遍历
public void inOrder(TreeNode tNode) {
if (tNode != null) {
inOrder(tNode.llink);
System.out.print(tNode.info + " ");
inOrder(tNode.rlink);
}
}
// 打印树
public void printTree() {
System.out.println("inOrder");
inOrder(root);
System.out.println();
System.out.println("preOrder");
preOrder(root);
System.out.println();
System.out.println("postOrder");
postOrder(root);
System.out.println();
}
// 前序遍历
public void preOrder(TreeNode tNode) {
if (tNode != null) {
System.out.print(tNode.info + " ");
preOrder(tNode.llink);
preOrder(tNode.rlink);
}
}
// 后序遍历
public void postOrder(TreeNode tNode) {
if (tNode != null) {
postOrder(tNode.llink);
postOrder(tNode.rlink);
System.out.print(tNode.info + " ");
}
}
public static void main(String[] args) {
Tree tree = new Tree();
System.out.println("insert tree start");
tree.insertBinaryTree(15);
tree.insertBinaryTree(5);
tree.insertBinaryTree(16);
tree.insertBinaryTree(3);
tree.insertBinaryTree(12);
tree.insertBinaryTree(20);
tree.insertBinaryTree(10);
tree.insertBinaryTree(13);
tree.insertBinaryTree(18);
tree.insertBinaryTree(23);
tree.insertBinaryTree(6);
tree.insertBinaryTree(7);
System.out.println("insert tree end");
System.out.println("print tree start");
tree.treeDelete(15);
tree.printTree();
System.out.println("print tree end");
}
}
package tree;
public class TreeNode {
TreeNode llink;
TreeNode rlink;
int info;
}
package tree;
public class Tree {
TreeNode root;
public Tree() {
}
public boolean isEmpty() {
return root == null;
}
// 插入
public void insertBinaryTree(int info) {
TreeNode node = new TreeNode();
node.info = info;
if (root == null) {
root = node;
} else {
TreeNode currentNode = root;
TreeNode parent;
while (currentNode != null) {
parent = currentNode;
if (info > currentNode.info) {
currentNode = currentNode.rlink;
if (currentNode == null) {
parent.rlink = node;
}
} else if (info < currentNode.info) {
currentNode = currentNode.llink;
if (currentNode == null) {
parent.llink = node;
}
}
}
}
}
// 删除
public void treeDelete(int info) {
TreeNode tNode = serach(info);
TreeNode deleteNode = new TreeNode();
TreeNode dNodeChild = new TreeNode();
if (tNode == null) {
System.out.println("node is not exists");
} else if (tNode.llink == null || tNode.rlink == null) {
deleteNode = tNode;
} else {
deleteNode = treeSuccessor(tNode);
}
if (deleteNode.llink != null) {
dNodeChild = deleteNode.llink;
} else {
dNodeChild = deleteNode.rlink;
}
if (getFatherNode(deleteNode) == null) {
root = dNodeChild;
} else {
if (deleteNode == getFatherNode(deleteNode).llink) {
getFatherNode(deleteNode).llink = dNodeChild;
} else {
getFatherNode(deleteNode).rlink = dNodeChild;
}
}
if (deleteNode != tNode) {
tNode.info = deleteNode.info;
}
}
// 搜索
public TreeNode serach(int info) {
return treeSerach(root, info);
}
// 搜索
public TreeNode treeSerach(TreeNode tNode, int info) {
if (tNode == null || tNode.info == info) {
return tNode;
}
if (info < tNode.info) {
return treeSerach(tNode.llink, info);
} else {
return treeSerach(tNode.rlink, info);
}
}
// 最大节点
public TreeNode treeMaxiMum(TreeNode tNode) {
while (tNode.rlink != null) {
tNode = tNode.rlink;
}
return tNode;
}
// 最小节点
public TreeNode treeMiniMun(TreeNode tNode) {
while (tNode.llink != null) {
tNode = tNode.llink;
}
return tNode;
}
// 找后继
public TreeNode treeSuccessor(TreeNode tNode) {
if (tNode.rlink != null) {
return treeMiniMun(tNode.rlink);
}
TreeNode currentNode = getFatherNode(tNode);
while (currentNode != null && tNode == currentNode.rlink) {
tNode = currentNode;
currentNode = getFatherNode(tNode);
}
return currentNode;
}
// 找前驱
public TreeNode treePrecursor(TreeNode tNode) {
if (tNode.llink != null) {
return treeMaxiMum(tNode.llink);
}
TreeNode currentNode = getFatherNode(tNode);
while (currentNode != null && tNode == currentNode.llink) {
tNode = currentNode;
currentNode = getFatherNode(tNode);
}
return currentNode;
}
// 找节点的父节点
public TreeNode getFatherNode(TreeNode tNode) {
TreeNode current = root;
TreeNode trailCurrent = null;
while (current != null) {
if (current.info == tNode.info) {
break;
}
trailCurrent = current;
if (tNode.info < current.info) {
current = current.llink;
} else {
current = current.rlink;
}
}
return trailCurrent;
}
// 中序遍历
public void inOrder(TreeNode tNode) {
if (tNode != null) {
inOrder(tNode.llink);
System.out.print(tNode.info + " ");
inOrder(tNode.rlink);
}
}
// 打印树
public void printTree() {
System.out.println("inOrder");
inOrder(root);
System.out.println();
System.out.println("preOrder");
preOrder(root);
System.out.println();
System.out.println("postOrder");
postOrder(root);
System.out.println();
}
// 前序遍历
public void preOrder(TreeNode tNode) {
if (tNode != null) {
System.out.print(tNode.info + " ");
preOrder(tNode.llink);
preOrder(tNode.rlink);
}
}
// 后序遍历
public void postOrder(TreeNode tNode) {
if (tNode != null) {
postOrder(tNode.llink);
postOrder(tNode.rlink);
System.out.print(tNode.info + " ");
}
}
public static void main(String[] args) {
Tree tree = new Tree();
System.out.println("insert tree start");
tree.insertBinaryTree(15);
tree.insertBinaryTree(5);
tree.insertBinaryTree(16);
tree.insertBinaryTree(3);
tree.insertBinaryTree(12);
tree.insertBinaryTree(20);
tree.insertBinaryTree(10);
tree.insertBinaryTree(13);
tree.insertBinaryTree(18);
tree.insertBinaryTree(23);
tree.insertBinaryTree(6);
tree.insertBinaryTree(7);
System.out.println("insert tree end");
System.out.println("print tree start");
tree.treeDelete(15);
tree.printTree();
System.out.println("print tree end");
}
}
Java代码
package tree;
/**
* @author B.Chen
*/
public class HuffmanTree {
/**
* 根节点
*/
public TreeNode root;
/**
* 数组下表
*/
public int nodeSize;
/**
* 长度
*/
public int length;
/**
* 哈夫曼节点数组
*/
public TreeNode[] nodeList;
/**
* 访问控制
*/
public boolean[] visited;
/**
* @param length
*/
public HuffmanTree(int length) {
this.length = length;
nodeList = new TreeNode[2 * length-1];
visited = new boolean[2*length-1];
}
/**
* @param info
*/
public void addNode(int info) {
TreeNode tNode = new TreeNode();
tNode.info = info;
if (nodeSize < length) {
nodeList[nodeSize++] = tNode;
} else {
System.out.println("out of size");
}
}
/**
* 构造哈夫曼树
*/
public void createHuffmanTree() {
int j = length;
while (nodeList[2*length -2] == null) {
TreeNode lNode = getMiniMumNode();
TreeNode rNode = getMiniMumNode();
TreeNode tNode = new TreeNode();
System.out.println(lNode.info + " " + rNode.info);
tNode.llink = lNode;
tNode.rlink = rNode;
tNode.info = lNode.info + rNode.info;
nodeList[j++] = tNode;
}
root = nodeList[2 * length - 2];
}
/**
* @return TreeNode
*/
public TreeNode getMiniMumNode() {
TreeNode tNode = null;
int size = 0;
for(int i=0;i<2*length-1;i++) {
if(!visited[i] && nodeList[i] != null) {
tNode = nodeList[i];
size = i;
for(int j=0;j<2*length-1;j++) {
if(!visited[j] && nodeList[j] != null) {
if(tNode.info > nodeList[j].info) {
tNode = nodeList[j];
size = j;
}
}
}
}
}
visited[size] = true;
return tNode;
}
/**
* 中序遍历
*
* @param tNode
*/
public void inOrder(TreeNode tNode) {
if (tNode != null) {
inOrder(tNode.llink);
System.out.print(tNode.info + " ");
inOrder(tNode.rlink);
}
}
/**
* 打印
*/
public void printHuffmanTree() {
System.out.println("inOrder");
inOrder(root);
}
/**
* @param args
*/
public static void main(String[] args) {
HuffmanTree hTree = new HuffmanTree(6);
hTree.addNode(4);
hTree.addNode(4);
hTree.addNode(4);
hTree.addNode(4);
hTree.addNode(5);
hTree.addNode(6);
hTree.createHuffmanTree();
hTree.printHuffmanTree();
}
}
package tree;
/**
* @author B.Chen
*/
public class HuffmanTree {
/**
* 根节点
*/
public TreeNode root;
/**
* 数组下表
*/
public int nodeSize;
/**
* 长度
*/
public int length;
/**
* 哈夫曼节点数组
*/
public TreeNode[] nodeList;
/**
* 访问控制
*/
public boolean[] visited;
/**
* @param length
*/
public HuffmanTree(int length) {
this.length = length;
nodeList = new TreeNode[2 * length-1];
visited = new boolean[2*length-1];
}
/**
* @param info
*/
public void addNode(int info) {
TreeNode tNode = new TreeNode();
tNode.info = info;
if (nodeSize < length) {
nodeList[nodeSize++] = tNode;
} else {
System.out.println("out of size");
}
}
/**
* 构造哈夫曼树
*/
public void createHuffmanTree() {
int j = length;
while (nodeList[2*length -2] == null) {
TreeNode lNode = getMiniMumNode();
TreeNode rNode = getMiniMumNode();
TreeNode tNode = new TreeNode();
System.out.println(lNode.info + " " + rNode.info);
tNode.llink = lNode;
tNode.rlink = rNode;
tNode.info = lNode.info + rNode.info;
nodeList[j++] = tNode;
}
root = nodeList[2 * length - 2];
}
/**
* @return TreeNode
*/
public TreeNode getMiniMumNode() {
TreeNode tNode = null;
int size = 0;
for(int i=0;i<2*length-1;i++) {
if(!visited[i] && nodeList[i] != null) {
tNode = nodeList[i];
size = i;
for(int j=0;j<2*length-1;j++) {
if(!visited[j] && nodeList[j] != null) {
if(tNode.info > nodeList[j].info) {
tNode = nodeList[j];
size = j;
}
}
}
}
}
visited[size] = true;
return tNode;
}
/**
* 中序遍历
*
* @param tNode
*/
public void inOrder(TreeNode tNode) {
if (tNode != null) {
inOrder(tNode.llink);
System.out.print(tNode.info + " ");
inOrder(tNode.rlink);
}
}
/**
* 打印
*/
public void printHuffmanTree() {
System.out.println("inOrder");
inOrder(root);
}
/**
* @param args
*/
public static void main(String[] args) {
HuffmanTree hTree = new HuffmanTree(6);
hTree.addNode(4);
hTree.addNode(4);
hTree.addNode(4);
hTree.addNode(4);
hTree.addNode(5);
hTree.addNode(6);
hTree.createHuffmanTree();
hTree.printHuffmanTree();
}
}
在8×8的棋盘上分布着n个骑士,他们想约在某一个格中聚会。骑士每天可以像国际象棋中的马那样移动一次,可以从中间向8个方向移动,请你计算n个骑士的最早聚会地点和要走多少天,要求尽早聚会,且n个人走的总步数最少,先到聚会地点的骑士可以不再移动等待其他的骑士。
从键盘输入n(0<n<=64),然后一次输入n个其实的初始位置xi,yi(0<=xi,y<=7)。屏幕输出以空格分割的三个数,分别为聚会的点(x,y) 以及要走的天数。="" ○ ○ ="" ○ ○="" ◎="" ○ ○="" ○ ○ ="" 骑士走法(中间为起始位置,空为走到位置)="" java代码="" package="" convex;="" public="" class="" point="" {="" public="" int="" x,="" y;="" public="" point(int="" x,="" int="" y)="" {="" if="" (x=""> 7 || y > 7) {
throw new RuntimeException("out of matrix");
}
this.x = x;
this.y = y;
}
public String toString() {
return "x=" + x + ",y=" + y;
}
}
package convex;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import convex.Point;
public class Algo {
private boolean[][] flg = new boolean[8][8];
private int[][] shortPath = new int[8][8];
//最短距离矩阵
public int[][] distanceSq(Point p1) {
djkst(p1);
return shortPath;
}
//BFS
private void djkst(Point p1) {
Point[] queue = new Point[64];
flg[p1.x][p1.y] = true;
queue[0] = p1;
int j=0;
int queueSize = 1;
while (j < queue.length) {
Point temp = queue[j];
Point[] list = getList(temp);
for(int i=0;i < list.length;i++) {
if(list[i] != null) {
Point w = list[i];
if (!flg[w.x][w.y]) {
shortPath[w.x][w.y] = shortPath[temp.x][temp.y] + 1;
queue[queueSize++] = w;
flg[w.x][w.y] = true;
}
}
}
j++;
}
}
//可行步数集
private static Point[] getList(Point point) {
Point[] list = new Point[8];
int length = 0;
if (point.x + 2 <= 7 && point.y + 1 <= 7) {
list[length++] = new Point(point.x + 2, point.y + 1);
}
if (point.x - 2 >= 0 && point.y - 1 >= 0) {
list[length++] = new Point(point.x - 2, point.y - 1);
}
if (point.x + 1 <= 7 && point.y + 2 <= 7) {
list[length++] = new Point(point.x + 1, point.y + 2);
}
if (point.x - 1 >= 0 && point.y - 2 >= 0) {
list[length++] = new Point(point.x - 1, point.y - 2);
}
if (point.x + 2 <= 7 && point.y - 1 >= 0) {
list[length++] = new Point(point.x + 2, point.y - 1);
}
if (point.x - 2 >= 0 && point.y + 1 <= 7) {
list[length++] = new Point(point.x - 2, point.y + 1);
}
if (point.x + 1 <= 7 && point.y - 2 >= 0) {
list[length++] = new Point(point.x + 1, point.y - 2);
}
if (point.x - 1 >= 0 && point.y + 2 <= 7) {
list[length++] = new Point(point.x - 1, point.y + 2);
}
return list;
}
public static int[] method(Point[] points, int i,int j,Object[] pointList) {
int maxDay = 0;
int distance = 0;
for(int k=0;k<pointlist.length;k++) {="" int="" day="((int[][])pointList[k])[i][j];" distance="" +="day;" if(maxday<day)="" {="" maxday="day;" }="" }="" return="" new="" int[]{maxday,distance};="" }="" public="" static="" void="" main(string[]="" args)="" throws="" ioexception="" {="" 数据输入="" 数据输入格式:第一个数字是骑士n,第2,3个数字是第一个骑士的坐标,依次类推。="" 每个数字之间以空格区分="" bufferedreader="" stdin="new" bufferedreader(="" new="" inputstreamreader(system.in));="" string="" line="stdin.readLine();" stringtokenizer="" st="new" stringtokenizer(line);="" int="" pointlength="Integer.parseInt(st.nextToken());" point[]="" points="new" point[pointlength];="" for(int="" i="0;i<points.length;i++)" {="" int="" x="Integer.parseInt(st.nextToken());" int="" y="Integer.parseInt(st.nextToken());" points[i]="new" point(x,y);="" }="" object[]="" pointlist="new" object[points.length];="" for="" (int="" j="0;" j="" <="" points.length;="" j++)="" {="" pointlist[j]="new" algo().distancesq(points[j]);="" }="" int="" minday="999999999;" int="" mindistance="999999999;" for(int="" i="0;i<7;i++)" {="" for(int="" j="0;j<7;j++)" {="" int[]="" result="Algo.method(points," i,j,pointlist);="" 找最短天数,最短天数相同,找最短距离="" if="" (minday=""> result[0]) {
minDay = result[0];
minDistance = result[1];
} else if(minDay == result[0]) {
if(minDistance > result[1]) {
minDistance = result[1];
}
}
}
}
for(int i=0;i<7;i++) {
for(int j=0;j<7;j++) {
int[] result = Algo.method(points, i,j,pointList);
if(minDay == result[0] && minDistance == result[1]) {
System.out.println(i+" " + j +" "+ minDay);
}
}
}
}
}
package convex;
public class Point {
public int x, y;
public Point(int x, int y) {
if (x > 7 || y > 7) {
throw new RuntimeException("out of matrix");
}
this.x = x;
this.y = y;
}
public String toString() {
return "x=" + x + ",y=" + y;
}
}
package convex;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import convex.Point;
public class Algo {
private boolean[][] flg = new boolean[8][8];
private int[][] shortPath = new int[8][8];
//最短距离矩阵
public int[][] distanceSq(Point p1) {
djkst(p1);
return shortPath;
}
//BFS
private void djkst(Point p1) {
Point[] queue = new Point[64];
flg[p1.x][p1.y] = true;
queue[0] = p1;
int j=0;
int queueSize = 1;
while (j < queue.length) {
Point temp = queue[j];
Point[] list = getList(temp);
for(int i=0;i < list.length;i++) {
if(list[i] != null) {
Point w = list[i];
if (!flg[w.x][w.y]) {
shortPath[w.x][w.y] = shortPath[temp.x][temp.y] + 1;
queue[queueSize++] = w;
flg[w.x][w.y] = true;
}
}
}
j++;
}
}
//可行步数集
private static Point[] getList(Point point) {
Point[] list = new Point[8];
int length = 0;
if (point.x + 2 <= 7 && point.y + 1 <= 7) {
list[length++] = new Point(point.x + 2, point.y + 1);
}
if (point.x - 2 >= 0 && point.y - 1 >= 0) {
list[length++] = new Point(point.x - 2, point.y - 1);
}
if (point.x + 1 <= 7 && point.y + 2 <= 7) {
list[length++] = new Point(point.x + 1, point.y + 2);
}
if (point.x - 1 >= 0 && point.y - 2 >= 0) {
list[length++] = new Point(point.x - 1, point.y - 2);
}
if (point.x + 2 <= 7 && point.y - 1 >= 0) {
list[length++] = new Point(point.x + 2, point.y - 1);
}
if (point.x - 2 >= 0 && point.y + 1 <= 7) {
list[length++] = new Point(point.x - 2, point.y + 1);
}
if (point.x + 1 <= 7 && point.y - 2 >= 0) {
list[length++] = new Point(point.x + 1, point.y - 2);
}
if (point.x - 1 >= 0 && point.y + 2 <= 7) {
list[length++] = new Point(point.x - 1, point.y + 2);
}
return list;
}
public static int[] method(Point[] points, int i,int j,Object[] pointList) {
int maxDay = 0;
int distance = 0;
for(int k=0;k<pointlist.length;k++) {="" int="" day="((int[][])pointList[k])[i][j];" distance="" +="day;" if(maxday<day)="" {="" maxday="day;" }="" }="" return="" new="" int[]{maxday,distance};="" }="" public="" static="" void="" main(string[]="" args)="" throws="" ioexception="" {="" 数据输入="" 数据输入格式:第一个数字是骑士n,第2,3个数字是第一个骑士的坐标,依次类推。="" 每个数字之间以空格区分="" bufferedreader="" stdin="new" bufferedreader(="" new="" inputstreamreader(system.in));="" string="" line="stdin.readLine();" stringtokenizer="" st="new" stringtokenizer(line);="" int="" pointlength="Integer.parseInt(st.nextToken());" point[]="" points="new" point[pointlength];="" for(int="" i="0;i<points.length;i++)" {="" int="" x="Integer.parseInt(st.nextToken());" int="" y="Integer.parseInt(st.nextToken());" points[i]="new" point(x,y);="" }="" object[]="" pointlist="new" object[points.length];="" for="" (int="" j="0;" j="" <="" points.length;="" j++)="" {="" pointlist[j]="new" algo().distancesq(points[j]);="" }="" int="" minday="999999999;" int="" mindistance="999999999;" for(int="" i="0;i<7;i++)" {="" for(int="" j="0;j<7;j++)" {="" int[]="" result="Algo.method(points," i,j,pointlist);="" 找最短天数,最短天数相同,找最短距离="" if="" (minday=""> result[0]) {
minDay = result[0];
minDistance = result[1];
} else if(minDay == result[0]) {
if(minDistance > result[1]) {
minDistance = result[1];
}
}
}
}
for(int i=0;i<7;i++) {
for(int j=0;j<7;j++) {
int[] result = Algo.method(points, i,j,pointList);
if(minDay == result[0] && minDistance == result[1]) {
System.out.println(i+" " + j +" "+ minDay);
}
}
}
}
}
下面开始写数据结构的基本代码:
1 节点类
Java代码
package graph;
public class GraphNode {
public GraphNode link;
public int info;
}
package graph;
public class GraphNode {
public GraphNode link;
public int info;
}
2 邻接表表示图的链表类
Java代码
package graph;
public class GraphList {
public GraphNode first;
public GraphNode last;
public boolean visitable;
public int getAjd(int[] ajd) {
GraphNode current = first;
int length = 0;
while(current != null) {
ajd[length++] = current.info;
current = current.link;
}
return length;
}
public void addNode(int v) {
GraphNode node = new GraphNode();
node.info = v;
if(first == null) {
first = node;
last = node;
} else {
last.link = node;
last = node;
}
}
}
package graph;
public class GraphList {
public GraphNode first;
public GraphNode last;
public boolean visitable;
public int getAjd(int[] ajd) {
GraphNode current = first;
int length = 0;
while(current != null) {
ajd[length++] = current.info;
current = current.link;
}
return length;
}
public void addNode(int v) {
GraphNode node = new GraphNode();
node.info = v;
if(first == null) {
first = node;
last = node;
} else {
last.link = node;
last = node;
}
}
}
3 图类
Java代码
package graph;
public class Graph {
private int length;
private GraphList[] list;
public Graph(int length) {
this.length = length;
list = new GraphList[length];
}
public void dfs(int v) {
int[] ajd = new int[length];
int ajdlength = list[v].getAjd(ajd);
list[v].visitable = true;
System.out.print(v+" ");
for(int i=0;i<ajdlength;i++) {="" int="" w="ajd[i];" if(!list[w].visitable)="" {="" dfs(w);="" }="" }="" }="" 深度优先遍历="" public="" void="" dfstravel()="" {="" for(int="" i="0;i<length;i++)" {="" list[i].visitable="false;" }="" for(int="" i="0;i<length;i++)" {="" if(!list[i].visitable)="" {="" dfs(i);="" }="" }="" }="" 广度优先遍历="" public="" void="" bfstravel()="" {="" for(int="" i="0;i<length;i++)" {="" list[i].visitable="false;" }="" bfs();="" }="" private="" void="" bfs()="" {="" queue="" queue="new" queue();="" for(int="" index="0;index<length;index++)" {="" if(!list[index].visitable)="" {="" queue.addqueue(index);="" list[index].visitable="true;" system.out.print(index+"="" ");="" while(!queue.isempty())="" {="" int="" temp="queue.front();" queue.deletequeue();="" int[]="" adj="new" int[length];="" int="" ajdlength="list[temp].getAjd(adj);" for(int="" i="0;i<ajdlength;i++)" {="" int="" w="adj[i];" if(!list[w].visitable)="" {="" system.out.print(w+"="" ");="" queue.addqueue(w);="" list[w].visitable="true;" }="" }="" }="" }="" }="" }="" 长度="" public="" void="" length()="" {="" system.out.println(length);="" }="" public="" boolean="" isempty()="" {="" return="" length="=" 0;="" }="" 添加节点="" public="" void="" addgraph(int="" info)="" {="" for(int="" i="0;i<length;i++)" {="" if(list[i]="=" null)="" {="" graphlist="" g="new" graphlist();="" g.addnode(info);="" list[i]="g;" break;="" }="" }="" }="" 添加边="" public="" void="" addside(int="" vfrom,int="" vto)="" {="" list[vfrom].addnode(vto);="" }="" 打印图="" public="" void="" print()="" {="" for(int="" i="0;i<length;i++)" {="" graphnode="" current="list[i].first;" while(current="" !="null)" {="" system.out.print(current.info+"="" ");="" current="current.link;" }="" system.out.println();="" }="" }="" public="" static="" void="" main(string[]="" args)="" {="" graph="" graph="new" graph(11);="" system.out.println("create="" graph="" start");="" for(int="" i="0;i<11;i++)" {="" graph.addgraph(i);="" }="" graph.addside(0,="" 1);="" graph.addside(0,="" 5);="" graph.addside(1,="" 2);="" graph.addside(1,="" 3);="" graph.addside(1,="" 5);="" graph.addside(2,="" 4);="" graph.addside(4,="" 3);="" graph.addside(5,="" 6);="" graph.addside(6,="" 8="" );="" graph.addside(7,="" 3);="" graph.addside(7,="" 8="" );="" graph.addside(8,="" 10);="" graph.addside(9,="" 4);="" graph.addside(9,="" 7);="" graph.addside(9,="" 10);="" graph.print();="" system.out.println("create="" graph="" end");="" graph.bfstravel();="" }="" }="" package="" graph;="" public="" class="" graph="" {="" private="" int="" length;="" private="" graphlist[]="" list;="" public="" graph(int="" length)="" {="" this.length="length;" list="new" graphlist[length];="" }="" public="" void="" dfs(int="" v)="" {="" int[]="" ajd="new" int[length];="" int="" ajdlength="list[v].getAjd(ajd);" list[v].visitable="true;" system.out.print(v+"="" ");="" for(int="" i="0;i<ajdlength;i++)" {="" int="" w="ajd[i];" if(!list[w].visitable)="" {="" dfs(w);="" }="" }="" }="" 深度优先遍历="" public="" void="" dfstravel()="" {="" for(int="" i="0;i<length;i++)" {="" list[i].visitable="false;" }="" for(int="" i="0;i<length;i++)" {="" if(!list[i].visitable)="" {="" dfs(i);="" }="" }="" }="" 广度优先遍历="" public="" void="" bfstravel()="" {="" for(int="" i="0;i<length;i++)" {="" list[i].visitable="false;" }="" bfs();="" }="" private="" void="" bfs()="" {="" queue="" queue="new" queue();="" for(int="" index="0;index<length;index++)" {="" if(!list[index].visitable)="" {="" queue.addqueue(index);="" list[index].visitable="true;" system.out.print(index+"="" ");="" while(!queue.isempty())="" {="" int="" temp="queue.front();" queue.deletequeue();="" int[]="" adj="new" int[length];="" int="" ajdlength="list[temp].getAjd(adj);" for(int="" i="0;i<ajdlength;i++)" {="" int="" w="adj[i];" if(!list[w].visitable)="" {="" system.out.print(w+"="" ");="" queue.addqueue(w);="" list[w].visitable="true;" }="" }="" }="" }="" }="" }="" 长度="" public="" void="" length()="" {="" system.out.println(length);="" }="" public="" boolean="" isempty()="" {="" return="" length="=" 0;="" }="" 添加节点="" public="" void="" addgraph(int="" info)="" {="" for(int="" i="0;i<length;i++)" {="" if(list[i]="=" null)="" {="" graphlist="" g="new" graphlist();="" g.addnode(info);="" list[i]="g;" break;="" }="" }="" }="" 添加边="" public="" void="" addside(int="" vfrom,int="" vto)="" {="" list[vfrom].addnode(vto);="" }="" 打印图="" public="" void="" print()="" {="" for(int="" i="0;i<length;i++)" {="" graphnode="" current="list[i].first;" while(current="" !="null)" {="" system.out.print(current.info+"="" ");="" current="current.link;" }="" system.out.println();="" }="" }="" public="" static="" void="" main(string[]="" args)="" {="" graph="" graph="new" graph(11);="" system.out.println("create="" graph="" start");="" for(int="" i="0;i<11;i++)" {="" graph.addgraph(i);="" }="" graph.addside(0,="" 1);="" graph.addside(0,="" 5);="" graph.addside(1,="" 2);="" graph.addside(1,="" 3);="" graph.addside(1,="" 5);="" graph.addside(2,="" 4);="" graph.addside(4,="" 3);="" graph.addside(5,="" 6);="" graph.addside(6,="" 8="" );="" graph.addside(7,="" 3);="" graph.addside(7,="" 8="" );="" graph.addside(8,="" 10);="" graph.addside(9,="" 4);="" graph.addside(9,="" 7);="" graph.addside(9,="" 10);="" graph.print();="" system.out.println("create="" graph="" end");="" graph.bfstravel();="" }="" }="" 4="" 队列="" java代码="" package="" graph;="" public="" class="" queue="" {="" public="" graphnode="" first;="" public="" graphnode="" last;="" public="" int="" count;="" public="" void="" addqueue(int="" info)="" {="" graphnode="" node="new" graphnode();="" node.info="info;" if(first="=" null)="" {="" first="node;" last="node;" }="" else="" {="" last.link="node;" last="last.link;" }="" count++;="" }="" public="" void="" deletequeue()="" {="" if(first="=" null)="" {="" system.out.println("null="" queue");="" }="" else="" {="" first="first.link;" count--;="" }="" }="" public="" boolean="" isempty()="" {="" return="" count="=" 0;="" }="" public="" int="" front()="" {="" if(first="=" null)="" {="" return="" -1;="" }="" return="" first.info;="" }="" public="" int="" back()="" {="" if(last="=" null)="" {="" return="" -1;="" }="" return="" last.info;="" }="" }="" package="" graph;="" public="" class="" queue="" {="" public="" graphnode="" first;="" public="" graphnode="" last;="" public="" int="" count;="" public="" void="" addqueue(int="" info)="" {="" graphnode="" node="new" graphnode();="" node.info="info;" if(first="=" null)="" {="" first="node;" last="node;" }="" else="" {="" last.link="node;" last="last.link;" }="" count++;="" }="" public="" void="" deletequeue()="" {="" if(first="=" null)="" {="" system.out.println("null="" queue");="" }="" else="" {="" first="first.link;" count--;="" }="" }="" public="" boolean="" isempty()="" {="" return="" count="=" 0;="" }="" public="" int="" front()="" {="" if(first="=" null)="" {="" return="" -1;="" }="" return="" first.info;="" }="" public="" int="" back()="" {="" if(last="=" null)="" {="" return="" -1;="" }="" return="" last.info;="" }="" }="" 5="" 堆栈="" java代码="" package="" graph;="" public="" class="" stack="" {="" public="" graphnode="" stacktop;="" public="" int="" count;="" public="" void="" push(int="" info)="" {="" graphnode="" node="new" graphnode();="" node.info="info;" node.link="stackTop;" stacktop="node;" count++;="" }="" public="" void="" pop()="" {="" if(stacktop="=" null)="" {="" system.out.println("null="" stack");="" }="" else="" {="" stacktop="stackTop.link;" count--;="" }="" }="" public="" int="" top()="" {="" if(stacktop="=" null)="" {="" return="" -1;="" }="" return="" stacktop.info;="" }="" }="" package="" graph;="" public="" class="" stack="" {="" public="" graphnode="" stacktop;="" public="" int="" count;="" public="" void="" push(int="" info)="" {="" graphnode="" node="new" graphnode();="" node.info="info;" node.link="stackTop;" stacktop="node;" count++;="" }="" public="" void="" pop()="" {="" if(stacktop="=" null)="" {="" system.out.println("null="" stack");="" }="" else="" {="" stacktop="stackTop.link;" count--;="" }="" }="" public="" int="" top()="" {="" if(stacktop="=" null)="" {="" return="" -1;="" }="" return="" stacktop.info;="" }="" }="" 6="" 图的最短路径算法="" java代码="" package="" graph;="" public="" class="" graph="" {="" private="" int="" length;="" private="" graphlist[]="" list;="" private="" int[][]="" weight;="" public="" graph(int="" length)="" {="" this.length="length;" list="new" graphlist[length];="" weight="new" int[length][length];="" }="" public="" void="" dfs(int="" v)="" {="" int[]="" ajd="new" int[length];="" int="" ajdlength="list[v].getAjd(ajd);" list[v].visitable="true;" system.out.print(v="" +="" "="" ");="" for="" (int="" i="0;" i="" <="" ajdlength;="" i++)="" {="" int="" w="ajd[i];" if="" (!list[w].visitable)="" {="" dfs(w);="" }="" }="" }="" 深度优先遍历="" public="" void="" dfstravel()="" {="" for="" (int="" i="0;" i="" <="" length;="" i++)="" {="" list[i].visitable="false;" }="" for="" (int="" i="0;" i="" <="" length;="" i++)="" {="" if="" (!list[i].visitable)="" {="" dfs(i);="" }="" }="" }="" 广度优先遍历="" public="" void="" bfstravel()="" {="" for="" (int="" i="0;" i="" <="" length;="" i++)="" {="" list[i].visitable="false;" }="" bfs();="" }="" private="" void="" bfs()="" {="" queue="" queue="new" queue();="" for="" (int="" index="0;" index="" <="" length;="" index++)="" {="" if="" (!list[index].visitable)="" {="" queue.addqueue(index);="" list[index].visitable="true;" system.out.print(index="" +="" "="" ");="" while="" (!queue.isempty())="" {="" int="" temp="queue.front();" queue.deletequeue();="" int[]="" ajd="new" int[length];="" int="" ajdlength="list[temp].getAjd(ajd);" for="" (int="" i="0;" i="" <="" ajdlength;="" i++)="" {="" int="" w="ajd[i];" if="" (!list[w].visitable)="" {="" system.out.print(w="" +="" "="" ");="" queue.addqueue(w);="" list[w].visitable="true;" }="" }="" }="" }="" }="" }="" 最短路径="" private="" int[]="" shortpath(int="" v)="" {="" int[]="" shortpath="new" int[length];="" boolean[]="" weightfound="new" boolean[length];="" for="" (int="" i="0;" i="" <="" length;="" i++)="" {="" 趋近无穷="" shortpath[i]="9999;" weightfound[i]="false;" }="" shortpath[v]="0;" weightfound[v]="true;" queue="" queue="new" queue();="" queue.addqueue(v);="" while="" (!queue.isempty())="" {="" int="" temp="queue.front();" queue.deletequeue();="" int[]="" ajd="new" int[length];="" int="" ajdlength="list[temp].getAjd(ajd);" for="" (int="" i="0;" i="" <="" ajdlength;="" i++)="" {="" int="" w="ajd[i];" if="" (!weightfound[w])="" {="" if="" (shortpath[w]=""> shortPath[temp] + weight[temp][w]) {
shortPath[w] = shortPath[temp] + weight[temp][w];
}
}
}
int minWeightNode = 0;
for (int i = 0; i < length; i++) {
if (!weightFound[i]) {
minWeightNode = i;
for (int j = 0; j < length; j++) {
if (!weightFound[j]) {
if (shortPath[j] < shortPath[minWeightNode]) {
minWeightNode = j;
}
}
}
break;
}
}
if (!weightFound[minWeightNode]) {
weightFound[minWeightNode] = true;
queue.addQueue(minWeightNode);
}
}
return shortPath;
}
// 长度
public void length() {
System.out.println(length);
}
public boolean isEmpty() {
return length == 0;
}
// 添加节点
public void addGraph(int info) {
for (int i = 0; i < length; i++) {
if (list[i] == null) {
GraphList g = new GraphList();
g.addNode(info);
list[i] = g;
break;
}
}
}
// 添加边
public void addSide(int vfrom, int vto, int value) {
list[vfrom].addNode(vto);
weight[vfrom][vto] = value;
}
// 打印图
public void print() {
for (int i = 0; i < length; i++) {
GraphNode current = list[i].first;
while (current != null) {
System.out.print(current.info + " ");
current = current.link;
}
System.out.println();
}
}
public static void main(String[] args) {
Graph graph = new Graph(5);
System.out.println("create graph start");
for (int i = 0; i < 5; i++) {
graph.addGraph(i);
}
graph.addSide(0, 1, 16);
graph.addSide(0, 3, 2);
graph.addSide(0, 4, 3);
graph.addSide(3, 4, 7);
graph.addSide(3, 1, 12);
graph.addSide(4, 1, 10);
graph.addSide(4, 3, 5);
graph.addSide(4, 2, 4);
graph.addSide(2, 1, 3);
graph.addSide(1, 2, 5);
graph.print();
System.out.println("create graph end");
int[] shortPath = graph.shortPath(0);
for (int i = 0; i < shortPath.length; i++) {
System.out.print(shortPath[i] + " ");
}
}
}
package graph;
public class Graph {
private int length;
private GraphList[] list;
private int[][] weight;
public Graph(int length) {
this.length = length;
list = new GraphList[length];
weight = new int[length][length];
}
public void dfs(int v) {
int[] ajd = new int[length];
int ajdlength = list[v].getAjd(ajd);
list[v].visitable = true;
System.out.print(v + " ");
for (int i = 0; i < ajdlength; i++) {
int w = ajd[i];
if (!list[w].visitable) {
dfs(w);
}
}
}
// 深度优先遍历
public void dfsTravel() {
for (int i = 0; i < length; i++) {
list[i].visitable = false;
}
for (int i = 0; i < length; i++) {
if (!list[i].visitable) {
dfs(i);
}
}
}
// 广度优先遍历
public void bfsTravel() {
for (int i = 0; i < length; i++) {
list[i].visitable = false;
}
bfs();
}
private void bfs() {
Queue queue = new Queue();
for (int index = 0; index < length; index++) {
if (!list[index].visitable) {
queue.addQueue(index);
list[index].visitable = true;
System.out.print(index + " ");
while (!queue.isEmpty()) {
int temp = queue.front();
queue.deleteQueue();
int[] ajd = new int[length];
int ajdlength = list[temp].getAjd(ajd);
for (int i = 0; i < ajdlength; i++) {
int w = ajd[i];
if (!list[w].visitable) {
System.out.print(w + " ");
queue.addQueue(w);
list[w].visitable = true;
}
}
}
}
}
}
// 最短路径
private int[] shortPath(int v) {
int[] shortPath = new int[length];
boolean[] weightFound = new boolean[length];
for (int i = 0; i < length; i++) {
// 趋近无穷
shortPath[i] = 9999;
weightFound[i] = false;
}
shortPath[v] = 0;
weightFound[v] = true;
Queue queue = new Queue();
queue.addQueue(v);
while (!queue.isEmpty()) {
int temp = queue.front();
queue.deleteQueue();
int[] ajd = new int[length];
int ajdlength = list[temp].getAjd(ajd);
for (int i = 0; i < ajdlength; i++) {
int w = ajd[i];
if (!weightFound[w]) {
if (shortPath[w] > shortPath[temp] + weight[temp][w]) {
shortPath[w] = shortPath[temp] + weight[temp][w];
}
}
}
int minWeightNode = 0;
for (int i = 0; i < length; i++) {
if (!weightFound[i]) {
minWeightNode = i;
for (int j = 0; j < length; j++) {
if (!weightFound[j]) {
if (shortPath[j] < shortPath[minWeightNode]) {
minWeightNode = j;
}
}
}
break;
}
}
if (!weightFound[minWeightNode]) {
weightFound[minWeightNode] = true;
queue.addQueue(minWeightNode);
}
}
return shortPath;
}
// 长度
public void length() {
System.out.println(length);
}
public boolean isEmpty() {
return length == 0;
}
// 添加节点
public void addGraph(int info) {
for (int i = 0; i < length; i++) {
if (list[i] == null) {
GraphList g = new GraphList();
g.addNode(info);
list[i] = g;
break;
}
}
}
// 添加边
public void addSide(int vfrom, int vto, int value) {
list[vfrom].addNode(vto);
weight[vfrom][vto] = value;
}
// 打印图
public void print() {
for (int i = 0; i < length; i++) {
GraphNode current = list[i].first;
while (current != null) {
System.out.print(current.info + " ");
current = current.link;
}
System.out.println();
}
}
public static void main(String[] args) {
Graph graph = new Graph(5);
System.out.println("create graph start");
for (int i = 0; i < 5; i++) {
graph.addGraph(i);
}
graph.addSide(0, 1, 16);
graph.addSide(0, 3, 2);
graph.addSide(0, 4, 3);
graph.addSide(3, 4, 7);
graph.addSide(3, 1, 12);
graph.addSide(4, 1, 10);
graph.addSide(4, 3, 5);
graph.addSide(4, 2, 4);
graph.addSide(2, 1, 3);
graph.addSide(1, 2, 5);
graph.print();
System.out.println("create graph end");
int[] shortPath = graph.shortPath(0);
for (int i = 0; i < shortPath.length; i++) {
System.out.print(shortPath[i] + " ");
}
}
}
8 普里姆最小生成树
Java代码
package graph;
/**
* @author B.Chen
*
*/
public class Graph {
/**
* 节点数
*/
private int length;
/**
* 链表
*/
private GraphList[] list;
/**
* 权集
*/
private int[][] weight;
/**
* 轻边集
*/
private int[][] lightSide;
/**
* @param length
*/
public Graph(int length) {
this.length = length;
list = new GraphList[length];
weight = new int[length][length];
lightSide = new int[length][length];
}
/**
* @param v
*/
public void dfs(int v) {
int[] ajd = new int[length];
int ajdlength = list[v].getAjd(ajd);
list[v].visitable = true;
System.out.print(v + " ");
for (int i = 0; i < ajdlength; i++) {
int w = ajd[i];
if (!list[w].visitable) {
dfs(w);
}
}
}
/**
* 深度优先遍历
*/
public void dfsTravel() {
for (int i = 0; i < length; i++) {
list[i].visitable = false;
}
for (int i = 0; i < length; i++) {
if (!list[i].visitable) {
dfs(i);
}
}
}
/**
* 广度优先遍历
*/
public void bfsTravel() {
for (int i = 0; i < length; i++) {
list[i].visitable = false;
}
bfs();
}
/**
* bfs
*/
private void bfs() {
Queue queue = new Queue();
for (int index = 0; index < length; index++) {
if (!list[index].visitable) {
queue.addQueue(index);
list[index].visitable = true;
System.out.print(index + " ");
while (!queue.isEmpty()) {
int temp = queue.front();
queue.deleteQueue();
int[] adj = new int[length];
int ajdlength = list[temp].getAjd(adj);
for (int i = 0; i < ajdlength; i++) {
int w = adj[i];
if (!list[w].visitable) {
System.out.print(w + " ");
queue.addQueue(w);
list[w].visitable = true;
}
}
}
}
}
}
/**
* 长度
*/
public void length() {
System.out.println(length);
}
/**
* @return boolean
*/
public boolean isEmpty() {
return length == 0;
}
/**
* @param info
*/
public void addGraph(int info) {
for (int i = 0; i < length; i++) {
if (list[i] == null) {
GraphList g = new GraphList();
g.addNode(info);
list[i] = g;
break;
}
}
}
/**
* 添加有向图的一条边
* @param vfrom
* @param vto
* @param value 权
*/
public void addSide(int vfrom, int vto, int value) {
list[vfrom].addNode(vto);
weight[vfrom][vto] = value;
}
/**
* 添加无向图的一条边
* @param vfrom
* @param vto
* @param value
*/
public void addDoubleSide(int vfrom, int vto, int value) {
list[vfrom].addNode(vto);
list[vto].addNode(vfrom);
weight[vfrom][vto] = value;
weight[vto][vfrom] = value;
}
/**
* 打印图
*/
public void print() {
for (int i = 0; i < length; i++) {
GraphNode current = list[i].first;
while (current != null) {
System.out.print(current.info + " ");
current = current.link;
}
System.out.println();
}
}
/**
* Dijkstra
*
* @param v
* @return int[]
*/
public int[] shortPath(int v) {
int[] shortPath = new int[length];
boolean[] weightFound = new boolean[length];
for (int i = 0; i < length; i++) {
// 趋近无穷
shortPath[i] = 9999;
weightFound[i] = false;
}
shortPath[v] = 0;
weightFound[v] = true;
Queue queue = new Queue();
queue.addQueue(v);
while (!queue.isEmpty()) {
int temp = queue.front();
queue.deleteQueue();
int[] ajd = new int[length];
int ajdlength = list[temp].getAjd(ajd);
for (int i = 0; i < ajdlength; i++) {
int w = ajd[i];
if (!weightFound[w]) {
if (shortPath[w] > shortPath[temp] + weight[temp][w]) {
shortPath[w] = shortPath[temp] + weight[temp][w];
}
}
}
int minWeightNode = 0;
for (int i = 0; i < length; i++) {
if (!weightFound[i]) {
minWeightNode = i;
for (int j = 0; j < length; j++) {
if (!weightFound[j]) {
if (shortPath[j] < shortPath[minWeightNode]) {
minWeightNode = j;
}
}
}
break;
}
}
if (!weightFound[minWeightNode]) {
weightFound[minWeightNode] = true;
queue.addQueue(minWeightNode);
}
}
return shortPath;
}
/**
* 普里姆最小生成树
*
* @param v
*/
public void primMST(int v) {
boolean[] visited = new boolean[length];
for (int i = 0; i < length; i++) {
visited[i] = false;
for (int j = 0; j < length; j++) {
lightSide[i][j] = 9999;
}
}
visited[v] = true;
Queue queue = new Queue();
queue.addQueue(v);
while (!queue.isEmpty()) {
int temp = queue.front();
queue.deleteQueue();
int[] ajd = new int[length];
int ajdlength = list[temp].getAjd(ajd);
for (int i = 0; i < ajdlength; i++) {
int w = ajd[i];
lightSide[temp][w] = weight[temp][w];
}
// 找到最小边
int minSide = 0;
int vfrom =0;
int vto = 0;
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
if (visited[i] && visited[j]) {
continue;
}
minSide = lightSide[i][j];
vfrom = i;
vto = j;
for (int k = 0; k < length; k++) {
for (int l = 0; l < length; l++) {
if (visited[k] && visited[l]) {
continue;
}
if (lightSide[k][l] < minSide) {
minSide = lightSide[k][l];
vfrom = k;
vto = l;
}
}
}
break;
}
}
//将最小边的节点vto设为true,并输出vto
if (!visited[vto]) {
visited[vto] = true;
System.out.print(vfrom+"==>" + vto+", ");
queue.addQueue(vto);
}
}
}
/**
* @param args
*/
相关推荐
在这个名为“数据结构JAVA实现”的压缩包中,我们可以看到作者提供了三种重要的数据结构——链表、有序二叉树和队列的Java代码实现。 首先,让我们详细探讨链表。链表是一种线性数据结构,与数组不同,它不连续存储...
找到了当初存的Java实现的数据结构ppt一份,分享给大家~!
数据结构Java实现(基于《算法四》),实现线性表、排序、查找、树、图等基本数据结构_DataStructure
了解和熟练掌握这些数据结构及其Java实现对于提升编程技能和解决实际问题至关重要。通过实践和不断学习,我们可以更有效地设计和优化算法,从而提高程序的性能和效率。在实际项目中,选择合适的数据结构往往能显著...
Java作为一种广泛使用的编程语言,提供了丰富的库和工具来实现各种数据结构。下面将详细介绍Java中实现链表、栈、队列、优先级队列以及哈希表这些基本数据结构的方法。 首先,我们来看链表。链表是一种线性数据结构...
9. **排序算法**:在Java中实现的各种排序算法,如冒泡排序、选择排序、插入排序、快速排序、归并排序和堆排序,都是基于特定数据结构的优化操作。 10. **搜索算法**:深度优先搜索(DFS)和广度优先搜索(BFS)是...
8. **排序算法**:包括冒泡排序、选择排序、插入排序、快速排序、归并排序、堆排序等,这些排序算法的Java实现可以帮助你理解它们的工作原理和效率。 9. **查找算法**:如二分查找、哈希查找等,这些算法在处理大...
《数据结构(Java版本)》这本书正是为此目的而编写,旨在将理论与实际编程相结合,通过Java语言来实现各种经典的数据结构。 首先,书中的基础部分会介绍数据结构的基本概念,如数组、链表、栈和队列。数组是最基本...
这里我们主要关注的是"数据结构java代码实现",这通常涉及到一系列用于教学或实践的数据结构示例。 首先,我们要理解数据结构的基本概念。数据结构是组织和管理大量数据的方式,如数组、链表、树、图等。它们直接...
虽然这里没有明确的堆实现文件,但Java提供了`java.util.PriorityQueue`来实现堆数据结构。 在实际编程中,这些数据结构的选择取决于特定的需求,例如快速访问、高效的插入和删除等。理解并熟练使用这些数据结构...
《邓俊辉版Java数据结构源码》是学习数据结构与算法的重要参考资料,它与邓俊辉教授编写的《Java数据结构》教材相配套,旨在帮助读者深入理解数据结构的概念和实现方法。邓俊辉教授的讲解风格清晰易懂,他的源码同样...
文中提到了数据结构(Data Structures)和Java语言的结合,这表明文档可能涉及数据结构在Java中的实现方式,包括基本的结构如数组、链表、栈、队列、树和图等,以及它们的特性、操作方法和应用场景。 2. Java中的...
学习数据结构Java版,你需要理解这些基础概念,熟悉它们的实现原理,并能够根据实际需求选择合适的数据结构。通过实践,你将掌握如何利用Java的数据结构优化算法,提高代码性能,解决复杂问题。
全国交通咨询系统(数据结构课设 图的应用 java实现 内含课设报告)全国交通咨询系统(数据结构课设 图的应用 java实现 内含课设报告)全国交通咨询系统(数据结构课设 图的应用 java实现 内含课设报告)全国交通...
C、C++ 和 Java 都是广泛用于实现数据结构的编程语言,每种语言都有其独特的特性和优势。 在C语言中,由于其低级特性,可以直接对内存进行操作,这使得C语言在实现数据结构时更加灵活,但同时也需要开发者具有较高...
Java作为一门面向对象的编程语言,提供了丰富的类库来支持这些数据结构的实现。 在Java中,数组是最基本的数据结构,它允许存储同类型的元素序列。数组提供了直接访问元素的能力,但插入和删除元素的效率较低。链表...
总之,《数据结构(Java描述)》一书涵盖了数据结构的基础理论和Java实现,对于理解算法和提升编程能力有着重要的作用。通过学习,读者不仅能掌握各种数据结构的特性,还能学会如何在实际问题中选择合适的数据结构和...