微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

协助确定算法的执行时间

如何解决协助确定算法的执行时间

创建以下程序以对按顺序排列的一组选票进行成对选举。

最终结果是让成对的选择在经过的执行时间的0.01秒内完成所有测试用例。

当前,程序最终结果时间性能约为.02秒。 需要帮助以将时间缩短至0.01秒或更短。

下面是程序代码

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

/**
 * 
 * 
 *         Utility class to compute the pairwise winner of elections
 */
public class PairwiseVote {
    /**
     * Get the placement order of a candidate in a rank order list of candidates
     *
     * @param VotersVotes
     *            an array of rank order candidates. First has the highest rank.
     */
    public static int getPlacement(int candidate,int[] VotersVotes) {
        for (int placement = 0; placement < VotersVotes.length; placement++) {
            if (candidate == VotersVotes[placement]) {
                return placement;
            }
        }
        return VotersVotes.length + 1;
    }

    /**
     * Get the candidate winner from a set of rank ordered ballots
     *
     * @param Votes
     *            - a two dimensional array,first dimension is the Voter,second
     *            dimension is the rank ordered ballot of candidates for the given
     *            Voter
     */
    public static int getPairwiseWinner(int[][] Votes) {
        int noVoters = Votes.length;

        if (noVoters == 0) {
            return -1;
        }

        int noCandidates = Votes[0].length;

        if (noCandidates == 0) {
            return -1;
        }

        // Compare every pair of candidates
        for (int candidate1 = 0; candidate1 < noCandidates; candidate1++) {
            int noTimesCandidate1Wins = 0;
            for (int candidate2 = 0; candidate2 < noCandidates; candidate2++) {
                // Consider a candidate compared with themselves to be a winner
                if (candidate1 == candidate2) {
                    noTimesCandidate1Wins++;
                    continue;
                }

                // Determine count the ballots for each candidate
                int candidate1Votes = 0;
                int candidate2Votes = 0;
                for (int Voter = 0; Voter < noVoters; Voter++) {
                    int placement1 = getPlacement(candidate1,Votes[Voter]);
                    int placement2 = getPlacement(candidate2,Votes[Voter]);
                    if (placement1 < placement2) {
                        candidate1Votes++;
                        ;
                    } else {
                        candidate2Votes++;
                        ;
                    }
                }

                // determine if candidate1 is the winner if so increment the number of wins
                if (candidate1Votes > candidate2Votes) {
                    noTimesCandidate1Wins++;
                }
            }

            // Determine if candidate 1 wins all
            if (noTimesCandidate1Wins == noCandidates) {
                return candidate1;
            }
        }

        // No winner
        return -1;
    }

    static int electionNo = 0;

    /**
     * Main - reads several test elections using the text file Votes.txt. Each
     * election begins with two number,the number of Voters and the number of
     * candidates,all followed by the ballots of each Voter.
     */
    public static void main(String[] args) throws FileNotFoundException {
        int noVoters;
        int noCandidates;

        Scanner in = new Scanner(new FileInputStream("Votes.txt"));

        // read ballots for each election
        for (;;) {
            noVoters = in.nextInt();
            noCandidates = in.nextInt();

            if (noVoters == 0 && noCandidates == 0) {
                break;
            }

            final int[][] Votes = new int[noVoters][noCandidates];

            // Read the ballots
            for (int i = 0; i < noVoters; i++) {
                for (int j = 0; j < noCandidates; j++) {
                    Votes[i][j] = in.nextInt();
                }
            }

            new TimeExec(new Runnable() {
                public void run() {
                    int winner = getPairwiseWinner(Votes);
                    if (winner >= 0) {
                        System.out.printf("Winner of election %d is candidate %d\n",electionNo,winner);
                    } else {
                        System.out.printf("No winner for election %d\n",electionNo);
                    }
                }
            },"Election " + ++electionNo,System.out).start();
        }
    }
}

感谢您的帮助!

解决方法

当前代码调用了noCandidates*noCandidates*noVoters*2次getPlacement,但是只要调用noCandidates*noVoters次就足够了。

您可以通过预先计算值并像这样存储它们来管理它(不确定它是否正在编译,因为我没有在Java上编程,但是我想您可以理解):

public static int getPairwiseWinner(int[][] votes) {
    int noVoters = votes.length;

    if (noVoters == 0) {
        return -1;
    }

    int noCandidates = votes[0].length;

    if (noCandidates == 0) {

        return -1;
    }

    int[][] placements = new int[noVoters][noCandidates]; // to store precalculated placements

    // go through each of the voters and precalculate the placements of each candidate
    for (int voter = 0; voter<noVoters; voter++) {

        for (int candidate = 0; candidate < noCandidates; candidate++) {

            placements[voter][candidate] = getPlacement(candidate,votes[voter]);
        }
    }

    // Compare every pair of candidates
    for (int candidate1 = 0; candidate1 < noCandidates; candidate1++) {

        int noTimesCandidate1Wins = 0;
        for (int candidate2 = 0; candidate2 < noCandidates; candidate2++) {

            // Consider a candidate compared with themselves to be a winner
            if (candidate1 == candidate2) {

                noTimesCandidate1Wins++;
                continue;
            }

            // Determine count the ballots for each candidate
            int candidate1votes = 0;
            int candidate2votes = 0;
            for (int voter = 0; voter < noVoters; voter++) {

                // use precalculated placements
                if (placements[voter][candidate1] < placements[voter][candidate2]) {

                    candidate1votes++;
                    ;
                } else {


                    candidate2votes++;
                    ;
                }
            }

            // determine if candidate1 is the winner if so increment the number of wins
            if (candidate1votes > candidate2votes) {

                noTimesCandidate1Wins++;
            }
        }

        // Determine if candidate 1 wins all
        if (noTimesCandidate1Wins == noCandidates) {

            return candidate1;
        }
    }

    // No winner
    return -1;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。