Saddle Point of a Given Matrix

Saddle Point of a matrix is the element which is lowest in the row and highest in the corresponding column. A matrix may have one or two saddle points or may not have a saddle point.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication2

{

    class Program

    {

        static void Main(string[] args)

        {

            //Saddle point of a matrix

            int[,] matrix = {

                                {20, 30, 40},

                                {56, 78, 45},

                                {1, 2, 3}

                            };

            int count = 0;

            List<int> rowFilter = new List<int>();

            List<int> colFilter = new List<int>();

            //Row

            for (int i = 0; i < matrix.GetLength(0) && !rowFilter.Contains(i); i++)

            {

                //Col

                for (int j = 0; j < matrix.GetLength(1) && !colFilter.Contains(j); j++)

                {

                    //Row check

                    int flag = 0;

                    for (int col = 0; col < matrix.GetLength(1); col++)

                    {

                        if (j != col && matrix[i, j] > matrix[i, col])

                        {

                            flag = 1;

                            break;

                        }

                    }

                    if (flag == 0) //Row check passed

                    {

                        //Column check

                        for (int row = 0; row < matrix.GetLength(0); row++)

                        {

                            if (i != row && matrix[i, j] < matrix[row, j])

                            {

                                flag = 1;

                                break;

                            }

                        }

                        if (flag == 0)

                        {

                            count++;

                            Console.WriteLine("Saddle Point " + count + ": " + matrix[i, j]);

                            rowFilter.Add(i);

                            colFilter.Add(j);

                        }

                    }

                }

            }

            if (count == 0)

            {

                Console.WriteLine("There are no saddle points.");

            }

        }

    }

}

Saddle Point 1: 45
Press any key to continue . . .

Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s