顯示具有 演算法作業 標籤的文章。 顯示所有文章
顯示具有 演算法作業 標籤的文章。 顯示所有文章

2013年6月3日 星期一

圍棋算氣程式

Java code:
import java.util.Scanner;

public class Chi {
    static int[][] board = new int[19][19];
    static boolean[][] gone = new boolean[19][19];
    static boolean[][] isCount = new boolean[19][19];
    static int chessColor;
    static int chiNum = 0;
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int x, y;
        
        try {
            System.out.println("請輸入棋盤:");
            for (int i=0; i<board.length; i++) {
                for (int j=0; j<board[i].length; j++)
                    board[i][j] = sc.nextInt();
            }
            System.out.print("請輸入座標: ");
            x = sc.nextInt();
            y = sc.nextInt();
        } finally {
            sc.close();
        }
        if (board[x][y] == 0)
            System.out.println("這個座標沒有任何棋子");
        else {
            chessColor = board[x][y];
            countChi(x, y);
            if (chessColor == 1)
                System.out.println("您算的是黑子的氣");
            else
                System.out.println("您算的是白子的氣");
            System.out.println("共有" + chiNum + "個氣");
        }
    }

    private static void countChi(int x, int y) {
        if (board[x][y] == 0 && !isCount[x][y]) {
            chiNum++;
            isCount[x][y] = true;
        }
        else if (!gone[x][y] && board[x][y] == chessColor) {
            gone[x][y] = true;
            countChi(x - 1, y);
            countChi(x, y - 1);
            countChi(x, y + 1);
            countChi(x + 1, y);
            gone[x][y] = false;
        }
    }
}

範例輸入輸出:



這是老師出的額外題目,題目的要求是輸入一個棋盤,並且指定一個座標,並算出指定座標棋子所在區域的氣。此程式會自動辨識您指定的座標是黑子區域還是白子區域,若您指定黑子就會為您算出黑子的氣;若您指定白子就會為您算出白子的氣,若您指定的地方沒有任何棋子,程式會告訴您這地方沒有任何棋子。此程式的作法就是不斷地搜尋這區域周圍的氣(也就是沒有任何棋子的地方),若已經搜尋過得地方用gone這個布林陣列把它記起來,以避免無限的搜尋,若找到氣了,就把氣數+1然後用布林陣列記住這個地方已經算過氣了,避免未來再重複算一次。
此程式的輸入為一個19 * 19的棋盤,0代表沒有棋子, 1代表黑棋, 2代表白棋,完成後程式會要求您輸入一個座標,格式為(x y),x和y的範圍皆為0~18。輸出為你算的是什麼棋子的氣,以及總共是多少氣。

0-1 Knapsack problem

Java code:
import java.util.LinkedList;
import java.util.List;

class Item {
    int price;
    int weight;
    int pricePerWeight;
    
    public Item(int price, int weight, int pricePerWeight) {
        this.price = price;
        this.weight = weight;
        this.pricePerWeight = pricePerWeight;
    }
}

public class KnapsackProblem {
    static Item[] allItem =
        {new Item(20, 2, 10), new Item(30, 5, 6), new Item(35, 7, 5),
         new Item(12, 3, 4), new Item(3, 1, 3)};
    static int limitWeight = 9;
    static int nowPrice = 0;
    static int nowWeight = 0;
    static int maxPrice = Integer.MIN_VALUE;
    static List<Integer> takeItems = new LinkedList<Integer>();
    static List<Integer> maxPriceTakeItems = new LinkedList<Integer>();
    
    public static void main(String[] args) {
        stealItem(0);
        System.out.print("要拿的物品: ");
        for (int index: maxPriceTakeItems)
            System.out.print(index + 1 + " ");
        System.out.println("\n最大價值: " + maxPrice);
    }

