Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8612461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:37:54+00:00 2026-06-12T04:37:54+00:00

I am trying to implement AHP(Analytic Hierarchy Process) algorithm for computing criterion’s weights(using eigen

  • 0

I am trying to implement AHP(Analytic Hierarchy Process) algorithm for computing criterion’s weights(using eigen vetors). For example, I want to buy a smart phone. My criteria are: color, memory, delivery. For computing weights I have to make pair wise comparison among criteria. I will compare color with memory, color with delivery, and memory with delivery.
For comparing 2 criteria, we use a scale from 9 to 1/9.
For example I compare color with memory: if in my opinion color is more important than memory 4 times, I will use 4 ,if color has the same importance like memory, I will use 1, if color is less important than memory 4 times, I use 1/4=0.25.
For computing weights, I have to build a matrix:

          color       memory       delivery

color     1           value1       value2

memory    1/value1      1          value3 

delivery  1/value2   1/value3       1          

In my case the matrix is 3×3 because I have only 3 criteria. The program is working for 3 criteria, but not for 4, 5 or more. After the matrix is build, I can compute eigen vectors that will give me the weights.Any suggestion would be appreciated. Thank you in advance!

Here is the code for Criteria class:

public class Criteria
{
public static void main(String[] args)
{
    AHP ahp=new AHP();

    int n;
    int NUMBER_COMPARISON;
    Scanner keyboard=new Scanner(System.in);

    System.out.println("Enter the number of criteria");
    System.out.println("n=");
    n=keyboard.nextInt();
    NUMBER_COMPARISON=(n*n-n)/2;

    double [][] a=new double[n][n];
    String [] criteria=new String[n];
    double [] p=new double[NUMBER_COMPARISON];//used to hold the values of comparisons

    System.out.println("Enter the criteria:");
    for(int i=0; i<n;i++)
    {
        System.out.print("Criterion "+(i+1)+":");
        criteria[i]=keyboard.next();
    }

    System.out.println("Enter the comparison");
        int m=0; 
        for(int i=0; i<n;i++)
        {
            for(int j=i+1; j<n;j++)
            {
                System.out.println("Compare "+criteria[i]+" with "+criteria[j]+":");
                p[m]=keyboard.nextDouble();
                m++;
            }
        }

    a=ahp.initialize_matrix(p);
    ahp.show_matrix(a);
   }    
}

Here is the code for AHP class:

public class AHP
{
public static double[][] initialize_matrix(double[] p)
{
    //initialize the matrix a
    double a[][]=new double[p.length][p.length];    
    int k=0;        
    for(int i=0; i<p.length; i++)
    {
        for(int j=0; j<p.length;j++)
        {
            if(i==j)
                a[i][j]=1;
            else if(i<j)
            {

                a[i][j]=p[k];
                k++;
            }

            else if(i>j)
                a[i][j]=1/a[j][i];
        }
    }
    return a;
}

public static void show_matrix(double[][] b )
{
    //display the elements of the matrix a
    System.out.println("\nThe matrix a is:");
    for(int i=0; i<b.length;i++)
    {
        for(int j=0; j<b[i].length; j++)
            System.out.print(b[i][j]+"    ");
        System.out.println();   
    }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:37:55+00:00Added an answer on June 12, 2026 at 4:37 am

    From an analytical point of view the variables j and i in the initialize_matrix method are alway inside the array boundaries.
    However, there is the variable k, which can be incremented p.length^2 times. As you also use this variable to access the array p it must be < p.length.

    I think you want to add the value at position k, but online every line. I suggest to set k to zero after the inner for loop is done.

    EDIT: As predicted…

    Output for n = 4:

    Enter the number of criteria
    n=
    4
    Enter the criteria:
    Criterion 1:a
    Criterion 2:b
    Criterion 3:c
    Criterion 4:d
    Enter the comparison
    Compare a with b:
    0.3
    Compare a with c:
    0.1
    Compare a with d:
    0.6
    Compare b with c:
    0.5
    Compare b with d:
    0.8
    Compare c with d:
    0.2

    The matrix a is:
    1.0 0.3 0.1 0.6 0.5 0.8
    3.3333333333333335 1.0 0.3 0.1 0.6 0.5
    10.0 3.3333333333333335 1.0 0.3 0.1 0.6
    1.6666666666666667 10.0 3.3333333333333335 1.0 0.3 0.1
    2.0 1.6666666666666667 10.0 3.3333333333333335 1.0 0.3
    1.25 2.0 1.6666666666666667 10.0 3.3333333333333335 1.0

    The method

    public static double[][] initialize_matrix(double[] p)
    {
    
        double a[][]=new double[p.length][p.length];    
        int k=0;        
        for(int i=0; i<p.length; i++)
        {
            k = 0;
    
            for(int j=0; j<p.length;j++)
            {
                if(i==j)
                    a[i][j]=1;
                else if(i<j)
                {
    
                    a[i][j]=p[k];
                    k++;
                }
    
                else if(i>j)
                    a[i][j]=1/a[j][i];
            }
        }
        return a;
    }
    

    I would appreciate it if you would mark the question as answered.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying implement the Data transformation using Reflection 1 example in my code.
Trying to implement google C2DM service. registrationIntent.putExtra(app, PendingIntent.getBroadcast(context,0,new Intent(), 0)); registrationIntent.putExtra(sender,example@gmail.com); context.startService(registrationIntent); Almost every
I'm trying implement a bracket in my program (using C#/.NET MVC) and I am
Trying to implement search with Sunspot Gem wich is using Solr.Fulltext search works fine
I'm trying implement a way to recursively template using jsRender. The issue is, my
I have this weird kind of error. I am trying implement basic Euclidean algorithm
trying to implement a multiplayer. Using the sample from Game Center - Sending and
Trying to implement the example from here. Facebook Share passes along the URL of
Im trying to implement this example, but with $.getScript: and for some reason, it
trying to implement a dialog-box style behaviour using a separate div section with all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.