在vi终端编辑器不知怎么创文件名及书写其他代码,想了两个小时着实写不出来特来找方向一样牛人的指点
1条回答 默认 最新
关注 【相关推荐】
- 你可以看下这个问题的回答https://ask.csdn.net/questions/7477075
- 你也可以参考下这篇文章:JAVA数组的遍历 获取最大值最小值 数组的反转 数组的查找 练习 定义方法创建指定大小的数组,并添加指定元素 拼接两个数组
- 您还可以看一下 徐无忌老师的Java进阶高手课-并发编程透彻理解课程中的 什么是多线程的上下文切换,有什么影响?如何进行优化?(二)小节, 巩固相关知识点
- 除此之外, 这篇博客: JAVA数据结构——利用图的广度优先遍历搜索算法确定无向连通图的连通分量中的 代码实现利用广度优先遍历搜索确定无向图的连通分量: 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
1. 图类型:
package com.usts.edu.graphic; // 图的种类 有向图、有向网、无向图、无向网 public enum GraphKind { UDG, // 无向图(UnDirected Graph) DG, // 有向图(Directed Graph) UDN, // 无向网(UnDirected Network) DN; // 有向网(Directed Network) }
2. 图的接口
package com.usts.edu.graphic; //图的接口 public interface IGraph { void createGraph();//创建一个图 int getVexNum(); // 返回顶点数 int getArcNum();// 返回边数 Object getVex(int v) throws Exception;// 返回v表示结点的值, 0 <= v < vexNum int locateVex(Object vex);// 给定顶点的值vex,返回其在图中的位置,如果图中不包含此顶点,则返回-1 int firstAdjVex(int v) throws Exception; // 返回v的第一个邻接点,若v没有邻接点,则返回-1,其中0≤v<vexNum int nextAdjVex(int v, int w) throws Exception;// 返回v相对于w的下一个邻接点,若w是v的最后一个邻接点,则返回-1,其中0≤v, w<vexNum }
3. 创建图:
package com.usts.edu.graphic; import java.util.Scanner; public class MGraph implements IGraph { public final static int INFINITY = Integer.MAX_VALUE; private GraphKind kind; private int vexNum, arcNum; private Object[] vexs; private int[][] arcs; public MGraph() { this(null, 0, 0, null, null); } public MGraph(GraphKind kind, int vexNum, int arcNum, Object[] vexs, int[][] arcs) { this.kind = kind; this.vexNum = vexNum; this.arcNum = arcNum; this.vexs = vexs; this.arcs = arcs; } public void createGraph() { Scanner sc = new Scanner(System.in); System.out.println("请输入图的类型"); GraphKind kind = GraphKind.valueOf(sc.next()); switch (kind) { case UDG: createUDG(); return; case DG: createDG(); return; case UDN: createUDN(); return; case DN: createDN(); return; } } private void createUDG() { }; private void createDG() { }; private void createUDN() { Scanner sc = new Scanner(System.in); vexNum = sc.nextInt(); arcNum = sc.nextInt(); vexs = new Object[vexNum]; for (int v = 0; v < vexNum; v++) vexs[v] = sc.next(); arcs = new int[vexNum][vexNum]; for (int v = 0; v < vexNum; v++) for (int u = 0; u < vexNum; u++) arcs[v][u] = INFINITY; for (int k = 0; k < arcNum; k++) { int v = locateVex(sc.next()); int u = locateVex(sc.next()); arcs[v][u] = arcs[u][v] = sc.nextInt(); } } private void createDN() { Scanner sc = new Scanner(System.in); vexNum = sc.nextInt(); arcNum = sc.nextInt(); vexs = new Object[vexNum]; for (int v = 0; v < vexNum; v++) vexs[v] = sc.next(); arcs = new int[vexNum][vexNum]; for (int v = 0; v < vexNum; v++) for (int u = 0; u < vexNum; u++) arcs[v][u] = INFINITY; for (int k = 0; k < arcNum; k++) { int v = locateVex(sc.next()); int u = locateVex(sc.next()); arcs[v][u] = sc.nextInt(); } } public int getVexNum() { return vexNum; } public int getArcNum() { return arcNum; } public int locateVex(Object vex) { for (int v = 0; v < vexNum; v++) if (vexs[v].equals(vex)) return v; return -1; } public Object getVex(int v) throws Exception { if (v < 0 && v >= vexNum) throw new Exception("第" + v + "个顶点不存在!"); return vexs[v]; } public int firstAdjVex(int v) throws Exception { if (v < 0 && v >= vexNum) throw new Exception("第" + v + "个顶点不存在!"); for (int j = 0; j < vexNum; j++) if (arcs[v][j] != 0 && arcs[v][j] < INFINITY) return j; return -1; } public int nextAdjVex(int v, int w) throws Exception { if (v < 0 && v >= vexNum) throw new Exception("第" + v + "个顶点不存在!"); for (int j = w + 1; j < vexNum; j++) if (arcs[v][j] != 0 && arcs[v][j] < INFINITY) return j; return -1; } public GraphKind getKind() { return kind; } public int[][] getArcs() { return arcs; } public Object[] getVexs() { return vexs; } public void setArcNum(int arcNum) { this.arcNum = arcNum; } public void setArcs(int[][] arcs) { this.arcs = arcs; } public void setKind(GraphKind kind) { this.kind = kind; } public void setVexNum(int vexNum) { this.vexNum = vexNum; } public void setVexs(Object[] vexs) { this.vexs = vexs; } }
4. 实现搜索:
package com.usts.edu.graphic; import com.usts.edu.Queue.LinkQueue; /** * Created by Guanzhong Hu * Date :2020/3/30 * Description : * Version :1.0 */ public class GDSeach { public final static int INFINITY = Integer.MAX_VALUE; public static void CC_BFS(IGraph G) throws Exception { boolean[] visited = new boolean[G.getVexNum()]; for (int v = 0; v < G.getVexNum(); v++) visited[v] = false; LinkQueue Q = new LinkQueue(); LinkQueue P = new LinkQueue(); int i = 0; for (int v = 0; v < G.getVexNum(); v++) { P.clear(); if (!visited[v]) { visited[v] = true; P.offer(G.getVex(v)); Q.offer(v); while (!Q.isEmpty()) { int u = (Integer) Q.poll(); for (int w = G.firstAdjVex(u); w >= 0; w = G.nextAdjVex(u, w)) { if (!visited[w]) { visited[w] = true; P.offer(G.getVex(w)); Q.offer(w); } } } System.out.println("图的第" + ++i + "个连通分量为:"); while (!P.isEmpty()) System.out.print(P.poll().toString() + " "); System.out.println(); } } } public static void main(String[] args) throws Exception { Object vexs[] = { "A", "B", "C", "D", "E", "F", "G" }; int[][] arcs = { { 0, 1, INFINITY, 1, INFINITY, INFINITY, INFINITY }, { 1, 0, 1, INFINITY, INFINITY, INFINITY, INFINITY }, { INFINITY, 1, 0, 1, INFINITY, INFINITY, INFINITY }, { 1, INFINITY, 1, 0, INFINITY, INFINITY, INFINITY }, { INFINITY, INFINITY, INFINITY, INFINITY, 0, 1, INFINITY }, { INFINITY, INFINITY, INFINITY, INFINITY, 1, 0, 1 }, { INFINITY, INFINITY, INFINITY, INFINITY, INFINITY, 1, 0 }, }; MGraph G = new MGraph(GraphKind.UDG, 7, 6, vexs, arcs); CC_BFS(G); } }
无向图的连通分量搜索是一种广度优先遍历的应用,当然还有如Dijkstra单源最短路径算法和Prim最小生成树算法都采用了和宽度优先搜索类似的思想。
源码地址:
https://gitee.com/jockhome/data_structure
如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 软件供应链安全是跟可靠性有关还是跟安全性有关?
- ¥15 电脑蓝屏logfilessrtsrttrail问题
- ¥20 关于wordpress建站遇到的问题!(语言-php)(相关搜索:云服务器)
- ¥15 【求职】怎么找到一个周围人素质都很高不会欺负他人,并且未来月薪能够达到一万以上(技术岗)的工作?希望可以收到写有具体,可靠,已经实践过了的路径的回答?
- ¥15 Java+vue部署版本反编译
- ¥100 对反编译和ai熟悉的开发者。
- ¥15 带序列特征的多输出预测模型
- ¥15 Python 如何安装 distutils模块
- ¥15 关于#网络#的问题:网络是从楼上引一根网线下来,接了2台傻瓜交换机,也更换了ip还是不行
- ¥15 资源泄露软件闪退怎么解决?