    private static void stealItem(int itemIndex) {
        if (nowWeight <= limitWeight) {
            int boundPrice;
            
            if (nowPrice > maxPrice) {
                maxPrice = nowPrice;
                maxPriceTakeItems.clear();
                for (int index: takeItems)
                    maxPriceTakeItems.add(index);
            }
            boundPrice = calBound(itemIndex);
            if (boundPrice > nowPrice && maxPrice < boundPrice) {
                for (int i=itemIndex; i<allItem.length; i++) {
                    nowPrice += allItem[itemIndex].price;
                    nowWeight += allItem[itemIndex].weight;
                    takeItems.add(itemIndex);
                    stealItem(i + 1);
                    nowPrice -= allItem[itemIndex].price;
                    nowWeight -= allItem[itemIndex].weight;
                    takeItems.remove(takeItems.size() - 1);
                }
            }    
        }
    }

    private static int calBound(int itemIndex) {
        int thePrice = nowPrice;
        int theWeight = nowWeight;
        int i;
        
        for (i=itemIndex; i<allItem.length && theWeight<=limitWeight; i++) {
            thePrice += allItem[i].price;
            theWeight += allItem[i].weight;
        }
        i--;
        thePrice -= allItem[i].price;
        theWeight -= allItem[i].weight;
        return thePrice + allItem[i].pricePerWeight * (limitWeight - theWeight);
    }
}

範例輸入輸出:



0-1背包問題就是解決在物品沒有辦法被分割且背包負重有限的情況下能帶走物品的最大價值。程式裡的類別Item就是代表物品,裡面的field有price(價值), weight(重量), 還有pricePerWeight(單位重量的價值),allItem陣列裡面放的是所有的物品(我偷偷地把它們依照單位重量的價值排序好了XD)。程式的作法就是不斷地拿物品和放物品來找到最大價值,重點是有幾種情況就可以知道物品不要再往下拿了。第一種情況就是總重量已經超過背包的負重,第二種情況就是未來可以拿到的最大價值(利用單位重量的價值計算)等於現在的價值,第三種情況就是未來可以拿到的最大價值比現在已經拿到的最大價值還要少。
因為此程式是解決課本第5章的第33題,所以輸入參數已經是寫死的,輸出為最後拿的物品和最大總價值。

Hamilton Circuit

Java code:
import java.util.LinkedList;
import java.util.List;

public class HamiltonCircuit {
    static boolean[][] graph =
        {{false, true, false, false, true, false, false, false, false, false, false, false},
         {true, false, true, false, false, false, true, true, false, false, false, false},
         {false, true, false, true, false, false, false, true, false, false, false, false},
         {false, false, true, false, false, false, false, false, true, false, false, false},
         {true, false, false, false, false, true, false, false, false, true, false, false},
         {false, false, false, false, true, false, true, false, false, false, true, false},
         {false, true, false, false, false, true, false, true, false, false, false, false},
         {false, true, true, false, false , false, true, false, true, false, false, false},
         {false, false, false, true, false, false, false, true, false, false, false, true},
         {false, false, false, false, true, false, false, false, false, false, true, false},
         {false, false, false, false, false, true, false, false, false, true, false, true},
         {false, false, false, false, false, false, false, false, true, false, true, false}};
    static boolean[] isGone = new boolean[graph.length];
    static List<Integer> path = new LinkedList<Integer>();
    
    public static void main(String[] args) {
        for (int i=0; i<graph.length; i++)
            findCircuit(i);
    }

    private static void findCircuit(int vertex) {
        if (path.size() == graph.length) {
            if (graph[vertex][path.get(0)]) {
                for (int v: path)
                    System.out.print(v + 1 + " ");
                System.out.print((path.get(0) + 1) + "\n\n");
            }
        }
        else {
            isGone[vertex] = true;
            path.add(vertex);
            for (int i=0; i<graph.length; i++) {
                if (graph[vertex][i] && !isGone[i])
                    findCircuit(i);
            }
            isGone[vertex] = false;
            path.remove(path.size() - 1);
        }
    }
}

範例輸入輸出:



