亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? binaryheap.java

?? JAVA版的螞蟻算法(Ant Colony Optimization Algorithms)
?? JAVA
字號:
package dk.itu.nulx30.util;

/**
 * This implementation of a binary heap is inspired by "Introduction to
 * Algorithms" Second Edition by Thomas H. Cormen et. al.
 * The <code>BinaryHeap</code> is implemented by an array starting at index 1, 
 * because of the description in the above-mentioned book.<br> 
 * The <code>BinaryHeap</code> uses two arrays:
 * <UL>
 *   <LI> <code>a</code> - which is used to implement the functionality and 
 *   <LI> <code>placement</code> - which is used to lookup the correct elements 
 *        (even though they have changed position in array <code>a</code> due
 *        to an operation).
 * </UL>
 *
 * @author  Mikkel Bundgaard
 * @author  Troels C. Damgaard
 * @author  Federico Decara
 * @author  Jacob W. Winther
 */
public class BinaryHeap {
  /** The number of elements in this object */  
  private int size;
  /** The array which is representing the <code>BinaryHeap</code>*/
  private BinaryHeapElement[] a;
  /** This array is used as lookup for the vertices in the binary heap*/
  private int[] placement;

  /** 
   * Class constructor which takes an initial capacity of the binary heap. 
   * If this constructor is used then the methods 
   * {@link #decreaseKey(int,double) decreaseKey( int, double )} and 
   * {@link #add(BinaryHeapElement,int) add( BinaryHeapElement, int )}
   * must not be called, because this would result in a NullPointerException.
   *
   * @param initCapacity the initial capacity of the binary heap.
   *
   * @see #decreaseKey(int,double)
   * @see #add(BinaryHeapElement,int)
   */
  public BinaryHeap( int initCapacity ) {
    size = 0;
    a = new BinaryHeapElement[ initCapacity ];
  }     

  /**
   * Class constructor which takes an array of BinaryHeapElements.
   *
   * @param newArray an array of BinaryHeapElements.
   */
  public BinaryHeap( BinaryHeapElement[] newArray ) {
    size = newArray.length;
    // This array is one element longer because index 0 isn't used. 
    a = new BinaryHeapElement[ newArray.length + 1 ];
    placement = new int[ newArray.length ];

    for ( int i = 0; i < newArray.length; i++ ) {
      a[ i + 1 ] = newArray[ i ];
      placement[ i ] = i + 1;
    }

    // Build this heap using minHeapify
    for ( int i = newArray.length / 2; i > 0; i-- ) {
      minHeapify( i );
    }
  }
  
  /**
   * returns <code>true</code> if and only if this binary heap is empty, 
   * otherwise <code>false</code>.
   *
   * @return <code>true</code> if and only if this binary heap is empty, 
   * otherwise <code>false</code>.
   */
  public boolean isEmpty() {
    return size == 0;
  }

  /**
   * Returns the number of elements in this binary heap.
   *
   * @return the size of this <code>BinaryHeap</code> (the number of elements
   * in this heap.
   */
  public int size() {
    return size;
  }
  
  /**
   * <code>checkSize</code> examines if this binary heap is full. 
   * If this is the case then the size of this binary heap is doubled.
   */  
  private void checkSize() {
    // Index 0 is not used
    if ( size == a.length - 1 ) {
      BinaryHeapElement[] tmp = new BinaryHeapElement[ a.length * 2 ];
      System.arraycopy( a, 1, tmp, 1, size );
      a = tmp;

      // Double the size of the placement array if lookup is used.
      if ( placement != null ) {
        int[] tmp2 = new int[ placement.length * 2 ];
        System.arraycopy( placement, 1, tmp2, 1, placement.length - 1 );
        placement = tmp2;
      }
    }
  }

  /**
   * <code>insertElement</code> inserts the element into this binary
   * heap without destroying the min-heap property.
   *
   * @param element the <code>BinaryHeapElement</code> to insert into this
   * binary heap.
   *
   * @return the index of the newly inserted element.
   */
  private int insertElement( BinaryHeapElement element ) {
    int hole = ++size;

    // Percolate hole up
    for ( ; parent( hole ) > 0 && element.getPriority() <
                         a[ parent( hole ) ].getPriority(); hole = parent( hole ) ) {
      a[ hole ] = a[ parent( hole ) ];
      if ( placement != null ) {
        placement[ a[ hole ].getIndex() ] =
                                    placement[ a[ parent( hole ) ].getIndex() ];
      }
    }
    a[ hole ] = element;
    
    return hole;    
  }
  
