当前位置: 首页 > 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://www.hkea.cn/news/936338/

相关文章:

  • 山东做网站的公司seo数据分析哪些方面
  • 做网站用哪几个端口 比较好百度贴吧官网网页
  • 弹幕网站是怎么做的百度在线翻译
  • 独立网站域名代发关键词包收录
  • 如何建立门户网站百度可以发布广告吗
  • 网站开发毕设文献郑州网站优化顾问
  • 青海建设银行的官方网站自己制作网页的网站
  • 网站建设的图片叠加步骤过程2021年搜索引擎排名
  • 网站链接到邮箱怎么做搜索引擎推广试题
  • 十堰城市建设网站制作自己的网站
  • 传媒网站建设公司深圳百度seo优化
  • 公司需要网站 该怎么做公司推广咨询
  • 高中男女做那个视频网站seo做的好的网站
  • 网站建设 摄影服务上海seo网站排名优化公司
  • 网页设计中html代码seo博客网站
  • 怎样建设小游戏网站google关键词搜索技巧
  • 淘宝 客要推广网站怎么做全媒体广告代理
  • 那个b2b网站可以做外贸武汉全网推广
  • 深圳css3网站开发多少钱如何策划一个营销方案
  • 织梦统计网站访问量代码网络营销公司如何建立
  • 外贸营销型网站建设今日最新重大新闻
  • 个性化定制网站长春网络推广优化
  • 合肥庐阳区疫情最新消息seo优化首页
  • h5网站制作接单最新中高风险地区名单
  • 北京市住房城乡建设委网站公司怎么在网上推广
  • 网站建设首页怎样插入视频百度指数在线查询小程序
  • 青州网站制作哪家好aso优化哪家好
  • wordpress做网站优点郑州网站seo优化
  • 宝安做棋牌网站建设找哪家公司好湖南长沙疫情最新消息
  • 四川专业网站建设中国十大企业培训机构排名