Hamilton Circuit 是要在圖上找一個點當作起點,接著把圖上所有的點走訪一遍後回到原點。程式的作法是把所有點都當作起點看看,然後想辦法把每個點都先走過一遍,最後再看看終點能不能回到起點。
因為此程式是解決課本第5章的第26題,所以輸入參數已經寫死在程式裡,之所以沒有範例輸入輸出,是因為這題找不到Hamilton Circuit(如果我的程式沒有錯的話)。

Coloring Problem

Java code:
public class ColoringProblem {
    private enum Color {
        RED, GREEN, WHITE
    }
    static boolean[][] graph =
        {{false, true, false, true, false, false},
         {true, false, true, false, true, false},
         {false, true, false, false, false, true},
         {true, false, false, false, true, false},
         {false, true, false, true, false, true},
         {false, false, true, false, true ,false}};
    static Color[] graphColor = new Color[graph.length];
    static int countComb = 0;
    
    public static void main(String[] args) {
        paintColor(0);
        System.out.println("總共有" + countComb + "種塗色方法");
    }

    private static void paintColor(int vertex) {
        if (vertex < graph.length) {
            for (Color c: Color.values()) {
                boolean isUsed = false;
                
                for (int i=0; i<graph.length; i++) {
                    if (graph[vertex][i] && graphColor[i] == c) {
                        isUsed = true;
                        break;
                    }
                }
                if (!isUsed) {
                    graphColor[vertex] = c;
                    paintColor(vertex + 1);
                    graphColor[vertex] = null;
                }
            }
        }
        else {
            for (int i=0; i<graphColor.length; i++)
                System.out.printf("%-6s", "v" + (i + 1));
            System.out.println();
            for (Color c: graphColor) {
                switch (c) {
                    case RED:
                        System.out.printf("%-6s", "red");
                        break;
                    case GREEN:
                        System.out.printf("%-6s", "green");
                        break;
                    case WHITE:
                        System.out.printf("%-6s", "white");
                }
            }
            System.out.print("\n\n");
            countComb++;
        }
    }
}

範例輸入輸出:



此程式解決的是塗色問題,也就是在一個graph上面,用m個顏色去著色,並且相鄰的點不可以塗上相同的顏色。程式的作法是先選一個點並且從第一個顏色開始嘗試,塗上顏色後在選擇下一個點塗色。塗色時需要檢查相鄰的點有沒有使用一樣的顏色,若有使用一樣的顏色,就必須換色,若沒有顏色可以塗,就必須更改上一個點的顏色。若所有點都成功上色了,就表示找到一個塗色方法了,依照這個方法不斷做下去,就可以找出所有塗色的方法了。
因為此程式是解決課本第5章第18題的問題,所以輸入參數已經寫死在程式裡,輸出為所有的塗色方法和塗色方法的總數目。因為輸出結果過長,所以以上只貼出部份輸出結果。

Sum-of-Subsets

Java code:
import java.util.LinkedList;
import java.util.List;

public class SumOfSubsets {
    static int[] nums = {2, 10, 13, 17, 22, 42};
    static List<Integer> ansList = new LinkedList<Integer>();
    
    public static void main(String[] args) {
        int totalWeight = 52;
        
        System.out.println("以下是所有的解:");
        findAns(totalWeight, 0);
    }

    private static void findAns(int restWeight, int startIndex) {
        if (restWeight > 0) {
            for (int i=startIndex; i<nums.length; i++) {
                ansList.add(nums[i]);
                findAns(restWeight - nums[i], i + 1);
                ansList.remove(ansList.size() - 1);
            }    
        }
        else if (restWeight == 0) {
            for (int i: ansList)
                System.out.print(i + " ");
            System.out.println();
        }
    }
}

範例輸入輸出:



此程式解決的問題是Sum-of-Subsets問題,問題是給一個數字集合S還有一個數字N,找出S裡面所有加起來總和會等於N的組合。解法就是窮舉所有的加法組合看看那一組有符合上述條件。
此程式是解決課本第5章第13題的問題,所以參數已經設死在程式裡。輸出結果就是所有總和為52的組合。