  /**
   * adds the <code>element</code> into this binary heap without destroying
   * the min-heap property. This method cannot be used together with lookup.
   *
   * @param element the <code>BinaryHeapElement</code> to insert into this 
   * binary heap.
   */
  public void add( BinaryHeapElement element ) {
    checkSize();
    insertElement( element );
  }
  
  /**
   * adds the <code>element</code> into this binary heap without destroying
   * the min-heap property and maintaining a lookup entry. This method can only
   * be called if this binary heap was constructed with the
   * {@link #BinaryHeap(BinaryHeapElement[]) constructor} which takes an array
   * as argument.
   *
   * @param element the <code>BinaryHeapElement</code> to insert into this
   * binary heap.
   * @param index The <code>index</code> must be unique otherwise decreaseKey
   * cannot function correctly. <code>index</code> is a handle into this binary
   * heap, so decreaseKey can be called on the correct element.
   *
   * @see #BinaryHeap(BinaryHeapElement[])
   */
  public void add( BinaryHeapElement element, int index ) {
    checkSize();
    
    if ( index >= placement.length ) {
      int[] tmp2 = new int[ index * 2 ];
      System.arraycopy( placement, 1, tmp2, 1, placement.length - 1 );
      placement = tmp2;
    }

    placement[ index ] = insertElement( element );
  }

  /**
   * <code>extractMin</code> extracts the element with the minimum priority.
   * Then it reconstructs this binary heap
   *
   * @return the index of the vertex with the smallest priority.
   */
  public BinaryHeapElement extractMin() {
    if ( isEmpty() ) {
      throw new RuntimeException( "Heap underflow" );
    }
    
    BinaryHeapElement min = a[ 1 ];
    a[ 1 ] = a[ size ];
    size -= 1;
    minHeapify( 1 );

    // Make this heap half size if the size of this heap is less than 1/4
    if ( size < ( a.length - 1 ) / 4 ) {
      BinaryHeapElement[] tmp = new BinaryHeapElement[ ( a.length - 1 ) / 2 ];
      System.arraycopy( a, 1, tmp, 1, size );
      a = tmp;
    }
    
    return min;
  }

  /**
   * This method is called on this <code>BinaryHeap</code> to decrease the
   * priority of the element at index <code>i</code>. This method can only be
   * called if this binary heap was constructed with the
   * {@link #BinaryHeap(BinaryHeapElement[]) constructor} which takes an array
   * as argument and the only {@link #add(BinaryHeapElement) add-method} called
   * is the one with a lookup.
   *
   * @param index the index of the element.
   * @param key the new priority of the element.
   *
   * @see #BinaryHeap(BinaryHeapElement[])
   * @see #add(BinaryHeapElement,int)
   */
  public void decreaseKey( int index, double key ) {
    // get the correct index through the lookuplist.
    int i = placement[ index ];

    if ( key > a[ i ].getPriority() ) {
      throw new RuntimeException( "New key is larger than current key" );
    }

    a[ i ].setPriority( key );
    while ( i > 1 && a[ parent( i ) ].getPriority() > a[ i ].getPriority() ) {
      // Swap the to elements ( a[ i ] and a[ parent( i ) ] )
      // This way the indexes in the placement array still points
      // to the corresponding vertex.
      BinaryHeapElement tmp = a[ i ];
      a[ i ] = a[ parent( i ) ];
      a[ parent( i ) ] = tmp;
      // Update the indexes so that the elements points at their own
      // location
      if ( placement != null ) {
        int tmpPlacement = placement[ a[ i ].getIndex() ];
        placement[ a[ i ].getIndex() ] = placement[ a[ parent( i ) ].getIndex() ];
        placement[ a[ parent( i ) ].getIndex() ] = tmpPlacement;
      }

      i = parent( i );
    }
  }

