关闭

各种图算法

标签: 各种图
58人阅读 评论(0) 收藏 举报
分类:

public class TestForString{
public static void main(String[] arg){
int[] array=new int[]{1,8,6,3,9,7};
mergeSort(array,0,5);
for(int i:array){
System.out.print(i+” “);
}
}

 public static int[] mergeSort(int[] array,int low,int heigh){
     if(low>=heigh){
         return array;
     }

     int mid =(low + heigh)/2;
     mergeSort(array,low,mid);
     mergeSort(array,mid+1,heigh);
     return merge(array,low,mid,heigh);

 }



 private static int[] merge(int[] array, int low, int mid, int heigh) {
    int[] temp = new int[heigh-low+1];
    int indexOfLow = low;
    int indexOfHeigh = mid+1;
    int indexOfTemp = 0;



    while(indexOfLow<=mid&&indexOfHeigh<=heigh){
        if(array[indexOfLow]<array[indexOfHeigh]){
            temp[indexOfTemp++]=array[indexOfLow++];

        }else{
            temp[indexOfTemp++]=array[indexOfHeigh++];

        }
    }
    if(indexOfLow<=mid){
        temp[indexOfTemp++]=array[indexOfLow++];

    }
    if(indexOfHeigh<=heigh){
        temp[indexOfTemp++]=array[indexOfHeigh++];
    }

    for(int i:temp){

        array[low++]=i;
    }

    System.out.println();

    return array;
}

public static void insertSort(int[] array){
     for(int i=1;i<array.length;i++){
         int toBeInsert=array[i];
         int n=i;
         while(n>0&&toBeInsert>array[n-1]){
             array[n]=array[n-1];
             n--;
         }
         array[n]=toBeInsert;
    }
 }


 public static void swap(int[] array,int index1,int index2){
     int temp = array[index1];
     array[index1]=array[index2];
     array[index2]=temp;
 }

}

class Edge{
public int srcVert;
public int destVert;
public int distance;

public Edge(int sv,int dv,int d){
    srcVert = sv;
    destVert = dv;
    distance = d;
}

}

class PriorityQ{

private final int SIZE=20;
private Edge[] queArray;
private int size;

public PriorityQ(){
    queArray = new Edge[SIZE];
    size = 0;
}

public void insert(Edge item){
    int j;
    for(j=0;j<size;j++){
        if(item.distance >= queArray[j].distance){
            break;
        }
    }

    for(int i=size;i>j;i--){
        queArray[i]=queArray[i-1];
    }

    queArray[j]=item;

    size++;
}

public Edge removeMin(){
    return queArray[--size];
}

public void removeN(int n){
    for(int i=n;i<size-1;i++){
        queArray[i]=queArray[i+1];
    }

    size--;
}

public Edge peekMin(){
    return queArray[size-1];
}

public int size(){
    return size;
}

public boolean isEmpty(){
    return size==0;
}

public Edge peekN(int n){
    return queArray[n];
}

public int find(int findDex){
    for(int i=0;i<size;i++){
        if(queArray[i].destVert==findDex){
            return i;
        }
    }

    return -1;
}

}

class DistPar{
public int distance;
public int parentVert;
public DistPar(int pv,int d){
distance = d;
parentVert = pv;
}
}

class Vertex{
public char label;
public boolean isInTree;

public Vertex(char lab){
    this.label = lab;
    isInTree = false;
}

}

class Graph{
private final int MAX_VERS=20;
private final int INFINITY=100000;
private Vertex vertexList[];
private int adjMat[][];
private int currentVert;
private PriorityQ thePQ;
private int nTree;
private int nVerts;
private int startToCurrent;
private DistPar sPath[];

public Graph(){
    vertexList = new Vertex[MAX_VERS];
    adjMat = new int[MAX_VERS][MAX_VERS];
    nVerts =0;
    nTree=0;
    for(int i=0;i<MAX_VERS;i++){
        for(int j=0;j<MAX_VERS;j++){
            adjMat[i][j] = INFINITY;
        }
    }

    sPath = new DistPar[MAX_VERS];
}

public void addVertex(char lab){
    if(nVerts<MAX_VERS){
        vertexList[nVerts++]=new Vertex(lab);
    }
}

public void addEdge(int start,int end,int weight){
    adjMat[start][end]=weight;
    //adjMat[end][start]=weight;
}

public void displayVertex(int v){
    System.out.println(vertexList[v].label);
}

public void mstw(){
    currentVert = 0;
    thePQ = new PriorityQ();
    nTree = 0;

    while(nTree<nVerts-1){
        vertexList[currentVert].isInTree = true;
        nTree++;
        for(int i=0;i<nVerts;i++){
            if(vertexList[i].isInTree
                    ||i==currentVert
                    ||adjMat[currentVert][i]==INFINITY){
                continue;
            }

            putIntoPq(currentVert,i,adjMat[currentVert][i]);

        }

        if(thePQ.isEmpty()){
            System.out.print("THE GRIP IS NOT CONNECTED!!!");
            return;
        }

        Edge min = thePQ.peekMin();
        currentVert = min.destVert;
    }
}

private void putIntoPq(int src, int des, int distance) {
    Edge edge = new Edge(src,des,distance);

    int indexOfEdge = thePQ.find(des);
    if(indexOfEdge==-1){
        thePQ.insert(edge);
        return;
    }

    Edge old = thePQ.peekN(indexOfEdge);
    if(old.distance>distance){
        thePQ.removeN(indexOfEdge);
        thePQ.insert(edge);
    }


}

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1049次
    • 积分:99
    • 等级:
    • 排名:千里之外
    • 原创:9篇
    • 转载:2篇
    • 译文:0篇
    • 评论:1条
    文章分类
    最新评论