N-Queen

Java code:
import java.util.Scanner;

public class N_Queen {
    static int n;
    static int countQueen = 0;
    static int ansCount = 0;
    static boolean[] colHasQueen, leftSlashHasQueen, rightSlashHasQueen;
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        try {
            System.out.print("請輸入n: ");
            n = sc.nextInt();
        } finally {
            sc.close();
        }
        colHasQueen = new boolean[n];
        leftSlashHasQueen = new boolean[2 * n - 1];
        rightSlashHasQueen = new boolean[2 * n - 1];
        findQueen(0, 0);
        System.out.println("總共有" + ansCount + "組解");
    }

    private static void findQueen(int row, int col) {
        if (countQueen == n)
            ansCount++;
        else if (col < n) {
            if (!colHasQueen[col] && !leftSlashHasQueen[row - col + n - 1] &&
                    !rightSlashHasQueen[row + col]) {
                colHasQueen[col] = true;
                leftSlashHasQueen[row - col + n - 1] = true;
                rightSlashHasQueen[row + col] = true;
                countQueen++;
                findQueen(row + 1, 0);
                colHasQueen[col] = false;
                leftSlashHasQueen[row - col + n - 1] = false;
                rightSlashHasQueen[row + col] = false;
                countQueen--;
            }
            findQueen(row, col + 1);
        }
    }
}

範例輸入輸出:



此程式解決的問題是經典的n-queen問題,三個布林陣列存的分別是行,  左斜線, 右斜線有沒有被擺過皇后,之所以沒有存列有無擺過皇后是因為這列擺過皇后之後,直接換下一列,所以沒有會碰到同一列的可能。若放置此位置後無法找到皇后共存的組合,則此皇后會往右移一格,若皇后移到此列的盡頭都沒有位置可以擺放則直接回到上一列更改皇后的位置,因為是在n*n的棋盤上擺放n個皇后,所以不可能有任何一列沒有被擺到皇后。
輸入為一個整數n,輸出則為總共有多少種擺放的方式,並沒有去除掉對稱的情況。

2013年5月10日 星期五

簡單校園導覽程式

Java code:
import java.util.Arrays;
import java.util.Scanner;

public class FjuMap {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int startVertex;
        int endVertex;
        String[] names = {"利瑪竇大樓", "伯達樓", "樹德樓", "羅耀拉大樓", "濟時樓", "仁愛學苑", "聖心學苑", "信義/和平學苑/心園", "法園(社會服務中心)/仁園", "創新育成中心"};
        int[][] path = new int[10][10];
        int[][] graph =
                {{0, 13, Integer.MAX_VALUE, 25, Integer.MAX_VALUE, 27, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE},
                {13, 0, 10 , 20, Integer.MAX_VALUE, 35, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE},
                {Integer.MAX_VALUE, 10, 0, 12, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE},
                {25, 20, 12, 0, 20, 22, Integer.MAX_VALUE, 28, 27, Integer.MAX_VALUE},
                {Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, 20, 0, 25, Integer.MAX_VALUE, Integer.MAX_VALUE, 16, Integer.MAX_VALUE},
                {27, 35, Integer.MAX_VALUE, 22, 25, 0, 9, 9, Integer.MAX_VALUE, Integer.MAX_VALUE},
                {Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, 9, 0, 8, Integer.MAX_VALUE, Integer.MAX_VALUE},
                {Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, 28, Integer.MAX_VALUE, 9, 8, 0, 10, 12},
                {Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, 27, 16, Integer.MAX_VALUE, Integer.MAX_VALUE, 10, 0, 7},
                {Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, 12, 7, 0}};
        