  /**
   * This method percolates down the element at index <code>i</code> if
   * it violates the min-heap property.
   *
   * @param i the index of the element to percolate down.
   */
  private void minHeapify( int i ) {
    int l = left( i );
    int r = right( i );
    int smallest;

    if ( l <= size && a[ l ].getPriority() < a[ i ].getPriority() ) {
      smallest = l;
    }
    else {
      smallest = i;
    }

    if ( r <= size && a[ r ].getPriority() < a[ smallest ].getPriority() ) {
      smallest = r;
    }

    if ( smallest != i ) {
      // Swap the elements 
      BinaryHeapElement tmp = a[ i ];

      a[ i ] = a[ smallest ];
      a[ smallest ] = tmp;
      
      if ( placement != null ) {
        int tmpPlacement = placement[ a[ i ].getIndex() ];
        placement[ a[ i ].getIndex() ] = placement[ a[ smallest ].getIndex() ];
        placement[ a[ smallest ].getIndex() ] = tmpPlacement;
      }

      // Move further down in the heap
      minHeapify( smallest );
    }
  }

  /**
   * <code>parent</code> returns the index of the parent to the
   * given node.
   *
   * @param i the index of the element.
   *
   * @return the index of the parent to <code>i</code>.
   */
  private static int parent( int i ) {
    return i / 2;
  }

  /**
   * <code>left</code> returns the index of the left child to the
   * given node.
   *
   * @param i the index of the element.
   *
   * @return the index of the left child to <code>i</code>.
   */
  private static int left( int i ) {
    return 2 * i;
  }

  /**
   * <code>right</code> returns the index of the right child to the
   * given node.
   *
   * @param i the index of the element.
   *
   * @return the index of the right child to <code>i</code>.
   */
  private static int right( int i ) {
    return 2 * i + 1;
  }
}


