Java-11

article/2025/8/23 23:45:48

学习来源:日撸 Java 三百行(31-40天,图)_闵帆的博客-CSDN博客

36 邻接表

36.1 相当于图的压缩存储. 每一行数据用一个单链表存储。

36.2 重写了广度优先遍历. 可以发现, 使用队列的机制不变. 仅仅是把其中的 for 循环换成了 while, 避免检查不存在的边. 如果图很稀疏的话, 可以降低时间复杂度。

36.3 深度优先遍历可以当作作业, 这里不写了。

代码:

package 日撸Java300行_31_40;/*** Adjacency list for directed graph.* @time 2022/4/20* @author Hui Xiao*/public class AdjacencyList {/*** An inner class for adjacent node.*/class AdjacencyNode {/*** The column index.*/int column;/*** The next adjacent node.*/AdjacencyNode next;/********************** The first constructor.* * @param paraColumn* 			The column.********************/public AdjacencyNode(int paraColumn) {column = paraColumn;next = null;} // Of AdjacencyNode} // Of class AdjacencyNode/*** The number of nodes. This member variable may be redundant since it is* always equal to headers.length.*/int numNodes;/*** The headers for each row.*/AdjacencyNode[] headers;/********************** The first constructor.* * @param paraMatrix* 			The matrix indicating the graph.********************/public AdjacencyList(int[][] paraMatrix) {numNodes = paraMatrix.length;// Step 1. Initialize. The data in the headers are not meaningful.AdjacencyNode tempPreviousNode, tempNode;headers = new AdjacencyNode[numNodes];for (int i = 0; i < numNodes; i++) {headers[i] = new AdjacencyNode(-1);tempPreviousNode = headers[i];for (int j = 0; j < numNodes; j++) {if (paraMatrix[i][j] == 0) {continue;} // Of if// Create a new node.tempNode = new AdjacencyNode(j);//LinktempPreviousNode.next = tempNode;tempPreviousNode = tempNode;} // Of for j} // Of for i} // Of class AdjacenTable/********************** Override the method claimed in Object, the superclass of any class.********************/public String toString() {String resultString = "";AdjacencyNode tempNode;for (int i = 0; i < numNodes; i++) {tempNode = headers[i].next;while (tempNode != null) {resultString += " (" + i + ", " + tempNode.column + ")";tempNode = tempNode.next;} // Of whileresultString += "\r\n";} // Of for ireturn resultString;} // Of toString/********************** Breadth first traversal.* * @param paraStartIndex* 			The start index.* @return The sequeue of the visit.********************/public String breadthFirstTraversal(int paraStartIndex) {CircleObjectQueue tempQueue = new CircleObjectQueue();String resultString = "";boolean[] tempVisitedArray = new boolean[numNodes];tempVisitedArray[paraStartIndex] = true;// Initialize the queque.// Visit before enqueue.tempVisitedArray[paraStartIndex] = true;resultString += paraStartIndex;tempQueue.enqueue(new Integer(paraStartIndex));// Now visit the rest of the graph.int tempIndex;Integer tempInteger = (Integer)tempQueue.dequeue();AdjacencyNode tempNode;while (tempInteger != null) {tempIndex = tempInteger.intValue();// Enqueue all its unvisited neighbors. The neighbor are linked// already.tempNode = headers[tempIndex].next;while (tempNode != null) {if (!tempVisitedArray[tempNode.column]) {// Viisit before enqueue.tempVisitedArray[tempNode.column] = true;resultString += tempNode.column;tempQueue.enqueue(new Integer(tempNode.column));} // Of iftempNode = tempNode.next;} // Of for i//Take out one from the head.tempInteger = (Integer) tempQueue.dequeue();} // Of whilereturn resultString;} // Of breadthFirstTraversal/********************** Unit test for breadthFirstTraversal. The same as the one in class Graph.********************/public static void breadthFirstTraversalTest() {// Test an undirected graph.int[][] tempMatrix = { { 0, 1, 1, 0 }, { 1, 0, 0, 1 }, { 1, 0, 0, 1}, { 0, 1, 1, 0 } };Graph tempGraph = new Graph(tempMatrix);System.out.println(tempGraph);String tempSequence = "";try {tempSequence = tempGraph.breadthFirstTraversal(2);} catch (Exception ee) {System.out.println(ee);} // Of try.System.out.println("The breadth first order of visit " + tempSequence);} // Of breadthFirstTraversalTest/************************ The entrance of the program.* * @param args*            Not used now.**********************/public static void main(String[] args) {int[][] tempMatrix = { { 0, 1, 0 }, { 1, 0, 1 }, { 0, 1, 0} };AdjacencyList tempTable = new AdjacencyList(tempMatrix);System.out.println("The data are: \r\n" + tempTable);breadthFirstTraversalTest();}// Of main} // Of class AdjacencyList

截图:

 

37 十字链表

37.1 比邻接表难一点点, 毕竟多一个指针域。

37.2 为控制代码量, 只做了建表和 toString。

代码:

package 日撸Java300行_31_40;/*** Orthogonal List for directed graph.* @time 2022/4/20* @author Hui Xiao*/public class OrthogonalList {/*** An inner class for adjacent node.*/class OrthogonalNode {/*** The row index.*/int row;/*** The column index.*/int column;/*** The next out node.*/OrthogonalNode nextOut;/*** The next in node.*/OrthogonalNode nextIn;/********************** The first constructor.* * @param paraRow    The row.* @param paraColumn The column.********************/public OrthogonalNode(int paraRow, int paraColumn) {row = paraRow;column = paraColumn;nextOut = null;nextIn = null;}  // Of OrthogonalNode}// Of class OrthogonalNode/*** The number of nodes. This member variable may be redundant since it is always* equal to header.length.*/int numNodes;/*** The header for each row.*/OrthogonalNode[] headers;/********************** The first constructor.* * @param paraMatrix The matrix indicating the graph.********************/public OrthogonalList(int[][] paraMatrix) {numNodes = paraMatrix.length;// Step 1. Initialize. The data in the headers are not meaningful.OrthogonalNode tempPreviousNodes,tempNode;headers = new OrthogonalNode[numNodes];// Step 2. Link to its out nodes.for (int i = 0; i < numNodes; i++) {headers[i] = new OrthogonalNode(i, -1);tempPreviousNodes = headers[i];for (int j = 0; j < numNodes; j++) {if (paraMatrix[i][j] == 0) {continue;} // Of if// Create a new node.tempNode = new OrthogonalNode(i, j);// Link.tempPreviousNodes.nextOut = tempNode;tempPreviousNodes = tempNode;} // Of for j} // Of for i// Step 3. Link to its in nodes. This step is harder.OrthogonalNode[] tempColumnNodes = new OrthogonalNode[numNodes];for (int i = 0; i < numNodes; i++) {tempColumnNodes[i] = headers[i];} // Of for ifor (int i = 0; i < numNodes; i++) {tempNode = headers[i].nextOut;while (tempNode != null) {tempColumnNodes[tempNode.column].nextIn = tempNode;tempColumnNodes[tempNode.column] = tempNode;tempNode = tempNode.nextOut;} // Of while} // Of for i} // Of the constructor/********************** Overrides the method claimed in Object, the superclass of any class.********************/public String toString() {String resultString = "Out arcs: ";OrthogonalNode tempNode;for (int i = 0; i < numNodes; i++) {tempNode = headers[i].nextOut;while (tempNode != null) {resultString += " (" + tempNode.row + ", " + tempNode.column + ")";tempNode = tempNode.nextOut;} // Of whileresultString += "\r\n";} // Of for iresultString += "\r\nIn arcs:";for (int i = 0; i < numNodes; i++) {tempNode = headers[i].nextIn;while (tempNode !=null) {resultString += " (" + tempNode.row + ", " + tempNode.column + ")";tempNode = tempNode.nextIn;} // Of whileresultString += "\r\n";} // Of for ireturn resultString;} // Of toString/********************** The entrance of the program.* * @param args Not used now.********************/public static void main(String[] args) {int[][] tempMatrix = { { 0, 1, 0, 0 }, { 0, 0, 0, 1 }, { 1, 0, 0, 0}, { 0, 1, 1, 0 } };OrthogonalList tempList = new OrthogonalList(tempMatrix);System.out.println("The data ars: \r\n" + tempList);} // Of main
} // Of class OrthogonalList

截图:

 

38 Dijkstra 算法与 Prim 算法

38.1 Dijkstra 算法需要有向图, Prim 算法需要无向图. 代码中也需要更换后者。

38.2 两个算法的结构相同. 不同之处是比较距离的时候, 是用累计的 (Dijkstra) 还是当前边的 (Prim). 建议先写 Dijkstra, 然后拷贝、修改变成 Prim. 到这个阶段, 应该已经具备这样的能力。

代码:

package 日撸Java300行_31_40;import java.util.Arrays;/*** Weighted graph are called nets.* @time 2022/4/6* @author Hui Xiao*/public class Net {/*** The maximal distance. Do not use Integer.MAX_VALUE.*/public static final int MAX_DISTANCE = 10000;/*** The number of nodes.*/int numNodes;/*** The weight matrix. We use int to represent weight for simplisity.*/IntMatrix weightMatrix;/********************** The first constructor.* * @param paraNumNodes* 			The number of nodes in the graph.********************/public Net(int paraNumNodes) {numNodes = paraNumNodes;weightMatrix = new IntMatrix(numNodes,numNodes);for (int i = 0; i < numNodes; i++) {// For better readability, you may need to write fill() in class// IntMatrix.Arrays.fill(weightMatrix.getData()[i], MAX_DISTANCE);} // Of for i} // Of the first constructor/********************** The second constructor.* * @param paraMatrix* 			The data matrix.********************/public Net(int[][] paraMatrix) {weightMatrix = new IntMatrix(paraMatrix);numNodes = weightMatrix.getRows();} // Of the second constructor/************************ Overrides the method claimed in Object, the superclass of any class.**********************/public String toString() {String resultString = "This is the weight matrix of the graph.\r\n" + weightMatrix;return resultString;} // Of toString/********************** The Dijkstra algorithm: shortest path from source to all nodes.* * @param paraSource* 			The source node.* @return The distances to all nodes.********************/public int[] dijkstra(int paraSource) {// Step 1. Initialize.int[] tempDistanceArray = new int[numNodes];for (int i = 0; i < numNodes; i++) {tempDistanceArray[i] = weightMatrix.getValue(paraSource, i);} // Of for iint[] tempParentArray = new int[numNodes];Arrays.fill(tempParentArray, paraSource);// -1 for no parent.tempParentArray[paraSource] = -1;// Visited nodes will not be considered further.boolean[] tempVisitedArray = new boolean[numNodes];tempVisitedArray[paraSource] = true;// Step 2. Main loops.int tempMinDistance;int tempBestNode = -1;for (int i = 0; i < numNodes; i++) {// Step 2.1 Find out the best next node.tempMinDistance = Integer.MAX_VALUE;for (int j = 0; j < numNodes; j++) {// This node is visited.if (tempVisitedArray[j]) {continue;} // Of ifif (tempMinDistance > tempDistanceArray[j]) {tempMinDistance = tempDistanceArray[j];tempBestNode = j;} // Of if} // Of for jtempVisitedArray[tempBestNode] = true;// Step 2.2 Prepare for the next round.for (int j = 0; j < numNodes; j++) {//This node is visited.if (tempVisitedArray[j]) {continue;} // Of ifif (tempMinDistance > tempDistanceArray[j]) {tempMinDistance = tempDistanceArray[j];tempBestNode = j;} // Of if} // Of for jtempVisitedArray[tempBestNode] = true;// Step 2.2 Prepare for the next round.for (int j = 0; j < numNodes; j++) {// This node is visited.if (tempVisitedArray[j]) {continue;} // Of if// This node cannot be reached.if (weightMatrix.getValue(tempBestNode, j) >= MAX_DISTANCE) {continue;					} // Of ifif (tempDistanceArray[j] > tempDistanceArray[tempBestNode] + weightMatrix.getValue(tempBestNode, j)) {// Change the distance.tempDistanceArray[j] = tempDistanceArray[tempBestNode]+ weightMatrix.getValue(tempBestNode, j);// Change the parent.tempParentArray[j] = tempBestNode;} // Of if} // Of for j// For testSystem.out.println("The distance to each node: " + Arrays.toString(tempDistanceArray));System.out.println("The parent of each node: " + Arrays.toString(tempParentArray));} // Of for j// Step 3. Output for debug.System.out.println("Finally");System.out.println("The distance to each node: " + Arrays.toString(tempDistanceArray));System.out.println("The parent of each node: " + Arrays.toString(tempParentArray));return tempDistanceArray;} // Of dijksta/********************** The minial spanning tree.* * @return The total cost of the tree.********************/public int prim() {// Step 1. Initialize.// Any node can be the source.int tempSource = 0;int[] tempDistanceArray = new int[numNodes];for (int i = 0; i < numNodes; i++) {tempDistanceArray[i] = weightMatrix.getValue(tempSource, i);} // Of for iint[] tempParentArray = new int[numNodes];Arrays.fill(tempParentArray, tempSource);// -1 for no parent.tempParentArray[tempSource] = -1;//  Visited nodes will not be considered further.boolean[] tempVisitedArray = new boolean[numNodes];tempVisitedArray[tempSource] =true;// Step 2. Main loops.int tempMinDistance;int tempBestNode = -1;for (int i = 0; i < numNodes; i++) {// Step 2.1 Find out the best next node.tempMinDistance = Integer.MAX_VALUE;for (int j = 0; j < numNodes; j++) {// This node is visited.if (tempVisitedArray[j]) {continue;} // Of ifif (tempMinDistance > tempDistanceArray[j]) {tempMinDistance = tempDistanceArray[j];tempBestNode = j;} // Of if} // Of for jtempVisitedArray[tempBestNode] = true;// Step 2.2 Prepare for the next round.for (int j = 0; j < numNodes; j++) {// This node is visited.if (tempVisitedArray[j]) {continue;} // Of if// This node cannot be reached.if (weightMatrix.getValue(tempBestNode, j) >= MAX_DISTANCE) {continue;} // Of if//Attention the difference from the Dijkstra algorithm.if (tempDistanceArray[j] > weightMatrix.getValue(tempBestNode, j)) {// Change the distance.tempDistanceArray[j] = weightMatrix.getValue(tempBestNode, j);// Change the parent.tempParentArray[j] = tempBestNode;} // Of if} // Of for j// For testSystem.out.println("The selected disatnce to each node: " + Arrays.toString(tempDistanceArray));System.out.println("The parent of each node: " + Arrays.toString(tempParentArray));} // Of for iint resultCost = 0;for (int i = 0; i < numNodes; i++) {resultCost += tempDistanceArray[i];}// Step 3. Output for debug.System.out.println("Finally");System.out.println("The distance to each node: " + Arrays.toString(tempDistanceArray));System.out.println("The total cost: " + resultCost);return resultCost;} // Of prim/************************ The entrance of the program.* * @param args*            Not used now.**********************/public static void main(String[] args) {Net tempNet0 = new Net(3);System.out.println(tempNet0);int[][] tempMatrix1 = { { 0, 9, 3, 6 }, { 5, 0, 2, 4 }, { 3, 2, 0, 1}, { 2, 8, 7, 0 } };Net tempNet1 = new Net(tempMatrix1);System.out.println(tempNet1);// DijkstratempNet1.dijkstra(1);// An undirected net is requiredint[][] tempMatrix2 = { { 0, 7, MAX_DISTANCE, 5, MAX_DISTANCE }, { 7, 0, 8, 9, 7 },{ MAX_DISTANCE, 8, 0, MAX_DISTANCE, 5 }, { 5, 9, MAX_DISTANCE, 0, 15 },{ MAX_DISTANCE, 7, 5, 15, 0 } };Net tempNet2 = new Net(tempMatrix2);tempNet2.prim();} // Of main} // Of class Net

截图:

 

 


http://chatgpt.dhexx.cn/article/B7h87O0x.shtml

相关文章

JAVA101-135

JAVA101-150 字符串StringBuilder链式编程简化代码对应的关系可以使用查表法&#xff0c;通过数组的对应的下表来改变成相应的值 修改字符串字符串变整数重点&#xff1a;字符串变为数组 ArrayList集合的基本使用集合一开始的长度为0&#xff0c;如果用循环&#xff0c;进不去 …

Java-1214

Spring5总体学习内容 Spring基本概念IOC容器AopJdbcTemplate事务管理Spring5新特性 框架概述 Spring是轻量级的开源的JavaEE框架Spring可以解决企业应用开发的复杂性Spring有两个核心部分&#xff1a;IOC、Aop IOC&#xff1a;控制反转&#xff0c;把创建对象的过程交给Spri…

下载Google Play外国区APP技巧

安卓用户若遇到喜欢的APP是外国区的&#xff0c;只要翻墙就能下载。比起果粉还要注册&#xff0c;是简便很多。但有没有更简单的办法&#xff1f;这个必须有&#xff01;笔者前几天在网上闲逛时&#xff0c;就发现了一个给力的网站。让你不用翻墙&#xff0c;只需3个步骤&#…

Google Play国内应用市场发布版本步骤指导

应用发布步骤指导 前言Google Play华为小米Vivooppo 博客创建时间&#xff1a;2022.08.19 博客更新时间&#xff1a;2022.08.22 以Android studio build7.0.0&#xff0c;SDKVersion 31来分析讲解。如图文和网上其他资料不一致&#xff0c;可能是别的资料版本较低而已。 前言 …

Google Play App Signing的问题以及解决方式

Google Play App Signing是Google Play 的应用签名&#xff0c;在Google Play上创建项目的时候如果勾选了它&#xff0c;那么它就会生成一个签名文件&#xff0c;不管你上传到Google Play的apk是否用你的签名文件打包&#xff0c;最终都会被替换成Google Play App Signing里的签…

如何将Flutter开发的Android app 发布Google Play(谷歌应用商店)流程

将Flutter Android app 发布Google Play&#xff08;谷歌应用商店&#xff09;流程 一、首先就是要做到科学&#xff01; 二、打开google play官网&#xff0c;注册谷歌账号 三、打开谷歌开发者站点https://play.google.com/apps/publish/signup/创建你的App应用 四、创建完…

h5/uni-app打开手机app,没有则跳转到商店下载

需求&#xff1a;在做商品分享/直播分享时&#xff0c;app内分享出去的链接&#xff0c;能够在微信、手机浏览器打开。 遇到的问题&#xff1a; 1&#xff0c;Android&#xff0c;当手机没有下载app时&#xff0c;在浏览器打开&#xff0c;会下载app&#xff0c;但是手机下载了…

最新版Google Pay上传App指南

现在2022年&#xff0c;是时候来个最新版的操作指南 创建应用 使用谷歌市场开发者账号登录 开发者平台。成功登录后&#xff0c;单击 创建应用。 填写应用的 应用名称。选择应用的 默认语言。在应用或游戏处&#xff0c;选择 应用。根据个人情况选择免费或付费。 勾选 开发者…

网页下载Google Play 的App

网页下载Google Play 的App 文章目录[点击展开](?)[] 前言 当你想在google play上下载某个应用&#xff0c;而无奈手机的系统并没有安装google servicess&#xff0c;此刻是否有些捉急&#xff1f; 本文分享的是一个网站&#xff0c;它可以无需手机而直接通过网页下载Google P…

【Google Play】App Bundle 使用详解 ( 简介 | 应用内更新 | 即时更新 | 灵活更新 )

Google Play 上架完整流程 系列文章目录 【Google Play】创建 Google 开发者账号 ( 注册邮箱账号 | 创建开发者账号 ) 【Google Play】创建并设置应用 ( 访问权限 | 内容分级 | 受众群体 | 类别及联系方式 | 商品详情 ) 【Google Play】App Bundle 使用详解 ( 简介 | 应用内更…

Google Play上架App设置隐私政策声明问题

APP上架Google Play一定要设置隐私政策声明,否则是不给上架的 隐私政策解决方法,生成隐私内容&#xff1a; 点击网址进入 App Privacy Policy Generator 之后根据app的名称&#xff0c;类型&#xff0c;平台&#xff0c;选择对应的选项&#xff0c; 包含对应的第三方隐私服务…

WhatsApp的下载与更新

这两天登录手机&#xff08;安卓&#xff09;的WhatsApp&#xff0c;一直显示我的WhatsApp即将几天后更新&#xff0c;请及时更新到最新的版本&#xff0c;尝试了网上的多种方法&#xff0c;还是没有成功&#xff0c;当然不排除我笨的因素&#xff0c;后来我的小脑瓜子那么一转…

ubuntu安装google app engine环境

需要goog app engine的运行环境&#xff0c;结果翻找半天找不到怎么安装&#xff0c;做记录&#xff1a; 下载app engine &#xff0c; 地址如下&#xff1a; https://cloud.google.com/appengine/downloads?hlzh-TW 到这个网页&#xff0c;找不到下载地址&#xff0c;但却…

google play app下载方法测试

部分参考&#xff1a;http://www.zhihu.com/question/20232626 因为需求&#xff0c;需要从Google play上下载一个APP&#xff1a;Ticketmaster 寻找了一些方法&#xff1a; 基本要求&#xff1a;需要翻墙。 方法1&#xff1a;http://apk-dl.com/ &#xff08;不用翻墙&…

在电脑端下载google play上的app,将其下载成apk

想要下载googleplay上的app&#xff0c;但是没有直接的下载链接。这里推荐一个chrome浏览器上的插件&#xff1a;APK Downloader。 插件安装完成后&#xff0c;在google play搜索到需要下载的app后&#xff0c;将其网页URL复制到插件上生成下载链接即可。

直接下载Google Play上APP的安装包

1、先在GooglePlay上找到自己需要下载的Package Name或者软件的地址链接. 下图是APP提取网站的示意图&#xff1a; 2、打开Online APK Downloader(点击进入)&#xff0c;在输入框中粘贴刚才复制的Package Name或地址。点击“Generate DownIoad Link”。如果输入地址提示错误&a…

Google市场,APP版本更新实现方式

一、直接跳转google play应用详情 直接跳转到google play应用详情内由用户手动触发版本更新。 实现方式包括两种&#xff1a;跳转到google play app应用详情内和跳转到google play网页版应用详情内。 一般实现原则是用户如果安装了google play app跳转到app&#xff0c;未安…

如何从google play 网页下载app到本地

前提&#xff1a; 浏览器可以翻墙 操作 首先&#xff0c;通过电脑打开google play的官方页面&#xff08;https://play.google.com/store/apps&#xff09;&#xff0c;并找到你希望下载的应用&#xff0c;本文以微信为例子&#xff1b;打开微信的安装页面&#xff0c;找到浏…

APP下载页源码-带后台

简介&#xff1a; 带后台 带下载统计,带数据分析图,可一键编辑APP信息 此为1.0版本,后期会推出更多的新功能,尽请期待 注意:这个不是PHP开发的,用ASP.NET开发的,需要部署到window服务器,部署有点麻烦,选择win2019server版本,先在服务器上手动安装iis,不要用宝塔一键安装,宝塔安…