        for (int[] i: path)
            Arrays.fill(i, -1);
        for (int i=0; i<graph.length; i++) {
            for (int j=0; j<graph.length; j++) {
                for (int k=0; k<graph.length; k++) {
                    if (!(graph[i][k] == Integer.MAX_VALUE) && !(graph[k][j] == Integer.MAX_VALUE)) {
                        int newWeight = graph[i][k] + graph[k][j];
                        
                        if (newWeight < graph[i][j]) {
                            graph[i][j] = newWeight;
                            path[i][j] = k;
                        }
                    }
                }
            }
        }
        System.out.println("地點清單:");
        System.out.println("-----------------------");
        for (int i=0; i<names.length; i++)
        System.out.println(i + "." + names[i]);
        System.out.println("-----------------------");
        try {
            while (true) {
                System.out.print("請輸入起點編號(輸入10結束):");
                startVertex = sc.nextInt();
                if (startVertex == 10)
                    break;
                System.out.print("請輸入終點編號:");
                endVertex = sc.nextInt();
                System.out.print("最短路徑:");
                System.out.print(names[startVertex]);
                tracePath(path, names, startVertex, endVertex);
                System.out.println("->" + names[endVertex] + "\n");
            }
        } finally {
            sc.close();
        }
    }

    private static void tracePath(int[][] path, String[] names, int startVertex, int endVertex) {
        if (path[startVertex][endVertex] != -1) {
            tracePath(path, names, startVertex, path[startVertex][endVertex]);
            System.out.print("->" + names[path[startVertex][endVertex]]);
            tracePath(path, names, path[startVertex][endVertex], endVertex);
        }
    }
}

範例輸入輸出:



抱歉沒空說明程式碼,只能說明一下輸入輸出的部份。首先依照地點清單輸入起點編號,再輸入終點編號,程式就會印出最短路徑,若在起點編號那裡輸入10,程式就會結束。

2013年5月9日 星期四

Dijkstra's Algorithm

Java code:
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class DijkstraAlgorithm {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int vertexNum;
        Set<Integer> used = new HashSet<Integer>();
        Map<Integer, Map<Integer, Integer>> graph = new HashMap<Integer, Map<Integer, Integer>>();
        Map<Integer, Integer> shortestPath = new HashMap<Integer, Integer>();
        int startVertex;
        
        try {
            System.out.print("please input the number of total vertexes:");
            vertexNum = sc.nextInt();
            System.out.println("please input the graph:");
            for (int i=1; i<=vertexNum; i++)
                graph.put(i, new HashMap<Integer, Integer>());
            for (int i=1; i<=vertexNum; i++) {
                for (int j=1; j<=vertexNum; j++) {
                    String input = sc.next();
                    
                    if (input.equals("i"))
                        graph.get(i).put(j, Integer.MAX_VALUE);
                    else
                        graph.get(i).put(j, Integer.parseInt(input));
                }
            }
            System.out.print("please input the start vertex:");
            startVertex = sc.nextInt();
        } finally {
            sc.close();
        }
        for (int i=1; i<=vertexNum; i++)
            shortestPath.put(i, graph.get(startVertex).get(i));
        used.add(startVertex);
        while (true) {
            int nearestVertex = -1;
            int smallestWeight = Integer.MAX_VALUE;
            
            for (int i=1; i<=vertexNum; i++) {
                if (!used.contains(i) && shortestPath.get(i) < smallestWeight) {
                    nearestVertex = i;
                    smallestWeight = shortestPath.get(i);
                }
            }
            used.add(nearestVertex);
            if (used.size() == vertexNum)
                break;
            for (int i=1; i<=vertexNum; i++) {
                if (graph.get(nearestVertex).get(i) != 0 && graph.get(nearestVertex).get(i) != Integer.MAX_VALUE) {
                    int newPath = shortestPath.get(nearestVertex) + graph.get(nearestVertex).get(i);
                    
                    if (newPath < shortestPath.get(i))
                        shortestPath.put(i, newPath);
                }
            }
        }
        System.out.println("shortest path from v" + startVertex + " to all other vertexes:");
        for (int i=1; i<=vertexNum; i++)
            System.out.printf("%4s", "v" + i);
        System.out.println();
        for (int i=1; i<=vertexNum; i++)
            System.out.printf("%4d", shortestPath.get(i));
        System.out.println();
    }
}