?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
97精品国产97久久久久久久久久久久 | 日韩一级大片在线观看| kk眼镜猥琐国模调教系列一区二区 | 欧美日韩日日骚| 欧美精品日韩一区| 精品国产区一区| 国产精品私人影院| 一区二区三区在线视频观看| 三级亚洲高清视频| 国产jizzjizz一区二区| 欧美变态口味重另类| 欧美一区二区视频网站| 成人a级免费电影| 91论坛在线播放| 欧美日本一区二区三区| 欧美成人性战久久| 中文字幕免费观看一区| 亚洲成在人线免费| 国产成人综合在线| 在线亚洲人成电影网站色www| 欧美视频一区在线| 久久香蕉国产线看观看99| 中文字幕在线观看不卡| 天天av天天翘天天综合网色鬼国产| 久久疯狂做爰流白浆xx| 97精品国产97久久久久久久久久久久| 欧美日韩成人综合在线一区二区| 欧美成人bangbros| 亚洲风情在线资源站| 懂色中文一区二区在线播放| 欧美午夜影院一区| 国产欧美日本一区视频| 免费看日韩a级影片| 91久久国产最好的精华液| 久久精品亚洲麻豆av一区二区| 三级影片在线观看欧美日韩一区二区| 国产麻豆日韩欧美久久| 91精品麻豆日日躁夜夜躁| 亚洲精品中文在线观看| 成人高清伦理免费影院在线观看| 欧美另类高清zo欧美| 亚洲美女屁股眼交| 成人免费毛片aaaaa**| 久久精品综合网| 紧缚奴在线一区二区三区| 宅男噜噜噜66一区二区66| 天天影视网天天综合色在线播放 | 欧美色国产精品| 亚洲天堂中文字幕| 91农村精品一区二区在线| 国产精品三级视频| 成人毛片视频在线观看| 国产欧美日韩在线| 成人精品gif动图一区| 中文av一区二区| 91在线看国产| 一区二区三区不卡在线观看| 91在线免费播放| 一区二区三区在线免费观看| 在线精品国精品国产尤物884a| 亚洲一区二区三区美女| 欧美性大战久久久| 老司机午夜精品| 26uuu久久天堂性欧美| 成人毛片在线观看| 亚洲高清一区二区三区| 日韩精品一区二区三区三区免费| 极品少妇xxxx精品少妇偷拍| 亚洲国产精品成人久久综合一区| 99re在线视频这里只有精品| 亚洲国产精品嫩草影院| 欧美一区二区免费视频| 成人午夜av电影| 日韩国产成人精品| 国产精品久久久久永久免费观看| 欧美午夜精品免费| 国产福利精品导航| 午夜精品久久久久久| 欧美精品一区二区精品网| 色一情一伦一子一伦一区| 精品一区二区三区日韩| 亚洲另类中文字| 国产午夜精品一区二区| 欧美精品久久99久久在免费线 | 亚洲黄色小视频| 精品免费一区二区三区| 欧美色爱综合网| 色999日韩国产欧美一区二区| 六月丁香婷婷久久| 日韩精品电影在线观看| ...xxx性欧美| 日本一区二区电影| 久久久一区二区三区| 91精品国产乱| 这里只有精品99re| 欧美日本一道本| 欧美综合一区二区| 日本久久电影网| 色婷婷av一区二区三区软件| 风间由美性色一区二区三区| 国产在线不卡一卡二卡三卡四卡| 亚洲.国产.中文慕字在线| 亚洲精品乱码久久久久久| 1024国产精品| 国产精品九色蝌蚪自拍| 国产精品久久久久久久久免费樱桃| 久久午夜老司机| 国产女人18水真多18精品一级做 | 一本久道中文字幕精品亚洲嫩| 高清在线不卡av| www.成人网.com| av在线一区二区三区| 欧洲国产伦久久久久久久| 欧美三级电影在线看| 88在线观看91蜜桃国自产| 欧美日韩成人一区二区| 91精品国产欧美日韩| 日韩精品最新网址| 国产农村妇女精品| 亚洲精品国产a久久久久久| 日日摸夜夜添夜夜添国产精品| 偷拍亚洲欧洲综合| 国产精品1区2区| 色综合亚洲欧洲| 欧美大片拔萝卜| 国产精品麻豆视频| 无码av中文一区二区三区桃花岛| 看电影不卡的网站| 一本色道**综合亚洲精品蜜桃冫| 欧美一区二区三区男人的天堂| 久久亚洲欧美国产精品乐播| 亚洲视频资源在线| 另类人妖一区二区av| 91毛片在线观看| 久久综合色婷婷| 午夜精品福利一区二区蜜股av | 91啦中文在线观看| 日韩精品最新网址| 亚洲精品中文在线影院| 国产麻豆一精品一av一免费| 欧美日韩国产精品自在自线| 国产视频一区在线播放| 日本特黄久久久高潮| 972aa.com艺术欧美| 日韩精品一区二区三区在线播放| 亚洲欧美乱综合| 成人免费va视频| 日韩午夜小视频| 日韩av在线发布| 69久久99精品久久久久婷婷| 亚洲狠狠丁香婷婷综合久久久| 国产精品18久久久| 国产亚洲人成网站| 狠狠色狠狠色合久久伊人| 7777精品伊人久久久大香线蕉| 亚洲精品中文在线| 欧美影视一区在线| 亚洲精选一二三| 色狠狠一区二区| 亚洲主播在线播放| 欧美日韩一区二区欧美激情| 亚洲欧美色一区| 欧美亚日韩国产aⅴ精品中极品| 亚洲视频免费观看| 欧美色精品天天在线观看视频| 亚洲欧洲制服丝袜| 色综合久久天天| 亚洲国产精品一区二区久久恐怖片| 在线视频国产一区| 亚洲1区2区3区4区| 精品国产乱码久久久久久1区2区 | 成人精品国产免费网站| 国产精品理论在线观看| av电影在线观看完整版一区二区| 亚洲人123区| 欧美一区二区三区免费视频| 精品一区二区三区视频| 国产婷婷色一区二区三区在线| 国产aⅴ精品一区二区三区色成熟| 国产欧美日韩卡一| 欧美理论在线播放| 国产精品99久久久久久宅男| 亚洲三级在线免费观看| 日韩视频在线你懂得| 丁香激情综合国产| 日韩不卡一二三区| 国产精品久久久久久久久搜平片 | 日韩精品中午字幕| 99精品热视频| 激情综合一区二区三区| 中文字幕一区二区三区乱码在线| 欧美久久一区二区| 99久久婷婷国产精品综合| 日本不卡视频在线| 一区二区视频在线看| 精品福利av导航| 欧美精品在线观看播放| 91免费在线视频观看| 成人夜色视频网站在线观看| 午夜精品久久久久久久久|