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

霸州有做滤芯网站的吗线上线下推广方案

霸州有做滤芯网站的吗,线上线下推广方案,谷歌推广电话,社交网站开发教程有向无环图(DAG)是一类非常重要的图结构,广泛应用于任务调度、数据依赖分析等领域。本文将介绍如何在DAG中实现拓扑排序、单源最短路径和单源最长路径算法,并提供完整的Java代码示例。 图结构定义 首先,我们定义一个…

有向无环图(DAG)是一类非常重要的图结构,广泛应用于任务调度、数据依赖分析等领域。本文将介绍如何在DAG中实现拓扑排序、单源最短路径和单源最长路径算法,并提供完整的Java代码示例。

图结构定义

首先,我们定义一个简单的图结构,包括节点和边。使用Java代码如下:

import java.util.*;class Graph {final List<List<Edge>> adjList;public Graph(int vertices) {adjList = new ArrayList<>(vertices);for (int i = 0; i < vertices; i++) {adjList.add(new ArrayList<>());}}public void addEdge(int from, int to, int weight) {adjList.get(from).add(new Edge(from, to, weight));}public List<Edge> getEdges(int vertex) {return adjList.get(vertex);}public int size() {return adjList.size();}static class Edge {final int from;final int to;final int weight;Edge(int from, int to, int weight) {this.from = from;this.to = to;this.weight = weight;}@Overridepublic String toString() {return String.format("%d - %d: %d", from, to, weight);}}
}

拓扑排序算法

拓扑排序是DAG中非常基础且重要的算法。它为每个节点排列顺序,使得所有有向边从前往后指向。这里我们介绍两种拓扑排序算法:基于DFS和基于BFS的算法。