範例輸入輸出:



抱歉沒空說明程式碼,只能說明一下輸入輸出的部份。首先輸入圖形上總共點的數量,再輸入相鄰矩陣,橫軸表示點1~點10,縱軸也是表示點1~點10,輸入的是點到點之間的權重,若輸入i表示這兩點不相連,也就是權重無限大,最後再輸入以哪個點當作起點,程式會找出由這個點到其他所有點的最短距離。輸出結果表示由出發點到各點的距離。本範例輸入是採用課本習題2的圖,所以可以參照課本的圖方便了解。

Kruskal's Algorithm

Java code:
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

class Edge implements Comparable<Edge> {
    int startVertex, endVertex, weight;

    @Override
    public int compareTo(Edge otherEdge) {
        if (weight < otherEdge.weight)
            return -1;
        else if (weight == otherEdge.weight)
            return 0;
        else
            return 1;
    }
}

public class KruskalAlgorithm {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int vertexNum;
        int countEdge;
        int totalEdge;
        List<Edge> allEdgeList = new ArrayList<Edge>();
        Map<Integer, Map<Integer, Integer>> minSpanTree = new HashMap<Integer, Map<Integer, Integer>>();
        
        try {
            System.out.print("please input the number of total vertexes:");
            vertexNum = sc.nextInt();
            System.out.println("please input the graph:");
            for (int i=1; i<=vertexNum; i++) {
                for (int j=1; j<=vertexNum; j++) {
                    String theWeight = sc.next();
                    
                    if (!theWeight.equals("i") && i < j) {
                        Edge theEdge = new Edge();
                        
                        theEdge.startVertex = i;
                        theEdge.endVertex = j;
                        theEdge.weight = Integer.parseInt(theWeight);
                        allEdgeList.add(theEdge);
                    }
                }    
            }
        } finally {
            sc.close();
        }
        for (int i=1; i<=vertexNum; i++) {
            minSpanTree.put(i, new HashMap<Integer, Integer>());
            for (int j=1; j<=vertexNum; j++)
                minSpanTree.get(i).put(j, Integer.MAX_VALUE);
        }
        Collections.sort(allEdgeList);
        countEdge = 0;
        totalEdge = vertexNum - 1;
        while (countEdge != totalEdge) {
            Edge smallestEdge = allEdgeList.remove(0);
            
            if (!checkCycle(minSpanTree, vertexNum, smallestEdge.startVertex, smallestEdge.startVertex, smallestEdge.endVertex)) {
                minSpanTree.get(smallestEdge.startVertex).put(smallestEdge.endVertex, smallestEdge.weight);
                minSpanTree.get(smallestEdge.endVertex).put(smallestEdge.startVertex, smallestEdge.weight);
                countEdge++;
            }
        }
        System.out.println("the minimal spanning tree:");
        for (int i=1; i<=vertexNum; i++) {
            for (int j=1; j<=vertexNum; j++) {
                if (minSpanTree.get(i).get(j) == Integer.MAX_VALUE)
                    System.out.printf("%3s", "i");
                else
                    System.out.printf("%3d", minSpanTree.get(i).get(j));
            }
            System.out.println();
        }
    }
    
    private static boolean checkCycle(Map<Integer, Map<Integer, Integer>> minSpanTree, int vertexNum, int originalVertex, int preVertex, int nowVertex) {
        if (originalVertex == nowVertex)
            return true;
        else {
            for (int i=1; i<=vertexNum; i++) {
                if (minSpanTree.get(nowVertex).get(i) != Integer.MAX_VALUE && i != preVertex && checkCycle(minSpanTree, vertexNum, originalVertex, nowVertex, i))
                    return true;
            }
            return false;
        }
    }
}

範例輸入輸出:



