简体   繁体   English

高效的数据结构代表矩阵

[英]Efficient Data Structure to represent matrix

I am currently working on a solution for exam timetable scheduling for a university. 我目前正在为大学的考试时间表安排解决方案。 I was thinking of using Genetic Algorithms. 我当时正在考虑使用遗传算法。

Right now I started with a simple example so that I catch on what is happening since I am new to all this. 现在,我从一个简单的示例开始,以便我了解所有发生的事情,因为我不熟悉这一切。 In this example I have 10 exams, 6 students and 6 available time-slots. 在此示例中,我有10项考试,6名学生和6个可用时隙。 I am representing a chromosome such that I have 10 positions in a bit-string with values varying from 1-6 representing time-slots so for example: [3 1 4 1 3 5 5 6 4 2] implies that exam 1 takes place in the 3rd time-slot, exam 2 in the 1st time-slot, etc... 我代表一条染色体,这样我在位串中有10个位置,其值从1-6到代表时隙的范围不等,例如: [3 1 4 1 3 5 5 6 4 2]表示检查1在第三个时隙,第一个时隙的考试2,依此类推...

Now if I have the following array representing which exams are each of the students taking: 现在,如果我有以下数组代表每个学生参加的考试:

int[][] students_exams = 
{
    {3, 1, 2, 5, 8},   // student 1
    {10, 4, 5, 7},     // student 2
    {1, 2, 3, 6},      // student 3
    {2, 7, 4, 5},      // student 4
    {1, 6, 2, 10},     // student 5
    {8, 9, 1, 3}       // student 6
};

how can I efficiently represent this information as a [nxn] matrix where n is the number of exams (in this case 10) where M[i][j] is equal to the number of students taking exam i and exam j? 我如何有效地将该信息表示为[nxn]矩阵,其中n是考试数量(在这种情况下为10),其中M [i] [j]等于参加考试i和考试j的学生数量?

Is there a data structure I can use or do I have to compare each exam with each other exam and have an incrementing counter? 我是否可以使用数据结构,还是必须将每个检查与另一个检查进行比较并具有递增计数器? Because thinking about it, I think this will not be efficient for the amount of students and exams I will have in reality. 因为考虑了这一点,所以我认为这对于我实际拥有的学生和考试数量而言并不有效。

If you can refer me to any papers or references you'll be of much help to me. 如果您可以将我推荐给任何论文或参考文献,将对我有很大帮助。

Thanks 谢谢

Here is the brute force approach you are probably trying to avoid: 这是您可能要避免的蛮力方法:

class testMatrixSO{
    public static void main(String[] args){

        int[][] students_exams = 
        {
        {3, 1, 2, 5, 8},   // student 1
        {10, 4, 5, 7},     // student 2
        {1, 2, 3, 6},      // student 3
        {2, 7, 4, 5},      // student 4
        {1, 6, 2, 10},     // student 5
        {8, 9, 1, 3}       // student 6
        };

        int[][] finalMatrix = new int[10][10];
        //here is the part that creates your matrix
        for(int i=0; i<students_exams.length; i++)
            for(int j=0; j<students_exams[i].length; j++)
                for(int k=0; k<students_exams[i].length; k++)
                    if(j != k) finalMatrix[students_exams[i][j]-1][students_exams[i][k]-1]++;



        //print results
        for(int i=0; i < finalMatrix.length; i++){
            for(int j=0; j < finalMatrix[i].length; j++)
                System.out.print(finalMatrix[i][j] + " ");
            System.out.println();
        }

    }
}

on a positive note, if you assume each student will have 6 or less exams, then we can confidently say that the loop that updates the matrix will execute less than (number of students)*(6*6) times. 积极地说,如果您假设每个学生将进行6项或更少的考试,那么我们可以自信地说,更新矩阵的循环执行的次数少于(学生人数)*(6 * 6)次。 ie the algorith is of linear order, O(n). 即算法是线性的,O(n)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM