当前位置: 首页 > news >正文

城建中心官网查询证书网站搜索优化价格

城建中心官网查询证书,网站搜索优化价格,网站备案 密码找回,渭南市住房和城乡建设部网站Java手写最短路径算法和案例拓展 1. 算法手写的必要性 在实际开发中#xff0c;经常需要处理图的最短路径问题。虽然Java提供了一些图算法库#xff0c;但手写最短路径算法的必要性体现在以下几个方面#xff1a; 理解算法原理#xff1a;手写算法可以帮助我们深入理解最…Java手写最短路径算法和案例拓展 1. 算法手写的必要性 在实际开发中经常需要处理图的最短路径问题。虽然Java提供了一些图算法库但手写最短路径算法的必要性体现在以下几个方面 理解算法原理手写算法可以帮助我们深入理解最短路径算法的原理和思路提高对算法的理解程度。灵活性和可定制性手写算法可以根据具体需求进行定制满足不同场景下的需求。性能优化手写算法可以根据具体情况进行性能优化提高算法的效率。 2. 市场调查 在市场调查中我们发现最短路径算法在物流、导航、网络通信等领域有着广泛的应用。例如物流公司需要确定最短路径来优化运输成本导航软件需要找到最短路径来指导用户行驶网络通信需要确定最短路径来提高数据传输效率。 3. 实现思路原理 为了更好地理解最短路径算法的实现思路我们使用Mermanid代码表示思维导图解释实现思路的原理。 #mermaid-svg-BwAVxZLG6g9EgIzz {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-BwAVxZLG6g9EgIzz .error-icon{fill:#552222;}#mermaid-svg-BwAVxZLG6g9EgIzz .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-BwAVxZLG6g9EgIzz .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-BwAVxZLG6g9EgIzz .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-BwAVxZLG6g9EgIzz .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-BwAVxZLG6g9EgIzz .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-BwAVxZLG6g9EgIzz .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-BwAVxZLG6g9EgIzz .marker{fill:#333333;stroke:#333333;}#mermaid-svg-BwAVxZLG6g9EgIzz .marker.cross{stroke:#333333;}#mermaid-svg-BwAVxZLG6g9EgIzz svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-BwAVxZLG6g9EgIzz .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-BwAVxZLG6g9EgIzz .cluster-label text{fill:#333;}#mermaid-svg-BwAVxZLG6g9EgIzz .cluster-label span{color:#333;}#mermaid-svg-BwAVxZLG6g9EgIzz .label text,#mermaid-svg-BwAVxZLG6g9EgIzz span{fill:#333;color:#333;}#mermaid-svg-BwAVxZLG6g9EgIzz .node rect,#mermaid-svg-BwAVxZLG6g9EgIzz .node circle,#mermaid-svg-BwAVxZLG6g9EgIzz .node ellipse,#mermaid-svg-BwAVxZLG6g9EgIzz .node polygon,#mermaid-svg-BwAVxZLG6g9EgIzz .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-BwAVxZLG6g9EgIzz .node .label{text-align:center;}#mermaid-svg-BwAVxZLG6g9EgIzz .node.clickable{cursor:pointer;}#mermaid-svg-BwAVxZLG6g9EgIzz .arrowheadPath{fill:#333333;}#mermaid-svg-BwAVxZLG6g9EgIzz .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-BwAVxZLG6g9EgIzz .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-BwAVxZLG6g9EgIzz .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-BwAVxZLG6g9EgIzz .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-BwAVxZLG6g9EgIzz .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-BwAVxZLG6g9EgIzz .cluster text{fill:#333;}#mermaid-svg-BwAVxZLG6g9EgIzz .cluster span{color:#333;}#mermaid-svg-BwAVxZLG6g9EgIzz div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-BwAVxZLG6g9EgIzz :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 初始化距离和路径 选择起点 更新距离和路径 选择下一个顶点 更新距离和路径 重复选择下一个顶点 输出最短路径 上述思维导图描述了最短路径算法的基本思路从起点开始逐步选择下一个顶点并更新距离和路径直到到达目标顶点输出最短路径。 4. 实现的详细介绍和详细步骤 步骤1初始化距离和路径 在开始算法之前需要初始化距离和路径。距离表示从起点到每个顶点的最短距离路径表示从起点到每个顶点的最短路径。 // 初始化距离和路径 for (int i 0; i vertexCount; i) {distance[i] Integer.MAX_VALUE;path[i] -1; } distance[start] 0;步骤2选择起点 选择起点作为当前顶点并标记为已访问。 int current start; visited[current] true;步骤3更新距离和路径 遍历当前顶点的所有邻接顶点更新距离和路径。 for (int neighbor : getNeighbors(current)) {if (!visited[neighbor]) {int newDistance distance[current] getWeight(current, neighbor);if (newDistance distance[neighbor]) {distance[neighbor] newDistance;path[neighbor] current;}} }步骤4选择下一个顶点 从未访问的顶点中选择距离最小的顶点作为下一个顶点。 int minDistance Integer.MAX_VALUE; for (int i 0; i vertexCount; i) {if (!visited[i] distance[i] minDistance) {minDistance distance[i];current i;} } visited[current] true;步骤5重复选择下一个顶点 重复步骤3和步骤4直到所有顶点都被访问。 步骤6输出最短路径 根据路径数组输出从起点到目标顶点的最短路径。 ListInteger shortestPath new ArrayList(); int vertex target; while (vertex ! -1) {shortestPath.add(vertex);vertex path[vertex]; } Collections.reverse(shortestPath);5. 手写实现总结及必要性 通过手写最短路径算法的实现我们深入理解了算法的原理和思路。手写实现能够提高我们对算法的理解程度并且具有灵活性和可定制性可以根据具体需求进行定制和性能优化。手写最短路径算法在物流、导航、网络通信等领域有着广泛的应用前景。 5.1 手写完整代码 以下是一个使用Dijkstra算法求解最短路径的完整代码示例 import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List;public class DijkstraAlgorithm {private int vertexCount;private int[][] adjacencyMatrix;public DijkstraAlgorithm(int vertexCount) {this.vertexCount vertexCount;adjacencyMatrix new int[vertexCount][vertexCount];}public void addEdge(int source, int destination, int weight) {adjacencyMatrix[source][destination] weight;adjacencyMatrix[destination][source] weight;}public ListInteger shortestPath(int start, int target) {int[] distance new int[vertexCount];int[] path new int[vertexCount];boolean[] visited new boolean[vertexCount];// 初始化距离和路径Arrays.fill(distance, Integer.MAX_VALUE);Arrays.fill(path, -1);distance[start] 0;// 选择起点int current start;visited[current] true;// 更新距离和路径for (int neighbor 0; neighbor vertexCount; neighbor) {if (!visited[neighbor] adjacencyMatrix[current][neighbor] 0) {int newDistance distance[current] adjacencyMatrix[current][neighbor];if (newDistance distance[neighbor]) {distance[neighbor] newDistance;path[neighbor] current;}}}// 选择下一个顶点for (int i 1; i vertexCount; i) {int minDistance Integer.MAX_VALUE;for (int j 0; j vertexCount; j) {if (!visited[j] distance[j] minDistance) {minDistance distance[j];current j;}}visited[current] true;// 更新距离和路径for (int neighbor 0; neighbor vertexCount; neighbor) {if (!visited[neighbor] adjacencyMatrix[current][neighbor] 0) {int newDistance distance[current] adjacencyMatrix[current][neighbor];if (newDistance distance[neighbor]) {distance[neighbor] newDistance;path[neighbor] current;}}}}// 输出最短路径ListInteger shortestPath new ArrayList();int vertex target;while (vertex ! -1) {shortestPath.add(vertex);vertex path[vertex];}Collections.reverse(shortestPath);return shortestPath;}public static void main(String[] args) {DijkstraAlgorithm graph new DijkstraAlgorithm(6);graph.addEdge(0, 1, 2);graph.addEdge(0, 2, 4);graph.addEdge(1, 2, 1);graph.addEdge(1, 3, 7);graph.addEdge(2, 4, 3);graph.addEdge(3, 4, 1);graph.addEdge(3, 5, 5);graph.addEdge(4, 5, 2);ListInteger shortestPath graph.shortestPath(0, 5);System.out.println(Shortest Path: shortestPath);} }以上代码实现了一个Dijkstra算法的最短路径求解器。在示例中我们创建了一个有6个顶点的图并添加了8条边。然后我们使用Dijkstra算法计算从顶点0到顶点5的最短路径并打印出结果。输出结果为Shortest Path: [0, 2, 4, 5]表示从顶点0到顶点5的最短路径为0 - 2 - 4 - 5。 6. 拓展案例 下面是一个拓展案例展示了每个步骤的代码进行文字描述 // 步骤1初始化距离和路径 for (int i 0; i vertexCount; i) {distance[i] Integer.MAX_VALUE;path[i] -1; } distance[start] 0;// 步骤2选择起点 int current start; visited[current] true;// 步骤3更新距离和路径 for (int neighbor : getNeighbors(current)) {if (!visited[neighbor]) {int newDistance distance[current] getWeight(current, neighbor);if (newDistance distance[neighbor]) {distance[neighbor] newDistance;path[neighbor] current;}} }// 步骤4选择下一个顶点 int minDistance Integer.MAX_VALUE; for (int i 0; i vertexCount; i) {if (!visited[i] distance[i] minDistance) {minDistance distance[i];current i;} } visited[current] true;// 步骤5重复选择下一个顶点// 步骤6输出最短路径 ListInteger shortestPath new ArrayList(); int vertex target; while (vertex ! -1) {shortestPath.add(vertex);vertex path[vertex]; } Collections.reverse(shortestPath);通过以上拓展案例我们可以更加清晰地了解每个步骤的代码实现和作用。
http://www.hkea.cn/news/14548296/