抱歉沒空說明程式碼,只能說明一下輸入輸出的部份。首先輸入圖形上總共點的數量,再輸入相鄰矩陣,橫軸表示點1~點10,縱軸也是表示點1~點10,輸入的是點到點之間的權重,若輸入i表示這兩點不相連,也就是權重無限大。輸出結果也是相鄰矩陣,表示找到的最小成本擴展樹。本範例輸入是採用課本習題2的圖,所以可以參照課本的圖方便了解。

Prim's Algorithm

Java code:
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;

class Edge {
    int startVertex, endVertex, weight;
}

class PriQueueComparator implements Comparator<Edge> {

    @Override
    public int compare(Edge thisEdge, Edge otherEdge) {
        if (thisEdge.weight < otherEdge.weight)
            return -1;
        else if (thisEdge.weight == otherEdge.weight)
            return 0;
        else
            return 1;
    }
}

public class PrimAlgorithm {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int vertexNum;
        int nextVertex;
        Set<Integer> used = new HashSet<Integer>();
        Map<Integer, Map<Integer, Integer>> graph = new HashMap<Integer, Map<Integer, Integer>>();
        Map<Integer, Map<Integer, Integer>> minSpanTree = new HashMap<Integer, Map<Integer, Integer>>();
        Queue<Edge> priQueue;
        
        try {
            System.out.print("please input the number of total vertexes:");
            vertexNum = sc.nextInt();
            priQueue = new PriorityQueue<Edge>(vertexNum - 1, new PriQueueComparator());
            System.out.println("please input the graph:");
            for (int i=1; i<=vertexNum; i++)
                graph.put(i, new HashMap<Integer, Integer>());
            for (int i=1; i<=vertexNum; i++) {
                for (int j=1; j<=vertexNum; j++) {
                    String input = sc.next();
                    
                    if (input.equals("i"))
                        graph.get(i).put(j, Integer.MAX_VALUE);
                    else
                        graph.get(i).put(j, Integer.parseInt(input));
                }
            }
        } finally {
            sc.close();
        }
        for (int i=1; i<=vertexNum; i++) {
            minSpanTree.put(i, new HashMap<Integer, Integer>());
            for (int j=1; j<=vertexNum; j++)
                minSpanTree.get(i).put(j, Integer.MAX_VALUE);
        }
        nextVertex = 1;
        while (true) {
            Edge smallestEdge;
            
            used.add(nextVertex);
            if (used.size() == vertexNum)
                break;
            for (int i=1; i<=vertexNum; i++) {
                Edge theEdge = new Edge();
                
                if (graph.get(nextVertex).get(i) != Integer.MAX_VALUE) {
                    theEdge.startVertex = nextVertex;
                    theEdge.endVertex = i;
                    theEdge.weight = graph.get(nextVertex).get(i);
                    priQueue.add(theEdge);
                }
            }
            while (true) {
                smallestEdge = priQueue.poll();
                if (!used.contains(smallestEdge.endVertex))
                    break;
            }
            minSpanTree.get(smallestEdge.startVertex).put(smallestEdge.endVertex, smallestEdge.weight);
            minSpanTree.get(smallestEdge.endVertex).put(smallestEdge.startVertex, smallestEdge.weight);
            nextVertex = smallestEdge.endVertex;
        }
        System.out.println("minimal spanning tree");
        for (int i=1; i<=vertexNum; i++) {
            for (int j=1; j<=vertexNum; j++) {
                if (minSpanTree.get(i).get(j) == Integer.MAX_VALUE)
                    System.out.printf("%3s", "i");
                else
                    System.out.printf("%3d", minSpanTree.get(i).get(j));
            }
            System.out.println();
        }
    }
}

範例輸入輸出:



抱歉沒空說明程式碼,只能說明一下輸入輸出的部份。首先輸入圖形上總共點的數量,再輸入相鄰矩陣,橫軸表示點1~點10,縱軸也是表示點1~點10,輸入的是點到點之間的權重,若輸入i表示這兩點不相連,也就是權重無限大。輸出結果也是相鄰矩陣,表示找到的最小成本擴展樹。本範例輸入是採用課本習題2的圖,所以可以參照課本的圖方便了解。