标签:site let alt 无向图 邻接表 操作 最小 显示 nod
//插入结点
public void insertVertex(Object vertex) {
vertexList.add(vertex);
}
//删除结点
public void deleteVertex(Object vertex) {
vertexList.remove(vertex);
}
//插入边
public void insertEdge(int v1, int v2) {
edges[v1][v2] = 1;
numOfEdges++;
}
//删除边
public void deleteEdge(int v1, int v2) {
edges[v1][v2] = 0;
numOfEdges--;
if (edges[v1][v2] == Integer.parseInt(null)) {
System.out.print("该边不存在。");
}
}
public int size(){
return vertexList.size();
}
public boolean isEmpty(){
if(vertexList == null && edges == null)
return true;
else
return false;
}
//进行广度优先遍历的内部递归调用方法使用
public void bfs(int i) {
MyQueue queue = new MyQueue(100);
System.out.print(i + " ");
int visited[] = new int[vertexList.size()];
for(int j= 0;j<visited.length;j++) {
visited[j] = 0;
queue.insert(i);
visited[i] = 1;
String result = vertexList.get(j) + " ";
while (!queue.isEmpty()) {
j = queue.peek();
// 得到未访问过的邻接点
int unvisitedVertex = getUnvisitedVertex(j);
if (unvisitedVertex == -1) {
queue.remove();
} else {
vertexList.get(unvisitedVertex);
result += vertexList.get(unvisitedVertex) + " ";
queue.insert(unvisitedVertex);
}
for (int i1 = 0; i1 < visited.length; i1++) {
if (vertexList.get(i1)!= null)
if (visited[i1] != 0) {
queue.insert(i1);
visited[i1] = 1;
}
}
}
System.out.print(result);
}
}
//bfs中获取未访问的
public int getUnvisitedVertex(int v) {
for (int i = 0; i < numOfEdges; i++) {
if(edges[v][i] == 1 && vertexList.get(i) == null) {
return i;
}
}
return -1;
}
//深度优先遍历
public void depthFirstSearch(int v) { //驱动函数
boolean visited[] = new boolean[vertexList.size()];
for (int i = 0; i < vertexList.size(); i++) {
visited[i] = false;
}
dfs(v, visited); //把每个结点遍历一次。
System.out.println();
}
//进行深度优先遍历的内部递归方法使用
private void dfs(int i, boolean visited[]) { //工作函数
System.out.print(i + " ");
visited[i] = true;
for (int j = 0; j < vertexList.size(); j++) {
if (edges[i][j] != 0 && edges[i][j] != vertexList.size() && !visited[j]) {
dfs(j, visited);
}
}
}
public boolean addNode(Object data){
Node a = new Node(data);
return node.add(a);
}
public boolean removeNode(Object data){
boolean result = false;
for (int i = 0;i<node.size();i++) {
if (data == node.get(i).data)
node.remove(data);
result = true;
}
return result;
}
public void add (Edge<Integer> edge){
int fromVertexIndex = edge.fromVertexIndex;
int toVertexIndex = edge.toVertexIndex;
Node <E, T> fromVertex = node.get(fromVertexIndex);
Node <E, T> toVertex = node.get(toVertexIndex);
if (fromVertex.firstOut == null) {
//插入到顶点的出边属性
fromVertex.firstOut = (Edge <T>) edge;
} else {
// 插入到edge的nextSameFromVertex属性
Edge<Integer> tempEdge = (Edge <Integer>) fromVertex.firstOut;
//找到最后一个Edge
while (tempEdge.nextSameFromVertex != null) {
tempEdge = tempEdge.nextSameFromVertex;
}
tempEdge.nextSameFromVertex = edge;
}
if (toVertex.firstIn == null) {
//插入到顶点的入边属性
toVertex.firstIn = (Edge <T>) edge;
} else {
// 插入到edge的nextSameToVertex属性
Edge<Integer> tempEdge = (Edge <Integer>) toVertex.firstIn;
//找到最后一个Edge
while (tempEdge.nextSameToVertex != null) {
tempEdge = tempEdge.nextSameToVertex;
}
tempEdge.nextSameToVertex = edge;
}
}
public void removeEdge(int a,int b){
Edge <T> node1;
Edge <T> node2;
node1 = node.get(a).firstOut;
node2 = node.get(b).firstIn;
if(node1.toVertexIndex==b)
node.get(a).firstOut = node1.nextSameToVertex;
else {
while (node1.nextSameToVertex.toVertexIndex!=b){
node1 = node1.nextSameToVertex;
}
node1.nextSameToVertex = (node1.nextSameToVertex).nextSameToVertex;
}
if (node2.fromVertexIndex == a){
node.get(b).firstIn = node1.nextSameFromVertex;
}else {
while (node2.nextSameFromVertex.fromVertexIndex != b){
node2 = node2.nextSameFromVertex;
}
node1.nextSameFromVertex = (node1.nextSameFromVertex).nextSameFromVertex;
}
}
public ArrayList<E> bfs(int a) {
//初始化队列
LinkedQueue linkedQueue = new LinkedQueue();
//设立一个访问标志数组
int[] visited = new int[node.size()];
//设立list
ArrayList<E> list = new ArrayList<E>();
int current;
Edge <T> headNode = null;
//未访问的元素标记为0
for (int i = 0; i<visited.length;i++){
visited[i] = 0;
}
//标记访问a
linkedQueue.enqueue(a);
//访问后标记为1
visited[a] = 1;
while (!linkedQueue.isEmpty()){
current = (int) linkedQueue.dequeue();
list.add(node.get(current).data);
for (int j = 0;j<visited.length;j++)
headNode = node.get(j).firstOut;
while (headNode!=null){
if (visited[headNode.toVertexIndex] == 0) {
linkedQueue.enqueue(headNode.toVertexIndex);
visited[headNode.toVertexIndex] = 1;
}
headNode = headNode.nextSameToVertex;
}
}
return list;
}
public int size(){
return node.size();
}
public boolean isEmpty(){
if (node.size() == 0)
return true;
else
return false;
}
void dijkstra() {
int[] shortPath = new int[size];
boolean[] isgetPath = new boolean[size];
int minPath;
int minVertex = 0;// 找到的最短权值的顶点
int sum = 0;
// 初始化第一行顶点权值路径
for (int i = 0; i < size; i++) {
shortPath[i] = ints[0][i];
}
isgetPath[0] = true;
System.out.println("从第0个顶点开始查找");
for (int i = 1; i < size; i++) {
minPath = MAX_VALUE;
// 找到已知权值数组中最小的权值
for (int j = 1; j < size; j++) {
if (!isgetPath[j] && shortPath[j] < minPath) {
minPath = shortPath[j];
minVertex = j;
}
}
sum+= minPath;
System.out.println("找到最短路径顶点:" + minVertex+" 权值为:"+minPath);
isgetPath[minVertex] = true;
for (int j = 1; j < size; j++) {
// 根据找到的最端路径的顶点去遍历添加,最新路径
if (!isgetPath[j] && (minPath + ints[minVertex][j]) < shortPath[j]) {
shortPath[j] = minPath + ints[minVertex][j];
}
}
}
System.out.println("---------最短路径为:"+sum);
for (int j = 1; j < size; j++) {
System.out.println("找到V0到顶点V" + j + "的最短路径:"+shortPath[j]);
}
}
标签:site let alt 无向图 邻接表 操作 最小 显示 nod
原文地址:http://www.cnblogs.com/lyxwatm/p/7897098.html