相关文章:

  • 大同网站建设哪家好网站建设与管理实践收获
  • 万网网站备案证书仙居制作网站
  • 网站目标规划wordpress 转载 插件
  • 影响网站alexa排名的主要因素有wordpress奇客影院
  • 平面设计类的网站蓝色机械营销型网站
  • 类似织梦的建站cms知名品牌策划设计公司
  • 什么网站做一手项目好网站商城html模板
  • 有域名怎样做网站网站部署 模板
  • 安徽国贸网站建设网站开发是什么专业百度
  • 网站制作过程内容湖南网站建设的公司排名
  • 移动互联网的概念贵阳seo推广一般费用是
  • 连云港建设局电力网站邢台网站推广多少钱
  • 杭州高端定制网站西部数码WordPress开启伪静态
  • 五个h5制作网站服务器购买网站
  • 怎样运营推广网站建立企业网站方案
  • 有什么交易网站源码dj网站模板免费下载
  • 搜索引擎 网站推广 举例免费的网页制作软件
  • 服务器怎么放网站吗上海公司注册流程和费用
  • 室内设计素材网站哪个最好西安网站开发软件
  • 工程行业做的好的网站有哪些内容wordpress分类页不要显示全文
  • 广东省网站集约化建设方案php网站建设思路方案
  • nodejs网站开发教程小程序代理加盟条仿
  • 中国十大购物网站公司名字大全集
  • 化妆网站模板免费企业名录搜索
  • 做网站的的价位研发网站要多久
  • 阿里去可以做几个网站网站qq访客统计
  • 百度收不到我的网站手机网站开发是什么
  • WordPress建站去掉后缀优速网站建设工作室
  • 平台网站开发可行性分析linux下网站搭建
  • 网站建设域名的购买购物网站html模板下载