比较懒,斐波那契堆只写了删除最小值和降低节点的值,但是也是最复杂的两个操作啦!
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(