From 8438077c6ee5299e852da6a581508aeea92092e0 Mon Sep 17 00:00:00 2001 From: null796 <982254607@qq.com> Date: Mon, 11 Jan 2021 22:54:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=BE=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Graph.java | 53 +++++++++++++++++++++ GraphBreadthdepth.java | 88 +++++++++++++++++++++++++++++++++++ GraphLoopTest.java | 92 ++++++++++++++++++++++++++++++++++++ ListUDG.java | 103 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 336 insertions(+) create mode 100644 Graph.java create mode 100644 GraphBreadthdepth.java create mode 100644 GraphLoopTest.java create mode 100644 ListUDG.java diff --git a/Graph.java b/Graph.java new file mode 100644 index 0000000..351833a --- /dev/null +++ b/Graph.java @@ -0,0 +1,53 @@ +package Graph; + +import java.util.ArrayList; +import java.util.Arrays; + +public class Graph { + private static ArrayList vertexList; + private int [][] edges; + private int numOfEdges; + public static void main(String[] args) { + + int n = 5; + String Vertexs[] = {"A","B","C","D","E"}; + + Graph graph= new Graph(n); + + for(String vertex : Vertexs){ + graph.insertVertex(vertex); + } + graph.insert_char_Edge("A","B",1); + graph.insert_char_Edge("A","D",1); + graph.insert_char_Edge("B","C",1); + graph.insert_char_Edge("B","D",1); + graph.insert_char_Edge("D","E",1); + + graph.showGraph(); + } + + + public void insertVertex(String vertex) { + vertexList.add(vertex); + } + public Graph(int n){ + + edges = new int[n][n]; + vertexList = new ArrayList(n); + numOfEdges = 0; + } + + public void showGraph(){ + for(int[]link:edges){ + System.out.println(Arrays.toString(link)); + } + } + + public void insert_char_Edge(String a,String b,int weight){ + int v1 = vertexList.indexOf(a); + int v2 = vertexList.indexOf(b); + edges[v1][v2] = weight; + edges[v2][v1] = weight; + numOfEdges++; + } +} \ No newline at end of file diff --git a/GraphBreadthdepth.java b/GraphBreadthdepth.java new file mode 100644 index 0000000..46835cd --- /dev/null +++ b/GraphBreadthdepth.java @@ -0,0 +1,88 @@ +package Graph; + +import java.util.ArrayDeque; +import java.util.Deque; +import java.util.LinkedList; +import java.util.Queue; + +public class GraphBreadthdepth { + //存储节点的连接关系 + int[][] connectRelation; + //节点的内容 + int[] content; + + public static void main(String[] args) { + int[][] connectRelation = new int[][]{ + { 0, 1, 1, 0, 0, 0, 0, 0}, + { 1, 0, 0, 1, 1, 0, 0, 0}, + { 1, 0, 0, 0, 0, 1, 1, 0}, + { 0, 1, 0, 0, 0, 0, 0, 1}, + { 0, 1, 0, 0, 0, 0, 0, 1}, + { 0, 0, 1, 0, 0, 0, 0, 1}, + { 0, 0, 1, 0, 0, 0, 0, 1}, + { 0, 0, 0, 1, 1, 1, 1, 0} + }; + int[] content = new int[]{ 1, 2, 3, 4, 5, 6, 7, 8}; + GraphBreadthdepth graph1 = new GraphBreadthdepth(); + graph1.setConnectRelation(connectRelation); + graph1.setContent(content); + System. out.println( "深度优先遍历:"); + graph1.depthFirstSearch(); + System. out.println( "广度优先遍历:"); + graph1.breadthFirstSearch(); + } + + //广度优先遍历 + public void breadthFirstSearch(){ + Deque deque = new ArrayDeque(); + deque.offer( 0); + boolean[] visited = new boolean[ this. content. length]; //记录节点是否已经被访问 + while (!deque.isEmpty()){ + int node = (Integer)deque.pollFirst(); + if (visited[node] == false){ + System. out.println( this. content[node]); + visited[node] = true; + for ( int i = 0; i < this. content. length ; ++i){ + if ( this. connectRelation[node][i] == 1){ + deque.offer(i); + } + } + } + } + + } + + //深度优先遍历 + public void depthFirstSearch(){ + boolean[] visited = new boolean[ this. content. length]; //记录节点是否已经被访问 + depthFirstSearch(visited, 0); + } + + public void depthFirstSearch( boolean[] visited, int node){ + if (visited[node] == false){ //如果节点未被访问 + System. out.println( this. content[node]); + visited[node] = true; + for ( int i = 0; i < this. content. length; ++i){ + if ( this. connectRelation[node][i] == 1){ + depthFirstSearch(visited, i); + } + } + } + } + + public int[][] getConnectRelation() { + return connectRelation; + } + + public void setConnectRelation( int[][] connectRelation) { + this. connectRelation = connectRelation; + } + + public int[] getContent() { + return content; + } + + public void setContent( int[] content) { + this. content = content; + } +} \ No newline at end of file diff --git a/GraphLoopTest.java b/GraphLoopTest.java new file mode 100644 index 0000000..03f5389 --- /dev/null +++ b/GraphLoopTest.java @@ -0,0 +1,92 @@ +package Graph; +import java.util.*; +public class GraphLoopTest { + private Map> graph = new HashMap>(); + + + public void initGraphData() { +// 图结构如下 +// 1 +// / \ +// 2 3 +// / \ / \ +// 4 5 6 7 +// \ | / \ / +// 8 9 + graph.put("1", Arrays.asList("2", "3")); + graph.put("2", Arrays.asList("1", "4", "5")); + graph.put("3", Arrays.asList("1", "6", "7")); + graph.put("4", Arrays.asList("2", "8")); + graph.put("5", Arrays.asList("2", "8")); + graph.put("6", Arrays.asList("3", "8", "9")); + graph.put("7", Arrays.asList("3", "9")); + graph.put("8", Arrays.asList("4", "5", "6")); + graph.put("9", Arrays.asList("6", "7")); + } + +//广度优先遍历 + private Queue queue = new LinkedList(); + private Map status = new HashMap(); + + + public void BFSSearch(String startPoint) { + + queue.add(startPoint); + status.put(startPoint, false); + bfsLoop(); + } + + private void bfsLoop() { + + String currentQueueHeader = queue.poll(); //出队 + status.put(currentQueueHeader, true); + System.out.println(currentQueueHeader); + + List neighborPoints = graph.get(currentQueueHeader); + for (String poinit : neighborPoints) { + if (!status.getOrDefault(poinit, false)) { //未被遍历 + if (queue.contains(poinit)) continue; + queue.add(poinit); + status.put(poinit, false); + } + } + if (!queue.isEmpty()) { + bfsLoop(); + } + } + + + + private Stack stack = new Stack(); + public void DFSSearch(String startPoint) { + stack.push(startPoint); + status.put(startPoint, true); + dfsLoop(); + } + + private void dfsLoop() { + if(stack.empty()){ + return; + } + + String stackTopPoint = stack.peek(); + + List neighborPoints = graph.get(stackTopPoint); + for (String point : neighborPoints) { + if (!status.getOrDefault(point, false)) { + stack.push(point); + status.put(point, true); + dfsLoop(); + } + } + String popPoint = stack.pop(); + System.out.println(popPoint); + } + + public static void main(String[] args) { + GraphLoopTest test = new GraphLoopTest(); + test.initGraphData(); +// test.BFSSearch("1"); + test.DFSSearch("1"); + } +} \ No newline at end of file diff --git a/ListUDG.java b/ListUDG.java new file mode 100644 index 0000000..f0ec37b --- /dev/null +++ b/ListUDG.java @@ -0,0 +1,103 @@ +package Graph; + +public class ListUDG { + int vlen; + int elen; + VertexNode[] vertexNodeList; + EdgeNode edgeNode; + + public ListUDG(char[] vexs, char[][] edges) { + vlen = vexs.length; + elen = edges.length; + + + vertexNodeList = new VertexNode[vlen]; + for (int i = 0; i < vlen; i++) { + vertexNodeList[i] = new VertexNode(); + vertexNodeList[i].vertex = vexs[i]; + vertexNodeList[i].firstedge = null; + } + + + for (int i = 0; i < elen; i++) { + EdgeNode edgeNode_1 = new EdgeNode(); + EdgeNode edgeNode_2 = new EdgeNode(); + int vi = getPosition(edges[i][0], vexs); + int vj = getPosition(edges[i][1], vexs); + + edgeNode_1.adjvex = edges[i][1]; + edgeNode_1.next = vertexNodeList[vi].firstedge; + vertexNodeList[vi].firstedge = edgeNode_1; + + edgeNode_2.adjvex = edges[i][0]; + edgeNode_2.next = vertexNodeList[vj].firstedge; + vertexNodeList[vj].firstedge = edgeNode_2; + } + } + + + private class VertexNode { + char vertex; + EdgeNode firstedge; + } + + + private class EdgeNode { + char adjvex; + EdgeNode next; + } + + + private int getPosition(char ch, char[] vexs) { + for (int i = 0; i < vlen; i++) + if (vexs[i] == ch) + return i; + return -1; + } + + + public void print() { + System.out.printf("AdjList:\n"); + for (int i = 0; i < vlen; i++) { + System.out.print(vertexNodeList[i].vertex + "-->"); + if (vertexNodeList[i].firstedge != null) { + EdgeNode mEdgeNode = new EdgeNode(); + mEdgeNode = vertexNodeList[i].firstedge; + System.out.print(mEdgeNode.adjvex); + while (mEdgeNode.next != null) { + mEdgeNode = mEdgeNode.next; + System.out.print(mEdgeNode.adjvex); + } + System.out.print("\n"); + } else { + System.out.print("\n"); + } + } + } + + + public static void main(String args[]) { + // + char[] vexs = { + 'A', 'B', 'C', 'D' + }; + // + char[][] edges = new char[][] { + { + 'A', 'B' + }, { + 'A', 'C' + }, { + 'A', 'D' + }, { + 'B', 'D' + }, { + 'C', 'D' + } + }; + + ListUDG listUDG = new ListUDG(vexs, edges); + listUDG.print(); + } + +} \ No newline at end of file -- Gitee