各种图算法

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);
    }


}

}

阅读更多
文章标签: 各种图
个人分类:
想对作者说点什么? 我来说一句

ACM算法大全

2012年12月03日 973KB 下载

经典算法之多段图算法

2018年04月15日 106KB 下载

C++算法:图算法 (第3版) pdf

2017年11月19日 53.02MB 下载

第2卷:图算法.part4

2010年11月12日 2.47MB 下载

第2卷:图算法.part2

2010年11月12日 10MB 下载

没有更多推荐了,返回首页

不良信息举报

各种图算法

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