标签:定义 mat .com length git let while 维度 修改
AMatrix、AMatrixTest、CrossList、CrossListTest、Road、RoadTest
实验目的:用邻接矩阵实现无向图(边和顶点都要保存),实现在包含添加和删除结点的方法,添加和删除边的方法,size(),isEmpty(),广度优先迭代器,深度优先迭代器
确定图的顶点个数和边的个数;
输入顶点信息存储在一维数组vertex中;
初始化邻接矩阵;
依次输入每条边存储在邻接矩阵中;
输入边依附的两个顶点的序号i, j;
将邻接矩阵的第i行第j列的元素值置为1;
将邻接矩阵的第j行第i列的元素值置为1;
size()
和isEmpty()
的方法,返回二维数组的大小和判定二维数组是否为空。 public void addVertex(Object vertex) {
vertexList.add(vertexList.size(),vertex);
}
public void insertEdge(int v1,int v2,int weight) {
edges[v1][v2]=weight;
numOfEdges++;
}
public void deleteEdge(int v1,int v2) {
edges[v1][v2]=0;
numOfEdges--;
}
public int getNextNeighbor(int v1,int v2) {
for (int j=v2+1;j<vertexList.size();j++) {
if (edges[v1][j]>0) {
return j;
}
}
return -1;
}
private void broadFirstSearch(boolean[] isVisited, int i) {
int a, b;
LinkedList queue = new LinkedList();
System.out.print(getValueByIndex(i) + " ");
isVisited[i] = true;
queue.addLast(i);
while (!queue.isEmpty()) {
a = ((Integer) queue.removeFirst()).intValue();
b = getFirstNeighbor(a);
while (b != -1) {
if (!isVisited[b]) {
System.out.print(getValueByIndex(b) + " ");
isVisited[b] = true;
queue.addLast(b);
}
b = getNextNeighbor(a, b);
}
}
}
public void broadFirstSearch() {
for(int i = 0; i< size(); i++) {
if(!isVisited[i]) {
broadFirstSearch(isVisited, i);
}
}
}
public void depthFirstSearch(boolean[] isVisited,int i) {
System.out.print(getValueByIndex(i)+" ");
isVisited[i]=true;
int w=getFirstNeighbor(i);//
while (w!=-1) {
if (!isVisited[w]) {
depthFirstSearch(isVisited,w);
}
w=getNextNeighbor(i, w);
}
}
public void depthFirstSearch() {
for(int i = 0; i< size(); i++) {
if (!isVisited[i]) {
depthFirstSearch(isVisited,i);
}
}
}
实验目的:用十字链表实现无向图(边和顶点都要保存),实现在包含添加和删除结点的方法,添加和删除边的方法,size(),isEmpty(),广度优先迭代器,深度优先迭代器
public static class Vertex<E,T> {
E data;
Edge<T> firstIn;
Edge<T> firstOut;
public Vertex(E data) {
this.data = data;
}
}
public static class Edge<E> {
E data;
int From;
int To;
Edge<E> SameFromVertex;
Edge<E> SameToVertex;
public Edge(E data, int From, int To) {
this.data = data;
this.From = From;
this.To = To;
}
}
public void removeV(Vertex<Integer, Integer> vex){
for (int index = 0; index< AllEdge.size(); index++){
if (AllEdge.get(index).From== AllVertex.indexOf(vex)|| AllEdge.get(index).To== AllVertex.indexOf(vex)){
AllEdge.remove(index);
index=0;
}
}
AllVertex.remove(vex);
}
public void addE(Edge<Integer> edge) {
Edge<Integer> edge1 = new Edge<>(edge.data,edge.From,edge.To);
Edge<Integer> edge2 = new Edge<>(edge.data,edge.From,edge.To);
AllEdge.add(edge);
int fromVertexIndex = edge.From;
int toVertexIndex = edge.To;
Vertex<Integer, Integer> fromVertex = AllVertex.get(fromVertexIndex);
Vertex<Integer, Integer> toVertex = AllVertex.get(toVertexIndex);
if (fromVertex.firstOut == null) {
fromVertex.firstOut = edge1;
} else {
Edge<Integer> tempEdge = fromVertex.firstOut;
while (tempEdge.SameFromVertex != null) {
tempEdge = tempEdge.SameFromVertex;
System.out.println();
}
tempEdge.SameFromVertex = edge1;
}
if (toVertex.firstIn == null) {
toVertex.firstIn = edge2;
} else {
Edge<Integer> tempEdge = toVertex.firstIn;
while (tempEdge.SameToVertex != null) {
tempEdge = tempEdge.SameToVertex;
}
tempEdge.SameToVertex = edge2;
}
System.out.println();
}
size()
方法需要将添加的结点保存至一个集合,否则无法得到返回值。 static List<Vertex<Integer,Integer>> AllVertex = new ArrayList<>();
public int size(){
return AllVertex.size();
}
实验目的:创建计算机网络路由系统,输入网络中点到点的线路,以及每条线路使用的费用,系统输出网络中各点之间最便宜的路径,指出不相通的所有位置。
public Road(boolean graphType, boolean method, int size) {
this.graphType = graphType;
this.method = method;
this.NumVertex = 0;
this.matrix = size;
if (this.method) {
visited = new boolean[matrix];
vertexesArr = new Object[matrix];
edgesMatrix = new int[matrix][matrix];
for (int row = 0; row < edgesMatrix.length; row++) {
for (int column = 0; column < edgesMatrix.length; column++) {
edgesMatrix[row][column] = MAX_VALUE;
System.out.print("");
}
}
}
}
public void Dijkstra(int v0) {
int[] dist = new int[matrix];
int[] prev = new int[matrix];
for (int i = 0; i < NumVertex; i++) {
dist[i] = edgesMatrix[v0][i];
visited[i] = false;
if (i != v0 && dist[i] < MAX_VALUE)
prev[i] = v0;
else
prev[i] = -1;
}
visited[v0] = true;
int minDist;
int v = 0;
for (int i = 1; i < NumVertex; i++) {
minDist = MAX_VALUE;
for (int j = 0; j < NumVertex; j++) {
if ((!visited[j]) && dist[j] < minDist) {
v = j;
minDist = dist[j];
}
}
visited[v] = true;
for (int j = 0; j < NumVertex; j++) {
if ((!visited[j]) && edgesMatrix[v][j] < MAX_VALUE) {
if (minDist + edgesMatrix[v][j] <= dist[j]) {
dist[j] = minDist + edgesMatrix[v][j];
prev[j] = v;
}
}
}
}
for (int i = 0; i < matrix; i++) {
if (dist[i] > 1000) {
dist[i] = 0;
}
System.out.println(vertexesArr[v0] + "到" + vertexesArr[i] + "的最短路径是:" + dist[i]);
}
}
public void DRoad(int v0) {
Queue<Integer> queue = new LinkedList<Integer>();
for (int i = 0; i < NumVertex; i++) {
visited[i] = false;
}
for (int i = 0; i < NumVertex; i++) {
if (!visited[i]) {
queue.add(i);
visited[i] = true;
while (!queue.isEmpty()) {
int row = queue.remove();
System.out.print(vertexesArr[row] + "→");
for (int k = getMin(row); k >= 0; k = getMin(row)) {
if (!visited[k]) {
queue.add(k);
visited[k] = true;
}
}
}
}
}
}
标签:定义 mat .com length git let while 维度 修改
原文地址:http://www.cnblogs.com/lxrlxr/p/7871589.html