比较懒,斐波那契堆只写了删除最小值和降低节点的值,但是也是最复杂的两个操作啦!

In this assignment, there are three main parts in my code including the random directed graph generator, the dijkstra algorithm with list, and the dijkstra algorithm with Fibonacci Heap. The number of the node V is from 100 to 12000. In each random generated graph, I use these two data structures to implement with Java language and calculate their corresponding running time.

From these two figures, we can see that the average time of Dijkstra algorithm with the List perform worse than the Dijkstra algorithm with the Fibonacci Heap. From the analysis o the time complexity to thse two data structures, we know that for the Fibonacci Heap, its inertion time is O(1) and the time of decreasing key is O(log n) and delete minimum element is (1). Therefore, the time complexity of the Dijkstr algorithm with the Fibonacci Heap is O(VlgV + E) because in thewrst ase, i will decrease all the vertex’s key and has to check all the edges to find the minimum path. For the Djkstra with the list, its insertion time is O(1) and decrease the key is O(1) but delete min is O(n) because it has to iterate fining the minimum element in the list. In the mos case, all the vertexes have been checked so its time complexity is O(VV). From the above anaysis, we can see that the actual running time of Dijkstra algorithm with Fibonacci Heap is not as stable as he List because the time of merging two sub trees in the deletion process and the time of cutting the connectio between the decreased key and its parent or even its grandparents are not sure. That is th reason why the blue line shakes strongly. Code includes the Fibonacci Heap class, and the Normal Dijkstra Test class wth two methods including the Dijkstra algorithm with the Fibonacci Heap and the normal list. A part of the detailed data set of the above test is attached in the below screenshot.

