//测试是否改变某个堆中某个数字只需要,对该数字ShiftUP 和shiftDown即可 public void testChangeInIndexHeap(IndexMinHeap <int> minIndexHeap) { print("改变前"); minIndexHeap.print(); int index = Random.Range(1, minIndexHeap.Size()); int item = 10; minIndexHeap.Change(index, 10); print("改变后,改变第" + index + "元素,改为" + item); minIndexHeap.print(); }
public Dijkstra(WeightSpareGraph <float> graph, int src) { Debug.Assert(src >= 0 && src < graph.V()); this.src = src; this.graph = graph; this.n = graph.V(); this.m = graph.E(); shortPath = new float[n]; book = new bool[n]; from = new int[n]; for (int i = 0; i != n; i++) { book [i] = false; from [i] = -1; //假设都是可达的 } from [src] = -1; shortPath [src] = 0f; IndexMinHeap <float> indexMinHeap = new IndexMinHeap <float> (n); indexMinHeap.Insert(src, shortPath[src]); while (indexMinHeap.Size() != 0) { int u = indexMinHeap.ExtraMinItemIndex(); book [u] = true; WeightSpareGraph <float> .adjIterator iter = new WeightSpareGraph <float> .adjIterator(graph, u); for (Edge <float> e = iter.begin(); !iter.isEnd(); e = iter.Next()) { int v = e.Other(u); if (!book[v]) { if (from [v] == -1 || shortPath [v] > shortPath [u] + e.weight) { from [v] = u; shortPath [v] = shortPath [u] + e.weight; if (indexMinHeap.isContain(v)) { indexMinHeap.Change(v, shortPath [v]); } else { indexMinHeap.Insert(v, shortPath[v]); } } } } } }
public PrimMST(WeightSpareGraph <float> graph) { this.graph = graph; this.n = graph.V(); this.m = graph.E(); minSpanTreeValue = 0; book = new bool[n]; toEdgeArr = new List <Edge <float> >(); for (int i = 0; i != n; i++) { toEdgeArr.Add(null); } for (int i = 0; i != n; i++) { book [i] = false; } indexMinHeap = new IndexMinHeap <float> (m); book [0] = true; visit(0); while (indexMinHeap.Size() != 0) { int index = indexMinHeap.ExtraMinItemIndex(); Debug.Assert(toEdgeArr[index] != null); book[index] = true; visit(index); // int u = toEdgeArr [index].U (); // int v = toEdgeArr [index].V (); // if (book [u] && !book [v]) { // book [v] = true; // visit (v); // } else if(!book [u] && book [v]){ // book [u] = true; // visit (u); // } } for (int i = 1; i != n; i++) { minSpanTreeValue += toEdgeArr [i].weight; } }
// Use this for initialization void Start() { //测试极小堆 print("------------------测试极小堆------------------"); int capicity = 100; int n = 10; int[] arr = AlgorithmsHelp.generateRandomArray(n, 1, 20); MinHeap <int> minHeap = new MinHeap <int> (100); MinHeap <int> minarrHeap = new MinHeap <int> (arr, 100); for (int i = 0; i != n; i++) { minHeap.Insert(arr[i]); } minHeap.print(); minarrHeap.print(); string str = ""; while (minHeap.Size() > 0) { str += minHeap.ExtraMinItem() + " "; } Debug.Log(str); testChangeInNormalHeap(minarrHeap); print("------------------------------------------"); print("------------------测试极小索引堆------------------"); //测试极小索引堆 arr = AlgorithmsHelp.generateRandomArray(n, 1, 20); IndexMinHeap <int> minIndexHeap = new IndexMinHeap <int>(100); IndexMinHeap <int> minarrIndexHeap = new IndexMinHeap <int>(arr, 100); for (int i = 0; i != n; i++) { minIndexHeap.Insert(arr[i]); } minIndexHeap.print(); minarrIndexHeap.print(); str = ""; while (minIndexHeap.Size() > 0) { str += minIndexHeap.ExtraMinItem() + " "; } print("从小到大排序 " + str); testChangeInIndexHeap(minarrIndexHeap); // 测试极大堆 // int capicity = 100; // int n = 10; // Heap<int> maxHeap = new Heap<int> (100); // int[] arr = AlgorithmsHelp.generateRandomArray (n,1,20); // for (int i = 0; i != n; i++) { // maxHeap.Insert (arr[i]); // } // maxHeap.printData (); // // string str = ""; // for (int i = 0; i != n; i++) { // str +=maxHeap.ExtractBigItem()+" "; // } // print ("data[] : " +str); }