Java中的Prim和Kruskal算法

时间:2017-02-25 14:26:46

标签: java algorithm

我必须在Java中实现Prim和Kruskal的算法,以便在给定的无向加权图中找到最小生成树。我怎样才能做到这一点 ?实现必须至少实现Prim算法的O(2)和Kruskal算法的O(3)(n是节点数)。

entity.get()

1 个答案:

答案 0 :(得分:0)

这是我的实施:

public class Library {
// Add the missing implementation to this class 

    java.util.ArrayList<Book> books;

    private static String openingHours = "The Library is open every day from 9AM to 5PM";
    private String a  ddress = "";
    private String[] collection = new String[10];
    private int collectionCount = 0;

    public Library(String libraries) {
        address = libraries;
        books = new java.util.ArrayList<>();
    }

    public
            String getAddress() {
        return this.address;
    }

    void setaddress(String address) {
        this.address = address;
    }

    public static void printOpeningHours() {
        System.out.println("Library Hours: ");
        System.out.println(openingHours);
    }

    public void printAddress() {
        System.out.println(address);
    }

    public void addBook(Book book) {
        books.add(book);
    }

    public void printAvailableBooks() {
        boolean bookPresent = false;
        for (Book book : books) {
            if (!book.isBorrowed()) {
                System.out.println(book.getTitle());
                bookPresent = true;
            }
        }
        if (!bookPresent) {
            System.out.println("No such book in our catalog");
        }
    }

    private void borrowBook(String title) {
        int found = 0;
        for (Book book : books) {
            if (book.getTitle().equals(title)) {
                if (found == 0) {
                    found = 1;
                }
                if (!book.isBorrowed()) {
                    book.borrowed();
                    found = 2;
                    break;
                };
            }
        }
        if (found == 0) {
            System.out.println("Sorry, this book is not in our catalog.");
        } else if (found == 1) {
            System.out.println("Sorry, this book is already borrowed.");
        } else if (found == 2) {
            System.out.println("You successfully borrowed " + title);
        }
    }

    public void returnBook(String title) {
        boolean found = false;
        for (Book book : books) {
            if (book.getTitle().equals(title) && book.isBorrowed()) {
                book.returned();
                found = true;
                break;
            }
        }
        if (found) {
            System.out.println("You successfully returned " + title);
        }
    }

    public static void main(String[] args) {
        Library firstLibrary = new Library("Moylish Park, Limerick");
        Library secondLibrary = new Library("Clare Street, Limerick");
        Library thirdLibrary = new Library("Nenagh Road, Thurles");

        // Add four books to the first library
        firstLibrary.addBook(new Book("Java How To Program"));
        firstLibrary.addBook(new Book("What's new in Java 7?"));
        firstLibrary.addBook(new Book("Java in a nutshell"));
        firstLibrary.addBook(new Book("Pro Android 2"));

        // Print Opening hours and the addresses
        Library.printOpeningHours();
        System.out.println();
        System.out.println("Library Addresses: ");
        firstLibrary.printAddress();
        secondLibrary.printAddress();
        thirdLibrary.printAddress();
        System.out.println();

        // Print the titles of all available books from both libraries
        System.out.println("Books available in the first library:");
        firstLibrary.printAvailableBooks();
        System.out.println();
        System.out.println("Books available in the second library:");
        secondLibrary.printAvailableBooks();
        System.out.println();
        System.out.println("Books available in the third library:");
        thirdLibrary.printAvailableBooks();
        System.out.println();

        // Return Java How To Program 
        System.out.println("Returning Java How To Program");
        firstLibrary.returnBook("Java How To Program");
        System.out.println();

        // Print the titles of available from the first library
        System.out.println("Books available in the first library:");
        firstLibrary.printAvailableBooks();
    }
}