Java实现基于邻接表的图的几个应用

来源:互联网 发布:淘宝卖家退款流程 编辑:程序博客网 时间:2024/06/04 18:33

Java实现基于邻接表的图的几个应用

图建立类:

package graph1;import java.util.LinkedList;import graph.Graph.edgeNode;public class Graph {class EdgeNode{int adjvex;EdgeNode nextEdge;}class VexNode{int data;EdgeNode firstEdge;boolean isVisted;public boolean isVisted() {return isVisted;}public void setVisted(boolean isVisted) {this.isVisted = isVisted;}}VexNode[] vexsarray ;int[] visited = new int[100];boolean[] isVisited = new boolean[100];public void linkLast(EdgeNode target,EdgeNode node) {while (target.nextEdge!=null) {target=target.nextEdge;}target.nextEdge=node;}public int getPosition(int data) {for(int i=0;i<vexsarray.length;i++) {if (data==vexsarray[i].data) {return i;}}return -1;}public void buildGraph(int[] vexs,int[][] edges ) {int vLen = vexs.length;int eLen = edges.length;vexsarray = new VexNode[vLen];for(int i=0;i<vLen;i++) {vexsarray[i] = new VexNode();vexsarray[i].data = vexs[i];vexsarray[i].firstEdge = null;}for(int i=0;i<eLen;i++) {int a = edges[i][0];int b = edges[i][1];int start = getPosition(a);int end = getPosition(b);EdgeNode edgeNode = new EdgeNode();edgeNode.adjvex = end;if (vexsarray[start].firstEdge == null) {vexsarray[start].firstEdge = edgeNode;} else {linkLast(vexsarray[start].firstEdge,edgeNode);}}}public void printGraph() {for(int i=0;i<vexsarray.length;i++) {System.out.printf("%d--",vexsarray[i].data);EdgeNode node = vexsarray[i].firstEdge;while (node!=null) {System.out.printf("%d(%d)--",node.adjvex,vexsarray[node.adjvex].data);node = node.nextEdge;}System.out.println("\n");}}/* * 深度遍历 */public void DFS(int vex) {int w;EdgeNode node;visited[vex] = 1;//System.out.println(vex);node=vexsarray[getPosition(vex)].firstEdge;while (node!=null) {w=node.adjvex;if (visited[vexsarray[w].data]==0) {DFS(vexsarray[w].data);}node=node.nextEdge;}}/* * 广度遍历 */public void BFS(int vex) {VexNode start = vexsarray[getPosition(vex)];LinkedList<VexNode> queue = new  LinkedList<>();start.setVisted(true);queue.add(start);System.out.println(start.data);VexNode currVex;while (!queue.isEmpty()) {currVex=queue.remove(0);EdgeNode node = currVex.firstEdge;while (node!=null) {if (vexsarray[node.adjvex].isVisted==false) {System.out.println(vexsarray[node.adjvex].data);vexsarray[node.adjvex].setVisted(true);queue.add(vexsarray[node.adjvex]);}node=node.nextEdge;}}}}

应用类:

package graph1;import java.util.Iterator;import java.util.LinkedList;import java.util.Stack;import graph.DFS;import graph.Graph.vexNode;import graph1.Graph.EdgeNode;import graph1.Graph.VexNode;public class Graph_App {int[] index = new int[20];LinkedList<VexNode> path = new LinkedList<>();LinkedList<VexNode> path2 = new LinkedList<>();boolean[] vis = new boolean[15];//int d = -1;/* * 判断是否为连通图 */public boolean connect(Graph graph) {boolean flag=true;graph.DFS(0);for(int i=0;i<graph.vexsarray.length;i++) {if (!graph.vexsarray[i].isVisted) {flag=flag;break;}}return flag;}/* * 判断顶点v1到v2之间是否有路径 */public boolean HasaPath(Graph graph,VexNode vexNode1,VexNode vexNode2) {EdgeNode node = vexNode1.firstEdge;int i=0;boolean flag=false;while (node!=null) {index[i] = graph.vexsarray[node.adjvex].data;i++;node=node.nextEdge;}for(int j=0;j<index.length;j++) {if (index[j]==vexNode2.data) {flag=true;}}return flag;}/* * 找到顶点v1到v2之间的路径 */public void FindPath(Graph graph,VexNode vexNode1,VexNode vexNode2) {EdgeNode node = vexNode1.firstEdge;int w;VexNode vexNode ;vexNode1.setVisted(true);path.add(vexNode1);while (node!=null) {w=node.adjvex;if (!graph.vexsarray[w].isVisted) {FindPath(graph, graph.vexsarray[w], vexNode2);}node=node.nextEdge;}}}

测试类:

package graph1;import java.util.Iterator;import graph1.Graph.VexNode;public class Tset2 {public static void main(String[] args) {int[] vexs = {0,1,2,3,4};int[][] edges = {{0,1},{0,3},{1,0},{1,2},{2,1},{2,3},{2,4},{3,0},{3,2},{3,4},{4,2},{4,3},};Graph graph = new Graph();graph.buildGraph(vexs, edges);graph.printGraph();Graph_App app = new Graph_App();System.out.println(app.connect(graph));System.out.println(app.HasaPath(graph, graph.vexsarray[0],graph.vexsarray[3]));app.FindPath(graph, graph.vexsarray[0],graph.vexsarray[4]);Iterator<VexNode> iterator = app.path.iterator();while (iterator.hasNext()) {System.out.println(iterator.next().data);}}}
原创粉丝点击