diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..26d33521af10bcc7fd8cea344038eaaeb78d0ef5 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000000000000000000000000000000000000..f4acef59005c686f8fe94d5cca63331b903da053 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000000000000000000000000000000000000..5ecbb46dd45084452d0362070e17bf0290550dba --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000000000000000000000000000000000000..e96534fb27b68192f27f985d3879e173ec77adb8 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000000000000000000000000000000000..35eb1ddfbbc029bcab630581847471d7f238ec53 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.project b/.project new file mode 100644 index 0000000000000000000000000000000000000000..8d6dad47c4584f4f1d070dfe117863ccdbba8fcd --- /dev/null +++ b/.project @@ -0,0 +1,11 @@ + + + lec09-graph + + + + + + + + diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000000000000000000000000000000000..99f26c0203a7844de00dbfc56e6a35d8ed3c022c --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/lec09-graph.iml b/lec09-graph.iml new file mode 100644 index 0000000000000000000000000000000000000000..48426fcce918b28a721230cc9a22a71d40ba14f5 --- /dev/null +++ b/lec09-graph.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git "a/\344\275\234\344\270\232/src/\345\271\277\345\272\246\351\201\215\345\216\206/Graph.java" "b/\344\275\234\344\270\232/src/\345\271\277\345\272\246\351\201\215\345\216\206/Graph.java" new file mode 100644 index 0000000000000000000000000000000000000000..b41a6ed2d1184806a2cf4ca1928d9228828d2302 --- /dev/null +++ "b/\344\275\234\344\270\232/src/\345\271\277\345\272\246\351\201\215\345\216\206/Graph.java" @@ -0,0 +1,137 @@ +package 广度遍历; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedList; + +class Graph { + + private ArrayList vertexList; // 存储顶点集合 + private int[][] edges; // 存储图对应的邻结矩阵 + private int numOfEdges; // 表示边的数目 + + // 构造器 + public Graph(int n) { + // 初始化矩阵和vertexList + edges = new int[n][n]; + vertexList = new ArrayList(n); + numOfEdges = 0; + + } + + // 插入结点 + public void insertVertex(String vertex) { + vertexList.add(vertex); + } + + // 添加边 + /** + * + * @param v1 第二个顶点对应的下标 + * @param v2 第二个顶点对应的下标 + * @param weight 表示权值,0:不连接;1:连接 + */ + public void insertEdge(int v1, int v2, int weight) { + edges[v1][v2] = weight; + edges[v2][v1] = weight; + numOfEdges++; + } + + // 图中常用的方法 + // 返回结点的个数 + public int getNumOfVertex() { + return vertexList.size(); + } + + // 得到边的数目 + public int getNumOfEdges() { + return numOfEdges; + } + + // 返回结点i(下标)对应的数据 0->"A" 1->"B" 2->"C" + public String getValueByIndex(int i) { + return vertexList.get(i); + } + + // 返回v1和v2的权值 + public int getWeight(int v1, int v2) { + return edges[v1][v2]; + } + + // 显示图对应的矩阵 + public void showGraph() { + for (int[] link : edges) { + System.out.println(Arrays.toString(link)); + } + } + + // 得到第一个邻接结点的下标 w + /** + * + * @param index + * @return 如果存在就返回对应的下标,否则返回-1 + */ + public int getFirstNeighbor(int index) { + for (int j = 0; j < vertexList.size(); j++) { + if (edges[index][j] > 0) { + return j; + } + } + return -1; + } + + // 根据前一个邻接结点的下标来获取下一个邻接结点 + 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 bfs(boolean[] isVisited, int i) { + int u; // 表示队列的头结点对应下标 + int w; // 邻接结点w + // 队列,记录结点访问的顺序 + LinkedList queue = new LinkedList(); + // 访问结点,输出结点信息 + System.out.print(getValueByIndex(i) + "=>"); + // 标记为已访问 + isVisited[i] = true; + // 将结点加入队列 + queue.addLast(i); + + while (!queue.isEmpty()) {// 体现出我们的广度优先 + // 取出队列的头结点下标 + u = queue.removeFirst(); + // 得到第一个邻接结点的下标 w + w = getFirstNeighbor(u); + while (w != -1) {// 找到 + // 是否访问过 + if (!isVisited[w]) { + System.out.print(getValueByIndex(w) + "=>"); + // 标记已经访问 + isVisited[w] = true; + // 入队 + queue.addLast(w); + } + // 以u为前驱点,找w后面的下一个邻结点 + w = getNextNeighbor(u, w); + } + } + + } + + // 遍历所有的结点,都进行广度优先搜索 + public void bfs() { + boolean[] isVisited = new boolean[vertexList.size()]; + for (int i = 0; i < getNumOfVertex(); i++) { + if (!isVisited[i]) { + bfs(isVisited, i); + } + } + } + +} \ No newline at end of file diff --git "a/\344\275\234\344\270\232/src/\346\267\261\345\272\246\351\201\215\345\216\206/Graph.java" "b/\344\275\234\344\270\232/src/\346\267\261\345\272\246\351\201\215\345\216\206/Graph.java" new file mode 100644 index 0000000000000000000000000000000000000000..86073e42ebe89d2058e6badabc032c71a19dde1e --- /dev/null +++ "b/\344\275\234\344\270\232/src/\346\267\261\345\272\246\351\201\215\345\216\206/Graph.java" @@ -0,0 +1,119 @@ +package 深度遍历; +import java.util.ArrayList; +import java.util.Arrays; + +class Graph { + + private ArrayList vertexList; //存储顶点集合 + private int[][] edges; //存储图对应的邻结矩阵 + private int numOfEdges; //表示边的数目 + + //构造器 + public Graph(int n) { + //初始化矩阵和vertexList + edges = new int[n][n]; + vertexList = new ArrayList(n); + numOfEdges = 0; + + } + + //插入结点 + public void insertVertex(String vertex) { + vertexList.add(vertex); + } + //添加边 + /** + * + * @param v1 第二个顶点对应的下标 + * @param v2 第二个顶点对应的下标 + * @param weight 表示权值,0:不连接;1:连接 + */ + public void insertEdge(int v1, int v2, int weight) { + edges[v1][v2] = weight; + edges[v2][v1] = weight; + numOfEdges++; + } + + //图中常用的方法 + //返回结点的个数 + public int getNumOfVertex() { + return vertexList.size(); + } + + // 得到边的数目 + public int getNumOfEdges() { + return numOfEdges; + } + + // 返回结点i(下标)对应的数据 0->"A" 1->"B" 2->"C" + public String getValueByIndex(int i) { + return vertexList.get(i); + } + + // 返回v1和v2的权值 + public int getWeight(int v1, int v2) { + return edges[v1][v2]; + } + + // 显示图对应的矩阵 + public void showGraph() { + for (int[] link : edges) { + System.out.println(Arrays.toString(link)); + } + } + + //得到第一个邻接结点的下标 w + /** + * + * @param index + * @return 如果存在就返回对应的下标,否则返回-1 + */ + public int getFirstNeighbor(int index) { + for(int j = 0; j < vertexList.size(); j++) { + if(edges[index][j] > 0) { + return j; + } + } + return -1; + } + //根据前一个邻接结点的下标来获取下一个邻接结点 + 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; + } + + //深度优先遍历算法 + //i 第一次就是 0 + private void dfs(boolean[] isVisited, int i) { + //首先我们访问该结点,输出 + System.out.print(getValueByIndex(i) + "->"); + //将结点设置为已经访问 + isVisited[i] = true; + //查找结点i的第一个邻接结点w + int w = getFirstNeighbor(i); + while(w != -1) {//说明有 + if(!isVisited[w]) { + dfs(isVisited, w); + } + //如果w结点已经被访问过 + w = getNextNeighbor(i, w); + } + + } + + //对dfs 进行一个重载, 遍历我们所有的结点,并进行 dfs + public void dfs() { + boolean[] isVisited = new boolean[vertexList.size()]; + //遍历所有的结点,进行dfs[回溯] + for(int i = 0; i < getNumOfVertex(); i++) { + if(!isVisited[i]) { + dfs(isVisited, i); + } + } + } + +} \ No newline at end of file diff --git "a/\344\275\234\344\270\232/src/\347\237\251\351\230\265\350\241\250\347\244\272/Graph.java" "b/\344\275\234\344\270\232/src/\347\237\251\351\230\265\350\241\250\347\244\272/Graph.java" new file mode 100644 index 0000000000000000000000000000000000000000..e344238ae049a416d3cdefebe0bede65f0b9b95c --- /dev/null +++ "b/\344\275\234\344\270\232/src/\347\237\251\351\230\265\350\241\250\347\244\272/Graph.java" @@ -0,0 +1,65 @@ +package 矩阵表示; + +import java.util.ArrayList; +import java.util.Arrays; + +class Graph { + + private ArrayList vertexList; //存储顶点集合 + private int[][] edges; //存储图对应的邻结矩阵 + private int numOfEdges; //表示边的数目 + + //构造器 + public Graph(int n) { + //初始化矩阵和vertexList + edges = new int[n][n]; + vertexList = new ArrayList(n); + numOfEdges = 0; + + } + + //插入结点 + public void insertVertex(String vertex) { + vertexList.add(vertex); + } + //添加边 + /** + * + * @param v1 第二个顶点对应的下标 + * @param v2 第二个顶点对应的下标 + * @param weight 表示权值,0:不连接;1:连接 + */ + public void insertEdge(int v1, int v2, int weight) { + edges[v1][v2] = weight; + edges[v2][v1] = weight; + numOfEdges++; + } + + //图中常用的方法 + //返回结点的个数 + public int getNumOfVertex() { + return vertexList.size(); + } + + // 得到边的数目 + public int getNumOfEdges() { + return numOfEdges; + } + + // 返回结点i(下标)对应的数据 0->"A" 1->"B" 2->"C" + public String getValueByIndex(int i) { + return vertexList.get(i); + } + + // 返回v1和v2的权值 + public int getWeight(int v1, int v2) { + return edges[v1][v2]; + } + + // 显示图对应的矩阵 + public void showGraph() { + for (int[] link : edges) { + System.out.println(Arrays.toString(link)); + } + } +} \ No newline at end of file diff --git "a/\344\275\234\344\270\232/src/\351\202\273\346\216\245\350\241\250_\347\224\250\347\202\271\350\241\250\347\244\272/CreateGraph3.java" "b/\344\275\234\344\270\232/src/\351\202\273\346\216\245\350\241\250_\347\224\250\347\202\271\350\241\250\347\244\272/CreateGraph3.java" new file mode 100644 index 0000000000000000000000000000000000000000..a69a0c67a374155195711a0b82a9112de9411a2b --- /dev/null +++ "b/\344\275\234\344\270\232/src/\351\202\273\346\216\245\350\241\250_\347\224\250\347\202\271\350\241\250\347\244\272/CreateGraph3.java" @@ -0,0 +1,83 @@ +package 邻接表_用点表示; + +import java.util.Scanner; +public class CreateGraph3 { + /** + * 根据用户输入的string类型的顶点返回该顶点 + * @param graph 图 + * @param str 输入数据 + * @return返回一个顶点 + */ + public Vertex1 getVertex(Graph1 graph,String str){ + for(int i=0;i"+current.verName); + current=current.nextNode; + } + System.out.println(); + } + } + public static void main(String[] args) { + Graph1 graph=new Graph1(); + CreateGraph3 createGraph=new CreateGraph3(); + createGraph.initialGraph(graph); + createGraph.outputGraph(graph); + } +} \ No newline at end of file diff --git "a/\344\275\234\344\270\232/src/\351\202\273\346\216\245\350\241\250_\347\224\250\347\202\271\350\241\250\347\244\272/Graph1.java" "b/\344\275\234\344\270\232/src/\351\202\273\346\216\245\350\241\250_\347\224\250\347\202\271\350\241\250\347\244\272/Graph1.java" new file mode 100644 index 0000000000000000000000000000000000000000..053c92f4e2461c7b1f48ba0437aa420c51177bcf --- /dev/null +++ "b/\344\275\234\344\270\232/src/\351\202\273\346\216\245\350\241\250_\347\224\250\347\202\271\350\241\250\347\244\272/Graph1.java" @@ -0,0 +1,11 @@ +package 邻接表_用点表示; + +/** + * 自定义图类 + * @author King + */ +public class Graph1 { + Vertex1[] vertexArray=new Vertex1[100]; + int verNum=0; + int edgeNum=0; +} \ No newline at end of file diff --git "a/\344\275\234\344\270\232/src/\351\202\273\346\216\245\350\241\250_\347\224\250\347\202\271\350\241\250\347\244\272/Vertex1.java" "b/\344\275\234\344\270\232/src/\351\202\273\346\216\245\350\241\250_\347\224\250\347\202\271\350\241\250\347\244\272/Vertex1.java" new file mode 100644 index 0000000000000000000000000000000000000000..31749c84c82adf55a1be9292d36887b8b414b06a --- /dev/null +++ "b/\344\275\234\344\270\232/src/\351\202\273\346\216\245\350\241\250_\347\224\250\347\202\271\350\241\250\347\244\272/Vertex1.java" @@ -0,0 +1,10 @@ +package 邻接表_用点表示; + +/** + * 图的顶点类 + * @author King + */ +public class Vertex1 { + String verName; + Vertex1 nextNode; +} \ No newline at end of file diff --git "a/\344\275\234\344\270\232/src/\351\202\273\346\216\245\350\241\250_\347\224\250\350\276\271\350\241\250\347\244\272/CreateGraph.java" "b/\344\275\234\344\270\232/src/\351\202\273\346\216\245\350\241\250_\347\224\250\350\276\271\350\241\250\347\244\272/CreateGraph.java" new file mode 100644 index 0000000000000000000000000000000000000000..898a9de72a8ede41ae9d692c2c9673ea9d01c1f4 --- /dev/null +++ "b/\344\275\234\344\270\232/src/\351\202\273\346\216\245\350\241\250_\347\224\250\350\276\271\350\241\250\347\244\272/CreateGraph.java" @@ -0,0 +1,83 @@ +package 邻接表_用边表示; + +import java.util.Scanner; +/** + * 通过构建边和点的对象来创建图 + * @author King + */ +public class CreateGraph { + /** + * 根据顶点信息String,返回边的对象 + * @param graph 图 + * @param str 顶点名称 + * @return 返回的是边对象的标签 + */ + public int vtoe(Graph graph,String str){ + for(int i=0;i"+graph.vertexList.get(edge.edgeName).vertexName); + edge=edge.next; + } + System.out.println(); + } + } + public static void main(String[] args) { + CreateGraph createGraph=new CreateGraph(); + Graph graph=new Graph(); + createGraph.initialGraph(graph); + createGraph.outputGraph(graph); + } +} \ No newline at end of file diff --git "a/\344\275\234\344\270\232/src/\351\202\273\346\216\245\350\241\250_\347\224\250\350\276\271\350\241\250\347\244\272/Edge.java" "b/\344\275\234\344\270\232/src/\351\202\273\346\216\245\350\241\250_\347\224\250\350\276\271\350\241\250\347\244\272/Edge.java" new file mode 100644 index 0000000000000000000000000000000000000000..7dec63108bfe6e8fad190d02344756e894003c74 --- /dev/null +++ "b/\344\275\234\344\270\232/src/\351\202\273\346\216\245\350\241\250_\347\224\250\350\276\271\350\241\250\347\244\272/Edge.java" @@ -0,0 +1,12 @@ +package 邻接表_用边表示; + +/** + * 图的边对象类 + * @author King + */ +public class Edge { + int edgeName; + Edge next; + public Edge(){ + } +} \ No newline at end of file diff --git "a/\344\275\234\344\270\232/src/\351\202\273\346\216\245\350\241\250_\347\224\250\350\276\271\350\241\250\347\244\272/Graph.java" "b/\344\275\234\344\270\232/src/\351\202\273\346\216\245\350\241\250_\347\224\250\350\276\271\350\241\250\347\244\272/Graph.java" new file mode 100644 index 0000000000000000000000000000000000000000..41309a4b367f898f5f89df8084ce7fa6cee83350 --- /dev/null +++ "b/\344\275\234\344\270\232/src/\351\202\273\346\216\245\350\241\250_\347\224\250\350\276\271\350\241\250\347\244\272/Graph.java" @@ -0,0 +1,11 @@ +package 邻接表_用边表示; + + +import java.util.ArrayList; + +public class Graph { + ArrayList vertexList=new ArrayList(); + int vertexNum=0; + int edgeNum=0; + public Graph(){} +} \ No newline at end of file diff --git "a/\344\275\234\344\270\232/src/\351\202\273\346\216\245\350\241\250_\347\224\250\350\276\271\350\241\250\347\244\272/Vertex.java" "b/\344\275\234\344\270\232/src/\351\202\273\346\216\245\350\241\250_\347\224\250\350\276\271\350\241\250\347\244\272/Vertex.java" new file mode 100644 index 0000000000000000000000000000000000000000..e78e0623287f73646a4ef06a6669b2b69dba05ca --- /dev/null +++ "b/\344\275\234\344\270\232/src/\351\202\273\346\216\245\350\241\250_\347\224\250\350\276\271\350\241\250\347\244\272/Vertex.java" @@ -0,0 +1,12 @@ +package 邻接表_用边表示; + +/** + * 图的点对象类 + * @author King + */ +public class Vertex { + String vertexName; + Edge firstEdge=new Edge(); + public Vertex(){ + } +} \ No newline at end of file diff --git "a/\344\275\234\344\270\232/\344\275\234\344\270\232.iml" "b/\344\275\234\344\270\232/\344\275\234\344\270\232.iml" new file mode 100644 index 0000000000000000000000000000000000000000..c90834f2d607afe55e6104d8aa2cdfffb713f688 --- /dev/null +++ "b/\344\275\234\344\270\232/\344\275\234\344\270\232.iml" @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file