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

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

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 nodes
        for (int i = 0; i < number; i++) {
   
            Node node = new Node();
            graph.nodeList.add(node);
        }

//        Add 100 edges to the graph
        for (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(
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值