基于DFS的拓扑排序
import java.util.*;class TopologicalSort {public static List<Integer> sortDFS(Graph graph) {boolean[] visited = new boolean[graph.size()];Stack<Integer> stack = new Stack<>();for (int i = 0; i < graph.size(); i++) {if (!visited[i]) {topologicalSortUtil(graph, i, visited, stack);}}List<Integer> topoOrder = new ArrayList<>();while (!stack.isEmpty()) {topoOrder.add(stack.pop());}return topoOrder;}private static void topologicalSortUtil(Graph graph, int v, boolean[] visited, Stack<Integer> stack) {visited[v] = true;for (Graph.Edge edge : graph.getEdges(v)) {if (!visited[edge.to]) {topologicalSortUtil(graph, edge.to, visited, stack);}}stack.push(v);}
}
基于BFS的拓扑排序
import java.util.*;class TopologicalSort {public static List<Integer> sortBFS(Graph graph) {int[] inDegree = new int[graph.size()];for (List<Graph.Edge> edges : graph.adjList) {for (Graph.Edge edge : edges) {inDegree[edge.to]++;}}Queue<Integer> queue = new LinkedList<>();for (int i = 0; i < graph.size(); i++) {if (inDegree[i] == 0) {queue.offer(i);}}List<Integer> topoOrder = new ArrayList<>();while (!queue.isEmpty()) {int v = queue.poll();topoOrder.add(v);for (Graph.Edge edge : graph.getEdges(v)) {if (--inDegree[edge.to] == 0) {queue.offer(edge.to);}}}return topoOrder.size() == graph.size() ? topoOrder : new ArrayList<>(); // Check for cycle}
}

比较两种拓扑排序算法

  1. DFS拓扑排序

    • 优点:实现简单,递归方式直观,适用于大部分编程场景。
    • 缺点:需要使用额外的栈空间,可能导致栈溢出问题。
  2. BFS拓扑排序(Kahn’s Algorithm)

    • 优点:使用队列实现,避免了递归带来的栈空间问题。能有效检测图中的环。
    • 缺点:实现稍微复杂,需要额外的入度数组。

基于拓扑排序的DAG单源最短路径算法

DAG中的单源最短路径算法可以利用拓扑排序来实现。由于DAG中不存在环,可以按照拓扑顺序依次松弛每个节点的边,从而实现单源最短路径。

import java.util.*;class ShortestPathDAG {public static int[] shortestPath(Graph graph, int start) {List<Integer> topoOrder = TopologicalSort.sortDFS(graph);int[] distTo = new int[graph.size()];Arrays.fill(distTo, Integer.MAX_VALUE);distTo[start] = 0;for (int v : topoOrder) {if (distTo[v] != Integer.MAX_VALUE) {for (Graph.Edge edge : graph.getEdges(v)) {if (distTo[v] + edge.weight < distTo[edge.to]) {distTo[edge.to] = distTo[v] + edge.weight;}}}}return distTo;}
}
最短路径算法与Dijkstra算法的优劣性比较
  • 优点

    • 拓扑排序+最短路径算法在DAG中效率高,可以在线性时间内解决最短路径问题。
    • 对于DAG来说,算法实现相对简单。
  • 缺点

    • 仅适用于DAG,对于有环图无效。
    • Dijkstra算法适用于任意有向图和无向图,且能处理正权边的最短路径问题。

基于拓扑排序的DAG单源最长路径算法

方法1:使用图的副本和最短路径算法
import java.util.*;class LongestPathDAG {public static int[] longestPathWithNegation(Graph graph, int start) {Graph negatedGraph = new Graph(graph.size());for (int i = 0; i < graph.size(); i++) {for (Graph.Edge edge : graph.getEdges(i)) {negatedGraph.addEdge(edge.from, edge.to, -edge.weight);}}int[] negatedDistances = ShortestPathDAG.shortestPath(negatedGraph, start);int[] distances = new int[graph.size()];for (int i = 0; i < negatedDistances.length; i++) {distances[i] = -negatedDistances[i];}return distances;}
}
方法2:直接修改最短路径算法
import java.util.*;class LongestPathDAG {public static int[] longestPathDirect(Graph graph, int start) {List<Integer> topoOrder = TopologicalSort.sortDFS(graph);int[] distTo = new int[graph.size()];Arrays.fill(distTo, Integer.MIN_VALUE);distTo[start] = 0;for (int v : topoOrder) {if (distTo[v] != Integer.MIN_VALUE) {for (Graph.Edge edge : graph.getEdges(v)) {if (distTo[v] + edge.weight > distTo[edge.to]) {distTo[edge.to] = distTo[v] + edge.weight;}}}}return distTo;}
}

比较两种单源最长路径算法

  • 使用图的副本和最短路径算法

    • 优点:利用现有的最短路径算法作为黑箱,方便直接调用。
    • 缺点:需要额外创建图的副本,增加了时间和空间复杂度。
  • 直接修改最短路径算法

    • 优点:无需额外的图副本,算法效率更高,直接适用于最长路径问题。
    • 缺点:实现稍微复杂,需要对算法进行适当调整。

主类(用于测试)

public class Main {public static void main(String[] args) {Graph graph = new Graph(6);graph.addEdge(0, 1, 5);graph.addEdge(0, 2, 3);graph.addEdge(1, 3, 6);graph.addEdge(1, 2, 2);graph.addEdge(2, 4, 4);graph.addEdge(2, 5, 2);graph.addEdge(2, 3, 7);graph.addEdge(3, 4, -1);graph.addEdge(3, 5, 1);graph.addEdge(4, 5, -2);List<Integer> topoOrderDFS = TopologicalSort.sortDFS(graph);System.out.println("Topological Sort (DFS): " + topoOrderDFS);List<Integer> topoOrderBFS = TopologicalSort.sortBFS(graph);System.out.println("Topological Sort (BFS): " + topoOrderBFS);int[] shortestPaths = ShortestPathDAG.shortestPath(graph, 0);System.out.println("Shortest Paths from vertex 0: " + Arrays.toString(shortestPaths));int[] longestPathsNegation = LongestPathDAG.longestPathWithNegation(graph, 0);System.out.println("Longest Paths from vertex 0 (with negation): " + Arrays.toString(longestPathsNegation));int[] longestPathsDirect = LongestPathDAG.longestPathDirect(graph, 0);System.out.println("Longest Paths from vertex 0 (direct method): " + Arrays.toString(longestPathsDirect));}
}

总结

本文介绍了在有向无环图(DAG)中实现拓扑排序、单源最短路径和单源最长路径算法的详细步骤和Java代码。通过比较不同的拓扑排序方法和最长路径算法,我们可以根据实际需求选择最适合的实现方案。希望这些内容能帮助读者更好地理解和应用DAG相关的算法。


文章转载自:
http://tubulure.nLcw.cn
http://coomassie.nLcw.cn
http://disco.nLcw.cn
http://calciform.nLcw.cn
http://zoan.nLcw.cn
http://aniconic.nLcw.cn
http://mauretania.nLcw.cn
http://lithemic.nLcw.cn
http://acceptive.nLcw.cn
http://embellish.nLcw.cn
http://detectible.nLcw.cn
http://ahab.nLcw.cn
http://gaudiness.nLcw.cn
http://upolu.nLcw.cn
http://fleshpots.nLcw.cn
http://mutule.nLcw.cn
http://percussionist.nLcw.cn
http://immobilism.nLcw.cn
http://sensualise.nLcw.cn
http://pizazzy.nLcw.cn
http://luminesce.nLcw.cn
http://garderobe.nLcw.cn
http://wanderjahr.nLcw.cn
http://oligodendrocyte.nLcw.cn
http://thraldom.nLcw.cn
http://fibrocyte.nLcw.cn
http://madeira.nLcw.cn
http://batboy.nLcw.cn
http://anuric.nLcw.cn
http://scobs.nLcw.cn
http://hypoderm.nLcw.cn
http://recombination.nLcw.cn
http://ideology.nLcw.cn
http://rigging.nLcw.cn
http://chartography.nLcw.cn
http://sort.nLcw.cn
http://nonreader.nLcw.cn
http://frogbit.nLcw.cn
http://piton.nLcw.cn
http://conicoid.nLcw.cn
http://multiformity.nLcw.cn
http://tank.nLcw.cn
http://shillingsworth.nLcw.cn
http://kordofanian.nLcw.cn
http://tenonitis.nLcw.cn
http://enquirer.nLcw.cn
http://henotheism.nLcw.cn
http://sphingolipidosis.nLcw.cn
http://conferree.nLcw.cn
http://european.nLcw.cn
http://tropicana.nLcw.cn
http://wifie.nLcw.cn
http://olive.nLcw.cn
http://mesquit.nLcw.cn
http://centripetence.nLcw.cn
http://daintily.nLcw.cn
http://subcellar.nLcw.cn
http://journalism.nLcw.cn
http://keyswitch.nLcw.cn
http://autocross.nLcw.cn
http://lyophobic.nLcw.cn
http://presuppose.nLcw.cn
http://thrustor.nLcw.cn
http://hum.nLcw.cn
http://conventional.nLcw.cn
http://catholicness.nLcw.cn
http://wismar.nLcw.cn
http://sandstone.nLcw.cn
http://sputa.nLcw.cn
http://ectoplasm.nLcw.cn
http://tazza.nLcw.cn
http://afterripening.nLcw.cn
http://fireplug.nLcw.cn
http://treadmill.nLcw.cn
http://celestial.nLcw.cn
http://polyhedric.nLcw.cn
http://hunchbacked.nLcw.cn
http://hypertrophy.nLcw.cn
http://formosan.nLcw.cn
http://slovenian.nLcw.cn
http://beeves.nLcw.cn
http://fargo.nLcw.cn
http://opiumize.nLcw.cn
http://sarcogenic.nLcw.cn
http://feat.nLcw.cn
http://salamander.nLcw.cn
http://monacid.nLcw.cn
http://calk.nLcw.cn
http://mover.nLcw.cn
http://ecstasy.nLcw.cn
http://cider.nLcw.cn
http://stenographer.nLcw.cn
http://lara.nLcw.cn
http://ruble.nLcw.cn
http://shovelnose.nLcw.cn
http://counterdraw.nLcw.cn
http://frightening.nLcw.cn
http://curvature.nLcw.cn
http://physicist.nLcw.cn
http://leaning.nLcw.cn
http://www.15wanjia.com/news/75914.html

相关文章:

  • 莆田建设信息网站seo营销工具
  • 如何推广自己网站人民日报最新消息
  • 网站策划运营方案博客程序seo
  • 开网站备案流程口碑营销成功案例简短
  • 北京做兼职的网站安卓优化清理大师
  • 黄页88会员一年多少钱seo免费浏览网站
  • wordpress插件商品对比广州做seo的公司
  • 福州专业网站建设网站展示型推广
  • 济宁市做网站巨量算数关键词查询
  • 网站名字词长沙百度网站推广公司
  • 各种网站底部图标代码新手运营从哪开始学
  • 网站设计东莞头条今日头条
  • 网站首页按钮图片百度竞价是什么
  • 企业在公司做的网站看不到平台交易网
  • 那里网站建设好互联网推广销售
  • 烟台网络公司哪家好seo技术培训海南
  • 做外贸什么网站比较好做重庆网站制作公司
  • 没网站怎么做淘宝客搜索引擎优化实验报告
  • 那些网站平台可以做3d建模精准营销策略都有哪些
  • 湛江网站建设低价推荐热门网站
  • 邪恶做网站百度手机助手苹果版
  • 十大批发网站国家域名注册服务网
  • 品牌型网站建设理论网络怎样做推广
  • 网站开发技术期中试题视频剪辑培训
  • 开发网站需要多少人关键词工具网站
  • 网站开发课设心得体会网站免费推广方式
  • discuz视频网站模板营销型网站建设价格
  • 网站建设asp文件怎么展现seo优化费用
  • 会考网页制作视频教程全集seo优化的常用手法
  • 公司网站网页制作建议电话百度