package minimumPath;import fibonacci.FibonacciHeap;import javax.print.attribute.standard.NumberOfDocuments;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;public class DijkstraAlgorithm {public static void main(String[] args) {/*Node node0 = new Node();node0.name = "0";graph.nodeList.add(node0);Node node3 = new Node();node3.name = "3";graph.nodeList.add(node3);Node node5 = new Node();node5.name = "5";graph.nodeList.add(node5);Node node9 = new Node();node9.name = "9";graph.nodeList.add(node9);Node node11 = new Node();node11.name = "11";graph.nodeList.add(node11);graph.addEdge(new Edge(node0, node3, 3));graph.addEdge(new Edge(node0, node5, 5));graph.addEdge(new Edge(node3, node5, 2));graph.addEdge(new Edge(node3, node9, 6));graph.addEdge(new Edge(node5, node3, 1));graph.addEdge(new Edge(node5, node9, 4));graph.addEdge(new Edge(node5, node11, 6));graph.addEdge(new Edge(node9, node11, 2));graph.addEdge(new Edge(node11, node0, 3));graph.addEdge(new Edge(node11, node9, 7));*/for (int i = 100; i < 12000; i += 100) {Graph graph = generateNewGraph(i);long time1 = System.currentTimeMillis();
//            dijkstraWithFibonacciHeap(graph);dijkstraWithList(graph);long time2 = System.currentTimeMillis();System.out.println(time2 - time1);}}public static Graph generateNewGraph(int number) {Graph graph = new Graph();//        Create graph with 40 nodesfor (int i = 0; i < number; i++) {Node node = new Node();graph.nodeList.add(node);}//        Add 100 edges to the graphfor (int j = 0; j < 2 * number; j++) {int random1 = (int) (Math.random() * number);int random2 = (int) (Math.random() * number);if (random1 != random2) {Node startNode = graph.getNode(random1);Node endNode = graph.getNode(random2);List<Edge> startNodeEdges = startNode.edgeList;boolean contained = false;for (Edge edge : startNodeEdges) {if (edge.getEndNode() == endNode)contained = true;}if (!contained) {int weight = (int) (Math.random() * 9 + 1);Edge edge = new Edge(startNode, endNode, weight);startNode.addEdge(edge);endNode.addEdge(edge);}}}return graph;}public static void dijkstraWithList(Graph graph) {Node startNode = graph.getNode(0);if (startNode.edgeList.size() == 0) {//            System.out.println("The startNode does not have any connection with other nodes.");return;}LinkedList<Node> nodes = new LinkedList<>();for (Node node : graph.nodeList) {nodes.add(node);}for (Node node : nodes) {node.weight = Integer.MAX_VALUE;}startNode.weight = 0;while (true) {List<Edge> edges = startNode.edgeList;//            Update the weight that is connected to the start Nodefor (Edge edge : edges) {// Find the edge if the start node is this node with least weightif (edge.startNode == startNode) {Node endNode = edge.endNode;// Update the weight if this node has not been removed from the listif (nodes.contains(endNode)) {if (endNode.weight > edge.weight + startNode.weight) {endNode.weight = edge.weight + startNode.weight;endNode.parent = startNode;}}}}//            Remove the startNode from the listnodes.remove(startNode);if (nodes.size() == 0)break;//            Find the smallest node in the list as the next nodestartNode = nodes.get(0);for (Node node : nodes) {if (node.weight < startNode.weight)startNode = node;}}}public static void dijkstraWithFibonacciHeap(Graph graph) {Node startNode = graph.getNode(0);startNode.weight = 0;if (startNode.edgeList.size() == 0) {System.out.println("The startNode does not have any connection with other nodes.");return;}FibonacciHeap fibonacciHeap = new FibonacciHeap();for (Node node : graph.nodeList) {node.weight = Integer.MAX_VALUE;fibonacciHeap.insert(node);}while (true) {List<Edge> edges = startNode.edgeList;//            Update the weight that is connected to the start Nodefor (Edge edge : edges) {// Find the edge if the start node is this node with least weightif (edge.startNode == startNode) {Node endNode = edge.endNode;// Update the weight if this node has not been removed from the listif (fibonacciHeap.nodeList.contains(endNode)) {if (endNode.weight > edge.weight + startNode.weight) {endNode.weight = edge.weight + startNode.weight;fibonacciHeap.decreaseKey(endNode, endNode.weight);endNode.parent = startNode;}}}}//            Remove the startNode from the listfibonacciHeap.deleteMin();if (fibonacciHeap.rootList.size() == 0)break;//            Find the smallest node in the list as the next nodestartNode = fibonacciHeap.min;}}}
package minimumPath;import java.util.ArrayList;
import java.util.List;public class Graph {List<Node> nodeList = new ArrayList<>();List<Edge> edgeList = new ArrayList<>();public Graph(){}public Node getNode(int i){return nodeList.get(i);}public List<Node> getNodeList() {return nodeList;}public void setNodeList(List<Node> nodeList) {this.nodeList = nodeList;}public List<Edge> getEdgeList() {return edgeList;}public void setEdgeList(List<Edge> edgeList) {this.edgeList = edgeList;}public void addEdge(Edge edge){Node startNode = edge.startNode;Node endNode = edge.endNode;edgeList.add(edge);startNode.addEdge(edge);endNode.addEdge(edge);}
}
package fibonacci;import minimumPath.Node;import java.util.ArrayList;public class FibonacciHeap {public ArrayList<Node> rootList = new ArrayList<>();public int numberOfNode = 0;public Node min;public ArrayList<Node> nodeList = new ArrayList<>();public void insert(Node node){if (min == null)min = node;rootList.add(node);if (node.weight < min.weight)min = node;numberOfNode++;nodeList.add(node);}public Node getMin(){return min;}public Node deleteMin(){Node result = min;rootList.remove(min);nodeList.remove(min);numberOfNode--;if (rootList.size() == 0)return null;for (Node node: min.children) {rootList.add(node);node.fibonacciParent = null;}min = rootList.get(0);for (Node node: rootList){if (node.weight < min.weight)min = node;}while (true) {Node buffer[] = new Node[numberOfNode + 1];boolean condition = true;for (int i = 0; i < rootList.size(); i++) {Node node = rootList.get(i);int degree = node.children.size();if (buffer[degree] == null) {buffer[degree] = node;} else {condition = false;if (buffer[degree].weight <= node.weight){buffer[degree].children.add(node);node.fibonacciParent = buffer[degree];rootList.remove(node);}else {node.children.add(buffer[degree]);buffer[degree].fibonacciParent = node;rootList.remove(buffer[degree]);}break;}}if (condition)break;}return result;}public void decreaseKey(Node node, int value){node.weight = value;if (node.fibonacciParent != null) {node.fibonacciParent.children.remove(node);Node parent = node.fibonacciParent;node.fibonacciParent = null;rootList.add(node);if (node.weight < min.weight)min = node;cutParent(parent);}else {if (node.weight < min.weight)min = node;}}public void cutParent(Node node){if (node.mark = false) {node.mark = true;return;}else if (node.fibonacciParent == null){return;}else {Node parent = node.fibonacciParent;rootList.add(node);if (node.weight < min.weight)min = node;node.fibonacciParent.children.remove(node);node.fibonacciParent = null;cutParent(parent);}}//    public static void main(String[] args) {//        FibonacciHeap fibonacciHeap = new FibonacciHeap();
//        fibonacciHeap.insert(new Node(1));
//        fibonacciHeap.insert(new Node(4));
//        fibonacciHeap.insert(new Node(3));
//        Node node1 = new Node(5);
//        fibonacciHeap.insert(new Node(2));
//        fibonacciHeap.insert(node1);
//        fibonacciHeap.deleteMin();
//        for (Node node: fibonacciHeap.rootList) {//            System.out.println(node);
//            System.out.println(node.children.get(0));
//            System.out.println(node.children.get(1));
//            System.out.println(node.children.get(1).children.get(0));
//            System.out.println(node.children.get(1).children.size());
//        }
//        fibonacciHeap.decreaseKey(node1, 1);
//        for (Node node: fibonacciHeap.rootList) {//            System.out.println(node);
//        }
//    }}
package minimumPath;import java.util.ArrayList;
import java.util.List;public class Node {public String name;public List<Edge> edgeList = new ArrayList<>();public int weight;public Node parent;public Node fibonacciLeft;public Node fibonacciRight;public Node fibonacciParent;public List<Node> children = new ArrayList<>();public boolean mark;public void addEdge(Edge edge){edgeList.add(edge);}public Node(){}public Node(int weight){this.weight = weight;}@Overridepublic String toString() {return "Node{" +"name='" + name + '\'' +", weight=" + weight +", parent=" + parent +'}';}
}
package minimumPath;public class Edge {public Node startNode;public Node endNode;public int weight;public Edge(Node startNode, Node endNode, int weight) {this.startNode = startNode;this.endNode = endNode;this.weight = weight;}public Node getStartNode() {return startNode;}public void setStartNode(Node startNode) {this.startNode = startNode;}public Node getEndNode() {return endNode;}public void setEndNode(Node endNode) {this.endNode = endNode;}public int getWeight() {return weight;}public void setWeight(int weight) {this.weight = weight;}
}

Dijkstra迪杰斯特拉算法 最短路径 Fibonacci Heap斐波那契堆实现以及与链表速度对比相关推荐

  1. MATLAB轻松绘制地图路线——Dijkstra(迪杰斯特拉)算法最短路径规划

    文章目录 1. 地图绘制 2. 计算各节点之间的距离 3. Dijkstra(迪杰斯特拉)算法 4. 根据计算出的距离利用Dijkstra(迪杰斯特拉)算法找出指定节点之间的最短路径 工程文件(可直接 ...

  2. Dijkstra迪杰斯特拉算法的介绍(分为朴素dj和堆优化版dj),包含模板总结(必掌握)与具体例题应用

    (

  3. Dijkstra(迪杰斯特拉)算法求单源最短路径问题

    Dijkstra(迪杰斯特拉)算法求单源最短路径问题 重要的事情说三遍:代码不是我写的!代码不是我写的!代码不是我写的! 第一个算法是严蔚敏数据结构(C语言版)上写的,第二个算法是王道数据结构上写的, ...

  4. 最短路径之Dijkstra(迪杰斯特拉)算法(无向图)

    简介      Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止.由for循环可知,其时间 ...

  5. JavaScript实现dijkstra迪杰斯特拉算法(附完整源码)

    JavaScript实现dijkstra迪杰斯特拉算法 PriorityQueue完整源代码 MinHeap.js完整源代码 Heap.js完整源代码 Comparator.js完整源代码 dijks ...

  6. Dijkstra迪杰斯特拉算法 C++实现

    本篇文章主要介绍了Dijkstra迪杰斯特拉算法的C++实现,文章包含两个部分,在第一部分中我会简单介绍迪杰斯特拉算法以及一些个人的理解,第二部分会对C++代码的逻辑进行解释.下面是我已经上传的代码资 ...

  7. C++实现Dijkstra(迪杰斯特拉)算法(附完整源码)

    C++Dijkstra迪杰斯特拉算法的实现 C++Dijkstra(迪杰斯特拉)算法的完整源码(定义,实现,main函数测试) C++Dijkstra(迪杰斯特拉)算法的完整源码(定义,实现,main ...

  8. dijkstra迪杰斯特拉算法(邻接表法)

    算法简易过程: 迪杰斯特拉算法(朴素) O(n^2) G={V,E} V:点集合 E:边集合 初始化时 令 S={某源点ear}, T=V-S= {其余顶点},T中顶点对应的距离(ear, Vi)值若 ...

  9. Dijkstra(迪杰斯特拉)算法

    一.简介 迪克斯特拉算法又名Dijkstra算法(属于贪心算法).Dijkstra算法是从一节点到其余各节点最短路径计算方法. 迪杰斯特拉算法以起始点为中心向外层层扩展,直到扩展到终点为止. 算法思想 ...

  10. 算法提升:图的Dijkstra(迪杰斯特拉)算法

    目录 概念 思路 代码 概念 迪杰斯特拉算法(Dijkstra)是由荷兰计算机科学家狄克斯特拉于1959年提出的,因此又叫狄克斯特拉算法.是从一个顶点到其余各顶点的最短路径算法,解决的是有权图中最短路 ...

最新文章

  1. 发那科sub_一文搞定FANUC PMC
  2. 启明云端分享| ESP32-S3支持自定义离线语音,可支持 200 条本地命令语句,无需外加 DSP 芯片
  3. VUE计算属性关键词: computed
  4. 常用shader固有函数
  5. LoRaWAN的四大优势及适用领域
  6. 时间复杂度O(1) O(n) O(logn) O(nlogn)是什么意思?
  7. Levenshtein编辑距离C++实现
  8. 【扩频通信】基于matlab直接序列扩频通信【含Matlab源码 1004期】
  9. FPGROWTH算法
  10. EAS服务不可用或者更新一半中断解决方案
  11. 大数据与数据挖掘的基本概念,它们之间的内在联系是什么?
  12. Codeforces--965B--Battleship
  13. html盒子距离上边距50px,Margin的垂直外边距问题
  14. 服务器如何预防入侵问题
  15. 知乎liv笔记08 买车之后:如何保养不花冤枉钱?
  16. 幂函数的c语言程序,C ++中的幂函数
  17. 如何配置高性能的计算机,笔记本电脑如何设置电源计划为高性能
  18. qt飞扬青云 / Qt开发经验
  19. R语言和Rstudio下载 以及 R 包安装
  20. Geogebra里给带有曲线和直线混合边界的封闭区域填充颜色

热门文章

  1. 【语义分割】--SegNet理解
  2. jquery版本安全漏洞问题
  3. Unity 提取资源 Disunity、Unity Studio
  4. 剔除水印软件Inpaint 7.2 中文破解版 微笑一刀作品
  5. java.this的作用包括,智慧职教: 以下不是Java中this关键字的作用的是()。
  6. Arduino教程 初体验之点亮自带LED灯(含管脚图)
  7. 淘宝千万级并发架构的十四次演进
  8. linux livecd 挂载硬盘,网上的Ubuntu LiveCD硬盘安装方法
  9. linux ftp 服务配置
  10. 中级软考-软件设计师(一)